Debian Codegraph @

gopls/internal/cache/constraints_test.go

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