OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/extensions/matcher/substring_set_matcher.h" | 5 #include "chrome/common/extensions/matcher/substring_set_matcher.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 using extensions::SubstringPattern; | 13 using extensions::StringPattern; |
14 using extensions::SubstringSetMatcher; | 14 using extensions::SubstringSetMatcher; |
15 | 15 |
16 TEST(SubstringSetMatcherTest, SubstringPattern) { | |
17 SubstringPattern r1("Test", 2); | |
18 EXPECT_EQ("Test", r1.pattern()); | |
19 EXPECT_EQ(2, r1.id()); | |
20 | |
21 EXPECT_FALSE(r1 < r1); | |
22 SubstringPattern r2("Test", 3); | |
23 EXPECT_TRUE(r1 < r2); | |
24 SubstringPattern r3("ZZZZ", 2); | |
25 EXPECT_TRUE(r1 < r3); | |
26 } | |
27 | |
28 namespace { | 16 namespace { |
29 void TestOnePattern(const std::string& test_string, | 17 void TestOnePattern(const std::string& test_string, |
30 const std::string& pattern, | 18 const std::string& pattern, |
31 bool is_match) { | 19 bool is_match) { |
32 std::string test = | 20 std::string test = |
33 "TestOnePattern(" + test_string + ", " + pattern + ", " + | 21 "TestOnePattern(" + test_string + ", " + pattern + ", " + |
34 (is_match ? "1" : "0") + ")"; | 22 (is_match ? "1" : "0") + ")"; |
35 std::vector<const SubstringPattern*> patterns; | 23 std::vector<const StringPattern*> patterns; |
36 SubstringPattern substring_pattern(pattern, 1); | 24 StringPattern substring_pattern(pattern, 1); |
37 patterns.push_back(&substring_pattern); | 25 patterns.push_back(&substring_pattern); |
38 SubstringSetMatcher matcher; | 26 SubstringSetMatcher matcher; |
39 matcher.RegisterPatterns(patterns); | 27 matcher.RegisterPatterns(patterns); |
40 std::set<int> matches; | 28 std::set<int> matches; |
41 matcher.Match(test_string, &matches); | 29 matcher.Match(test_string, &matches); |
42 | 30 |
43 size_t expected_matches = (is_match ? 1 : 0); | 31 size_t expected_matches = (is_match ? 1 : 0); |
44 EXPECT_EQ(expected_matches, matches.size()) << test; | 32 EXPECT_EQ(expected_matches, matches.size()) << test; |
45 EXPECT_EQ(is_match, matches.find(1) != matches.end()) << test; | 33 EXPECT_EQ(is_match, matches.find(1) != matches.end()) << test; |
46 } | 34 } |
47 | 35 |
48 void TestTwoPatterns(const std::string& test_string, | 36 void TestTwoPatterns(const std::string& test_string, |
49 const std::string& pattern_1, | 37 const std::string& pattern_1, |
50 const std::string& pattern_2, | 38 const std::string& pattern_2, |
51 bool is_match_1, | 39 bool is_match_1, |
52 bool is_match_2) { | 40 bool is_match_2) { |
53 std::string test = | 41 std::string test = |
54 "TestTwoPatterns(" + test_string + ", " + pattern_1 + ", " + pattern_2 + | 42 "TestTwoPatterns(" + test_string + ", " + pattern_1 + ", " + pattern_2 + |
55 ", " + (is_match_1 ? "1" : "0") + ", " + (is_match_2 ? "1" : "0") + ")"; | 43 ", " + (is_match_1 ? "1" : "0") + ", " + (is_match_2 ? "1" : "0") + ")"; |
56 SubstringPattern substring_pattern_1(pattern_1, 1); | 44 StringPattern substring_pattern_1(pattern_1, 1); |
57 SubstringPattern substring_pattern_2(pattern_2, 2); | 45 StringPattern substring_pattern_2(pattern_2, 2); |
58 // In order to make sure that the order in which patterns are registered | 46 // In order to make sure that the order in which patterns are registered |
59 // does not make any difference we try both permutations. | 47 // does not make any difference we try both permutations. |
60 for (int permutation = 0; permutation < 2; ++permutation) { | 48 for (int permutation = 0; permutation < 2; ++permutation) { |
61 std::vector<const SubstringPattern*> patterns; | 49 std::vector<const StringPattern*> patterns; |
62 if (permutation == 0) { | 50 if (permutation == 0) { |
63 patterns.push_back(&substring_pattern_1); | 51 patterns.push_back(&substring_pattern_1); |
64 patterns.push_back(&substring_pattern_2); | 52 patterns.push_back(&substring_pattern_2); |
65 } else { | 53 } else { |
66 patterns.push_back(&substring_pattern_2); | 54 patterns.push_back(&substring_pattern_2); |
67 patterns.push_back(&substring_pattern_1); | 55 patterns.push_back(&substring_pattern_1); |
68 } | 56 } |
69 SubstringSetMatcher matcher; | 57 SubstringSetMatcher matcher; |
70 matcher.RegisterPatterns(patterns); | 58 matcher.RegisterPatterns(patterns); |
71 std::set<int> matches; | 59 std::set<int> matches; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 // Test empty pattern and too long pattern | 116 // Test empty pattern and too long pattern |
129 // String abcde | 117 // String abcde |
130 // Pattern 1 | 118 // Pattern 1 |
131 // Pattern 2 abcdef | 119 // Pattern 2 abcdef |
132 TestTwoPatterns("abcde", "", "abcdef", true, false); | 120 TestTwoPatterns("abcde", "", "abcdef", true, false); |
133 } | 121 } |
134 | 122 |
135 TEST(SubstringSetMatcherTest, RegisterAndRemove) { | 123 TEST(SubstringSetMatcherTest, RegisterAndRemove) { |
136 SubstringSetMatcher matcher; | 124 SubstringSetMatcher matcher; |
137 | 125 |
138 SubstringPattern pattern_1("a", 1); | 126 StringPattern pattern_1("a", 1); |
139 SubstringPattern pattern_2("b", 2); | 127 StringPattern pattern_2("b", 2); |
140 SubstringPattern pattern_3("c", 3); | 128 StringPattern pattern_3("c", 3); |
141 | 129 |
142 std::vector<const SubstringPattern*> patterns; | 130 std::vector<const StringPattern*> patterns; |
143 patterns.push_back(&pattern_1); | 131 patterns.push_back(&pattern_1); |
144 matcher.RegisterPatterns(patterns); | 132 matcher.RegisterPatterns(patterns); |
145 | 133 |
146 patterns.clear(); | 134 patterns.clear(); |
147 patterns.push_back(&pattern_2); | 135 patterns.push_back(&pattern_2); |
148 patterns.push_back(&pattern_3); | 136 patterns.push_back(&pattern_3); |
149 matcher.RegisterPatterns(patterns); | 137 matcher.RegisterPatterns(patterns); |
150 | 138 |
151 std::set<int> matches; | 139 std::set<int> matches; |
152 matcher.Match("abd", &matches); | 140 matcher.Match("abd", &matches); |
(...skipping 17 matching lines...) Expand all Loading... |
170 matcher.UnregisterPatterns(patterns); | 158 matcher.UnregisterPatterns(patterns); |
171 EXPECT_TRUE(matcher.IsEmpty()); | 159 EXPECT_TRUE(matcher.IsEmpty()); |
172 } | 160 } |
173 | 161 |
174 TEST(SubstringSetMatcherTest, TestEmptyMatcher) { | 162 TEST(SubstringSetMatcherTest, TestEmptyMatcher) { |
175 SubstringSetMatcher matcher; | 163 SubstringSetMatcher matcher; |
176 std::set<int> matches; | 164 std::set<int> matches; |
177 matcher.Match("abd", &matches); | 165 matcher.Match("abd", &matches); |
178 EXPECT_TRUE(matches.empty()); | 166 EXPECT_TRUE(matches.empty()); |
179 } | 167 } |
OLD | NEW |