| OLD | NEW |
| (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 TEST(SubstringSetMatcherTest, SubstringPattern) { |
| 16 SubstringPattern r1("Test", 2); |
| 17 EXPECT_EQ("Test", r1.pattern()); |
| 18 EXPECT_EQ(2, r1.id()); |
| 19 |
| 20 EXPECT_FALSE(r1 < r1); |
| 21 SubstringPattern r2("Test", 3); |
| 22 EXPECT_TRUE(r1 < r2); |
| 23 SubstringPattern r3("ZZZZ", 2); |
| 24 EXPECT_TRUE(r1 < r3); |
| 25 } |
| 26 |
| 27 namespace { |
| 28 void TestOnePattern(const std::string& test_string, |
| 29 const std::string& pattern, |
| 30 bool is_match) { |
| 31 std::vector<const SubstringPattern*> patterns; |
| 32 SubstringPattern substring_pattern(pattern, 1); |
| 33 patterns.push_back(&substring_pattern); |
| 34 SubstringSetMatcher matcher; |
| 35 matcher.RegisterPatterns(patterns); |
| 36 std::set<int> matches; |
| 37 matcher.Match(test_string, &matches); |
| 38 |
| 39 size_t expected_matches = (is_match ? 1 : 0); |
| 40 EXPECT_EQ(expected_matches, matches.size()); |
| 41 EXPECT_EQ(is_match, matches.find(1) != matches.end()); |
| 42 } |
| 43 |
| 44 void TestTwoPatterns(const std::string& test_string, |
| 45 const std::string& pattern_1, |
| 46 const std::string& pattern_2, |
| 47 bool is_match_1, |
| 48 bool is_match_2) { |
| 49 SubstringPattern substring_pattern_1(pattern_1, 1); |
| 50 SubstringPattern substring_pattern_2(pattern_2, 2); |
| 51 // In order to make sure that the order in which patterns are registered |
| 52 // does not make any difference we try both permutations. |
| 53 for (int permutation = 0; permutation < 2; ++permutation) { |
| 54 std::vector<const SubstringPattern*> patterns; |
| 55 if (permutation == 0) { |
| 56 patterns.push_back(&substring_pattern_1); |
| 57 patterns.push_back(&substring_pattern_2); |
| 58 } else { |
| 59 patterns.push_back(&substring_pattern_2); |
| 60 patterns.push_back(&substring_pattern_1); |
| 61 } |
| 62 SubstringSetMatcher matcher; |
| 63 matcher.RegisterPatterns(patterns); |
| 64 std::set<int> matches; |
| 65 matcher.Match(test_string, &matches); |
| 66 |
| 67 size_t expected_matches = (is_match_1 ? 1 : 0) + (is_match_2 ? 1 : 0); |
| 68 EXPECT_EQ(expected_matches, matches.size()); |
| 69 EXPECT_EQ(is_match_1, matches.find(1) != matches.end()); |
| 70 EXPECT_EQ(is_match_2, matches.find(2) != matches.end()); |
| 71 } |
| 72 } |
| 73 } |
| 74 |
| 75 TEST(SubstringSetMatcherTest, TestMatcher) { |
| 76 // Test overlapping patterns |
| 77 // String abcde |
| 78 // Pattern 1 bc |
| 79 // Pattern 2 cd |
| 80 TestTwoPatterns("abcde", "bc", "cd", true, true); |
| 81 |
| 82 // Test subpatterns - part 1 |
| 83 // String abcde |
| 84 // Pattern 1 bc |
| 85 // Pattern 2 b |
| 86 TestTwoPatterns("abcde", "bc", "b", true, true); |
| 87 |
| 88 // Test subpatterns - part 2 |
| 89 // String abcde |
| 90 // Pattern 1 bc |
| 91 // Pattern 2 c |
| 92 TestTwoPatterns("abcde", "bc", "c", true, true); |
| 93 |
| 94 // Test identical matches |
| 95 // String abcde |
| 96 // Pattern 1 abcde |
| 97 TestOnePattern("abcde", "abcde", true); |
| 98 |
| 99 // Test multiple matches |
| 100 // String aaaaa |
| 101 // Pattern 1 a |
| 102 TestOnePattern("abcde", "a", true); |
| 103 |
| 104 // Test matches at beginning and end |
| 105 // String abcde |
| 106 // Pattern 1 ab |
| 107 // Pattern 2 de |
| 108 TestTwoPatterns("abcde", "ab", "de", true, true); |
| 109 |
| 110 // Test duplicate patterns with different IDs |
| 111 // String abcde |
| 112 // Pattern 1 bc |
| 113 // Pattern 2 bc |
| 114 TestTwoPatterns("abcde", "bc", "bc", true, true); |
| 115 |
| 116 // Test non-match |
| 117 // String abcde |
| 118 // Pattern 1 fg |
| 119 TestOnePattern("abcde", "fg", false); |
| 120 |
| 121 // Test empty pattern and too long pattern |
| 122 // String abcde |
| 123 // Pattern 1 |
| 124 // Pattern 2 abcdef |
| 125 TestTwoPatterns("abcde", "", "abcdef", true, false); |
| 126 } |
| 127 |
| 128 TEST(SubstringSetMatcherTest, RegisterAndRemove) { |
| 129 SubstringSetMatcher matcher; |
| 130 |
| 131 SubstringPattern pattern_1("a", 1); |
| 132 SubstringPattern pattern_2("b", 2); |
| 133 SubstringPattern pattern_3("c", 3); |
| 134 |
| 135 std::vector<const SubstringPattern*> patterns; |
| 136 patterns.push_back(&pattern_1); |
| 137 matcher.RegisterPatterns(patterns); |
| 138 |
| 139 patterns.clear(); |
| 140 patterns.push_back(&pattern_2); |
| 141 patterns.push_back(&pattern_3); |
| 142 matcher.RegisterPatterns(patterns); |
| 143 |
| 144 std::set<int> matches; |
| 145 matcher.Match("abd", &matches); |
| 146 EXPECT_EQ(2u, matches.size()); |
| 147 EXPECT_TRUE(matches.end() != matches.find(1)); |
| 148 EXPECT_TRUE(matches.end() != matches.find(2)); |
| 149 |
| 150 patterns.clear(); |
| 151 patterns.push_back(&pattern_2); |
| 152 matcher.UnregisterPatterns(patterns); |
| 153 |
| 154 matches.clear(); |
| 155 matcher.Match("abd", &matches); |
| 156 EXPECT_EQ(1u, matches.size()); |
| 157 EXPECT_TRUE(matches.end() != matches.find(1)); |
| 158 EXPECT_TRUE(matches.end() == matches.find(2)); |
| 159 } |
| 160 |
| OLD | NEW |