-
Notifications
You must be signed in to change notification settings - Fork 1
/
bindata_test.go
183 lines (163 loc) · 5.98 KB
/
bindata_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
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
package main
import (
"bufio"
"bytes"
"io"
"os"
"path/filepath"
"testing"
)
// testdata is the absolute path to the directory containing test datafiles.
var testdata string
// init initializes the testdata variable.
func init() {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
testdata = filepath.Join(pwd, "testdata")
}
// runTest compares the output generated by the command to a reference.
func runTest(t *testing.T, ref string, args ...string) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
// redirect stdout, restore when we are done
defer func(orig *os.File) {
os.Stdout = orig
}(os.Stdout)
os.Stdout = w
// change args, restore when we are done
defer func(orig []string) {
os.Args = orig
}(os.Args)
os.Args = append(os.Args[:1], args...)
// run command
go func() {
if err := run(); err != nil {
t.Fatal(err)
}
w.Close()
}()
// compare generated file to reference output
r1 := bytes.NewBufferString(ref)
r2 := bufio.NewReader(r)
for n := 0; ; n++ {
b1, err1 := r1.ReadByte()
b2, err2 := r2.ReadByte()
if err1 != nil || err2 != nil {
if err1 != err2 {
t.Fatalf("mismatched errors: %q and %q\n", err1, err2)
}
if err1 != io.EOF {
t.Fatal(err1)
}
break
}
if b1 != b2 {
t.Fatalf("mismatched bytes: expected %#02x, got %#02x at position %d\n", b1, b2, n)
}
}
r.Close()
}
// TestEmpty compares the output produced when there are no files to convert
// to a reference output.
func TestEmpty(t *testing.T) {
const ref = `package main
// This file is generated. Do not edit directly.
// bindata stores binary files as byte slices indexed by file paths.
var bindata = map[string][]byte{
}
`
runTest(t, ref)
}
// TestFlags tests the -pkg and -map flags.
func TestFlags(t *testing.T) {
const ref = `package foo
// This file is generated. Do not edit directly.
// MyData stores binary files as byte slices indexed by file paths.
var MyData = map[string][]byte{
}
`
runTest(t, ref, "-p", "foo", "-m", "MyData")
}
// TestString tests the conversion to a map of strings.
func TestString(t *testing.T) {
const ref = `package main
// This file is generated. Do not edit directly.
// bindata stores binary files as strings indexed by file paths.
var bindata = map[string]string{
"play/hello.go": "" +
"\x70\x61\x63\x6b\x61\x67\x65\x20\x6d\x61\x69\x6e\x0a\x0a\x69\x6d" +
"\x70\x6f\x72\x74\x20\x22\x66\x6d\x74\x22\x0a\x0a\x66\x75\x6e\x63" +
"\x20\x6d\x61\x69\x6e\x28\x29\x20\x7b\x0a\x09\x66\x6d\x74\x2e\x50" +
"\x72\x69\x6e\x74\x6c\x6e\x28\x22\x48\x65\x6c\x6c\x6f\x2c\x20\xe4" +
"\xb8\x96\xe7\x95\x8c\x22\x29\x0a\x7d\x0a",
}
`
runTest(t, ref, "-s", "-r", testdata, filepath.Join(testdata, "play", "hello.go"))
}
// TestFiles tests the reference output when there is a hierarchy of files to convert.
func TestFiles(t *testing.T) {
const ref = `package main
// This file is generated. Do not edit directly.
// bindata stores binary files as byte slices indexed by file paths.
var bindata = map[string][]byte{
"empty": []byte{
},
"gopher.gif": []byte{
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x10, 0x00, 0x10, 0x00, 0xf5, 0x27,
0x00, 0x17, 0x13, 0x11, 0x23, 0x20, 0x1f, 0x27, 0x2c, 0x2e, 0x25, 0x3a,
0x41, 0x27, 0x45, 0x4e, 0x31, 0x4e, 0x57, 0x38, 0x4e, 0x54, 0x2d, 0x57,
0x65, 0x37, 0x65, 0x75, 0x34, 0x68, 0x78, 0x4e, 0x73, 0x7b, 0xc3, 0x8c,
0x74, 0x48, 0x92, 0xaa, 0x54, 0x92, 0xa5, 0x4e, 0xa7, 0xc8, 0x7a, 0xb2,
0xc2, 0x72, 0xb3, 0xc8, 0x79, 0xbb, 0xcf, 0x74, 0xd5, 0xf3, 0x77, 0xdd,
0xff, 0x77, 0xdf, 0xff, 0x78, 0xdf, 0xff, 0x75, 0xe1, 0xff, 0x76, 0xe1,
0xfe, 0x79, 0xe0, 0xff, 0xa6, 0x99, 0x8e, 0x8c, 0x9c, 0xa0, 0x89, 0xa3,
0xa6, 0x8f, 0xbf, 0xca, 0x96, 0xc1, 0xcc, 0xdd, 0xd9, 0xd7, 0xe3, 0xdf,
0xdc, 0xd2, 0xe1, 0xe7, 0xe5, 0xe2, 0xe1, 0xf1, 0xeb, 0xea, 0xfb, 0xf6,
0xf5, 0xff, 0xfa, 0xf7, 0xff, 0xfc, 0xfb, 0xff, 0xfe, 0xfb, 0xff, 0xff,
0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x21, 0xf9, 0x04, 0x01, 0x00, 0x00, 0x28, 0x00, 0x2c, 0x00, 0x00,
0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x00, 0x06, 0x80, 0x40, 0x94, 0x70,
0x28, 0xfa, 0x78, 0x44, 0xc3, 0x24, 0x2a, 0xa4, 0x51, 0x28, 0x0a, 0x03,
0xc2, 0xc1, 0xa9, 0x09, 0x09, 0x3d, 0x06, 0x05, 0xc3, 0xe1, 0xb8, 0x78,
0x2f, 0x1d, 0x4e, 0x43, 0x61, 0xc0, 0x3a, 0x18, 0x90, 0x11, 0x09, 0xe2,
0x85, 0x9c, 0x4e, 0x90, 0xad, 0xc1, 0x90, 0x70, 0x80, 0x4e, 0x00, 0xc1,
0x85, 0x72, 0xcf, 0x3b, 0x10, 0x65, 0x06, 0x17, 0x7d, 0x7a, 0x83, 0x78,
0x7a, 0x65, 0x28, 0x82, 0x12, 0x6f, 0x10, 0x01, 0x01, 0x6e, 0x27, 0x12,
0x17, 0x06, 0x42, 0x82, 0x14, 0x5e, 0x1b, 0x0b, 0x0b, 0x19, 0x5e, 0x15,
0x94, 0x96, 0x5f, 0x15, 0x0b, 0x6f, 0x0b, 0x14, 0xa7, 0x14, 0x95, 0x8a,
0x5f, 0x5e, 0x11, 0x0f, 0xa8, 0xa7, 0x05, 0xa1, 0xac, 0xb4, 0x5e, 0xaa,
0x82, 0xb5, 0xac, 0x18, 0xb7, 0xb9, 0xb4, 0xbc, 0xbd, 0x5f, 0xbf, 0xc0,
0xa0, 0xab, 0xc3, 0xa0, 0x41, 0x00, 0x3b,
},
"play/bytes/11": []byte{
0x31, 0x30, 0x2b, 0x31, 0x20, 0x62, 0x79, 0x74, 0x65, 0x73, 0x21,
},
"play/bytes/12": []byte{
0x31, 0x32, 0x20, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x6f, 0x6b, 0x3f,
},
"play/bytes/13": []byte{
0x6a, 0x75, 0x73, 0x74, 0x20, 0x31, 0x33, 0x20, 0x62, 0x79, 0x74, 0x65,
0x73,
},
"play/hello.go": []byte{
0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x20, 0x6d, 0x61, 0x69, 0x6e,
0x0a, 0x0a, 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x22, 0x66, 0x6d,
0x74, 0x22, 0x0a, 0x0a, 0x66, 0x75, 0x6e, 0x63, 0x20, 0x6d, 0x61, 0x69,
0x6e, 0x28, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x66, 0x6d, 0x74, 0x2e, 0x50,
0x72, 0x69, 0x6e, 0x74, 0x6c, 0x6e, 0x28, 0x22, 0x48, 0x65, 0x6c, 0x6c,
0x6f, 0x2c, 0x20, 0xe4, 0xb8, 0x96, 0xe7, 0x95, 0x8c, 0x22, 0x29, 0x0a,
0x7d, 0x0a,
},
}
`
runTest(t, ref, "-r", testdata, testdata)
}