1 // Copyright 2013 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 "components/url_matcher/regex_set_matcher.h"
6
7 #include <set>
8
9 #include "base/stl_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "url/gurl.h"
12
13 namespace url_matcher {
14
TEST(RegexSetMatcherTest,MatchRegexes)15 TEST(RegexSetMatcherTest, MatchRegexes) {
16 StringPattern pattern_1("ab.*c", 42);
17 StringPattern pattern_2("f*f", 17);
18 StringPattern pattern_3("c(ar|ra)b|brac", 239);
19 std::vector<const StringPattern*> regexes;
20 regexes.push_back(&pattern_1);
21 regexes.push_back(&pattern_2);
22 regexes.push_back(&pattern_3);
23 RegexSetMatcher matcher;
24 matcher.AddPatterns(regexes);
25
26 std::set<StringPattern::ID> result1;
27 matcher.Match("http://abracadabra.com", &result1);
28 EXPECT_EQ(2U, result1.size());
29 EXPECT_TRUE(ContainsKey(result1, 42));
30 EXPECT_TRUE(ContainsKey(result1, 239));
31
32 std::set<StringPattern::ID> result2;
33 matcher.Match("https://abfffffffffffffffffffffffffffffff.fi/cf", &result2);
34 EXPECT_EQ(2U, result2.size());
35 EXPECT_TRUE(ContainsKey(result2, 17));
36 EXPECT_TRUE(ContainsKey(result2, 42));
37
38 std::set<StringPattern::ID> result3;
39 matcher.Match("http://nothing.com/", &result3);
40 EXPECT_EQ(0U, result3.size());
41 }
42
TEST(RegexSetMatcherTest,CaseSensitivity)43 TEST(RegexSetMatcherTest, CaseSensitivity) {
44 StringPattern pattern_1("AAA", 51);
45 StringPattern pattern_2("aaA", 57);
46 std::vector<const StringPattern*> regexes;
47 regexes.push_back(&pattern_1);
48 regexes.push_back(&pattern_2);
49 RegexSetMatcher matcher;
50 matcher.AddPatterns(regexes);
51
52 std::set<StringPattern::ID> result1;
53 matcher.Match("http://aaa.net/", &result1);
54 EXPECT_EQ(0U, result1.size());
55
56 std::set<StringPattern::ID> result2;
57 matcher.Match("http://aaa.net/quaaACK", &result2);
58 EXPECT_EQ(1U, result2.size());
59 EXPECT_TRUE(ContainsKey(result2, 57));
60 }
61
62 } // namespace url_matcher
63