1// Copyright 2019 Google Inc. All rights reserved. 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// http://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 main 16 17import ( 18 "testing" 19) 20 21func TestMatch(t *testing.T) { 22 testCases := []struct { 23 pattern, name string 24 match bool 25 }{ 26 {"a/*", "b/", false}, 27 {"a/*", "b/a", false}, 28 {"a/*", "b/b/", false}, 29 {"a/*", "b/b/c", false}, 30 {"a/**/*", "b/", false}, 31 {"a/**/*", "b/a", false}, 32 {"a/**/*", "b/b/", false}, 33 {"a/**/*", "b/b/c", false}, 34 35 {"a/*", "a/", false}, 36 {"a/*", "a/a", true}, 37 {"a/*", "a/b/", false}, 38 {"a/*", "a/b/c", false}, 39 40 {"a/*/", "a/", false}, 41 {"a/*/", "a/a", false}, 42 {"a/*/", "a/b/", true}, 43 {"a/*/", "a/b/c", false}, 44 45 {"a/**/*", "a/", false}, 46 {"a/**/*", "a/a", true}, 47 {"a/**/*", "a/b/", false}, 48 {"a/**/*", "a/b/c", true}, 49 50 {"a/**/*/", "a/", false}, 51 {"a/**/*/", "a/a", false}, 52 {"a/**/*/", "a/b/", true}, 53 {"a/**/*/", "a/b/c", false}, 54 55 {"**/*", "a/", false}, 56 {"**/*", "a/a", true}, 57 {"**/*", "a/b/", false}, 58 {"**/*", "a/b/c", true}, 59 60 {"**/*/", "a/", true}, 61 {"**/*/", "a/a", false}, 62 {"**/*/", "a/b/", true}, 63 {"**/*/", "a/b/c", false}, 64 65 {`a/\*\*/\*`, `a/**/*`, true}, 66 {`a/\*\*/\*`, `a/a/*`, false}, 67 {`a/\*\*/\*`, `a/**/a`, false}, 68 {`a/\*\*/\*`, `a/a/a`, false}, 69 70 {`a/**/\*`, `a/**/*`, true}, 71 {`a/**/\*`, `a/a/*`, true}, 72 {`a/**/\*`, `a/**/a`, false}, 73 {`a/**/\*`, `a/a/a`, false}, 74 75 {`a/\*\*/*`, `a/**/*`, true}, 76 {`a/\*\*/*`, `a/a/*`, false}, 77 {`a/\*\*/*`, `a/**/a`, true}, 78 {`a/\*\*/*`, `a/a/a`, false}, 79 80 {`*/**/a`, `a/a/a`, true}, 81 {`*/**/a`, `*/a/a`, true}, 82 {`*/**/a`, `a/**/a`, true}, 83 {`*/**/a`, `*/**/a`, true}, 84 85 {`\*/\*\*/a`, `a/a/a`, false}, 86 {`\*/\*\*/a`, `*/a/a`, false}, 87 {`\*/\*\*/a`, `a/**/a`, false}, 88 {`\*/\*\*/a`, `*/**/a`, true}, 89 90 {`a/?`, `a/?`, true}, 91 {`a/?`, `a/a`, true}, 92 {`a/\?`, `a/?`, true}, 93 {`a/\?`, `a/a`, false}, 94 95 {`a/?`, `a/?`, true}, 96 {`a/?`, `a/a`, true}, 97 {`a/\?`, `a/?`, true}, 98 {`a/\?`, `a/a`, false}, 99 100 {`a/[a-c]`, `a/b`, true}, 101 {`a/[abc]`, `a/b`, true}, 102 103 {`a/\[abc]`, `a/b`, false}, 104 {`a/\[abc]`, `a/[abc]`, true}, 105 106 {`a/\[abc\]`, `a/b`, false}, 107 {`a/\[abc\]`, `a/[abc]`, true}, 108 109 {`a/?`, `a/?`, true}, 110 {`a/?`, `a/a`, true}, 111 {`a/\?`, `a/?`, true}, 112 {`a/\?`, `a/a`, false}, 113 114 {"/a/*", "/a/", false}, 115 {"/a/*", "/a/a", true}, 116 {"/a/*", "/a/b/", false}, 117 {"/a/*", "/a/b/c", false}, 118 119 {"/a/*/", "/a/", false}, 120 {"/a/*/", "/a/a", false}, 121 {"/a/*/", "/a/b/", true}, 122 {"/a/*/", "/a/b/c", false}, 123 124 {"/a/**/*", "/a/", false}, 125 {"/a/**/*", "/a/a", true}, 126 {"/a/**/*", "/a/b/", false}, 127 {"/a/**/*", "/a/b/c", true}, 128 129 {"/**/*", "/a/", false}, 130 {"/**/*", "/a/a", true}, 131 {"/**/*", "/a/b/", false}, 132 {"/**/*", "/a/b/c", true}, 133 134 {"/**/*/", "/a/", true}, 135 {"/**/*/", "/a/a", false}, 136 {"/**/*/", "/a/b/", true}, 137 {"/**/*/", "/a/b/c", false}, 138 139 {`a`, `/a`, false}, 140 {`/a`, `a`, false}, 141 {`*`, `/a`, false}, 142 {`/*`, `a`, false}, 143 {`**/*`, `/a`, false}, 144 {`/**/*`, `a`, false}, 145 } 146 147 for _, test := range testCases { 148 t.Run(test.pattern+","+test.name, func(t *testing.T) { 149 match, err := Match(test.pattern, test.name) 150 if err != nil { 151 t.Fatal(err) 152 } 153 if match != test.match { 154 t.Errorf("want: %v, got %v", test.match, match) 155 } 156 }) 157 } 158} 159