summaryrefslogtreecommitdiffstats
path: root/src/go/build/constraint/expr_test.go
blob: 15d189012efb7d91bf4c7b8bf9e9d93910a2e282 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package constraint

import (
	"fmt"
	"reflect"
	"strings"
	"testing"
)

var exprStringTests = []struct {
	x   Expr
	out string
}{
	{
		x:   tag("abc"),
		out: "abc",
	},
	{
		x:   not(tag("abc")),
		out: "!abc",
	},
	{
		x:   not(and(tag("abc"), tag("def"))),
		out: "!(abc && def)",
	},
	{
		x:   and(tag("abc"), or(tag("def"), tag("ghi"))),
		out: "abc && (def || ghi)",
	},
	{
		x:   or(and(tag("abc"), tag("def")), tag("ghi")),
		out: "(abc && def) || ghi",
	},
}

func TestExprString(t *testing.T) {
	for i, tt := range exprStringTests {
		t.Run(fmt.Sprint(i), func(t *testing.T) {
			s := tt.x.String()
			if s != tt.out {
				t.Errorf("String() mismatch:\nhave %s\nwant %s", s, tt.out)
			}
		})
	}
}

var lexTests = []struct {
	in  string
	out string
}{
	{"", ""},
	{"x", "x"},
	{"x.y", "x.y"},
	{"x_y", "x_y"},
	{"αx", "αx"},
	{"αx²", "αx err: invalid syntax at ²"},
	{"go1.2", "go1.2"},
	{"x y", "x y"},
	{"x!y", "x ! y"},
	{"&&||!()xy yx ", "&& || ! ( ) xy yx"},
	{"x~", "x err: invalid syntax at ~"},
	{"x ~", "x err: invalid syntax at ~"},
	{"x &", "x err: invalid syntax at &"},
	{"x &y", "x err: invalid syntax at &"},
}

func TestLex(t *testing.T) {
	for i, tt := range lexTests {
		t.Run(fmt.Sprint(i), func(t *testing.T) {
			p := &exprParser{s: tt.in}
			out := ""
			for {
				tok, err := lexHelp(p)
				if tok == "" && err == nil {
					break
				}
				if out != "" {
					out += " "
				}
				if err != nil {
					out += "err: " + err.Error()
					break
				}
				out += tok
			}
			if out != tt.out {
				t.Errorf("lex(%q):\nhave %s\nwant %s", tt.in, out, tt.out)
			}
		})
	}
}

func lexHelp(p *exprParser) (tok string, err error) {
	defer func() {
		if e := recover(); e != nil {
			if e, ok := e.(*SyntaxError); ok {
				err = e
				return
			}
			panic(e)
		}
	}()

	p.lex()
	return p.tok, nil
}

var parseExprTests = []struct {
	in string
	x  Expr
}{
	{"x", tag("x")},
	{"x&&y", and(tag("x"), tag("y"))},
	{"x||y", or(tag("x"), tag("y"))},
	{"(x)", tag("x")},
	{"x||y&&z", or(tag("x"), and(tag("y"), tag("z")))},
	{"x&&y||z", or(and(tag("x"), tag("y")), tag("z"))},
	{"x&&(y||z)", and(tag("x"), or(tag("y"), tag("z")))},
	{"(x||y)&&z", and(or(tag("x"), tag("y")), tag("z"))},
	{"!(x&&y)", not(and(tag("x"), tag("y")))},
}

func TestParseExpr(t *testing.T) {
	for i, tt := range parseExprTests {
		t.Run(fmt.Sprint(i), func(t *testing.T) {
			x, err := parseExpr(tt.in)
			if err != nil {
				t.Fatal(err)
			}
			if x.String() != tt.x.String() {
				t.Errorf("parseExpr(%q):\nhave %s\nwant %s", tt.in, x, tt.x)
			}
		})
	}
}

var parseExprErrorTests = []struct {
	in  string
	err error
}{
	{"x && ", &SyntaxError{Offset: 5, Err: "unexpected end of expression"}},
	{"x && (", &SyntaxError{Offset: 6, Err: "missing close paren"}},
	{"x && ||", &SyntaxError{Offset: 5, Err: "unexpected token ||"}},
	{"x && !", &SyntaxError{Offset: 6, Err: "unexpected end of expression"}},
	{"x && !!", &SyntaxError{Offset: 6, Err: "double negation not allowed"}},
	{"x !", &SyntaxError{Offset: 2, Err: "unexpected token !"}},
	{"x && (y", &SyntaxError{Offset: 5, Err: "missing close paren"}},
}

func TestParseError(t *testing.T) {
	for i, tt := range parseExprErrorTests {
		t.Run(fmt.Sprint(i), func(t *testing.T) {
			x, err := parseExpr(tt.in)
			if err == nil {
				t.Fatalf("parseExpr(%q) = %v, want error", tt.in, x)
			}
			if !reflect.DeepEqual(err, tt.err) {
				t.Fatalf("parseExpr(%q): wrong error:\nhave %#v\nwant %#v", tt.in, err, tt.err)
			}
		})
	}
}

var exprEvalTests = []struct {
	in   string
	ok   bool
	tags string
}{
	{"x", false, "x"},
	{"x && y", false, "x y"},
	{"x || y", false, "x y"},
	{"!x && yes", true, "x yes"},
	{"yes || y", true, "y yes"},
}

func TestExprEval(t *testing.T) {
	for i, tt := range exprEvalTests {
		t.Run(fmt.Sprint(i), func(t *testing.T) {
			x, err := parseExpr(tt.in)
			if err != nil {
				t.Fatal(err)
			}
			tags := make(map[string]bool)
			wantTags := make(map[string]bool)
			for _, tag := range strings.Fields(tt.tags) {
				wantTags[tag] = true
			}
			hasTag := func(tag string) bool {
				tags[tag] = true
				return tag == "yes"
			}
			ok := x.Eval(hasTag)
			if ok != tt.ok || !reflect.DeepEqual(tags, wantTags) {
				t.Errorf("Eval(%#q):\nhave ok=%v, tags=%v\nwant ok=%v, tags=%v",
					tt.in, ok, tags, tt.ok, wantTags)
			}
		})
	}
}

var parsePlusBuildExprTests = []struct {
	in string
	x  Expr
}{
	{"x", tag("x")},
	{"x,y", and(tag("x"), tag("y"))},
	{"x y", or(tag("x"), tag("y"))},
	{"x y,z", or(tag("x"), and(tag("y"), tag("z")))},
	{"x,y z", or(and(tag("x"), tag("y")), tag("z"))},
	{"x,!y !z", or(and(tag("x"), not(tag("y"))), not(tag("z")))},
	{"!! x", or(tag("ignore"), tag("x"))},
	{"!!x", tag("ignore")},
	{"!x", not(tag("x"))},
	{"!", tag("ignore")},
	{"", tag("ignore")},
}

func TestParsePlusBuildExpr(t *testing.T) {
	for i, tt := range parsePlusBuildExprTests {
		t.Run(fmt.Sprint(i), func(t *testing.T) {
			x := parsePlusBuildExpr(tt.in)
			if x.String() != tt.x.String() {
				t.Errorf("parsePlusBuildExpr(%q):\nhave %v\nwant %v", tt.in, x, tt.x)
			}
		})
	}
}

var constraintTests = []struct {
	in  string
	x   Expr
	err string
}{
	{"//+build !", tag("ignore"), ""},
	{"//+build", tag("ignore"), ""},
	{"//+build x y", or(tag("x"), tag("y")), ""},
	{"// +build x y \n", or(tag("x"), tag("y")), ""},
	{"// +build x y \n ", nil, "not a build constraint"},
	{"// +build x y \nmore", nil, "not a build constraint"},
	{" //+build x y", nil, "not a build constraint"},

	{"//go:build x && y", and(tag("x"), tag("y")), ""},
	{"//go:build x && y\n", and(tag("x"), tag("y")), ""},
	{"//go:build x && y\n ", nil, "not a build constraint"},
	{"//go:build x && y\nmore", nil, "not a build constraint"},
	{" //go:build x && y", nil, "not a build constraint"},
	{"//go:build\n", nil, "unexpected end of expression"},
}

func TestParse(t *testing.T) {
	for i, tt := range constraintTests {
		t.Run(fmt.Sprint(i), func(t *testing.T) {
			x, err := Parse(tt.in)
			if err != nil {
				if tt.err == "" {
					t.Errorf("Constraint(%q): unexpected error: %v", tt.in, err)
				} else if !strings.Contains(err.Error(), tt.err) {
					t.Errorf("Constraint(%q): error %v, want %v", tt.in, err, tt.err)
				}
				return
			}
			if tt.err != "" {
				t.Errorf("Constraint(%q) = %v, want error %v", tt.in, x, tt.err)
				return
			}
			if x.String() != tt.x.String() {
				t.Errorf("Constraint(%q):\nhave %v\nwant %v", tt.in, x, tt.x)
			}
		})
	}
}

var plusBuildLinesTests = []struct {
	in  string
	out []string
	err error
}{
	{"x", []string{"x"}, nil},
	{"x && !y", []string{"x,!y"}, nil},
	{"x || y", []string{"x y"}, nil},
	{"x && (y || z)", []string{"x", "y z"}, nil},
	{"!(x && y)", []string{"!x !y"}, nil},
	{"x || (y && z)", []string{"x y,z"}, nil},
	{"w && (x || (y && z))", []string{"w", "x y,z"}, nil},
	{"v || (w && (x || (y && z)))", nil, errComplex},
}

func TestPlusBuildLines(t *testing.T) {
	for i, tt := range plusBuildLinesTests {
		t.Run(fmt.Sprint(i), func(t *testing.T) {
			x, err := parseExpr(tt.in)
			if err != nil {
				t.Fatal(err)
			}
			lines, err := PlusBuildLines(x)
			if err != nil {
				if tt.err == nil {
					t.Errorf("PlusBuildLines(%q): unexpected error: %v", tt.in, err)
				} else if tt.err != err {
					t.Errorf("PlusBuildLines(%q): error %v, want %v", tt.in, err, tt.err)
				}
				return
			}
			if tt.err != nil {
				t.Errorf("PlusBuildLines(%q) = %v, want error %v", tt.in, lines, tt.err)
				return
			}
			var want []string
			for _, line := range tt.out {
				want = append(want, "// +build "+line)
			}
			if !reflect.DeepEqual(lines, want) {
				t.Errorf("PlusBuildLines(%q):\nhave %q\nwant %q", tt.in, lines, want)
			}
		})
	}
}