1// Copyright (C) 2013 Google Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "region_data_constants.h"
16
17#include <string>
18#include <vector>
19
20#include <gtest/gtest.h>
21
22namespace {
23
24using i18n::addressinput::RegionDataConstants;
25
26// Tests for region codes, for example "ZA".
27class RegionCodeTest : public testing::TestWithParam<std::string> {};
28
29// Verifies that a region code consists of two characters, for example "ZA".
30TEST_P(RegionCodeTest, RegionCodeHasTwoCharacters) {
31  EXPECT_EQ(2, GetParam().length());
32}
33
34// Test all region codes.
35INSTANTIATE_TEST_CASE_P(
36    AllRegionCodes, RegionCodeTest,
37    testing::ValuesIn(RegionDataConstants::GetRegionCodes()));
38
39// Returns AssertionSuccess if |data| begins with '{' and ends with '}'.
40testing::AssertionResult HasCurlyBraces(const std::string& data) {
41  if (data.empty()) {
42    return testing::AssertionFailure() << "data is empty";
43  }
44  if (data[0] != '{') {
45    return testing::AssertionFailure() << data << " does not start with '{'";
46  }
47  if (data[data.length() - 1] != '}') {
48    return testing::AssertionFailure() << data << " does not end with '}'";
49  }
50  return testing::AssertionSuccess();
51}
52
53// Verifies that the default region data begins with '{' and ends with '}'.
54TEST(DefaultRegionDataTest, DefaultRegionHasCurlyBraces) {
55  EXPECT_TRUE(HasCurlyBraces(RegionDataConstants::GetDefaultRegionData()));
56}
57
58// Tests for region data, for example "{\"fmt\":\"%C%S\"}".
59class RegionDataTest : public testing::TestWithParam<std::string> {
60 protected:
61  const std::string& GetData() const {
62    return RegionDataConstants::GetRegionData(GetParam());
63  }
64};
65
66// Verifies that a region data value begins with '{' and end with '}', for
67// example "{\"fmt\":\"%C%S\"}".
68TEST_P(RegionDataTest, RegionDataHasCurlyBraces) {
69  EXPECT_TRUE(HasCurlyBraces(GetData()));
70}
71
72// Test all region data.
73INSTANTIATE_TEST_CASE_P(
74    AllRegionData, RegionDataTest,
75    testing::ValuesIn(RegionDataConstants::GetRegionCodes()));
76
77TEST(RegionDataConstantsTest, GetMaxLookupKeyDepth) {
78  EXPECT_EQ(0, RegionDataConstants::GetMaxLookupKeyDepth("NZ"));
79  EXPECT_EQ(1, RegionDataConstants::GetMaxLookupKeyDepth("HK"));
80  EXPECT_EQ(2, RegionDataConstants::GetMaxLookupKeyDepth("US"));
81  EXPECT_EQ(3, RegionDataConstants::GetMaxLookupKeyDepth("CN"));
82}
83
84}  // namespace
85