]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/build/constraint/vers_test.go
go/build/constraint: add GoVersion
[gostls13.git] / src / go / build / constraint / vers_test.go
1 // Copyright 2023 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 constraint
6
7 import (
8         "fmt"
9         "testing"
10 )
11
12 var tests = []struct {
13         in  string
14         out int
15 }{
16         {"//go:build linux && go1.60", 60},
17         {"//go:build ignore && go1.60", 60},
18         {"//go:build ignore || go1.60", -1},
19         {"//go:build go1.50 || (ignore && go1.60)", 50},
20         {"// +build go1.60,linux", 60},
21         {"// +build go1.60 linux", -1},
22         {"//go:build go1.50 && !go1.60", 50},
23         {"//go:build !go1.60", -1},
24         {"//go:build linux && go1.50 || darwin && go1.60", 50},
25         {"//go:build linux && go1.50 || !(!darwin || !go1.60)", 50},
26 }
27
28 func TestGoVersion(t *testing.T) {
29         for _, tt := range tests {
30                 x, err := Parse(tt.in)
31                 if err != nil {
32                         t.Fatal(err)
33                 }
34                 v := GoVersion(x)
35                 want := ""
36                 if tt.out == 0 {
37                         want = "go1"
38                 } else if tt.out > 0 {
39                         want = fmt.Sprintf("go1.%d", tt.out)
40                 }
41                 if v != want {
42                         t.Errorf("GoVersion(%q) = %q, want %q, nil", tt.in, v, want)
43                 }
44         }
45 }