Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Steinar H. Gunderson | 5570febc | 2022-05-12 10:39:48 | [diff] [blame] | 5 | #include "base/substring_set_matcher/substring_set_matcher.h" |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 6 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
Karan Bhatia | 60409e89 | 2020-02-11 04:14:36 | [diff] [blame] | 13 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
Steinar H. Gunderson | 5570febc | 2022-05-12 10:39:48 | [diff] [blame] | 16 | namespace base { |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 17 | |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 18 | namespace { |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 19 | |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 20 | void TestOnePattern(const std::string& test_string, |
| 21 | const std::string& pattern, |
| 22 | bool is_match) { |
Steinar H. Gunderson | 45e5abb | 2022-05-10 09:42:54 | [diff] [blame] | 23 | std::string test = "TestOnePattern(" + test_string + ", " + pattern + ", " + |
| 24 | (is_match ? "1" : "0") + ")"; |
Steinar H. Gunderson | f817493 | 2022-05-21 00:25:17 | [diff] [blame] | 25 | std::vector<MatcherStringPattern> patterns; |
Karan Bhatia | e39bc57a | 2020-02-06 20:04:17 | [diff] [blame] | 26 | patterns.emplace_back(pattern, 1); |
Steinar H. Gunderson | 45e5abb | 2022-05-10 09:42:54 | [diff] [blame] | 27 | SubstringSetMatcher matcher; |
| 28 | ASSERT_TRUE(matcher.Build(patterns)); |
Peter Kasting | 78549f3 | 2022-05-31 18:20:20 | [diff] [blame] | 29 | std::set<MatcherStringPattern::ID> matches; |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 30 | matcher.Match(test_string, &matches); |
| 31 | |
| 32 | size_t expected_matches = (is_match ? 1 : 0); |
[email protected] | 78033cd | 2012-02-29 03:56:15 | [diff] [blame] | 33 | EXPECT_EQ(expected_matches, matches.size()) << test; |
| 34 | EXPECT_EQ(is_match, matches.find(1) != matches.end()) << test; |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void TestTwoPatterns(const std::string& test_string, |
| 38 | const std::string& pattern_1, |
| 39 | const std::string& pattern_2, |
| 40 | bool is_match_1, |
| 41 | bool is_match_2) { |
Steinar H. Gunderson | 45e5abb | 2022-05-10 09:42:54 | [diff] [blame] | 42 | std::string test = "TestTwoPatterns(" + test_string + ", " + pattern_1 + |
| 43 | ", " + pattern_2 + ", " + (is_match_1 ? "1" : "0") + ", " + |
| 44 | (is_match_2 ? "1" : "0") + ")"; |
Karan Bhatia | 60409e89 | 2020-02-11 04:14:36 | [diff] [blame] | 45 | ASSERT_NE(pattern_1, pattern_2); |
Steinar H. Gunderson | f817493 | 2022-05-21 00:25:17 | [diff] [blame] | 46 | MatcherStringPattern substring_pattern_1(pattern_1, 1); |
| 47 | MatcherStringPattern substring_pattern_2(pattern_2, 2); |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 48 | // In order to make sure that the order in which patterns are registered |
| 49 | // does not make any difference we try both permutations. |
| 50 | for (int permutation = 0; permutation < 2; ++permutation) { |
Steinar H. Gunderson | f817493 | 2022-05-21 00:25:17 | [diff] [blame] | 51 | std::vector<const MatcherStringPattern*> patterns; |
[email protected] | fb5bcc0 | 2012-02-17 14:05:42 | [diff] [blame] | 52 | if (permutation == 0) { |
| 53 | patterns.push_back(&substring_pattern_1); |
| 54 | patterns.push_back(&substring_pattern_2); |
| 55 | } else { |
|
|