• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2020 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//   https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package match_test
16
17import (
18	"strings"
19	"testing"
20
21	"dawn.googlesource.com/tint/tools/src/match"
22)
23
24func TestMatch(t *testing.T) {
25	for _, test := range []struct {
26		pattern string
27		path    string
28		expect  bool
29	}{
30		{"a", "a", true},
31		{"b", "a", false},
32
33		{"?", "a", true},
34		{"a/?/c", "a/x/c", true},
35		{"a/??/c", "a/x/c", false},
36		{"a/??/c", "a/xx/c", true},
37		{"a/???/c", "a/x z/c", true},
38		{"a/?/c", "a/xx/c", false},
39		{"a/?/?/c", "a/x/y/c", true},
40		{"a/?/?/?/c", "a/x/y/z/c", true},
41		{"a/???/c", "a/x/y/c", false},
42		{"a/?????", "a/x/y/c", false},
43
44		{"*", "a", true},
45		{"*", "abc", true},
46		{"*", "abc 123", true},
47		{"*", "xxx/yyy", false},
48		{"*/*", "xxx/yyy", true},
49		{"*/*", "xxx/yyy/zzz", false},
50		{"*/*/c", "xxx/yyy/c", true},
51		{"a/*/*", "a/xxx/yyy", true},
52		{"a/*/c", "a/xxx/c", true},
53		{"a/*/c", "a/xxx/c", true},
54		{"a/*/*/c", "a/b/c", false},
55
56		{"**", "a", true},
57		{"**", "abc", true},
58		{"**", "abc 123", true},
59		{"**", "xxx/yyy", true},
60		{"**", "xxx/yyy/zzz", true},
61		{"**/**", "xxx", false},
62		{"**/**", "xxx/yyy", true},
63		{"**/**", "xxx/yyy/zzz", true},
64		{"**/**/**", "xxx/yyy/zzz", true},
65		{"**/**/c", "xxx/yyy/c", true},
66		{"**/**/c", "xxx/yyy/c/d", false},
67		{"a/**/**", "a/xxx/yyy", true},
68		{"a/**/c", "a/xxx/c", true},
69		{"a/**/c", "a/xxx/yyy/c", true},
70		{"a/**/c", "a/xxx/y y/zzz/c", true},
71
72		{"a/**/c", "a/c", false},
73		{"a/**c", "a/c", true},
74
75		{"xxx/**.foo", "xxx/aaa.foo", true},
76		{"xxx/**.foo", "xxx/yyy/zzz/.foo", true},
77		{"xxx/**.foo", "xxx/yyy/zzz/bar.foo", true},
78	} {
79		f, err := match.New(test.pattern)
80		if err != nil {
81			t.Errorf(`match.New("%v")`, test.pattern)
82			continue
83		}
84		matched := f(test.path)
85		switch {
86		case matched && !test.expect:
87			t.Errorf(`Path "%v" matched against pattern "%v"`, test.path, test.pattern)
88		case !matched && test.expect:
89			t.Errorf(`Path "%v" did not match against pattern "%v"`, test.path, test.pattern)
90		}
91	}
92}
93
94func TestErrOnPlaceholder(t *testing.T) {
95	for _, pattern := range []string{"a/b••c", "a/b•c", "a/b/¿c"} {
96		_, err := match.New(pattern)
97		if err == nil {
98			t.Errorf(`match.New("%v") did not return an expected error`, pattern)
99			continue
100		}
101		if !strings.Contains(err.Error(), "Pattern must not contain") {
102			t.Errorf(`match.New("%v") returned unrecognised error: %v`, pattern, err)
103			continue
104		}
105	}
106}
107