-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_test.go
190 lines (161 loc) · 5.04 KB
/
run_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 sts
import (
"io/ioutil"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)
var _ = Describe("Run", func() {
Describe("split", func() {
DescribeTable("split",
func(input, exp1, exp2 string) {
got1, got2 := split(input)
Expect(got1).To(Equal(exp1))
Expect(got2).To(Equal(exp2))
},
Entry("Empty stirng", "", "", ""),
Entry("String without semicolon", "abc", "", ""),
Entry("Semicolon", ":", "", ""),
Entry("Semicolon with 1st part", "abc:", "abc", ""),
Entry("Semicolon with 2nd part", ":def", "", "def"),
Entry("String with 2 parts divided by :", "abc:def", "abc", "def"),
Entry("String with 3 parts divided by :", "abc:def:zzz", "", ""),
)
})
Describe("pkgFromPath", func() {
DescribeTable("pkgFromPath",
func(input, expected string) {
got := pkgFromPath(input)
Expect(got).To(Equal(expected))
},
Entry("Empty string", "", ""),
Entry("String without slash", "abc", "abc"),
Entry("Root", "/", ""),
Entry("1st level", "/abc", "abc"),
Entry("2nd level", "/abc/def", "def"),
Entry("3rd level", "/abc/def/ggg", "ggg"),
Entry("Relative with dot", "./abc/def", "def"),
Entry("Relative", "abc/def", "def"),
Entry("Deep down", "/a/b/c/d/f/e/g/h/i/j/k/l/m/n/o/p", "p"),
)
})
Describe("Run", func() {
type input struct {
left string
right string
sourceTag string
destTags string
outputDir string
helperPkg string
version string
expectedName string
expectedErr string
}
DescribeTable("cases",
func(in input) {
fname, content, err := Run(in.left, in.right, in.sourceTag, in.destTags,
in.outputDir, in.helperPkg, in.version, false, nil)
if in.expectedErr == "" {
Expect(err).NotTo(HaveOccurred())
} else {
Expect(err).To(MatchError(in.expectedErr))
}
Expect(fname).To(Equal(in.expectedName))
if in.expectedName == "" {
return
}
gldn := "./testdata/run/" + in.expectedName + ".golden"
Expect(gldn).To(BeAnExistingFile())
fc, err := ioutil.ReadFile(gldn)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal(string(fc)))
},
Entry("Empty params",
input{
expectedErr: `incorrect source, format is "/path/to/file.go:struct_name"`, //nolint
},
),
Entry("Source only",
input{
left: "a.go:A",
expectedErr: `incorrect destination, format is "/path/to/file.go:struct_name"`, //nolint
},
),
Entry("001: Field without tags",
input{
left: "./testdata/run/input/source/001_a.go:A001",
right: "./testdata/run/input/dest/001_b.go:B001",
sourceTag: "sts",
outputDir: ".",
helperPkg: "helpers",
version: "0.0.1",
expectedName: "a001_to_b001.sts.go",
},
),
Entry("002: Some field with tags",
input{
left: "./testdata/run/input/002_a.go:A002",
right: "./testdata/run/input/002_b.go:B002",
sourceTag: "sts",
outputDir: ".",
helperPkg: "helpers",
version: "0.0.2",
expectedName: "a002_to_b002.sts.go",
},
),
Entry("003: Source struct not found",
input{
left: "./testdata/run/input/source/001_a.go:None",
right: "./testdata/run/input/dest/001_b.go:B001",
sourceTag: "sts",
outputDir: ".",
expectedErr: `source structure "None" not found: `,
},
),
Entry("004: Destination struct not found",
input{
left: "./testdata/run/input/source/001_a.go:A001",
right: "./testdata/run/input/dest/001_b.go:None",
sourceTag: "sts",
outputDir: ".",
expectedErr: `destination structure "None" not found: `,
},
),
Entry("005: Source 'foo' tag is mapped to field name on destination", input{
left: "./testdata/run/input/002_a.go:A002",
right: "./testdata/run/input/foo.go:Foo",
sourceTag: "foo",
outputDir: ".",
version: "0.0.5",
expectedName: "a002_to_foo.sts.go",
}),
Entry("006: Source 'foo' tag is mapped to 'bar' on destination", input{
left: "./testdata/run/input/002_a.go:A002",
right: "./testdata/run/input/bar.go:Bar",
sourceTag: "bar",
destTags: "bar",
outputDir: ".",
version: "0.0.6",
expectedName: "a002_to_bar.sts.go",
}),
Entry("007: Same struct with identical names in diffrent packages", input{
left: "./testdata/run/input/bar.go:Bar",
right: "./testdata/run/input/dest/bar.go:Bar",
sourceTag: "bar",
destTags: "bar",
outputDir: ".",
version: "0.0.7",
expectedName: "input_bar_to_dest_bar.sts.go",
}),
Entry("008: Custom types with convertable underlying types.", input{
left: "./testdata/run/input/source/custom_type.go:AC",
right: "./testdata/run/input/dest/custom_type.go:DC",
sourceTag: "json",
destTags: "json",
outputDir: ".",
version: "0.0.8",
expectedName: "ac_to_dc.sts.go",
}),
)
})
})