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 "address_field_util.h" 16 17#include <libaddressinput/address_field.h> 18 19#include <string> 20#include <vector> 21 22#include <gtest/gtest.h> 23 24#include "format_element.h" 25 26namespace { 27 28using i18n::addressinput::AddressField; 29using i18n::addressinput::FormatElement; 30using i18n::addressinput::ParseFormatRule; 31 32using i18n::addressinput::COUNTRY; 33using i18n::addressinput::LOCALITY; 34using i18n::addressinput::POSTAL_CODE; 35using i18n::addressinput::STREET_ADDRESS; 36using i18n::addressinput::ORGANIZATION; 37using i18n::addressinput::RECIPIENT; 38 39TEST(AddressFieldUtilTest, FormatParseNewline) { 40 std::vector<FormatElement> actual; 41 ParseFormatRule("%O%n%N%n%A%nAX-%Z %C%n\xC3\x85LAND", &actual); /* "ÅLAND" */ 42 43 std::vector<FormatElement> expected; 44 expected.push_back(FormatElement(ORGANIZATION)); 45 expected.push_back(FormatElement()); 46 expected.push_back(FormatElement(RECIPIENT)); 47 expected.push_back(FormatElement()); 48 expected.push_back(FormatElement(STREET_ADDRESS)); 49 expected.push_back(FormatElement()); 50 expected.push_back(FormatElement("AX-")); 51 expected.push_back(FormatElement(POSTAL_CODE)); 52 expected.push_back(FormatElement(" ")); 53 expected.push_back(FormatElement(LOCALITY)); 54 expected.push_back(FormatElement()); 55 expected.push_back(FormatElement("\xC3\x85LAND")); /* "ÅLAND" */ 56 57 EXPECT_EQ(expected, actual); 58} 59 60TEST(AddressFieldUtilTest, FormatUnknownTokenIsIgnored) { 61 std::vector<FormatElement> actual; 62 ParseFormatRule("%1%R", &actual); // %1 is not supported. 63 std::vector<FormatElement> expected(1, FormatElement(COUNTRY)); 64 EXPECT_EQ(expected, actual); 65} 66 67TEST(AddressFieldUtilTest, FormatPrefixWithoutTokenIsIgnored) { 68 std::vector<FormatElement> actual; 69 ParseFormatRule("%", &actual); 70 EXPECT_TRUE(actual.empty()); 71} 72 73TEST(AddressFieldUtilTest, FormatEmptyString) { 74 std::vector<FormatElement> fields; 75 ParseFormatRule(std::string(), &fields); 76 EXPECT_TRUE(fields.empty()); 77} 78 79TEST(AddressFieldUtilTest, RequiredParseDefault) { 80 std::vector<AddressField> actual; 81 ParseAddressFieldsRequired("AC", &actual); 82 83 std::vector<AddressField> expected; 84 expected.push_back(STREET_ADDRESS); 85 expected.push_back(LOCALITY); 86 87 EXPECT_EQ(expected, actual); 88} 89 90TEST(AddressFieldUtilTest, RequiredEmptyString) { 91 std::vector<AddressField> fields; 92 ParseAddressFieldsRequired(std::string(), &fields); 93 EXPECT_TRUE(fields.empty()); 94} 95 96} // namespace 97