golang.org/x/tools/gopls@v0.15.3/internal/cache/constraints_test.go (about)

     1  // Copyright 2022 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  //go:build go1.16
     6  // +build go1.16
     7  
     8  package cache
     9  
    10  import (
    11  	"testing"
    12  )
    13  
    14  func TestIsStandaloneFile(t *testing.T) {
    15  	tests := []struct {
    16  		desc           string
    17  		contents       string
    18  		standaloneTags []string
    19  		want           bool
    20  	}{
    21  		{
    22  			"new syntax",
    23  			"//go:build ignore\n\npackage main\n",
    24  			[]string{"ignore"},
    25  			true,
    26  		},
    27  		{
    28  			"legacy syntax",
    29  			"// +build ignore\n\npackage main\n",
    30  			[]string{"ignore"},
    31  			true,
    32  		},
    33  		{
    34  			"multiple tags",
    35  			"//go:build ignore\n\npackage main\n",
    36  			[]string{"exclude", "ignore"},
    37  			true,
    38  		},
    39  		{
    40  			"invalid tag",
    41  			"// +build ignore\n\npackage main\n",
    42  			[]string{"script"},
    43  			false,
    44  		},
    45  		{
    46  			"non-main package",
    47  			"//go:build ignore\n\npackage p\n",
    48  			[]string{"ignore"},
    49  			false,
    50  		},
    51  		{
    52  			"alternate tag",
    53  			"// +build script\n\npackage main\n",
    54  			[]string{"script"},
    55  			true,
    56  		},
    57  		{
    58  			"both syntax",
    59  			"//go:build ignore\n// +build ignore\n\npackage main\n",
    60  			[]string{"ignore"},
    61  			true,
    62  		},
    63  		{
    64  			"after comments",
    65  			"// A non-directive comment\n//go:build ignore\n\npackage main\n",
    66  			[]string{"ignore"},
    67  			true,
    68  		},
    69  		{
    70  			"after package decl",
    71  			"package main //go:build ignore\n",
    72  			[]string{"ignore"},
    73  			false,
    74  		},
    75  		{
    76  			"on line after package decl",
    77  			"package main\n\n//go:build ignore\n",
    78  			[]string{"ignore"},
    79  			false,
    80  		},
    81  		{
    82  			"combined with other expressions",
    83  			"\n\n//go:build ignore || darwin\n\npackage main\n",
    84  			[]string{"ignore"},
    85  			false,
    86  		},
    87  	}
    88  
    89  	for _, test := range tests {
    90  		t.Run(test.desc, func(t *testing.T) {
    91  			if got := isStandaloneFile([]byte(test.contents), test.standaloneTags); got != test.want {
    92  				t.Errorf("isStandaloneFile(%q, %v) = %t, want %t", test.contents, test.standaloneTags, got, test.want)
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  func TestVersionRegexp(t *testing.T) {
    99  	// good
   100  	for _, s := range []string{
   101  		"go1",
   102  		"go1.2",
   103  		"go1.2.3",
   104  		"go1.0.33",
   105  	} {
   106  		if !goVersionRx.MatchString(s) {
   107  			t.Errorf("Valid Go version %q does not match the regexp", s)
   108  		}
   109  	}
   110  
   111  	// bad
   112  	for _, s := range []string{
   113  		"go",          // missing numbers
   114  		"go0",         // Go starts at 1
   115  		"go01",        // leading zero
   116  		"go1.π",       // non-decimal
   117  		"go1.-1",      // negative
   118  		"go1.02.3",    // leading zero
   119  		"go1.2.3.4",   // too many segments
   120  		"go1.2.3-pre", // textual suffix
   121  	} {
   122  		if goVersionRx.MatchString(s) {
   123  			t.Errorf("Invalid Go version %q unexpectedly matches the regexp", s)
   124  		}
   125  	}
   126  }