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.
5 #include "extensions/common/matcher/substring_set_matcher.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using extensions::StringPattern;
14 using extensions::SubstringSetMatcher;
17 void TestOnePattern(const std::string& test_string,
18 const std::string& pattern,
21 "TestOnePattern(" + test_string + ", " + pattern + ", " +
22 (is_match ? "1" : "0") + ")";
23 std::vector<const StringPattern*> patterns;
24 StringPattern substring_pattern(pattern, 1);
25 patterns.push_back(&substring_pattern);
26 SubstringSetMatcher matcher;
27 matcher.RegisterPatterns(patterns);
28 std::set<int> matches;
29 matcher.Match(test_string, &matches);
31 size_t expected_matches = (is_match ? 1 : 0);
32 EXPECT_EQ(expected_matches, matches.size()) << test;
33 EXPECT_EQ(is_match, matches.find(1) != matches.end()) << test;
36 void TestTwoPatterns(const std::string& test_string,
37 const std::string& pattern_1,
38 const std::string& pattern_2,
42 "TestTwoPatterns(" + test_string + ", " + pattern_1 + ", " + pattern_2 +
43 ", " + (is_match_1 ? "1" : "0") + ", " + (is_match_2 ? "1" : "0") + ")";
44 StringPattern substring_pattern_1(pattern_1, 1);
45 StringPattern substring_pattern_2(pattern_2, 2);
46 // In order to make sure that the order in which patterns are registered
47 // does not make any difference we try both permutations.
48 for (int permutation = 0; permutation < 2; ++permutation) {
49 std::vector<const StringPattern*> patterns;
50 if (permutation == 0) {
51 patterns.push_back(&substring_pattern_1);
52 patterns.push_back(&substring_pattern_2);
54 patterns.push_back(&substring_pattern_2);
55 patterns.push_back(&substring_pattern_1);
57 SubstringSetMatcher matcher;
58 matcher.RegisterPatterns(patterns);
59 std::set<int> matches;
60 matcher.Match(test_string, &matches);
62 size_t expected_matches = (is_match_1 ? 1 : 0) + (is_match_2 ? 1 : 0);
63 EXPECT_EQ(expected_matches, matches.size()) << test;
64 EXPECT_EQ(is_match_1, matches.find(1) != matches.end()) << test;
65 EXPECT_EQ(is_match_2, matches.find(2) != matches.end()) << test;
70 TEST(SubstringSetMatcherTest, TestMatcher) {
71 // Test overlapping patterns
75 TestTwoPatterns("abcde", "bc", "cd", true, true);
77 // Test subpatterns - part 1
81 TestTwoPatterns("abcde", "bc", "b", true, true);
83 // Test subpatterns - part 2
87 TestTwoPatterns("abcde", "bc", "c", true, true);
89 // Test identical matches
92 TestOnePattern("abcde", "abcde", true);
94 // Test multiple matches
97 TestOnePattern("abcde", "a", true);
99 // Test matches at beginning and end
103 TestTwoPatterns("abcde", "ab", "de", true, true);
105 // Test duplicate patterns with different IDs
109 TestTwoPatterns("abcde", "bc", "bc", true, true);
114 TestOnePattern("abcde", "fg", false);
116 // Test empty pattern and too long pattern
120 TestTwoPatterns("abcde", std::string(), "abcdef", true, false);
123 TEST(SubstringSetMatcherTest, RegisterAndRemove) {
124 SubstringSetMatcher matcher;
126 StringPattern pattern_1("a", 1);
127 StringPattern pattern_2("b", 2);
128 StringPattern pattern_3("c", 3);
130 std::vector<const StringPattern*> patterns;
131 patterns.push_back(&pattern_1);
132 matcher.RegisterPatterns(patterns);
135 patterns.push_back(&pattern_2);
136 patterns.push_back(&pattern_3);
137 matcher.RegisterPatterns(patterns);
139 std::set<int> matches;
140 matcher.Match("abd", &matches);
141 EXPECT_EQ(2u, matches.size());
142 EXPECT_TRUE(matches.end() != matches.find(1));
143 EXPECT_TRUE(matches.end() != matches.find(2));
146 patterns.push_back(&pattern_2);
147 matcher.UnregisterPatterns(patterns);
150 matcher.Match("abd", &matches);
151 EXPECT_EQ(1u, matches.size());
152 EXPECT_TRUE(matches.end() != matches.find(1));
153 EXPECT_TRUE(matches.end() == matches.find(2));
156 patterns.push_back(&pattern_1);
157 patterns.push_back(&pattern_3);
158 matcher.UnregisterPatterns(patterns);
159 EXPECT_TRUE(matcher.IsEmpty());
162 TEST(SubstringSetMatcherTest, TestEmptyMatcher) {
163 SubstringSetMatcher matcher;
164 std::set<int> matches;
165 matcher.Match("abd", &matches);
166 EXPECT_TRUE(matches.empty());