-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
99 lines (89 loc) · 2.4 KB
/
file_test.go
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
package buildutil
import (
"go/build"
"reflect"
"runtime"
"testing"
)
var (
thisOS = runtime.GOOS
thisArch = runtime.GOARCH
otherOS = anotherOS()
otherArch = anotherArch()
)
func anotherOS() string {
if thisOS != "darwin" {
return "darwin"
}
return "linux"
}
func anotherArch() string {
if thisArch != "amd64" {
return "amd64"
}
return "386"
}
type GoodFileTest struct {
name string
result bool
}
var tests = []GoodFileTest{
{"file.go", true},
{"file.c", true},
{"file_foo.go", true},
{"file_" + thisArch + ".go", true},
{"file_" + otherArch + ".go", false},
{"file_" + thisOS + ".go", true},
{"file_" + otherOS + ".go", false},
{"file_" + thisOS + "_" + thisArch + ".go", true},
{"file_" + otherOS + "_" + thisArch + ".go", false},
{"file_" + thisOS + "_" + otherArch + ".go", false},
{"file_" + otherOS + "_" + otherArch + ".go", false},
{"file_foo_" + thisArch + ".go", true},
{"file_foo_" + otherArch + ".go", false},
{"file_" + thisOS + ".c", true},
{"file_" + otherOS + ".c", false},
}
func TestGoodOSArch(t *testing.T) {
ctxt := build.Default
for _, test := range tests {
if goodOSArchFile(&ctxt, test.name, make(map[string]bool)) != test.result {
t.Fatalf("goodOSArchFile(%q) != %v", test.name, test.result)
}
}
}
func TestGoodOSArchFile_StdLib(t *testing.T) {
ctx := &build.Context{BuildTags: []string{"linux"}, GOOS: "darwin"}
m := map[string]bool{}
want := map[string]bool{"linux": true}
if !goodOSArchFile(ctx, "hello_linux.go", m) {
t.Errorf("goodOSArchFile(hello_linux.go) = false, want true")
}
if !reflect.DeepEqual(m, want) {
t.Errorf("goodOSArchFile(hello_linux.go) tags = %v, want %v", m, want)
}
}
var benchmark = [...]string{
"file.go",
"file_foo.go",
"file_" + thisArch + ".go",
"file_" + otherArch + ".go",
"file_" + thisOS + ".go",
"file_" + otherOS + ".go",
"file_" + thisOS + "_" + thisArch + ".go",
"file_" + otherOS + "_" + thisArch + ".go",
"file_" + thisOS + "_" + otherArch + "_test.go",
"file_" + otherOS + "_" + otherArch + "_test.go",
"file_foo_" + thisArch + ".go",
"file_foo_" + otherArch + ".go",
"file_foo_" + thisArch + "_test.go",
"file_foo_" + otherArch + "_test.go",
"long_file_name_foo_bar_" + otherArch + "_test.go",
"long_file_name_foo_bar_" + otherArch + ".go",
}
func BenchmarkGoodOSArch(b *testing.B) {
ctxt := build.Default
for i := 0; i < b.N; i++ {
goodOSArchFile(&ctxt, benchmark[i%len(benchmark)], nil)
}
}