-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
301 lines (284 loc) · 6.44 KB
/
main_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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMain(t *testing.T) {
info := func(msg string) {
fmt.Println("------------------------------------------------------------")
fmt.Println(" " + msg)
fmt.Println("------------------------------------------------------------")
}
info("普通の処理をファイル出力する")
os.Args = []string{
"main.go",
"-o",
"testdata/out/normal_clcode.list",
"testdata/in/normal_clcode.list",
}
main()
info("普通の処理を標準出力する")
os.Args = []string{
"main.go",
"testdata/in/normal_clcode.list",
}
main()
info("コンビネータ定義ファイルを読み込む")
os.Args = []string{
"main.go",
"-c",
"config/combinator.json",
"-o",
"testdata/out/read_combinator.list",
"testdata/in/normal_clcode.list",
}
main()
info("計算過程を標準出力する")
os.Args = []string{
"main.go",
"-p",
"testdata/in/normal_clcode.list",
}
main()
info("計算過程を標準出力するが、ヘッダを出力しない")
os.Args = []string{
"main.go",
"-pn",
"testdata/in/normal_clcode.list",
}
main()
info("計算結果をJSON出力する")
os.Args = []string{
"main.go",
"-t",
"json",
"testdata/in/normal_clcode.list",
}
main()
info("計算結果をJSON出力する + インデント")
os.Args = []string{
"main.go",
"-t",
"json",
"-i",
" ",
"testdata/in/normal_clcode.list",
}
main()
info("計算結果をJSON出力する + TABインデント")
os.Args = []string{
"main.go",
"-t",
"json",
"-i",
"\t",
"testdata/in/normal_clcode.list",
}
main()
info("計算結果をJSON出力する + インデント + 計算過程出力")
os.Args = []string{
"main.go",
"-pt",
"json",
"-i",
" ",
"testdata/in/normal_clcode.list",
}
main()
}
func TestCalcOut(t *testing.T) {
f := func(ss ...string) io.Reader {
return bytes.NewBufferString(strings.Join(ss, "\n"))
}
o1 := options{StepCount: -1}
o2 := options{StepCount: 1}
o3 := options{StepCount: -1, CombinatorFile: "config/combinator.json"}
type TD struct {
r io.Reader
opts options
success func([]string, options) error
failure func(error)
}
tds := []TD{
TD{
r: f("Sxyz"),
opts: o1,
success: func(ss []string, opts options) error {
assert.Equal(t, []string{"xz(yz)"}, ss)
return nil
},
failure: func(err error) {
assert.NoError(t, err)
},
},
TD{
r: f("Sxyz", "(SSSS)"),
opts: o1,
success: func(ss []string, opts options) error {
assert.Equal(t, []string{"xz(yz)", "SS(SS)"}, ss)
return nil
},
failure: func(err error) {
assert.NoError(t, err)
},
},
TD{
r: f("KKxy"),
opts: o2,
success: func(ss []string, opts options) error {
assert.Equal(t, []string{"Ky"}, ss)
return nil
},
failure: func(err error) {
assert.NoError(t, err)
},
},
TD{
r: f("<true>xy"),
opts: o3,
success: func(ss []string, opts options) error {
assert.Equal(t, []string{"x"}, ss)
return nil
},
failure: func(err error) {
assert.NoError(t, err)
},
},
TD{
r: f("SBKI"),
opts: o3,
success: func(ss []string, opts options) error {
assert.Equal(t, []string{"BI(KI)"}, ss)
return nil
},
failure: func(err error) {
assert.NoError(t, err)
},
},
}
for _, v := range tds {
r, opts, success, failure := v.r, v.opts, v.success, v.failure
err := calcOut(r, opts, success, failure)
assert.NoError(t, err)
}
}
func TestCalcCLCode(t *testing.T) {
f := func(ss ...string) io.Reader {
return bytes.NewBufferString(strings.Join(ss, "\n"))
}
type TD struct {
r io.Reader
opts options
s []string
desc string
}
o1 := options{StepCount: -1}
o2 := options{StepCount: 1, Indent: " "}
o3 := options{StepCount: 0}
o4 := options{StepCount: -1, OutFileType: "json"}
o5 := options{StepCount: -1, OutFileType: "json", PrintFlag: true}
o6 := options{StepCount: -1, OutFileType: "json", PrintFlag: true, Indent: " "}
o7 := options{StepCount: -1, PrintFlag: true}
o8 := options{StepCount: 2, PrintFlag: true}
o9 := options{StepCount: 2, OutFileType: "json", PrintFlag: true}
tds := []TD{
TD{
r: f("Sxyz"),
opts: o1,
s: []string{"xz(yz)"},
desc: "正常系:オプションなし",
},
TD{
r: f("Sxyz", "SKII"),
opts: o1,
s: []string{"xz(yz)", "I"},
desc: "正常系:複数行処理",
},
TD{
r: f("Sxyz", "SKII"),
opts: o2,
s: []string{"xz(yz)", "KI(II)"},
desc: "正常系:計算回数指定。及び、JSON指定がない状態でインデント指定をしても意味がない",
},
TD{
r: f("Sxyz", "SKII"),
opts: o3,
s: []string{"Sxyz", "SKII"},
desc: "正常系:計算しない",
},
TD{
r: f("SSSSS"),
opts: o1,
s: []string{"SS((SS)S)"},
desc: "正常系:ネスト括弧の計算をする",
},
TD{
r: f("Sxyz"),
opts: o4,
s: []string{`[{"input":"Sxyz","process":null,"result":"xz(yz)"}]`},
desc: "正常系:計算結果のJSON出力",
},
TD{
r: f("Sxyz", "Sxyz"),
opts: o4,
s: []string{`[{"input":"Sxyz","process":null,"result":"xz(yz)"},{"input":"Sxyz","process":null,"result":"xz(yz)"}]`},
desc: "正常系:計算結果の複数JSON出力",
},
TD{
r: f("SKIx"),
opts: o5,
s: []string{`[{"input":"SKIx","process":["Kx(Ix)","x"],"result":"x"}]`},
desc: "正常系:計算結果のJSON出力",
},
TD{
r: f("Sxyz", "Sxyz"),
opts: o6,
s: []string{`[
{
"input": "Sxyz",
"process": [
"xz(yz)"
],
"result": "xz(yz)"
},
{
"input": "Sxyz",
"process": [
"xz(yz)"
],
"result": "xz(yz)"
}
]`},
desc: "正常系:2スペースインデントされたJSON出力",
},
TD{
r: f("SKIx"),
opts: o7,
s: []string{"=== SKIx ===", "Kx(Ix)", "x", "x"},
desc: "正常系:計算過程の出力",
},
TD{
r: f("SSSSSS"),
opts: o8,
s: []string{"=== SSSSSS ===", "SS(SS)SS", "SS((SS)S)S", "SS((SS)S)S"},
desc: "正常系:計算回数指定",
},
TD{
r: f("SSSSSS"),
opts: o9,
s: []string{`[{"input":"SSSSSS","process":["SS(SS)SS","SS((SS)S)S"],"result":"SS((SS)S)S"}]`},
desc: "正常系:計算回数指定(json)",
},
}
for _, v := range tds {
r, opts, expect, desc := v.r, v.opts, v.s, v.desc
actual, err := calcCLCode(r, opts)
assert.Equal(t, expect, actual, desc, r, opts)
assert.NoError(t, err, desc)
}
}