-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenumeration_test.go
190 lines (147 loc) · 4.22 KB
/
enumeration_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
package main
import (
"fmt"
"github.com/onsi/gomega"
"github.com/rickb777/enumeration/v3/internal/model"
"github.com/rickb777/enumeration/v3/internal/parse"
"go/scanner"
"go/token"
"io"
"os"
"strings"
"testing"
)
func TestMainApp_Day(t *testing.T) {
g := gomega.NewWithT(t)
outputFile := "example/day_enum.go"
err := os.Remove(outputFile)
if err != nil {
t.Logf("rm %s: %s", outputFile, err.Error())
// continue anyway
}
os.Args = []string{"", "-f", "-type", "Day", "-i", "example/day.go", "-o", outputFile}
main()
compareGeneratedFile(g, outputFile)
}
func TestMainApp_Channel(t *testing.T) {
g := gomega.NewWithT(t)
inputGo = "example/channel.go"
outputGo = "example/channel_enum.go"
err := os.Remove(outputGo)
if err != nil {
t.Logf("rm %s: %s", "example/channel_enum.go", err.Error())
// continue anyway
}
force = true
lowercase, uppercase, showVersion = false, false, false
outputJSON, marshalTextRep, marshalJSONRep, storeRep = "", "None", "None", "None"
model.Prefix = ""
model.Suffix = "Sales"
parse.AliasTable = ""
config = model.Config{
MainType: "SalesChannel",
}
doMain()
compareGeneratedFile(g, outputGo)
}
func TestMainApp_Country(t *testing.T) {
g := gomega.NewWithT(t)
inputGo = "example/country.go"
outputGo = "example/country_enum.go"
err := os.Remove(outputGo)
if err != nil {
t.Logf("rm %s: %s", outputGo, err.Error())
// continue anyway
}
force = true
lowercase, uppercase, showVersion = false, false, false
outputJSON, marshalTextRep, marshalJSONRep, storeRep = "", "None", "None", "Number"
model.Prefix = ""
model.Suffix = ""
parse.AliasTable = "iso3166_3LetterCodes"
config = model.Config{
MainType: "Country",
Plural: "Countries",
IgnoreCase: true,
Unsnake: true,
}
doMain()
compareGeneratedFile(g, outputGo)
}
func TestMainApp_Method(t *testing.T) {
g := gomega.NewWithT(t)
inputGo = "example/method.go"
outputGo = "example/method_enum.go"
err := os.Remove(outputGo)
if err != nil {
t.Logf("rm %s: %s", "example/method_enum.go", err.Error())
// continue anyway
}
force = true
lowercase, uppercase, showVersion = false, false, false
outputJSON, marshalTextRep, marshalJSONRep, storeRep = "", "None", "None", "Number"
model.Prefix = ""
model.Suffix = ""
parse.AliasTable = ""
config = model.Config{
MainType: "Method",
IgnoreCase: true,
}
doMain()
compareGeneratedFile(g, outputGo)
}
func compareGeneratedFile(g *gomega.WithT, fileName string) {
f, err := os.Open(fileName)
g.Expect(err).NotTo(gomega.HaveOccurred())
defer f.Close()
src, err := io.ReadAll(f)
g.Expect(err).NotTo(gomega.HaveOccurred())
var s scanner.Scanner
fset := token.NewFileSet() // positions are relative to fset
file := fset.AddFile("", fset.Base(), len(src)) // register input "file"
s.Init(file, src, nil /* no error handler */, scanner.ScanComments)
// check just the first few lines of the generated Go source code
pos, tok, lit := s.Scan()
g.Expect(tok).To(gomega.Equal(token.COMMENT))
_, tok, lit = s.Scan()
g.Expect(tok).To(gomega.Equal(token.COMMENT))
_, tok, lit = s.Scan()
g.Expect(tok).To(gomega.Equal(token.PACKAGE))
_, tok, lit = s.Scan()
g.Expect(tok).To(gomega.Equal(token.IDENT))
g.Expect(lit).To(gomega.Equal("example"))
if testing.Verbose() {
for {
pos, tok, lit = s.Scan()
if tok == token.EOF {
break
}
fmt.Printf("%s\t%s\t%q\n", fset.Position(pos), tok, lit)
}
}
}
func TestScannerTryOut(t *testing.T) {
g := gomega.NewWithT(t)
if testing.Verbose() {
for _, n := range []string{"example/base.go", "example/day.go", "example/country.go", "example/month.go"} {
f, err := os.Open(n)
g.Expect(err).NotTo(gomega.HaveOccurred())
defer f.Close()
fmt.Printf("-- %s\n", n)
src, err := io.ReadAll(f)
g.Expect(err).NotTo(gomega.HaveOccurred())
var s scanner.Scanner
fset := token.NewFileSet() // positions are relative to fset
file := fset.AddFile("", fset.Base(), len(src)) // register input "file"
s.Init(file, src, nil /* no error handler */, 0)
for {
pos, tok, lit := s.Scan()
if tok == token.EOF {
break
}
fmt.Printf("%s\t%s\t%q\n", fset.Position(pos), tok, lit)
}
fmt.Printf("%s\n", strings.Repeat("-", 80))
}
}
}