Skip to content
Draft
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
1 change: 1 addition & 0 deletions fluent-bundle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ serde = { workspace = true, features = ["derive"] }
unic-langid = { workspace = true, features = ["macros"] }
rand = "0.8"
serde_yaml = "0.9"
rstest = "0.24.0"

[features]
default = []
Expand Down
94 changes: 93 additions & 1 deletion fluent-bundle/src/types/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ from_num!(f32 f64);

#[cfg(test)]
mod tests {
use crate::types::FluentValue;
use crate::types::{FluentNumber, FluentNumberOptions, FluentValue};
use rstest::*;

#[test]
fn value_from_copy_ref() {
Expand All @@ -261,4 +262,95 @@ mod tests {
let z: FluentValue = y.into();
assert_eq!(z, FluentValue::try_number("1"));
}

#[rstest]
#[case(10.12, 1, "10.12")]
#[case(10.0, 3, "10.000")]
#[case(10.12, 5, "10.12000")]
fn minimum_fraction_digits(#[case] input: f64, #[case] minfd: usize, #[case] expected: &str) {
let value = FluentNumber::new(
input,
FluentNumberOptions {
minimum_fraction_digits: Some(minfd),
..Default::default()
},
);

assert_eq!(value.as_string(), expected);
}

#[rstest]
#[case(10.12, 0, "10")]
#[case(10.0, 3, "10.0")]
#[case(10.123456, 3, "10.123")]
fn maximum_fraction_digits(#[case] input: f64, #[case] maxfd: usize, #[case] expected: &str) {
let value = FluentNumber::new(
input,
FluentNumberOptions {
maximum_fraction_digits: Some(maxfd),
..Default::default()
},
);

assert_eq!(value.as_string(), expected);
}

#[rstest]
#[case(10.12, 1, "10.12")]
#[case(10.0, 3, "010.0")]
#[case(10.12, 5, "00010.12")]
fn minimum_integer_digits(#[case] input: f64, #[case] minid: usize, #[case] expected: &str) {
let value = FluentNumber::new(
input,
FluentNumberOptions {
minimum_integer_digits: Some(minid),
..Default::default()
},
);

assert_eq!(value.as_string(), expected);
}

#[rstest]
#[case(10.1, 2, 5, "10.10")]
#[case(10.1, 2, 1, "10.1")]
#[case(10.12345, 2, 3, "10.123")]
fn multiple_options_minfd_maxfd(
#[case] input: f64,
#[case] minfd: usize,
#[case] maxfd: usize,
#[case] expected: &str,
) {
let value = FluentNumber::new(
input,
FluentNumberOptions {
minimum_fraction_digits: Some(minfd),
maximum_fraction_digits: Some(maxfd),
..Default::default()
},
);

assert_eq!(value.as_string(), expected);
}

#[rstest]
#[case(10.1, 2, 1, "10.10")]
#[case(10.1, 2, 3, "010.10")]
fn multiple_options_minfd_minid(
#[case] input: f64,
#[case] minfd: usize,
#[case] minid: usize,
#[case] expected: &str,
) {
let value = FluentNumber::new(
input,
FluentNumberOptions {
minimum_fraction_digits: Some(minfd),
maximum_fraction_digits: Some(minid),
..Default::default()
},
);

assert_eq!(value.as_string(), expected);
}
}
Loading