Home | History | Annotate | Download | only in test
      1 // Copyright (C) 2014 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 "post_box_matchers.h"
     16 
     17 #include <cstddef>
     18 #include <vector>
     19 
     20 #include "rule.h"
     21 
     22 #include <gtest/gtest.h>
     23 
     24 namespace i18n {
     25 namespace addressinput {
     26 class RE2ptr;
     27 }  // namespace addressinput
     28 }  // namespace i18n
     29 
     30 namespace {
     31 
     32 using i18n::addressinput::PostBoxMatchers;
     33 using i18n::addressinput::RE2ptr;
     34 using i18n::addressinput::Rule;
     35 
     36 TEST(PostBoxMatchersTest, AlwaysGetMatcherForLanguageUnd) {
     37   Rule rule;
     38   std::vector<const RE2ptr*> matchers = PostBoxMatchers::GetMatchers(rule);
     39   EXPECT_EQ(1, matchers.size());
     40   EXPECT_TRUE(matchers[0] != NULL);
     41 }
     42 
     43 TEST(PostBoxMatchersTest, NoMatcherForInvalidLanguage) {
     44   Rule rule;
     45   ASSERT_TRUE(rule.ParseSerializedRule("{\"languages\":\"xx\"}"));
     46   std::vector<const RE2ptr*> matchers = PostBoxMatchers::GetMatchers(rule);
     47   EXPECT_EQ(1, matchers.size());
     48   EXPECT_TRUE(matchers[0] != NULL);
     49 }
     50 
     51 TEST(PostBoxMatchersTest, HasMatcherForValidLanguage) {
     52   Rule rule;
     53   ASSERT_TRUE(rule.ParseSerializedRule("{\"languages\":\"sv\"}"));
     54   std::vector<const RE2ptr*> matchers = PostBoxMatchers::GetMatchers(rule);
     55   EXPECT_EQ(2, matchers.size());
     56   EXPECT_TRUE(matchers[0] != NULL);
     57   EXPECT_TRUE(matchers[1] != NULL);
     58 }
     59 
     60 TEST(PostBoxMatchersTest, MixValidAndInvalidLanguage) {
     61   Rule rule;
     62   ASSERT_TRUE(rule.ParseSerializedRule("{\"languages\":\"xx~sv\"}"));
     63   std::vector<const RE2ptr*> matchers = PostBoxMatchers::GetMatchers(rule);
     64   EXPECT_EQ(2, matchers.size());
     65   EXPECT_TRUE(matchers[0] != NULL);
     66   EXPECT_TRUE(matchers[1] != NULL);
     67 }
     68 
     69 TEST(PostBoxMatchersTest, UseBaseLanguageForMatching) {
     70   Rule rule;
     71   ASSERT_TRUE(rule.ParseSerializedRule("{\"languages\":\"sv-SE\"}"));
     72   std::vector<const RE2ptr*> matchers = PostBoxMatchers::GetMatchers(rule);
     73   EXPECT_EQ(2, matchers.size());
     74   EXPECT_TRUE(matchers[0] != NULL);
     75   EXPECT_TRUE(matchers[1] != NULL);
     76 }
     77 
     78 TEST(PostBoxMatchersTest, LenientLanguageTagParsing) {
     79   Rule rule;
     80   ASSERT_TRUE(rule.ParseSerializedRule("{\"languages\":\"SV_SE\"}"));
     81   std::vector<const RE2ptr*> matchers = PostBoxMatchers::GetMatchers(rule);
     82   EXPECT_EQ(2, matchers.size());
     83   EXPECT_TRUE(matchers[0] != NULL);
     84   EXPECT_TRUE(matchers[1] != NULL);
     85 }
     86 
     87 }  // namespace
     88