1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "third_party/libaddressinput/src/cpp/src/rule.h"
6
7#include <string>
8
9#include "base/strings/utf_string_conversions.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12TEST(RuleTest, CanonicalizeSubKeyTest) {
13  i18n::addressinput::Rule rule;
14  ASSERT_TRUE(rule.ParseSerializedRule(base::WideToUTF8(
15      L"{ \"sub_keys\": \"FOO~BAR~B\u00C4Z~\u0415\u0416\","
16      L"  \"sub_names\": \"Foolandia~Bartopolis~B\u00E4zmonia~"
17                          L"\u0415\u0436ville\" }")));
18  EXPECT_EQ(4U, rule.GetSubKeys().size());
19
20  static const struct {
21    const wchar_t* input;
22    // If empty, expect failure.
23    const wchar_t* output;
24  } expectations[] = {
25    { L"foo", L"FOO" },
26    { L"Foo", L"FOO" },
27    { L"FOO", L"FOO" },
28    { L"F", L"" },
29    { L"FOO2", L"" },
30    { L"", L"" },
31    { L"Bar", L"BAR" },
32    { L"Bartopolis", L"BAR" },
33    { L"BARTOPOLIS", L"BAR" },
34    { L"BaRToPoLiS", L"BAR" },
35    // Diacriticals.
36    { L"B\u00C4Z", L"B\u00C4Z" },
37    { L"BAZ", L"B\u00C4Z" },
38    { L"B\u00E4zmonia", L"B\u00C4Z" },
39    { L"bazmonia", L"B\u00C4Z" },
40    // Non-ascii (Cyrillic) case sensitivity.
41    { L"\u0415\u0416", L"\u0415\u0416" },
42    { L"\u0415\u0416VILLE", L"\u0415\u0416" },
43    { L"\u0435\u0436", L"\u0415\u0416" },
44    { L"\u0435\u0436VILLE", L"\u0415\u0416" },
45    { L"\u0435\u0436VILL", L"" },
46  };
47
48  const size_t num_cases = sizeof(expectations) / sizeof(expectations[0]);
49  for (size_t i = 0; i < num_cases; ++i) {
50    const std::string input(base::WideToUTF8(expectations[i].input));
51    const std::string expected_output(base::WideToUTF8(expectations[i].output));
52    std::string output;
53    EXPECT_EQ(!expected_output.empty(),
54              rule.CanonicalizeSubKey(input, true, &output))
55        << "Failed for input " << input;
56    EXPECT_EQ(expected_output, output);
57  }
58}
59