OLD | NEW |
(Empty) | |
| 1 // Copyright (C) 2011 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 // Author: George Yakovlev |
| 16 #include <gtest/gtest.h> |
| 17 |
| 18 #include "base/scoped_ptr.h" |
| 19 #include "regexp_adapter.h" |
| 20 |
| 21 namespace reg_exp { |
| 22 |
| 23 TEST(RegExpAdapter, TestConsumeRegExp) { |
| 24 scoped_ptr<const reg_exp::RegularExpression> reg_exp1( |
| 25 reg_exp::CreateRegularExpression("[0-9a-z]+")); |
| 26 scoped_ptr<const reg_exp::RegularExpression> reg_exp2( |
| 27 reg_exp::CreateRegularExpression(" \\(([0-9a-z]+)\\)")); |
| 28 scoped_ptr<const reg_exp::RegularExpression> reg_exp3( |
| 29 reg_exp::CreateRegularExpression("([0-9a-z]+)-([0-9a-z]+)")); |
| 30 |
| 31 scoped_ptr<reg_exp::RegularExpressionInput> reg_input1( |
| 32 reg_exp::CreateRegularExpressionInput("+1-123-456-789")); |
| 33 scoped_ptr<reg_exp::RegularExpressionInput> reg_input2( |
| 34 reg_exp::CreateRegularExpressionInput("1 (123)456-789")); |
| 35 |
| 36 EXPECT_FALSE(reg_exp1->Consume(reg_input1.get(), true, NULL, NULL)); |
| 37 EXPECT_EQ(reg_input1->ToString(), "+1-123-456-789"); |
| 38 EXPECT_TRUE(reg_exp1->Consume(reg_input1.get(), false, NULL, NULL)); |
| 39 EXPECT_EQ(reg_input1->ToString(), "-123-456-789"); |
| 40 std::string res1, res2; |
| 41 EXPECT_FALSE(reg_exp2->Consume(reg_input1.get(), true, &res1, NULL)); |
| 42 EXPECT_FALSE(reg_exp3->Consume(reg_input1.get(), true, &res1, &res2)); |
| 43 EXPECT_TRUE(reg_exp3->Consume(reg_input1.get(), false, &res1, &res2)); |
| 44 EXPECT_EQ(reg_input1->ToString(), "-789"); |
| 45 EXPECT_EQ(res1, "123"); |
| 46 EXPECT_EQ(res2, "456"); |
| 47 |
| 48 EXPECT_EQ(reg_input2->ToString(), "1 (123)456-789"); |
| 49 EXPECT_TRUE(reg_exp1->Consume(reg_input2.get(), true, NULL, NULL)); |
| 50 EXPECT_EQ(reg_input2->ToString(), " (123)456-789"); |
| 51 EXPECT_TRUE(reg_exp2->Consume(reg_input2.get(), true, &res1, NULL)); |
| 52 EXPECT_EQ(reg_input2->ToString(), "456-789"); |
| 53 EXPECT_EQ(res1, "123"); |
| 54 EXPECT_TRUE(reg_exp3->Consume(reg_input2.get(), true, &res1, &res2)); |
| 55 EXPECT_EQ(reg_input2->ToString(), ""); |
| 56 EXPECT_EQ(res1, "456"); |
| 57 EXPECT_EQ(res2, "789"); |
| 58 } |
| 59 |
| 60 TEST(RegExpAdapter, TestConsumeInput) { |
| 61 scoped_ptr<reg_exp::RegularExpressionInput> reg_input( |
| 62 reg_exp::CreateRegularExpressionInput("1 (123)456-789")); |
| 63 std::string res1, res2; |
| 64 EXPECT_EQ(reg_input->ToString(), "1 (123)456-789"); |
| 65 EXPECT_FALSE(reg_input->ConsumeRegExp(std::string("\\[1\\]"), |
| 66 true, |
| 67 &res1, |
| 68 &res2)); |
| 69 EXPECT_EQ(reg_input->ToString(), "1 (123)456-789"); |
| 70 EXPECT_FALSE(reg_input->ConsumeRegExp(std::string("([0-9]+) \\([0-9]+\\)"), |
| 71 true, |
| 72 &res1, |
| 73 &res2)); |
| 74 EXPECT_EQ(reg_input->ToString(), "1 (123)456-789"); |
| 75 EXPECT_TRUE(reg_input->ConsumeRegExp(std::string("([0-9]+) \\(([0-9]+)\\)"), |
| 76 true, |
| 77 &res1, |
| 78 &res2)); |
| 79 EXPECT_EQ(reg_input->ToString(), "456-789"); |
| 80 EXPECT_EQ(res1, "1"); |
| 81 EXPECT_EQ(res2, "123"); |
| 82 } |
| 83 |
| 84 TEST(RegExpAdapter, TestMatch) { |
| 85 scoped_ptr<const reg_exp::RegularExpression> reg_exp( |
| 86 reg_exp::CreateRegularExpression("([0-9a-z]+)")); |
| 87 std::string matched; |
| 88 EXPECT_TRUE(reg_exp->Match("12345af", true, &matched)); |
| 89 EXPECT_EQ(matched, "12345af"); |
| 90 EXPECT_TRUE(reg_exp->Match("12345af", false, &matched)); |
| 91 EXPECT_EQ(matched, "12345af"); |
| 92 EXPECT_TRUE(reg_exp->Match("12345af", false, NULL)); |
| 93 EXPECT_TRUE(reg_exp->Match("12345af", true, NULL)); |
| 94 |
| 95 EXPECT_FALSE(reg_exp->Match("[12]", true, &matched)); |
| 96 EXPECT_TRUE(reg_exp->Match("[12]", false, &matched)); |
| 97 EXPECT_EQ(matched, "12"); |
| 98 |
| 99 EXPECT_FALSE(reg_exp->Match("[]", true, &matched)); |
| 100 EXPECT_FALSE(reg_exp->Match("[]", false, &matched)); |
| 101 } |
| 102 |
| 103 TEST(RegExpAdapter, TestReplace) { |
| 104 scoped_ptr<const reg_exp::RegularExpression> reg_exp( |
| 105 reg_exp::CreateRegularExpression("[0-9]")); |
| 106 |
| 107 std::string s("123-4567 "); |
| 108 EXPECT_TRUE(reg_exp->Replace(&s, false, "+")); |
| 109 EXPECT_EQ(s, "+23-4567 "); |
| 110 EXPECT_TRUE(reg_exp->Replace(&s, false, "+")); |
| 111 EXPECT_EQ(s, "++3-4567 "); |
| 112 EXPECT_TRUE(reg_exp->Replace(&s, true, "*")); |
| 113 EXPECT_EQ(s, "++*-**** "); |
| 114 EXPECT_TRUE(reg_exp->Replace(&s, true, "*")); |
| 115 EXPECT_EQ(s, "++*-**** "); |
| 116 |
| 117 scoped_ptr<const reg_exp::RegularExpression> full_number_expr( |
| 118 reg_exp::CreateRegularExpression("(\\d{3})(\\d{3})(\\d{4})")); |
| 119 s = "1234567890:0987654321"; |
| 120 EXPECT_TRUE(full_number_expr->Replace(&s, true, "(\\1) \\2-\\3$1")); |
| 121 EXPECT_EQ(s, "(123) 456-7890$1:(098) 765-4321$1"); |
| 122 } |
| 123 |
| 124 TEST(RegExpAdapter, TestUtf8) { |
| 125 // Expression: <tel symbol><opening square bracket>[<alpha>-<omega>]* |
| 126 // <closing square bracket> |
| 127 scoped_ptr<const reg_exp::RegularExpression> reg_exp( |
| 128 reg_exp::CreateRegularExpression( |
| 129 "\xe2\x84\xa1\xe2\x8a\x8f([\xce\xb1-\xcf\x89]*)\xe2\x8a\x90")); |
| 130 std::string matched; |
| 131 // The string is split to avoid problem with MSVC compiler when it thinks |
| 132 // 123 is a part of character code. |
| 133 EXPECT_FALSE(reg_exp->Match("\xe2\x84\xa1\xe2\x8a\x8f" "123\xe2\x8a\x90", |
| 134 true, &matched)); |
| 135 EXPECT_TRUE(reg_exp->Match( |
| 136 "\xe2\x84\xa1\xe2\x8a\x8f\xce\xb1\xce\xb2\xe2\x8a\x90", true, &matched)); |
| 137 // <alpha><betha> |
| 138 EXPECT_EQ(matched, "\xce\xb1\xce\xb2"); |
| 139 } |
| 140 |
| 141 } // namespace reg_exp |
| 142 |
OLD | NEW |