tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestLocaleCanonicalizer.cpp (2348B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 #include "gtest/gtest.h"
      5 
      6 #include "mozilla/intl/LocaleCanonicalizer.h"
      7 #include "mozilla/Span.h"
      8 
      9 namespace mozilla::intl {
     10 
     11 static void CheckLocaleResult(LocaleCanonicalizer::Vector& ascii,
     12                              const char* before, const char* after) {
     13  auto result = LocaleCanonicalizer::CanonicalizeICULevel1(before, ascii);
     14  ASSERT_TRUE(result.isOk());
     15  ASSERT_EQ(Span(const_cast<const char*>(ascii.begin()), ascii.length()),
     16            MakeStringSpan(after));
     17 }
     18 
     19 /**
     20 * Asserts the behavior of canonicalization as defined in:
     21 * http://userguide.icu-project.org/locale#TOC-Canonicalization
     22 */
     23 TEST(IntlLocaleCanonicalizer, CanonicalizeICULevel1)
     24 {
     25  LocaleCanonicalizer::Vector ascii{};
     26 
     27  // Canonicalizes en-US
     28  CheckLocaleResult(ascii, "en-US", "en_US");
     29  // Canonicalizes POSIX
     30  CheckLocaleResult(ascii, "en-US-posix", "en_US_POSIX");
     31  // und gets changed to an empty string
     32  CheckLocaleResult(ascii, "und", "");
     33  // retains incorrect locales
     34  CheckLocaleResult(ascii, "asdf", "asdf");
     35  // makes text uppercase
     36  CheckLocaleResult(ascii, "es-es", "es_ES");
     37  // Converts 3 letter country codes to 2 letter.
     38  CheckLocaleResult(ascii, "en-USA", "en_US");
     39  // Does not perform level 2 canonicalization where the result would be
     40  // fr_FR@currency=EUR
     41  CheckLocaleResult(ascii, "fr-fr@EURO", "fr_FR_EURO");
     42  // Removes the .utf8 ends
     43  CheckLocaleResult(ascii, "ar-MA.utf8", "ar_MA");
     44 
     45  // Rejects non parseable ASCII inputs.
     46  ASSERT_EQ(
     47      LocaleCanonicalizer::CanonicalizeICULevel1(
     48          "abcdefghijlkmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ-_.0123456789",
     49          ascii)
     50          .unwrapErr(),
     51      ICUError::InternalError);
     52  ASSERT_EQ(
     53      LocaleCanonicalizer::CanonicalizeICULevel1("exotic ascii:", ascii)
     54          .unwrapErr(),
     55      ICUError::InternalError);
     56 
     57  // Does not accept non-ascii inputs.
     58  ASSERT_EQ(LocaleCanonicalizer::CanonicalizeICULevel1("👍", ascii).unwrapErr(),
     59            ICUError::InternalError);
     60  ASSERT_EQ(
     61      LocaleCanonicalizer::CanonicalizeICULevel1("ᏣᎳᎩ", ascii).unwrapErr(),
     62      ICUError::InternalError);
     63 }
     64 
     65 }  // namespace mozilla::intl