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