| Index: chrome/browser/extensions/api/declarative/substring_set_matcher_unittest.cc
|
| diff --git a/chrome/browser/extensions/api/declarative/substring_set_matcher_unittest.cc b/chrome/browser/extensions/api/declarative/substring_set_matcher_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..310c961e76f4004e4f3ee8015f2cd5f0cdddbaac
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/declarative/substring_set_matcher_unittest.cc
|
| @@ -0,0 +1,183 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/extensions/api/declarative/substring_set_matcher.h"
|
| +
|
| +#include <set>
|
| +#include <string>
|
| +#include <vector>
|
| +
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +using namespace extensions;
|
| +
|
| +// Basic tests of SubstringPattern objects
|
| +TEST(SubstringSetMatcherTest, SubstringPattern) {
|
| + SubstringPattern r1("Test", 2);
|
| + EXPECT_EQ("Test", r1.pattern());
|
| + EXPECT_EQ(2, r1.id());
|
| + EXPECT_EQ(r1, r1);
|
| +
|
| + SubstringPattern r2(r1);
|
| + EXPECT_EQ("Test", r2.pattern());
|
| + EXPECT_EQ(2, r2.id());
|
| + EXPECT_EQ(r1, r2);
|
| + EXPECT_EQ(r2, r1);
|
| +
|
| + SubstringPattern r3("Foo", 3);
|
| + r3 = r2;
|
| + EXPECT_EQ("Test", r3.pattern());
|
| + EXPECT_EQ(2, r3.id());
|
| + EXPECT_EQ(r2, r3);
|
| + EXPECT_EQ(r3, r2);
|
| +}
|
| +
|
| +namespace {
|
| +void TestOnePattern(const std::string& test_string,
|
| + const std::string& pattern,
|
| + bool is_match) {
|
| + std::vector<SubstringPattern> patterns;
|
| + patterns.push_back(SubstringPattern(pattern, 1));
|
| + SubstringSetMatcher matcher;
|
| + matcher.RegisterPatterns(patterns);
|
| + std::set<int> matches;
|
| + matcher.Match(test_string, &matches);
|
| +
|
| + size_t expected_matches = (is_match ? 1 : 0);
|
| + EXPECT_EQ(expected_matches, matches.size());
|
| + EXPECT_EQ(is_match, matches.find(1) != matches.end());
|
| +}
|
| +
|
| +void TestTwoPatterns(const std::string& test_string,
|
| + const std::string& pattern_1,
|
| + const std::string& pattern_2,
|
| + bool is_match_1,
|
| + bool is_match_2) {
|
| + // In order to make sure that the order in which patterns are registered
|
| + // does not make any difference we try both permutations.
|
| + for (int permutation = 0; permutation < 2; ++permutation) {
|
| + std::vector<SubstringPattern> patterns;
|
| + if (permutation == 0) {
|
| + patterns.push_back(SubstringPattern(pattern_1, 1));
|
| + patterns.push_back(SubstringPattern(pattern_2, 2));
|
| + } else {
|
| + patterns.push_back(SubstringPattern(pattern_2, 2));
|
| + patterns.push_back(SubstringPattern(pattern_1, 1));
|
| + }
|
| + SubstringSetMatcher matcher;
|
| + matcher.RegisterPatterns(patterns);
|
| + std::set<int> matches;
|
| + matcher.Match(test_string, &matches);
|
| +
|
| + size_t expected_matches = (is_match_1 ? 1 : 0) + (is_match_2 ? 1 : 0);
|
| + EXPECT_EQ(expected_matches, matches.size());
|
| + EXPECT_EQ(is_match_1, matches.find(1) != matches.end());
|
| + EXPECT_EQ(is_match_2, matches.find(2) != matches.end());
|
| + }
|
| +}
|
| +}
|
| +
|
| +TEST(SubstringSetMatcherTest, TestMatcher) {
|
| + // Test overlapping patterns
|
| + // String abcde
|
| + // Pattern 1 bc
|
| + // Pattern 2 cd
|
| + TestTwoPatterns("abcde", "bc", "cd", true, true);
|
| +
|
| + // Test subpatterns - part 1
|
| + // String abcde
|
| + // Pattern 1 bc
|
| + // Pattern 2 b
|
| + TestTwoPatterns("abcde", "bc", "b", true, true);
|
| +
|
| + // Test subpatterns - part 2
|
| + // String abcde
|
| + // Pattern 1 bc
|
| + // Pattern 2 c
|
| + TestTwoPatterns("abcde", "bc", "c", true, true);
|
| +
|
| + // Test identical matches
|
| + // String abcde
|
| + // Pattern 1 abcde
|
| + TestOnePattern("abcde", "abcde", true);
|
| +
|
| + // Test multiple matches
|
| + // String aaaaa
|
| + // Pattern 1 a
|
| + TestOnePattern("abcde", "a", true);
|
| +
|
| + // Test matches at beginning and end
|
| + // String abcde
|
| + // Pattern 1 ab
|
| + // Pattern 2 de
|
| + TestTwoPatterns("abcde", "ab", "de", true, true);
|
| +
|
| + // Test duplicate patterns with different IDs
|
| + // String abcde
|
| + // Pattern 1 bc
|
| + // Pattern 2 bc
|
| + TestTwoPatterns("abcde", "bc", "bc", true, true);
|
| +
|
| + // Test non-match
|
| + // String abcde
|
| + // Pattern 1 fg
|
| + TestOnePattern("abcde", "fg", false);
|
| +
|
| + // Test empty pattern and too long pattern
|
| + // String abcde
|
| + // Pattern 1
|
| + // Pattern 2 abcdef
|
| + TestTwoPatterns("abcde", "", "abcdef", true, false);
|
| +}
|
| +
|
| +TEST(SubstringSetMatcherTest, Clear) {
|
| + // Setup one pattern.
|
| + std::vector<SubstringPattern> patterns;
|
| + patterns.push_back(SubstringPattern("a", 1));
|
| + SubstringSetMatcher matcher;
|
| + matcher.RegisterPatterns(patterns);
|
| +
|
| + // Verify state.
|
| + std::set<int> matches;
|
| + matcher.Match("abc", &matches);
|
| + EXPECT_EQ(1u, matches.size());
|
| + EXPECT_NE(matches.end(), matches.find(1));
|
| +
|
| + matches.clear();
|
| +
|
| + // Check that clearing the pattern works.
|
| + matcher.Clear();
|
| + matcher.Match("abc", &matches);
|
| + EXPECT_EQ(0u, matches.size());
|
| + EXPECT_EQ(matches.end(), matches.find(1));
|
| +}
|
| +
|
| +TEST(SubstringSetMatcherTest, RegisterAndRemove) {
|
| + SubstringSetMatcher matcher;
|
| +
|
| + std::vector<SubstringPattern> patterns;
|
| + patterns.push_back(SubstringPattern("a", 1));
|
| + matcher.RegisterPatterns(patterns);
|
| +
|
| + patterns.clear();
|
| + patterns.push_back(SubstringPattern("b", 2));
|
| + patterns.push_back(SubstringPattern("c", 3));
|
| + matcher.RegisterPatterns(patterns);
|
| +
|
| + std::set<int> matches;
|
| + matcher.Match("abd", &matches);
|
| + EXPECT_EQ(2u, matches.size());
|
| + EXPECT_NE(matches.end(), matches.find(1));
|
| + EXPECT_NE(matches.end(), matches.find(2));
|
| +
|
| + patterns.clear();
|
| + patterns.push_back(SubstringPattern("b", 2));
|
| + matcher.UnregisterPatterns(patterns);
|
| +
|
| + matches.clear();
|
| + matcher.Match("abd", &matches);
|
| + EXPECT_EQ(1u, matches.size());
|
| + EXPECT_NE(matches.end(), matches.find(1));
|
| + EXPECT_EQ(matches.end(), matches.find(2));
|
| +}
|
|
|