#![cfg(feature = "intl")]
use blitz_script::ScriptDocument;
fn eval_string(doc: &mut ScriptDocument, code: &str) -> String {
doc.eval(&format!("globalThis.__out = String({code});"));
doc.eval_json("globalThis.__out")
.ok()
.and_then(|v| v.as_str().map(str::to_owned))
.unwrap_or_default()
}
fn page() -> ScriptDocument {
ScriptDocument::from_html(
"<html><body></body></html>",
blitz_dom::DocumentConfig::default(),
)
}
#[test]
fn intl_is_defined() {
let mut doc = page();
doc.execute_scripts();
assert_eq!(eval_string(&mut doc, "typeof Intl"), "object");
}
#[test]
fn number_format_follows_the_locale() {
let mut doc = page();
doc.execute_scripts();
let en = eval_string(
&mut doc,
"new Intl.NumberFormat('en-US').format(1234567.89)",
);
let de = eval_string(
&mut doc,
"new Intl.NumberFormat('de-DE').format(1234567.89)",
);
assert_eq!(en, "1,234,567.89");
assert_eq!(de, "1.234.567,89");
assert_ne!(en, de, "the locale tag was ignored");
}
#[test]
fn date_format_uses_localised_month_names() {
let mut doc = page();
doc.execute_scripts();
let out = eval_string(
&mut doc,
"new Intl.DateTimeFormat('fr-FR', { month: 'long', timeZone: 'UTC' })\
.format(new Date(Date.UTC(2020, 0, 15)))",
);
assert_eq!(out, "janvier");
}
#[test]
fn collation_is_language_aware() {
let mut doc = page();
doc.execute_scripts();
let sv = eval_string(&mut doc, "new Intl.Collator('sv').compare('ä', 'z')");
let de = eval_string(&mut doc, "new Intl.Collator('de').compare('ä', 'z')");
assert_eq!(sv, "1", "Swedish sorts ä after z");
assert_eq!(de, "-1", "German sorts ä before z");
}
#[test]
fn plural_rules_are_language_specific() {
let mut doc = page();
doc.execute_scripts();
let en = eval_string(&mut doc, "new Intl.PluralRules('en-US').select(2)");
let ru = eval_string(&mut doc, "new Intl.PluralRules('ru-RU').select(2)");
assert_eq!(en, "other");
assert_eq!(ru, "few");
}