• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 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 <stddef.h>
6 
7 #include <iterator>
8 
9 #include "base/macros.h"
10 #include "gn/pattern.h"
11 #include "util/test/test.h"
12 
13 namespace {
14 
15 struct Case {
16   const char* pattern;
17   const char* candidate;
18   bool expected_match;
19 };
20 
21 }  // namespace
22 
TEST(Pattern,Matches)23 TEST(Pattern, Matches) {
24   Case pattern_cases[] = {
25       // Empty pattern matches only empty string.
26       {"", "", true},
27       {"", "foo", false},
28       // Exact matches.
29       {"foo", "foo", true},
30       {"foo", "bar", false},
31       // Path boundaries.
32       {"\\b", "", true},
33       {"\\b", "/", true},
34       {"\\b\\b", "/", true},
35       {"\\b\\b\\b", "", false},
36       {"\\b\\b\\b", "/", true},
37       {"\\b", "//", false},
38       {"\\bfoo\\b", "foo", true},
39       {"\\bfoo\\b", "/foo/", true},
40       {"\\b\\bfoo", "/foo", true},
41       // *
42       {"*", "", true},
43       {"*", "foo", true},
44       {"*foo", "foo", true},
45       {"*foo", "gagafoo", true},
46       {"*foo", "gagafoob", false},
47       {"foo*bar", "foobar", true},
48       {"foo*bar", "foo-bar", true},
49       {"foo*bar", "foolalalalabar", true},
50       {"foo*bar", "foolalalalabaz", false},
51       {"*a*b*c*d*", "abcd", true},
52       {"*a*b*c*d*", "1a2b3c4d5", true},
53       {"*a*b*c*d*", "1a2b3c45", false},
54       {"*\\bfoo\\b*", "foo", true},
55       {"*\\bfoo\\b*", "/foo/", true},
56       {"*\\bfoo\\b*", "foob", false},
57       {"*\\bfoo\\b*", "lala/foo/bar/baz", true},
58   };
59   for (size_t i = 0; i < std::size(pattern_cases); i++) {
60     const Case& c = pattern_cases[i];
61     Pattern pattern(c.pattern);
62     bool result = pattern.MatchesString(c.candidate);
63     EXPECT_EQ(c.expected_match, result)
64         << i << ": \"" << c.pattern << "\", \"" << c.candidate << "\"";
65   }
66 }
67