...

Source file src/cuelang.org/go/cue/load/search_test.go

Documentation: cuelang.org/go/cue/load

     1  // Copyright 2018 The CUE Authors
     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  
    15  package load
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  )
    21  
    22  var matchPatternTests = `
    23  	pattern ...
    24  	match foo
    25  	
    26  	pattern net
    27  	match net
    28  	not net/http
    29  	
    30  	pattern net/http
    31  	match net/http
    32  	not net
    33  	
    34  	pattern net...
    35  	match net net/http netchan
    36  	not not/http not/net/http
    37  	
    38  	# Special cases. Quoting docs:
    39  
    40  	# First, /... at the end of the pattern can match an empty string,
    41  	# so that net/... matches both net and packages in its subdirectories, like net/http.
    42  	pattern net/...
    43  	match net net/http
    44  	not not/http not/net/http netchan
    45  
    46  	# Second, any slash-separted pattern element containing a wildcard never
    47  	# participates in a match of the "vendor" element in the path of a vendored
    48  	# package, so that ./... does not match packages in subdirectories of
    49  	# ./vendor or ./mycode/vendor, but ./vendor/... and ./mycode/vendor/... do.
    50  	# Note, however, that a directory named vendor that itself contains code
    51  	# is not a vendored package: cmd/vendor would be a command named vendor,
    52  	# and the pattern cmd/... matches it.
    53  	pattern ./...
    54  	match ./vendor ./mycode/vendor
    55  	not ./vendor/foo ./mycode/vendor/foo
    56  	
    57  	pattern ./vendor/...
    58  	match ./vendor/foo ./vendor/foo/vendor
    59  	not ./vendor/foo/vendor/bar
    60  	
    61  	pattern mycode/vendor/...
    62  	match mycode/vendor mycode/vendor/foo mycode/vendor/foo/vendor
    63  	not mycode/vendor/foo/vendor/bar
    64  	
    65  	pattern x/vendor/y
    66  	match x/vendor/y
    67  	not x/vendor
    68  	
    69  	pattern x/vendor/y/...
    70  	match x/vendor/y x/vendor/y/z x/vendor/y/vendor x/vendor/y/z/vendor
    71  	not x/vendor/y/vendor/z
    72  	
    73  	pattern .../vendor/...
    74  	match x/vendor/y x/vendor/y/z x/vendor/y/vendor x/vendor/y/z/vendor
    75  `
    76  
    77  func testPatterns(t *testing.T, name, tests string, fn func(string, string) bool) {
    78  	var patterns []string
    79  	for _, line := range strings.Split(tests, "\n") {
    80  		if i := strings.Index(line, "#"); i >= 0 {
    81  			line = line[:i]
    82  		}
    83  		f := strings.Fields(line)
    84  		if len(f) == 0 {
    85  			continue
    86  		}
    87  		switch f[0] {
    88  		default:
    89  			t.Fatalf("unknown directive %q", f[0])
    90  		case "pattern":
    91  			patterns = f[1:]
    92  		case "match", "not":
    93  			want := f[0] == "match"
    94  			for _, pattern := range patterns {
    95  				for _, in := range f[1:] {
    96  					if fn(pattern, in) != want {
    97  						t.Errorf("%s(%q, %q) = %v, want %v", name, pattern, in, !want, want)
    98  					}
    99  				}
   100  			}
   101  		}
   102  	}
   103  }
   104  

View as plain text