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 <libaddressinput/util/basictypes.h> 18 19 #include <string> 20 21 #include <gtest/gtest.h> 22 23 namespace { 24 25 using i18n::addressinput::RegionDataConstants; 26 27 // Tests for region codes, for example "ZA". 28 class RegionCodeTest : public testing::TestWithParam<std::string> { 29 protected: 30 RegionCodeTest() {} 31 32 private: 33 DISALLOW_COPY_AND_ASSIGN(RegionCodeTest); 34 }; 35 36 // Verifies that a region code consists of two characters, for example "ZA". 37 TEST_P(RegionCodeTest, RegionCodeHasTwoCharacters) { 38 EXPECT_EQ(2, GetParam().length()); 39 } 40 41 // Test all region codes. 42 INSTANTIATE_TEST_CASE_P( 43 AllRegionCodes, RegionCodeTest, 44 testing::ValuesIn(RegionDataConstants::GetRegionCodes())); 45 46 // Returns AssertionSuccess if |data| begins with '{' and ends with '}'. 47 testing::AssertionResult HasCurlyBraces(const std::string& data) { 48 if (data.empty()) { 49 return testing::AssertionFailure() << "data is empty"; 50 } 51 if (data[0] != '{') { 52 return testing::AssertionFailure() << data << " does not start with '{'"; 53 } 54 if (data[data.length() - 1] != '}') { 55 return testing::AssertionFailure() << data << " does not end with '}'"; 56 } 57 return testing::AssertionSuccess(); 58 } 59 60 // Verifies that the default region data begins with '{' and ends with '}'. 61 TEST(DefaultRegionDataTest, DefaultRegionHasCurlyBraces) { 62 EXPECT_TRUE(HasCurlyBraces(RegionDataConstants::GetDefaultRegionData())); 63 } 64 65 // Tests for region data, for example "{\"fmt\":\"%C%S\"}". 66 class RegionDataTest : public testing::TestWithParam<std::string> { 67 protected: 68 RegionDataTest() {} 69 70 const std::string& GetData() const { 71 return RegionDataConstants::GetRegionData(GetParam()); 72 } 73 74 private: 75 DISALLOW_COPY_AND_ASSIGN(RegionDataTest); 76 }; 77 78 // Verifies that a region data value begins with '{' and end with '}', for 79 // example "{\"fmt\":\"%C%S\"}". 80 TEST_P(RegionDataTest, RegionDataHasCurlyBraces) { 81 EXPECT_TRUE(HasCurlyBraces(GetData())); 82 } 83 84 // Test all region data. 85 INSTANTIATE_TEST_CASE_P( 86 AllRegionData, RegionDataTest, 87 testing::ValuesIn(RegionDataConstants::GetRegionCodes())); 88 89 TEST(RegionDataConstantsTest, GetMaxLookupKeyDepth) { 90 EXPECT_EQ(0, RegionDataConstants::GetMaxLookupKeyDepth("NZ")); 91 EXPECT_EQ(1, RegionDataConstants::GetMaxLookupKeyDepth("HK")); 92 EXPECT_EQ(2, RegionDataConstants::GetMaxLookupKeyDepth("US")); 93 EXPECT_EQ(3, RegionDataConstants::GetMaxLookupKeyDepth("CN")); 94 } 95 96 } // namespace 97