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