golang.org/x/tools/gopls@v0.15.3/internal/test/integration/fake/glob/glob_test.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package glob_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"golang.org/x/tools/gopls/internal/test/integration/fake/glob"
    11  )
    12  
    13  func TestParseErrors(t *testing.T) {
    14  	tests := []string{
    15  		"***",
    16  		"ab{c",
    17  		"[]",
    18  		"[a-]",
    19  		"ab{c{d}",
    20  	}
    21  
    22  	for _, test := range tests {
    23  		_, err := glob.Parse(test)
    24  		if err == nil {
    25  			t.Errorf("Parse(%q) succeeded unexpectedly", test)
    26  		}
    27  	}
    28  }
    29  
    30  func TestMatch(t *testing.T) {
    31  	tests := []struct {
    32  		pattern, input string
    33  		want           bool
    34  	}{
    35  		// Basic cases.
    36  		{"", "", true},
    37  		{"", "a", false},
    38  		{"", "/", false},
    39  		{"abc", "abc", true},
    40  
    41  		// ** behavior
    42  		{"**", "abc", true},
    43  		{"**/abc", "abc", true},
    44  		{"**", "abc/def", true},
    45  		{"{a/**/c,a/**/d}", "a/b/c", true},
    46  		{"{a/**/c,a/**/d}", "a/b/c/d", true},
    47  		{"{a/**/c,a/**/e}", "a/b/c/d", false},
    48  		{"{a/**/c,a/**/e,a/**/d}", "a/b/c/d", true},
    49  		{"{/a/**/c,a/**/e,a/**/d}", "a/b/c/d", true},
    50  		{"{/a/**/c,a/**/e,a/**/d}", "/a/b/c/d", false},
    51  		{"{/a/**/c,a/**/e,a/**/d}", "/a/b/c", true},
    52  		{"{/a/**/e,a/**/e,a/**/d}", "/a/b/c", false},
    53  
    54  		// * and ? behavior
    55  		{"/*", "/a", true},
    56  		{"*", "foo", true},
    57  		{"*o", "foo", true},
    58  		{"*o", "foox", false},
    59  		{"f*o", "foo", true},
    60  		{"f*o", "fo", true},
    61  		{"fo?", "foo", true},
    62  		{"fo?", "fox", true},
    63  		{"fo?", "fooo", false},
    64  		{"fo?", "fo", false},
    65  		{"?", "a", true},
    66  		{"?", "ab", false},
    67  		{"?", "", false},
    68  		{"*?", "", false},
    69  		{"?b", "ab", true},
    70  		{"?c", "ab", false},
    71  
    72  		// {} behavior
    73  		{"ab{c,d}e", "abce", true},
    74  		{"ab{c,d}e", "abde", true},
    75  		{"ab{c,d}e", "abxe", false},
    76  		{"ab{c,d}e", "abe", false},
    77  		{"{a,b}c", "ac", true},
    78  		{"{a,b}c", "bc", true},
    79  		{"{a,b}c", "ab", false},
    80  		{"a{b,c}", "ab", true},
    81  		{"a{b,c}", "ac", true},
    82  		{"a{b,c}", "bc", false},
    83  		{"ab{c{1,2},d}e", "abc1e", true},
    84  		{"ab{c{1,2},d}e", "abde", true},
    85  		{"ab{c{1,2},d}e", "abc1f", false},
    86  		{"ab{c{1,2},d}e", "abce", false},
    87  		{"ab{c[}-~]}d", "abc}d", true},
    88  		{"ab{c[}-~]}d", "abc~d", true},
    89  		{"ab{c[}-~],y}d", "abcxd", false},
    90  		{"ab{c[}-~],y}d", "abyd", true},
    91  		{"ab{c[}-~],y}d", "abd", false},
    92  		{"{a/b/c,d/e/f}", "a/b/c", true},
    93  		{"/ab{/c,d}e", "/ab/ce", true},
    94  		{"/ab{/c,d}e", "/ab/cf", false},
    95  
    96  		// [-] behavior
    97  		{"[a-c]", "a", true},
    98  		{"[a-c]", "b", true},
    99  		{"[a-c]", "c", true},
   100  		{"[a-c]", "d", false},
   101  		{"[a-c]", " ", false},
   102  
   103  		// Realistic examples.
   104  		{"**/*.{ts,js}", "path/to/foo.ts", true},
   105  		{"**/*.{ts,js}", "path/to/foo.js", true},
   106  		{"**/*.{ts,js}", "path/to/foo.go", false},
   107  	}
   108  
   109  	for _, test := range tests {
   110  		g, err := glob.Parse(test.pattern)
   111  		if err != nil {
   112  			t.Fatalf("New(%q) failed unexpectedly: %v", test.pattern, err)
   113  		}
   114  		if got := g.Match(test.input); got != test.want {
   115  			t.Errorf("New(%q).Match(%q) = %t, want %t", test.pattern, test.input, got, test.want)
   116  		}
   117  	}
   118  }