From 12c485ea12190fb92e972357dd6ed906b591ffc1 Mon Sep 17 00:00:00 2001 From: Jil Sahm Date: Tue, 27 Jan 2026 13:25:38 +0100 Subject: [PATCH] Add some basic rust unit tests --- Cargo.toml | 3 +++ src/lib.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 2eb6bd9..9e1b416 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,6 @@ unic-langid = "0.9.6" fluent-bundle = "0.16.0" chrono = "0.4.41" miette = { version = "7.6.0", features = ["fancy"] } + +[dev-dependencies] +pyo3 = { version = "*", features = ["auto-initialize"] } diff --git a/src/lib.rs b/src/lib.rs index 4e4b52f..9f7b3a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -141,4 +141,79 @@ mod rustfluent { Ok(value.to_string()) } } + + #[cfg(test)] + mod tests { + + use std::collections::HashMap; + + use pyo3::{Python, types::IntoPyDict}; + + use super::rustfluent; + + #[test] + fn bundle_new_success() { + vec![ + ("en-US", vec![], false), + ( + "en-US", + vec![std::path::PathBuf::from("tests/data/en.ftl")], + false, + ), + ( + "en-US", + vec![ + std::path::PathBuf::from("tests/data/en.ftl"), + std::path::PathBuf::from("tests/data/en_hello.ftl"), + ], + false, + ), + ] + .into_iter() + .enumerate() + .for_each(|(case, (language, ftl_filenames, strict))| { + let result = rustfluent::Bundle::new(language, ftl_filenames, strict); + assert!(result.is_ok(), "case {case} failed"); + }); + } + + #[test] + fn bundle_get_translation() { + let mut bundle = rustfluent::Bundle::new( + "en-US", + vec![std::path::PathBuf::from("tests/data/en.ftl")], + false, + ) + .expect("valid fluent bundle"); + + let result = bundle.get_translation("hello-world", None, false); + + assert!(result.is_ok()); + assert_eq!(result.unwrap(), "Hello World"); + } + + #[test] + fn bundle_get_translation_with_ctx() { + let mut bundle = rustfluent::Bundle::new( + "en-US", + vec![std::path::PathBuf::from("tests/data/en.ftl")], + false, + ) + .expect("valid fluent bundle"); + + Python::with_gil(|py| { + let mut ctx = HashMap::new(); + ctx.insert("user", "John"); + + let result = bundle.get_translation( + "hello-user", + Some(&ctx.into_py_dict(py).unwrap()), + false, + ); + + assert!(result.is_ok()); + assert_eq!(result.unwrap(), "Hello, John"); + }); + } + } }