Debian Codegraph @

constraints/constraints_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 //go:build ignore
2 // +build ignore
3
4 // Copyright 2021 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
7
8 package constraints
9
10 import (
11 "fmt"
12 "os"
13 "os/exec"
14 "path/filepath"
15 "regexp"
16 "runtime"
17 "testing"
18 )
19
20 type (
21 testSigned[T Signed] struct{ f T }
22 testUnsigned[T Unsigned] struct{ f T }
23 testInteger[T Integer] struct{ f T }
24 testFloat[T Float] struct{ f T }
25 testComplex[T Complex] struct{ f T }
26 testOrdered[T Ordered] struct{ f T }
27 )
28
29 // TestTypes passes if it compiles.
30 type TestTypes struct {
31 _ testSigned[int]
32 _ testSigned[int64]
33 _ testUnsigned[uint]
34 _ testUnsigned[uintptr]
35 _ testInteger[int8]
36 _ testInteger[uint8]
37 _ testInteger[uintptr]
38 _ testFloat[float32]
39 _ testComplex[complex64]
40 _ testOrdered[int]
41 _ testOrdered[float64]
42 _ testOrdered[string]
43 }
44
45 var prolog = []byte(`
46 package constrainttest
47
48 import "golang.org/x/exp/constraints"
49
50 type (
51 testSigned[T constraints.Signed] struct{ f T }
52 testUnsigned[T constraints.Unsigned] struct{ f T }
53 testInteger[T constraints.Integer] struct{ f T }
54 testFloat[T constraints.Float] struct{ f T }
55 testComplex[T constraints.Complex] struct{ f T }
56 testOrdered[T constraints.Ordered] struct{ f T }
57 )
58 `)
59
60 func TestFailure(t *testing.T) {
61 switch runtime.GOOS {
62 case "android", "js", "ios":
63 t.Skipf("can't run go tool on %s", runtime.GOOS)
64 }
65
66 var exeSuffix string
67 if runtime.GOOS == "windows" {
68 exeSuffix = ".exe"
69 }
70 gocmd := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
71 if _, err := os.Stat(gocmd); err != nil {
72 t.Skipf("skipping because can't stat %s: %v", gocmd, err)
73 }
74
75 tmpdir := t.TempDir()
76
77 cwd, err := os.Getwd()
78 if err != nil {
79 t.Fatal(err)
80 }
81 // This package is golang.org/x/exp/constraints, so the root of the x/exp
82 // module is the parent directory of the directory in which this test runs.
83 expModDir := filepath.Dir(cwd)
84
85 modFile := fmt.Sprintf(`module constraintest
86
87 go 1.18
88
89 replace golang.org/x/exp => %s
90 `, expModDir)
91 if err := os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte(modFile), 0666); err != nil {
92 t.Fatal(err)
93 }
94
95 // Write the prolog as its own file so that 'go mod tidy' has something to inspect.
96 // This will ensure that the go.mod and go.sum files include any dependencies
97 // needed by the constraints package (which should just be some version of
98 // x/exp itself).
99 if err := os.WriteFile(filepath.Join(tmpdir, "prolog.go"), []byte(prolog), 0666); err != nil {
100 t.Fatal(err)
101 }
102
103 tidyCmd := exec.Command(gocmd, "mod", "tidy")
104 tidyCmd.Dir = tmpdir
105 tidyCmd.Env = append(os.Environ(), "PWD="+tmpdir)
106 if out, err := tidyCmd.CombinedOutput(); err != nil {
107 t.Fatalf("%v: %v\n%s", tidyCmd, err, out)
108 } else {
109 t.Logf("%v:\n%s", tidyCmd, out)
110 }
111
112 // Test for types that should not satisfy a constraint.
113 // For each pair of constraint and type, write a Go file
114 // var V constraint[type]
115 // For example,
116 // var V testSigned[uint]
117 // This should not compile, as testSigned (above) uses
118 // constraints.Signed, and uint does not satisfy that constraint.
119 // Therefore, the build of that code should fail.
120 for i, test := range []struct {
121 constraint, typ string
122 }{
123 {"testSigned", "uint"},
124 {"testUnsigned", "int"},
125 {"testInteger", "float32"},
126 {"testFloat", "int8"},
127 {"testComplex", "float64"},
128 {"testOrdered", "bool"},
129 } {
130 i := i
131 test := test
132 t.Run(fmt.Sprintf("%s %d", test.constraint, i), func(t *testing.T) {
133 t.Parallel()
134 name := fmt.Sprintf("go%d.go", i)
135 f, err := os.Create(filepath.Join(tmpdir, name))
136 if err != nil {
137 t.Fatal(err)
138 }
139 if _, err := f.Write(prolog); err != nil {
140 t.Fatal(err)
141 }
142 if _, err := fmt.Fprintf(f, "var V %s[%s]\n", test.constraint, test.typ); err != nil {
143 t.Fatal(err)
144 }
145 if err := f.Close(); err != nil {
146 t.Fatal(err)
147 }
148 cmd := exec.Command(gocmd, "build", name)
149 cmd.Dir = tmpdir
150 if out, err := cmd.CombinedOutput(); err == nil {
151 t.Error("build succeeded, but expected to fail")
152 } else if len(out) > 0 {
153 t.Logf("%s", out)
154 if !wantRx.Match(out) {
155 t.Errorf("output does not match %q", wantRx)
156 }
157 } else {
158 t.Error("no error output, expected something")
159 }
160 })
161 }
162 }
163
164 var wantRx = regexp.MustCompile("does not (implement|satisfy)")