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
84 changes: 82 additions & 2 deletions components/experimental/src/dimension/currency/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
// TODO: add more tests for this module to cover more locales & currencies.
#[cfg(test)]
mod tests {
use crate::dimension::currency::{
formatter::CurrencyFormatter, options::CurrencyDisplay, options::CurrencyFormatterOptions,
CurrencyCode,
};
use icu_locale_core::locale;
use tinystr::*;
use writeable::assert_writeable_eq;

use crate::dimension::currency::{formatter::CurrencyFormatter, CurrencyCode};

#[test]
pub fn test_en_us() {
let locale = locale!("en-US").into();
Expand Down Expand Up @@ -66,4 +68,82 @@ mod tests {
"\u{61c}-\u{200f}١٢٬٣٤٥٫٦٧\u{a0}ج.م.\u{200f}"
);
}

#[test]
#[should_panic] // TODO(#7785): implement currencyDisplay
pub fn test_currency_display_code_en_us() {
let locale = locale!("en-US").into();
let currency_code = CurrencyCode(tinystr!(3, "USD"));
let fmt = CurrencyFormatter::try_new(
locale,
CurrencyFormatterOptions {
currency_display: CurrencyDisplay::Code,
..Default::default()
},
)
.unwrap();

let value = "12345.67".parse().unwrap();
let formatted_currency = fmt.format_fixed_decimal(&value, &currency_code);

assert_writeable_eq!(formatted_currency, "USD 12,345.67");
}

#[test]
pub fn test_currency_display_symbol_en_us() {
let locale = locale!("en-US").into();
let currency_code = CurrencyCode(tinystr!(3, "USD"));
let fmt = CurrencyFormatter::try_new(
locale,
CurrencyFormatterOptions {
currency_display: CurrencyDisplay::Symbol,
..Default::default()
},
)
.unwrap();

let value = "12345.67".parse().unwrap();
let formatted_currency = fmt.format_fixed_decimal(&value, &currency_code);

assert_writeable_eq!(formatted_currency, "$12,345.67");
}

#[test]
pub fn test_currency_display_narrow_symbol_en_us() {
let locale = locale!("en-US").into();
let currency_code = CurrencyCode(tinystr!(3, "USD"));
let fmt = CurrencyFormatter::try_new(
locale,
CurrencyFormatterOptions {
currency_display: CurrencyDisplay::NarrowSymbol,
..Default::default()
},
)
.unwrap();

let value = "12345.67".parse().unwrap();
let formatted_currency = fmt.format_fixed_decimal(&value, &currency_code);

assert_writeable_eq!(formatted_currency, "$12,345.67");
}

#[test]
#[should_panic] // TODO(#7785): implement currencyDisplay
pub fn test_currency_display_name_en_us() {
let locale = locale!("en-US").into();
let currency_code = CurrencyCode(tinystr!(3, "USD"));
let fmt = CurrencyFormatter::try_new(
locale,
CurrencyFormatterOptions {
currency_display: CurrencyDisplay::Name,
..Default::default()
},
)
.unwrap();

let value = "12345.67".parse().unwrap();
let formatted_currency = fmt.format_fixed_decimal(&value, &currency_code);

assert_writeable_eq!(formatted_currency, "12,345.67 US dollars");
}
}
36 changes: 35 additions & 1 deletion components/experimental/src/dimension/currency/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,48 @@ use serde::{Deserialize, Serialize};
pub struct CurrencyFormatterOptions {
/// The width of the currency format.
pub width: Width,

/// The display style for currencies.
/// Default is [`CurrencyDisplay::Symbol`].
pub currency_display: CurrencyDisplay,
Copy link
Member

Choose a reason for hiding this comment

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

As noted in the other PR, options should be optional:

Suggested change
pub currency_display: CurrencyDisplay,
pub currency_display: Option<CurrencyDisplay>,

}

impl From<Width> for CurrencyFormatterOptions {
fn from(width: Width) -> Self {
Self { width }
Self {
width,
currency_display: CurrencyDisplay::default(),
}
}
}

impl From<CurrencyDisplay> for CurrencyFormatterOptions {
fn from(currency_display: CurrencyDisplay) -> Self {
Self {
width: Width::default(),
currency_display,
}
}
}

#[derive(Default, Debug, Eq, PartialEq, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[non_exhaustive]
pub enum CurrencyDisplay {
/// Display with the default currency symbol.
#[default]
Symbol,

/// Display with the narrow currency symbol.
NarrowSymbol,

/// Display with the ISO currency code.
Code,

/// Display with the localized currency name.
Name,
}

#[derive(Default, Debug, Eq, PartialEq, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[non_exhaustive]
Expand Down
Loading