/
gogenerate_test.go
222 lines (196 loc) · 5.42 KB
/
gogenerate_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
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
// Copyright (c) 2016 Paul Jolly <paul@myitcv.org.uk>, all rights reserved.
// Use of this document is governed by a license found in the LICENSE document.
package gogenerate
import (
"os"
"path/filepath"
"runtime"
"testing"
)
func TestFilesContaining(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Error(err)
}
checks := []struct {
d string
cmds string
matches map[string]int
}{
{"_testFiles/eg01", "/bin/ls", map[string]int{"a.go": 1, "b.go": 1, "c.go": 1, "d.go": 2}},
}
Checks:
for _, c := range checks {
path := filepath.Join(cwd, c.d)
tags := make(map[string]bool)
goos := os.Getenv("GOOS")
if goos == "" {
goos = runtime.GOOS
}
tags[goos] = true
goarch := os.Getenv("GOARCH")
if goarch == "" {
goarch = runtime.GOARCH
}
tags[goarch] = true
res, err := FilesContainingCmd(path, c.cmds, tags)
if err != nil {
t.Errorf("Got unexpected error find matches in %v: %v", c.d, err)
continue Checks
}
if len(res) != len(c.matches) {
t.Errorf("Matches not up to expectations: %v vs %v", res, c.matches)
continue Checks
}
for i := range res {
if res[i] != c.matches[i] {
t.Errorf("Matches not up to expectations: %v vs %v", res, c.matches)
continue Checks
}
}
}
}
func TestNameFile(t *testing.T) {
checks := []struct {
n string
c string
r string
}{
{"a", "bananaGen", "gen_a_bananaGen.go"},
}
for _, c := range checks {
r := NameFile(c.n, c.c)
if r != c.r {
t.Errorf("Expected NameFile(%q, %q) to be %v got %v", c.n, c.c, c.r, r)
}
}
}
func TestNameTestFile(t *testing.T) {
checks := []struct {
n string
c string
r string
}{
{"a", "bananaGen", "gen_a_bananaGen_test.go"},
}
for _, c := range checks {
r := NameTestFile(c.n, c.c)
if r != c.r {
t.Errorf("Expected NameFile(%q, %q) to be %v got %v", c.n, c.c, c.r, r)
}
}
}
func TestFileIsGenerated(t *testing.T) {
checks := []struct {
p string
cmd string
r bool
}{
{"is_a_gen_file", "", false},
{"gen_file", "", false},
{"gen_cmd.go", "cmd", true},
{"gen_my_cmd.go", "cmd", true},
{"genfile.go", "", false},
{"/path/to/gen_file", "", false},
{"/path/to/gen_cmd.go", "cmd", true},
{"/path/to/gen_my_cmd.go", "cmd", true},
{"/path/to/genfile.go", "", false},
{"/", "", false},
}
for _, c := range checks {
cmd, r := FileIsGenerated(c.p)
if r != c.r || cmd != c.cmd {
t.Errorf("Expected FileIsGenerated(%q) to be (%v, %q) got (%v, %q)", c.p, c.r, c.cmd, r, cmd)
}
}
}
func TestFileGeneratedBy(t *testing.T) {
checks := []struct {
n string
addFileEnding bool
c string
r bool
}{
{"gen_bananaGen", true, "bananaGen", true},
{"gen_bananaGen", true, "example.com/bananaGen", true},
{"gen_bananaGen_test", true, "bananaGen", true},
{"gen_bananaGen_test", true, "exmaple.com/bananaGen", true},
{"gen_bananaGen_linux", true, "bananaGen", true},
{"gen_bananaGen_linux", true, "example.com/bananaGen", true},
{"gen_bananaGen_linux_test", true, "bananaGen", true},
{"gen_bananaGen_linux_test", true, "exmaple.com/bananaGen", true},
{"gen_bananaGen_amd64", true, "bananaGen", true},
{"gen_bananaGen_amd64", true, "example.com/bananaGen", true},
{"gen_bananaGen_amd64_test", true, "bananaGen", true},
{"gen_bananaGen_amd64_test", true, "exmaple.com/bananaGen", true},
{"gen_bananaGen_linux_amd64", true, "bananaGen", true},
{"gen_bananaGen_linux_amd64", true, "example.com/bananaGen", true},
{"gen_bananaGen_linux_amd64_test", true, "bananaGen", true},
{"gen_bananaGen_linux_amd64_test", true, "exmaple.com/bananaGen", true},
{"gen_a_bananaGen", true, "bananaGen", true},
{"gen_a_bananaGen", true, "example.com/bananaGen", true},
{"gen_a_bananaGen_test", true, "bananaGen", true},
{"gen_a_bananaGen_test", true, "example.com/bananaGen", true},
{"gen_abananaGen", true, "bananaGen", false},
{"gen_", false, "bananaGen", false},
}
for _, c := range checks {
fn := c.n
if c.addFileEnding {
fn += ".go"
}
r := FileGeneratedBy(fn, c.c)
if r != c.r {
t.Errorf("Expected FileGeneratedBy(%q, %q) to be %v got %v", fn, c.c, c.r, r)
}
}
for _, c := range checks {
fn := c.n
if c.addFileEnding {
fn += ".proto"
}
r := AnyFileGeneratedBy(fn, c.c)
if r != c.r {
t.Errorf("Expected FileGeneratedBy(%q, %q) to be %v got %v", fn, c.c, c.r, r)
}
}
}
func TestNameFileFromFile(t *testing.T) {
checks := []struct {
n string
o string
ok bool
}{
{"/path/to/a.txt", "", false},
{"/path/to/a.go", "/path/to/gen_a_bananaGen.go", true},
{"path/to/a.go", "path/to/gen_a_bananaGen.go", true},
{"a.go", "gen_a_bananaGen.go", true},
{"/path/to/a_test.go", "/path/to/gen_a_bananaGen_test.go", true},
{"path/to/a_test.go", "path/to/gen_a_bananaGen_test.go", true},
{"a_test.go", "gen_a_bananaGen_test.go", true},
}
for _, c := range checks {
o, ok := NameFileFromFile(c.n, "bananaGen")
if o != c.o || ok != c.ok {
t.Errorf("Expected NameFileFromFile(%q) to be %v got %v", c.n, c.o, o)
}
}
}
func TestCommentLicenseHeader(t *testing.T) {
checks := []struct {
fn string
exp string
}{
{"", ""},
{"_testFiles/licenseFile.txt", "// Copyright (c) Bananaman 2016\n// Line 2\n\n"},
}
for _, c := range checks {
res, err := CommentLicenseHeader(&c.fn)
if err != nil {
t.Fatalf("CommentLicenseHeader(&%q) failed when it should not have: %v", c.fn, err)
}
if res != c.exp {
t.Errorf("Actual output %q was not as expected %q", res, c.exp)
}
}
}