Skip to content
Open
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
85 changes: 68 additions & 17 deletions components/experimental/src/dimension/currency/compact_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,78 +8,129 @@ mod tests {
use tinystr::*;
use writeable::assert_writeable_eq;

use crate::dimension::currency::{CurrencyCode, compact_formatter::CompactCurrencyFormatter};
use crate::dimension::currency::{
CurrencyCode,
formatter::{Compact, CurrencyFormatter, CurrencyFormatterPreferences},
};

#[test]
pub fn test_en_us() {
let locale = locale!("en-US").into();
let prefs: CurrencyFormatterPreferences = locale!("en-US").into();
let currency_code = CurrencyCode(tinystr!(3, "USD"));
let fmt = CompactCurrencyFormatter::try_new(locale, Default::default()).unwrap();
let fmt =
CurrencyFormatter::<Compact>::try_new_short(prefs, &currency_code, Default::default())
.unwrap();

// Positive case
let positive_value = "12345.67".parse().unwrap();
let formatted_currency = fmt.format_fixed_decimal(&positive_value, &currency_code);
let formatted_currency = fmt.format_fixed_decimal(&positive_value);
assert_writeable_eq!(formatted_currency, "$12K");

// Negative case
let negative_value = "-12345.67".parse().unwrap();
let formatted_currency = fmt.format_fixed_decimal(&negative_value, &currency_code);
let formatted_currency = fmt.format_fixed_decimal(&negative_value);
assert_writeable_eq!(formatted_currency, "-$12K");
}

#[test]
pub fn test_fr_fr() {
let locale = locale!("fr-FR").into();
let prefs: CurrencyFormatterPreferences = locale!("fr-FR").into();
let currency_code = CurrencyCode(tinystr!(3, "EUR"));
let fmt = CompactCurrencyFormatter::try_new(locale, Default::default()).unwrap();
let fmt =
CurrencyFormatter::<Compact>::try_new_short(prefs, &currency_code, Default::default())
.unwrap();

// Positive case
let positive_value = "12345.67".parse().unwrap();
let formatted_currency = fmt.format_fixed_decimal(&positive_value, &currency_code);
let formatted_currency = fmt.format_fixed_decimal(&positive_value);
assert_writeable_eq!(formatted_currency, "12\u{a0}k\u{a0}€");

// Negative case
let negative_value = "-12345.67".parse().unwrap();
let formatted_currency = fmt.format_fixed_decimal(&negative_value, &currency_code);
let formatted_currency = fmt.format_fixed_decimal(&negative_value);
assert_writeable_eq!(formatted_currency, "-12\u{a0}k\u{a0}€");
}

#[test]
pub fn test_zh_cn() {
let locale = locale!("zh-CN").into();
let prefs: CurrencyFormatterPreferences = locale!("zh-CN").into();
let currency_code = CurrencyCode(tinystr!(3, "CNY"));
let fmt = CompactCurrencyFormatter::try_new(locale, Default::default()).unwrap();
let fmt =
CurrencyFormatter::<Compact>::try_new_short(prefs, &currency_code, Default::default())
.unwrap();

// Positive case
let positive_value = "12345.67".parse().unwrap();
let formatted_currency = fmt.format_fixed_decimal(&positive_value, &currency_code);
let formatted_currency = fmt.format_fixed_decimal(&positive_value);
assert_writeable_eq!(formatted_currency, "¥1.2万");

// Negative case
let negative_value = "-12345.67".parse().unwrap();
let formatted_currency = fmt.format_fixed_decimal(&negative_value, &currency_code);
let formatted_currency = fmt.format_fixed_decimal(&negative_value);
assert_writeable_eq!(formatted_currency, "-¥1.2万");
}

#[test]
pub fn test_ar_eg() {
let locale = locale!("ar-EG").into();
let prefs: CurrencyFormatterPreferences = locale!("ar-EG").into();
let currency_code = CurrencyCode(tinystr!(3, "EGP"));
let fmt = CompactCurrencyFormatter::try_new(locale, Default::default()).unwrap();
let fmt =
CurrencyFormatter::<Compact>::try_new_short(prefs, &currency_code, Default::default())
.unwrap();

// Positive case
let positive_value = "12345.67".parse().unwrap();
let formatted_currency = fmt.format_fixed_decimal(&positive_value, &currency_code);
let formatted_currency = fmt.format_fixed_decimal(&positive_value);
// TODO(#6064)
assert_writeable_eq!(formatted_currency, "\u{200f}١٢\u{a0}ألف\u{a0}ج.م.\u{200f}"); // "ج.م.١٢ألف"

// Negative case
let negative_value = "-12345.67".parse().unwrap();
let formatted_currency = fmt.format_fixed_decimal(&negative_value, &currency_code);
let formatted_currency = fmt.format_fixed_decimal(&negative_value);
// TODO(#6064)
assert_writeable_eq!(
formatted_currency,
"\u{61c}-\u{200f}١٢\u{a0}ألف\u{a0}ج.م.\u{200f}"
);
}

#[test]
pub fn test_alpha_next_to_number_and_small_numbers() {
let prefs: CurrencyFormatterPreferences = locale!("en-US").into();
let usd = CurrencyCode(tinystr!(3, "USD"));
let sek = CurrencyCode(tinystr!(3, "SEK"));

let fmt_usd =
CurrencyFormatter::<Compact>::try_new_short(prefs, &usd, Default::default()).unwrap();
let fmt_sek =
CurrencyFormatter::<Compact>::try_new_short(prefs, &sek, Default::default()).unwrap();

// Small number (magnitude < 3, no compact suffix): should fall back cleanly
let small_value = "123".parse().unwrap();
assert_writeable_eq!(fmt_usd.format_fixed_decimal(&small_value), "$123");
assert_writeable_eq!(fmt_sek.format_fixed_decimal(&small_value), "SEK\u{a0}123");

// Compact number with alphabetical currency code: should use alpha_next_to_number pattern with non-breaking space
let compact_value = "12345.67".parse().unwrap();
assert_writeable_eq!(fmt_sek.format_fixed_decimal(&compact_value), "SEK\u{a0}12K");
}

#[test]
pub fn test_accounting() {
use crate::dimension::currency::options::{CurrencyFormatterOptions, CurrencyUsage};
let prefs: CurrencyFormatterPreferences = locale!("en-US").into();
let usd = CurrencyCode(tinystr!(3, "USD"));
let options = CurrencyFormatterOptions {
usage: CurrencyUsage::Accounting,
};
let fmt = CurrencyFormatter::<Compact>::try_new_short(prefs, &usd, options).unwrap();

// Positive case: should format normally
let positive_value = "12345.67".parse().unwrap();
assert_writeable_eq!(fmt.format_fixed_decimal(&positive_value), "$12K");

// Negative case: in accounting format for en-US, should use parentheses instead of minus sign
let negative_value = "-12345.67".parse().unwrap();
assert_writeable_eq!(fmt.format_fixed_decimal(&negative_value), "($12K)");
}
}
Loading
Loading