github.com/searKing/golang/go@v1.2.117/path/filepath/match_test.go (about) 1 // Copyright 2023 The searKing Author. 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 filepath_test 6 7 import ( 8 "path/filepath" 9 "runtime" 10 "slices" 11 "testing" 12 13 filepath_ "github.com/searKing/golang/go/path/filepath" 14 ) 15 16 // contains reports whether vector contains the string s. 17 func contains(vector []string, s string) bool { 18 for _, elem := range vector { 19 if elem == s { 20 return true 21 } 22 } 23 return false 24 } 25 26 var globTests = []struct { 27 pattern, result string 28 }{ 29 {"match.go", "match.go"}, 30 {"mat?h.go", "match.go"}, 31 {"./mat?h.go", "match.go"}, 32 {"*", "match.go"}, 33 {"../*/match.go", "../filepath/match.go"}, 34 {"./*", "match.go"}, 35 {"../*/match.go", "../filepath/match.go"}, 36 {"../../*/*/match.go", "../../path/filepath/match.go"}, 37 38 // ** for zero or more directories Not Support, https://github.com/golang/go/issues/11862 39 //{"../../**/match.go", "../filepath/match.go"}, 40 41 // no magic characters recognized by [filepath.Match]. 42 {"./match.go", "./match.go"}, // return if no 43 {"../filepath/match.go", "../filepath/match.go"}, 44 } 45 46 func TestWalkGlobDir(t *testing.T) { 47 for _, tt := range globTests { 48 pattern := tt.pattern 49 result := tt.result 50 if runtime.GOOS == "windows" { 51 pattern = filepath.Clean(pattern) 52 result = filepath.Clean(result) 53 } 54 var matches []string 55 err := filepath_.WalkGlob(pattern, func(path string) error { 56 matches = append(matches, path) 57 return nil 58 }) 59 if err != nil { 60 t.Errorf("WalkGlob error for %q: %s", pattern, err) 61 continue 62 } 63 if !contains(matches, result) { 64 t.Errorf("WalkGlob(%#q, ) = %#v want %v", pattern, matches, result) 65 } 66 expectMatches, expectErr := filepath.Glob(pattern) 67 if expectErr != nil { 68 t.Errorf("filepath.Glob error for %q: %s", pattern, err) 69 continue 70 } 71 if !slices.Equal(matches, expectMatches) { 72 t.Errorf("WalkGlob(%#q, ) = %#v want %v", pattern, matches, expectMatches) 73 } 74 } 75 for _, pattern := range []string{"no_match", "../*/no_match"} { 76 var matches []string 77 err := filepath_.WalkGlob(pattern, func(path string) error { 78 matches = append(matches, path) 79 return nil 80 }) 81 if err != nil { 82 t.Errorf("WalkGlob error for %q: %s", pattern, err) 83 continue 84 } 85 if len(matches) != 0 { 86 t.Errorf("WalkGlob(%#q, ) = %#v want []", pattern, matches) 87 } 88 expectMatches, expectErr := filepath.Glob(pattern) 89 if expectErr != nil { 90 t.Errorf("filepath.Glob error for %q: %s", pattern, err) 91 continue 92 } 93 if !slices.Equal(matches, expectMatches) { 94 t.Errorf("WalkGlob(%#q, ) = %#v want %v", pattern, matches, expectMatches) 95 } 96 } 97 }