Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: chrome/browser/extensions/api/declarative/substring_set_matcher_unittest.cc

Issue 9390018: Implementation of a Matching strategy for URLs in the Declarative WebRequest API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/api/declarative/substring_set_matcher.h"
6
7 #include <set>
8 #include <string>
9 #include <vector>
10
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 using namespace extensions;
14
15 // Basic tests of SubstringPattern objects
16 TEST(SubstringSetMatcherTest, SubstringPattern) {
17 SubstringPattern r1("Test", 2);
18 EXPECT_EQ("Test", r1.pattern());
19 EXPECT_EQ(2, r1.id());
20 EXPECT_EQ(r1, r1);
21
22 SubstringPattern r2(r1);
23 EXPECT_EQ("Test", r2.pattern());
24 EXPECT_EQ(2, r2.id());
25 EXPECT_EQ(r1, r2);
26 EXPECT_EQ(r2, r1);
27
28 SubstringPattern r3("Foo", 3);
29 r3 = r2;
30 EXPECT_EQ("Test", r3.pattern());
31 EXPECT_EQ(2, r3.id());
32 EXPECT_EQ(r2, r3);
33 EXPECT_EQ(r3, r2);
34 }
35
36 namespace {
37 void TestOnePattern(const std::string& test_string,
38 const std::string& pattern,
39 bool is_match) {
40 std::vector<SubstringPattern> patterns;
41 patterns.push_back(SubstringPattern(pattern, 1));
42 SubstringSetMatcher matcher;
43 matcher.RegisterPatterns(patterns);
44 std::set<int> matches;
45 matcher.Match(test_string, &matches);
46
47 size_t expected_matches = (is_match ? 1 : 0);
48 EXPECT_EQ(expected_matches, matches.size());
49 EXPECT_EQ(is_match, matches.find(1) != matches.end());
50 }
51
52 void TestTwoPatterns(const std::string& test_string,
53 const std::string& pattern_1,
54 const std::string& pattern_2,
55 bool is_match_1,
56 bool is_match_2) {
57 // In order to make sure that the order in which patterns are registered
58 // does not make any difference we try both permutations.
59 for (int permutation = 0; permutation < 2; ++permutation) {
60 std::vector<SubstringPattern> patterns;
61 if (permutation == 0) {
62 patterns.push_back(SubstringPattern(pattern_1, 1));
63 patterns.push_back(SubstringPattern(pattern_2, 2));
64 } else {
65 patterns.push_back(SubstringPattern(pattern_2, 2));
66 patterns.push_back(SubstringPattern(pattern_1, 1));
67 }
68 SubstringSetMatcher matcher;
69 matcher.RegisterPatterns(patterns);
70 std::set<int> matches;
71 matcher.Match(test_string, &matches);
72
73 size_t expected_matches = (is_match_1 ? 1 : 0) + (is_match_2 ? 1 : 0);
74 EXPECT_EQ(expected_matches, matches.size());
75 EXPECT_EQ(is_match_1, matches.find(1) != matches.end());
76 EXPECT_EQ(is_match_2, matches.find(2) != matches.end());
77 }
78 }
79 }
80
81 TEST(SubstringSetMatcherTest, TestMatcher) {
82 // Test overlapping patterns
83 // String abcde
84 // Pattern 1 bc
85 // Pattern 2 cd
86 TestTwoPatterns("abcde", "bc", "cd", true, true);
87
88 // Test subpatterns - part 1
89 // String abcde
90 // Pattern 1 bc
91 // Pattern 2 b
92 TestTwoPatterns("abcde", "bc", "b", true, true);
93
94 // Test subpatterns - part 2
95 // String abcde
96 // Pattern 1 bc
97 // Pattern 2 c
98 TestTwoPatterns("abcde", "bc", "c", true, true);
99
100 // Test identical matches
101 // String abcde
102 // Pattern 1 abcde
103 TestOnePattern("abcde", "abcde", true);
104
105 // Test multiple matches
106 // String aaaaa
107 // Pattern 1 a
108 TestOnePattern("abcde", "a", true);
109
110 // Test matches at beginning and end
111 // String abcde
112 // Pattern 1 ab
113 // Pattern 2 de
114 TestTwoPatterns("abcde", "ab", "de", true, true);
115
116 // Test duplicate patterns with different IDs
117 // String abcde
118 // Pattern 1 bc
119 // Pattern 2 bc
120 TestTwoPatterns("abcde", "bc", "bc", true, true);
121
122 // Test non-match
123 // String abcde
124 // Pattern 1 fg
125 TestOnePattern("abcde", "fg", false);
126
127 // Test empty pattern and too long pattern
128 // String abcde
129 // Pattern 1
130 // Pattern 2 abcdef
131 TestTwoPatterns("abcde", "", "abcdef", true, false);
132 }
133
134 TEST(SubstringSetMatcherTest, Clear) {
135 // Setup one pattern.
136 std::vector<SubstringPattern> patterns;
137 patterns.push_back(SubstringPattern("a", 1));
138 SubstringSetMatcher matcher;
139 matcher.RegisterPatterns(patterns);
140
141 // Verify state.
142 std::set<int> matches;
143 matcher.Match("abc", &matches);
144 EXPECT_EQ(1u, matches.size());
145 EXPECT_NE(matches.end(), matches.find(1));
146
147 matches.clear();
148
149 // Check that clearing the pattern works.
150 matcher.Clear();
151 matcher.Match("abc", &matches);
152 EXPECT_EQ(0u, matches.size());
153 EXPECT_EQ(matches.end(), matches.find(1));
154 }
155
156 TEST(SubstringSetMatcherTest, RegisterAndRemove) {
157 SubstringSetMatcher matcher;
158
159 std::vector<SubstringPattern> patterns;
160 patterns.push_back(SubstringPattern("a", 1));
161 matcher.RegisterPatterns(patterns);
162
163 patterns.clear();
164 patterns.push_back(SubstringPattern("b", 2));
165 patterns.push_back(SubstringPattern("c", 3));
166 matcher.RegisterPatterns(patterns);
167
168 std::set<int> matches;
169 matcher.Match("abd", &matches);
170 EXPECT_EQ(2u, matches.size());
171 EXPECT_NE(matches.end(), matches.find(1));
172 EXPECT_NE(matches.end(), matches.find(2));
173
174 patterns.clear();
175 patterns.push_back(SubstringPattern("b", 2));
176 matcher.UnregisterPatterns(patterns);
177
178 matches.clear();
179 matcher.Match("abd", &matches);
180 EXPECT_EQ(1u, matches.size());
181 EXPECT_NE(matches.end(), matches.find(1));
182 EXPECT_EQ(matches.end(), matches.find(2));
183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698