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