#include #include #include #include namespace { using Void::Text::I18n::Argument; using Void::Text::I18n::Arguments; using Void::Text::I18n::Catalog; using Void::Text::I18n::I18nError; using Void::Text::I18n::PluralCategory; Arguments with(std::string key, Argument value) { Arguments args; args.emplace(std::move(key), std::move(value)); return args; } std::string plural_ru(double count) { const auto result = Void::Text::I18n::format( "{count, plural, one {# товар} few {# товара} " "many {# товаров} other {# товара}}", "ru", with("count", count)); Void::Testing::require(result.has_value()); return *result; } } // namespace // ── Plural rules ──────────────────────────────────────────────────────────── Void::Testing::Test plural_category_english() { Void::Testing::expect(Void::Text::I18n::plural_category("en", 1) == PluralCategory::one); Void::Testing::expect(Void::Text::I18n::plural_category("en", 0) == PluralCategory::other); Void::Testing::expect(Void::Text::I18n::plural_category("en-US", 2) == PluralCategory::other); Void::Testing::expect(Void::Text::I18n::plural_category("en", 1.5) == PluralCategory::other); } Void::Testing::Test plural_category_russian() { Void::Testing::expect(Void::Text::I18n::plural_category("ru", 1) == PluralCategory::one); Void::Testing::expect(Void::Text::I18n::plural_category("ru-RU", 21) == PluralCategory::one); Void::Testing::expect(Void::Text::I18n::plural_category("ru", 11) == PluralCategory::many); Void::Testing::expect(Void::Text::I18n::plural_category("ru", 2) == PluralCategory::few); Void::Testing::expect(Void::Text::I18n::plural_category("ru", 14) == PluralCategory::many); Void::Testing::expect(Void::Text::I18n::plural_category("ru", 5) == PluralCategory::many); Void::Testing::expect(Void::Text::I18n::plural_category("ru", 1.5) == PluralCategory::other); } Void::Testing::Test plural_category_no_plural_languages() { Void::Testing::expect(Void::Text::I18n::plural_category("ja", 1) == PluralCategory::other); Void::Testing::expect(Void::Text::I18n::plural_category("zh-CN", 2) == PluralCategory::other); } // ── Formatting ───────────────────────────────────────────────────────────── Void::Testing::Test format_simple_argument() { const auto result = Void::Text::I18n::format( "Hello, {name}!", "en", with("name", std::string{"World"})); Void::Testing::require(result.has_value()); Void::Testing::expect_eq(*result, std::string{"Hello, World!"}); } Void::Testing::Test format_escaped_braces() { const auto result = Void::Text::I18n::format("'{'literal'}' {name}", "en", with("name", std::string{"value"})); Void::Testing::require(result.has_value()); Void::Testing::expect_eq(*result, std::string{"{literal} value"}); } Void::Testing::Test format_apostrophe_quoting() { auto doubled = Void::Text::I18n::format("it''s", "en", {}); auto hash = Void::Text::I18n::format( "{count, plural, other {'#' is #}}", "en", with("count", 2.0)); Void::Testing::require(doubled.has_value()); Void::Testing::require(hash.has_value()); Void::Testing::expect_eq(*doubled, std::string{"it's"}); Void::Testing::expect_eq(*hash, std::string{"# is 2"}); } Void::Testing::Test format_english_plural() { constexpr std::string_view pattern = "{count, plural, one {# item} other {# items}}"; auto one = Void::Text::I18n::format(pattern, "en", with("count", 1.0)); auto many = Void::Text::I18n::format(pattern, "en", with("count", 3.0)); Void::Testing::require(one.has_value()); Void::Testing::require(many.has_value()); Void::Testing::expect_eq(*one, std::string{"1 item"}); Void::Testing::expect_eq(*many, std::string{"3 items"}); } Void::Testing::Test format_russian_plural_vectors() { Void::Testing::expect_eq(plural_ru(1), std::string{"1 товар"}); Void::Testing::expect_eq(plural_ru(2), std::string{"2 товара"}); Void::Testing::expect_eq(plural_ru(5), std::string{"5 товаров"}); Void::Testing::expect_eq(plural_ru(21), std::string{"21 товар"}); Void::Testing::expect_eq(plural_ru(11), std::string{"11 товаров"}); Void::Testing::expect_eq(plural_ru(1.5), std::string{"1.5 товара"}); } Void::Testing::Test format_exact_plural_branch() { constexpr std::string_view pattern = "{count, plural, =0 {none} one {one} other {#}}"; auto zero = Void::Text::I18n::format(pattern, "en", with("count", 0.0)); auto two = Void::Text::I18n::format(pattern, "en", with("count", 2.0)); Void::Testing::require(zero.has_value()); Void::Testing::require(two.has_value()); Void::Testing::expect_eq(*zero, std::string{"none"}); Void::Testing::expect_eq(*two, std::string{"2"}); } Void::Testing::Test format_plural_offset_and_pound() { constexpr std::string_view pattern = "{count, plural, offset:1 =0 {nobody} =1 {just you} " "one {you and # other} other {you and # others}}"; auto two = Void::Text::I18n::format(pattern, "en", with("count", 2.0)); auto four = Void::Text::I18n::format(pattern, "en", with("count", 4.0)); Void::Testing::require(two.has_value()); Void::Testing::require(four.has_value()); Void::Testing::expect_eq(*two, std::string{"you and 1 other"}); Void::Testing::expect_eq(*four, std::string{"you and 3 others"}); } Void::Testing::Test format_select() { constexpr std::string_view pattern = "{gender, select, male {he} female {she} other {they}}"; auto male = Void::Text::I18n::format(pattern, "en", with("gender", std::string{"male"})); auto unknown = Void::Text::I18n::format( pattern, "en", with("gender", std::string{"robot"})); Void::Testing::require(male.has_value()); Void::Testing::require(unknown.has_value()); Void::Testing::expect_eq(*male, std::string{"he"}); Void::Testing::expect_eq(*unknown, std::string{"they"}); } Void::Testing::Test format_nested_select_inside_plural() { constexpr std::string_view pattern = "{count, plural, one {{gender, select, male {he has} female {she has} " "other {they have}} # item} other {# items}}"; auto result = Void::Text::I18n::format( pattern, "en", Arguments{{"count", 1.0}, {"gender", std::string{"female"}}}); Void::Testing::require(result.has_value()); Void::Testing::expect_eq(*result, std::string{"she has 1 item"}); } // ── Pattern errors ───────────────────────────────────────────────────────── Void::Testing::Test format_rejects_unclosed_brace() { const auto result = Void::Text::I18n::format( "Hello {name", "en", with("name", std::string{"x"})); Void::Testing::require(!result.has_value()); Void::Testing::expect(result.error().code == I18nError::Code::invalid_pattern); } Void::Testing::Test format_rejects_missing_other_branch() { const auto result = Void::Text::I18n::format( "{count, plural, one {#}}", "en", with("count", 1.0)); Void::Testing::require(!result.has_value()); Void::Testing::expect(result.error().code == I18nError::Code::missing_branch); } Void::Testing::Test format_rejects_unsupported_type() { const auto result = Void::Text::I18n::format( "{value, number}", "en", with("value", 1.0)); Void::Testing::require(!result.has_value()); Void::Testing::expect(result.error().code == I18nError::Code::unsupported_type); } Void::Testing::Test format_reports_unknown_argument() { const auto result = Void::Text::I18n::format("Hello {name}", "en", {}); Void::Testing::require(!result.has_value()); Void::Testing::expect(result.error().code == I18nError::Code::unknown_argument); Void::Testing::expect_eq(result.error().key, std::string{"name"}); } Void::Testing::Test format_reports_wrong_argument_type() { const auto result = Void::Text::I18n::format( "{count, plural, other {#}}", "en", with("count", std::string{"not a number"})); Void::Testing::require(!result.has_value()); Void::Testing::expect(result.error().code == I18nError::Code::wrong_argument_type); } Void::Testing::Test validate_accepts_well_formed_pattern() { const auto valid = Void::Text::I18n::validate( "{count, plural, one {# item} other {# items}}"); Void::Testing::expect(valid.has_value()); const auto invalid = Void::Text::I18n::validate("{count, plural}"); Void::Testing::expect(!invalid.has_value()); } // ── Catalog ──────────────────────────────────────────────────────────────── Void::Testing::Test catalog_loads_and_formats() { Catalog catalog; const auto loaded = catalog.load( "# greetings\n" "greeting = Hello, {name}!\n" "items = {count, plural, one {# item} other {# items}}\n"); Void::Testing::require(loaded.has_value()); Void::Testing::expect_eq(catalog.size(), std::size_t{2}); auto greeting = catalog.format("greeting", "en", with("name", std::string{"Ada"})); auto items = catalog.format("items", "en", with("count", 2.0)); Void::Testing::require(greeting.has_value()); Void::Testing::require(items.has_value()); Void::Testing::expect_eq(*greeting, std::string{"Hello, Ada!"}); Void::Testing::expect_eq(*items, std::string{"2 items"}); } Void::Testing::Test catalog_reports_missing_key() { Catalog catalog; const auto result = catalog.format("absent", "en", {}); Void::Testing::require(!result.has_value()); Void::Testing::expect(result.error().code == I18nError::Code::missing_key); Void::Testing::expect_eq(result.error().key, std::string{"absent"}); } Void::Testing::Test catalog_rejects_duplicate_key() { Catalog catalog; Void::Testing::require(catalog.load("key = one\n").has_value()); const auto duplicate = catalog.load("key = two\n"); Void::Testing::require(!duplicate.has_value()); Void::Testing::expect(duplicate.error().code == I18nError::Code::invalid_catalog); } Void::Testing::Test catalog_rejects_malformed_pattern() { Catalog catalog; const auto result = catalog.load("key = {count, plural, one {#}}\n"); Void::Testing::require(!result.has_value()); Void::Testing::expect(result.error().code == I18nError::Code::missing_branch); } Void::Testing::Test catalog_rejects_line_without_assignment() { Catalog catalog; const auto result = catalog.load("not a catalog line\n"); Void::Testing::require(!result.has_value()); Void::Testing::expect(result.error().code == I18nError::Code::invalid_catalog); } // ── Locale-aware number formatting ──────────────────────────────────────── Void::Testing::Test format_decimal_uses_locale_symbols() { Void::Testing::expect_eq(Void::Text::I18n::format_decimal("en", 1234.5), std::string{"1,234.5"}); Void::Testing::expect_eq(Void::Text::I18n::format_decimal("de", 1234.5), std::string{"1.234,5"}); Void::Testing::expect_eq(Void::Text::I18n::format_decimal("ru", 1234.5), std::string{"1 234,5"}); Void::Testing::expect_eq(Void::Text::I18n::format_decimal("en", 100.0), std::string{"100"}); } Void::Testing::Test locale_language_normalizes() { Void::Testing::expect_eq(Void::Text::I18n::locale_language("ru-RU"), std::string{"ru"}); Void::Testing::expect_eq(Void::Text::I18n::locale_language("EN_us"), std::string{"en"}); }