Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
75 changes: 75 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look into using rstest it has a slightly nicer handling of parametrisation than vanilla rust: https://rstest.rs/

We've used it in a few other areas in Flex

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting tool. Never looked into it. I might try it out sometime and introduce it in a separate PR.

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");
});
}
}
}