83412fde83
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
96 lines
3.5 KiB
Rust
96 lines
3.5 KiB
Rust
//! Integration tests: loading catalogs from TOML, caps and locale ids.
|
|
|
|
use notescraft_i18n::{Catalog, Error, MAX_KEYS, is_valid_locale};
|
|
|
|
/// Parses or fails the test with the error text.
|
|
fn parse(locale: &str, source: &str) -> Catalog {
|
|
match Catalog::from_toml(locale, source) {
|
|
Ok(cat) => cat,
|
|
Err(e) => {
|
|
assert_eq!(e.to_string(), format!("expected a catalog, got: {e:?}"));
|
|
Catalog::default()
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn flat_and_nested_toml_flat_to_dotted_keys() {
|
|
let cat = parse("en", "top = \"Top\"\n[buttons]\nsave = \"Save\"\n[nav]\ndashboard = \"Dashboard\"\n");
|
|
assert_eq!(cat.get("top"), Some("Top"));
|
|
assert_eq!(cat.get("buttons.save"), Some("Save"));
|
|
assert_eq!(cat.get("nav.dashboard"), Some("Dashboard"));
|
|
assert_eq!(cat.locale(), "en");
|
|
assert_eq!(cat.len(), 3);
|
|
let keys: Vec<String> = cat.keys().map(ToOwned::to_owned).collect();
|
|
assert_eq!(keys, ["buttons.save", "nav.dashboard", "top"], "keys are sorted");
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_key_chars_are_errors() {
|
|
assert!(matches!(Catalog::from_toml("en", "\"a B\" = \"x\""), Err(Error::InvalidKey { .. })));
|
|
assert!(Catalog::from_toml("en", "\"é\" = \"x\"").is_err());
|
|
assert!(Catalog::from_toml("en", "\"KEY\" = \"x\"").is_err());
|
|
assert!(Catalog::from_toml("en", "[a]\n\"b.c\" = \"x\"\n[a.b]\nc = \"y\"").is_err(), "quoted key that duplicates a nested table");
|
|
}
|
|
|
|
#[test]
|
|
fn duplicate_keys_after_flattening_error() {
|
|
let src = "[a]\n\"b.c\" = \"one\"\n[a.b]\nc = \"two\"\n";
|
|
assert!(matches!(Catalog::from_toml("en", src), Err(Error::DuplicateKey { .. })));
|
|
}
|
|
|
|
#[test]
|
|
fn non_string_values_are_errors() {
|
|
assert!(Catalog::from_toml("en", "count = 3").is_err());
|
|
assert!(Catalog::from_toml("en", "flag = true").is_err());
|
|
assert!(Catalog::from_toml("en", "arr = [1, 2]").is_err());
|
|
assert!(Catalog::from_toml("en", "when = 1979-05-27T07:32:00Z").is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn key_budget_cap_is_enforced() {
|
|
let mut src = String::with_capacity(20_001 * 12);
|
|
for i in 0..=20_000 {
|
|
let _unused = std::fmt::Write::write_fmt(&mut src, format_args!("k{i} = \"v\"\n"));
|
|
}
|
|
let err = Catalog::from_toml("en", &src);
|
|
assert!(matches!(err, Err(Error::TooManyKeys { count, .. }) if count == MAX_KEYS + 1));
|
|
let ok_src = src.replace("k20000 = \"v\"\n", "");
|
|
assert_eq!(parse("en", &ok_src).len(), MAX_KEYS);
|
|
}
|
|
|
|
#[test]
|
|
fn size_cap_is_enforced() {
|
|
let one_mb = "x".repeat(1_200_000);
|
|
let src = format!("big = \"{one_mb}{one_mb}\"");
|
|
assert!(matches!(Catalog::from_toml("en", &src), Err(Error::TooLarge { .. })));
|
|
}
|
|
|
|
#[test]
|
|
fn locale_ids_are_validated() {
|
|
for id in ["en", "pt-BR", "zh-Hans", "en2", "a"] {
|
|
assert!(is_valid_locale(id), "{id} should be valid");
|
|
}
|
|
for id in ["", "pt_br", "-en", "é", "en{", "123", "xxxxxxxxxxxxxxxx0"] {
|
|
assert!(!is_valid_locale(id), "{id:?} should be invalid");
|
|
}
|
|
assert!(matches!(Catalog::from_toml("pt_BR", "a = \"x\""), Err(Error::InvalidLocale { .. })));
|
|
assert!(matches!(Catalog::from_toml("", "a = \"x\""), Err(Error::InvalidLocale { .. })));
|
|
}
|
|
|
|
#[test]
|
|
fn parse_errors_return_err() {
|
|
assert!(matches!(Catalog::from_toml("en", "key = "), Err(Error::Parse(_))));
|
|
assert!(Catalog::from_toml("en", "[] = \"x\"").is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn empty_and_default_catalogs() {
|
|
let cat = parse("en", "");
|
|
assert_eq!(cat.locale(), "en");
|
|
assert_eq!(cat.len(), 0);
|
|
assert!(cat.is_empty());
|
|
assert_eq!(cat.get("buttons.save"), None);
|
|
assert!(!cat.contains("buttons.save"));
|
|
}
|