summaryrefslogtreecommitdiffstats
path: root/intl/components/gtest/TestLocale.cpp
blob: e4cc6a093b01df2c7a28e742272d6bd757c09d0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"

#include "mozilla/intl/Locale.h"
#include "mozilla/Span.h"

#include "TestBuffer.h"

namespace mozilla::intl {

TEST(IntlLocale, LocaleSettersAndGetters)
{
  Locale locale;
  locale.SetLanguage("fr");
  locale.SetRegion("CA");
  locale.SetScript("Latn");
  ASSERT_TRUE(
      locale.SetUnicodeExtension(MakeStringSpan("u-ca-gregory")).isOk());
  ASSERT_TRUE(locale.Language().EqualTo("fr"));
  ASSERT_TRUE(locale.Region().EqualTo("CA"));
  ASSERT_TRUE(locale.Script().EqualTo("Latn"));
  ASSERT_EQ(locale.GetUnicodeExtension().value(),
            MakeStringSpan("u-ca-gregory"));

  TestBuffer<char> buffer;
  ASSERT_TRUE(locale.ToString(buffer).isOk());
  ASSERT_TRUE(buffer.verboseMatches("fr-Latn-CA-u-ca-gregory"));

  // No setters for variants or other extensions...
  Locale locale2;
  ASSERT_TRUE(LocaleParser::TryParse(
                  MakeStringSpan("fr-CA-fonipa-t-es-AR-h0-hybrid"), locale2)
                  .isOk());
  ASSERT_EQ(locale2.Variants()[0], MakeStringSpan("fonipa"));
  ASSERT_EQ(locale2.Extensions()[0], MakeStringSpan("t-es-AR-h0-hybrid"));
  locale2.ClearVariants();
  ASSERT_EQ(locale2.Variants().length(), 0UL);
}

TEST(IntlLocale, LocaleMove)
{
  Locale locale;
  ASSERT_TRUE(
      LocaleParser::TryParse(
          MakeStringSpan(
              "fr-Latn-CA-fonipa-u-ca-gregory-t-es-AR-h0-hybrid-x-private"),
          locale)
          .isOk());

  ASSERT_TRUE(locale.Language().EqualTo("fr"));
  ASSERT_TRUE(locale.Script().EqualTo("Latn"));
  ASSERT_TRUE(locale.Region().EqualTo("CA"));
  ASSERT_EQ(locale.Variants()[0], MakeStringSpan("fonipa"));
  ASSERT_EQ(locale.Extensions()[0], MakeStringSpan("u-ca-gregory"));
  ASSERT_EQ(locale.Extensions()[1], MakeStringSpan("t-es-AR-h0-hybrid"));
  ASSERT_EQ(locale.GetUnicodeExtension().value(),
            MakeStringSpan("u-ca-gregory"));
  ASSERT_EQ(locale.PrivateUse().value(), MakeStringSpan("x-private"));

  Locale locale2 = std::move(locale);

  ASSERT_TRUE(locale2.Language().EqualTo("fr"));
  ASSERT_TRUE(locale2.Script().EqualTo("Latn"));
  ASSERT_TRUE(locale2.Region().EqualTo("CA"));
  ASSERT_EQ(locale2.Variants()[0], MakeStringSpan("fonipa"));
  ASSERT_EQ(locale2.Extensions()[0], MakeStringSpan("u-ca-gregory"));
  ASSERT_EQ(locale2.Extensions()[1], MakeStringSpan("t-es-AR-h0-hybrid"));
  ASSERT_EQ(locale2.GetUnicodeExtension().value(),
            MakeStringSpan("u-ca-gregory"));
  ASSERT_EQ(locale2.PrivateUse().value(), MakeStringSpan("x-private"));
}

TEST(IntlLocale, LocaleParser)
{
  const char* tags[] = {
      "en-US",       "en-GB",       "es-AR",      "it",         "zh-Hans-CN",
      "de-AT",       "pl",          "fr-FR",      "de-AT",      "sr-Cyrl-SR",
      "nb-NO",       "fr-FR",       "mk",         "uk",         "und-PL",
      "und-Latn-AM", "ug-Cyrl",     "sr-ME",      "mn-Mong",    "lif-Limb",
      "gan",         "zh-Hant",     "yue-Hans",   "unr",        "unr-Deva",
      "und-Thai-CN", "ug-Cyrl",     "en-Latn-DE", "pl-FR",      "de-CH",
      "tuq",         "sr-ME",       "ng",         "klx",        "kk-Arab",
      "en-Cyrl",     "und-Cyrl-UK", "und-Arab",   "und-Arab-FO"};

  for (const auto* tag : tags) {
    Locale locale;
    ASSERT_TRUE(LocaleParser::TryParse(MakeStringSpan(tag), locale).isOk());
  }
}

TEST(IntlLocale, LikelySubtags)
{
  Locale locale;
  ASSERT_TRUE(LocaleParser::TryParse(MakeStringSpan("zh"), locale).isOk());
  ASSERT_TRUE(locale.AddLikelySubtags().isOk());
  TestBuffer<char> buffer;
  ASSERT_TRUE(locale.ToString(buffer).isOk());
  ASSERT_TRUE(buffer.verboseMatches("zh-Hans-CN"));
  ASSERT_TRUE(locale.RemoveLikelySubtags().isOk());
  buffer.clear();
  ASSERT_TRUE(locale.ToString(buffer).isOk());
  ASSERT_TRUE(buffer.verboseMatches("zh"));
}

TEST(IntlLocale, Canonicalize)
{
  Locale locale;
  ASSERT_TRUE(
      LocaleParser::TryParse(MakeStringSpan("nob-bokmal"), locale).isOk());
  ASSERT_TRUE(locale.Canonicalize().isOk());
  TestBuffer<char> buffer;
  ASSERT_TRUE(locale.ToString(buffer).isOk());
  ASSERT_TRUE(buffer.verboseMatches("nb"));
}

// These tests are dependent on the machine that this test is being run on.
TEST(IntlLocale, SystemDependentTests)
{
  // e.g. "en_US"
  const char* locale = Locale::GetDefaultLocale();
  ASSERT_TRUE(locale != nullptr);
}

TEST(IntlLocale, GetAvailableLocales)
{
  using namespace std::literals;

  int32_t english = 0;
  int32_t german = 0;
  int32_t chinese = 0;

  // Since this list is dependent on ICU, and may change between upgrades, only
  // test a subset of the available locales.
  for (const char* locale : Locale::GetAvailableLocales()) {
    if (locale == "en"sv) {
      english++;
    } else if (locale == "de"sv) {
      german++;
    } else if (locale == "zh"sv) {
      chinese++;
    }
  }

  // Each locale should be found exactly once.
  ASSERT_EQ(english, 1);
  ASSERT_EQ(german, 1);
  ASSERT_EQ(chinese, 1);
}

}  // namespace mozilla::intl