-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathroot_test.go
579 lines (571 loc) · 15.9 KB
/
root_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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//go:build !docker
package main
import (
"os"
"testing"
"github.com/jiro4989/textimg/v3/config"
"github.com/stretchr/testify/assert"
)
func TestRunRootCommand(t *testing.T) {
b, _ := os.ReadFile(inDir + "/red_grad.txt")
grad := string(b)
b, _ = os.ReadFile(inDir + "/255.txt")
c255 := string(b)
tests := []struct {
desc string
c config.Config
args []string
envs config.EnvVars
wantErr bool
existsFile string
}{
{
desc: "正常系: PrintEnvironmentsが設定されていると環境変数を出力して終了",
c: config.Config{
PrintEnvironments: true,
},
args: []string{"hello"},
envs: config.EnvVars{},
wantErr: false,
},
{
desc: "正常系: 正常系がパスする。出力先はモックWriterなのでファイルは生成されない",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = "t.png"
return c
}(),
args: []string{"hello"},
envs: config.EnvVars{},
wantErr: false,
},
{
desc: "異常系: Writerがエラーを返す",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = "t.png"
c.Writer = config.NewMockWriter(true, false)
return c
}(),
args: []string{"hello"},
envs: config.EnvVars{},
wantErr: true,
},
// 旧 main_test.go を移行してきたもの
{
desc: "正常系: 画像ファイルに出力する",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_font_is_red_and_background_is_black.png"
c.Writer = nil
return c
}(),
args: []string{"1234\x1b[31mred\x1b[m5678\nabcd\x1b[32mgreen\x1b[0mefgh\nあい\x1b[33mう\x1b[mえお"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_font_is_red_and_background_is_black.png",
},
{
desc: "正常系: Foregroundのみもとにもどす、Backgroundのみもとに戻す",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_foreground_default_background_default.png"
c.Writer = nil
return c
}(),
args: []string{"\x1b[31;42mRedGreen\x1b[39mRedGreen\x1b[49mRedGreen"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_foreground_default_background_default.png",
},
{
desc: "正常系: 256色を使う",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_color_256.png"
c.Writer = nil
return c
}(),
args: []string{c255},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_color_256.png",
},
{
desc: "正常系: RGB色を使う",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_color_rgb.png"
c.Writer = nil
return c
}(),
args: []string{grad},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_color_rgb.png",
},
{
desc: "正常系: JPEGで出力する",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_jpeg.jpeg"
c.Writer = nil
return c
}(),
args: []string{"jpeg"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_jpeg.jpeg",
},
{
desc: "正常系: GIFで出力する",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_gif.gif"
c.Writer = nil
return c
}(),
args: []string{"gif"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_gif.gif",
},
{
desc: "正常系: 日本語と絵文字を描画する(ただし豆腐になる)。このテストはDockerの方で実施する",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_tofu.png"
c.Writer = nil
return c
}(),
args: []string{"あいうえお👍"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_tofu.png",
},
{
desc: "正常系: 前景色と背景色を反転する",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_reverse.png"
c.Writer = nil
return c
}(),
args: []string{"\x1b[31;42mRED\x1b[7m\nGREEN\x1b[0m"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_reverse.png",
},
{
desc: "正常系: 文字色と背景色を変更する",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_font_is_green_and_background_is_blue.png"
c.Writer = nil
c.Foreground = "green"
c.Background = "blue"
return c
}(),
args: []string{"green"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_font_is_green_and_background_is_blue.png",
},
{
desc: "正常系: カンマ区切りで指定",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_font_is_blue_and_background_is_red.png"
c.Writer = nil
c.Foreground = "0,0,255,255"
c.Background = "255,0,0,255"
return c
}(),
args: []string{"blue"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_font_is_blue_and_background_is_red.png",
},
{
desc: "正常系: Slackアイコンサイズで生成する",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_font_is_blue_and_background_is_red_slack_icon_size.png"
c.Writer = nil
c.Foreground = "0,0,255,255"
c.Background = "255,0,0,255"
c.ToSlackIcon = true
return c
}(),
args: []string{"slack"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_font_is_blue_and_background_is_red_slack_icon_size.png",
},
{
desc: "正常系: 明示的に幅を指定できる",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_font_is_blue_and_background_is_red_100x200.png"
c.Writer = nil
c.Foreground = "0,0,255,255"
c.Background = "255,0,0,255"
c.ResizeWidth = 100
c.ResizeHeight = 200
return c
}(),
args: []string{"100x200"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_font_is_blue_and_background_is_red_100x200.png",
},
{
desc: "正常系: Widthのみを指定した場合はHeightが調整される",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_font_is_blue_and_background_is_red_100w.png"
c.Writer = nil
c.Foreground = "0,0,255,255"
c.Background = "255,0,0,255"
c.ResizeWidth = 100
return c
}(),
args: []string{"100w"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_font_is_blue_and_background_is_red_100w.png",
},
{
desc: "正常系: Heightのみを指定した場合はWidthが調整される",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_font_is_blue_and_background_is_red_100h.png"
c.Writer = nil
c.Foreground = "0,0,255,255"
c.Background = "255,0,0,255"
c.ResizeHeight = 100
return c
}(),
args: []string{"100h"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_font_is_blue_and_background_is_red_100h.png",
},
{
desc: "正常系: 1行のアニメを生成できる",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_animation_1_line.gif"
c.Writer = nil
c.UseAnimation = true
c.LineCount = 1
return c
}(),
args: []string{"\x1b[31m1\n\x1b[32m2\n\x1b[33m3\n\x1b[34m4"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_animation_1_line.gif",
},
{
desc: "正常系: 2行のアニメを生成できる",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_animation_2_line.gif"
c.Writer = nil
c.UseAnimation = true
c.LineCount = 2
return c
}(),
args: []string{"\x1b[31m1\n\x1b[32m2\n\x1b[33m3\n\x1b[34m4"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_animation_2_line.gif",
},
{
desc: "正常系: 4行のアニメを生成できる",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_animation_4_line.gif"
c.Writer = nil
c.UseAnimation = true
c.LineCount = 4
return c
}(),
args: []string{"\x1b[31m1\n\x1b[32m2\n\x1b[33m3\n\x1b[34m4\n\x1b[31m5\n\x1b[32m6\n\x1b[33m7\n\x1b[34m8"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_animation_4_line.gif",
},
{
desc: "正常系: 8行のアニメを生成できる",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_animation_8_line.gif"
c.Writer = nil
c.UseAnimation = true
c.LineCount = 8
return c
}(),
args: []string{"\x1b[31m1\n\x1b[32m2\n\x1b[33m3\n\x1b[34m4\n\x1b[31m5\n\x1b[32m6\n\x1b[33m7\n\x1b[34m8"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_animation_8_line.gif",
},
{
desc: "正常系: 4行のアニメを2行ずつスライドする",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_animation_4_line_slide_2_forever.gif"
c.Writer = nil
c.UseAnimation = true
c.LineCount = 4
c.UseSlideAnimation = true
c.SlideWidth = 2
c.SlideForever = true
c.Delay = 100
return c
}(),
args: []string{"\x1b[31m1\n\x1b[32m2\n\x1b[33m3\n\x1b[34m4\n\x1b[31m5\n\x1b[32m6\n\x1b[33m7\n\x1b[34m8"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_animation_4_line_slide_2_forever.gif",
},
{
desc: "正常系: 4行のアニメを3行ずつスライドする",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_animation_4_line_slide_3_forever.gif"
c.Writer = nil
c.UseAnimation = true
c.LineCount = 4
c.UseSlideAnimation = true
c.SlideWidth = 3
c.SlideForever = true
c.Delay = 100
return c
}(),
args: []string{"\x1b[31m1\n\x1b[32m2\n\x1b[33m3\n\x1b[34m4\n\x1b[31m5\n\x1b[32m6\n\x1b[33m7\n\x1b[34m8"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_animation_4_line_slide_3_forever.gif",
},
{
desc: "正常系: SlackアイコンサイズでアニメーションGIFを生成できる",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_slack_icon_size_animation.gif"
c.Writer = nil
c.ToSlackIcon = true
c.UseAnimation = true
return c
}(),
args: []string{"1\n2\n3\n4"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_slack_icon_size_animation.gif",
},
{
desc: "正常系: すでに同名のファイルが存在する時、別名で保存される",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_numbering.png"
c.Writer = nil
c.SaveNumberedFile = true
return c
}(),
args: []string{"number"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_numbering.png",
},
{
desc: "正常系: すでに同名のファイルが存在する時、別名で保存される_2",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_numbering.png"
c.Writer = nil
c.SaveNumberedFile = true
return c
}(),
args: []string{"number"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_numbering_2.png",
},
{
desc: "正常系: すでに同名のファイルが存在する時、別名で保存される_3",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_numbering.png"
c.Writer = nil
c.SaveNumberedFile = true
return c
}(),
args: []string{"number"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_numbering_3.png",
},
{
desc: "正常系: フォントインデックスを指定できる",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_index.png"
c.Writer = nil
c.FontIndex = 0
c.EmojiFontIndex = 0
return c
}(),
args: []string{"index"},
envs: config.EnvVars{},
wantErr: false,
existsFile: outDir + "/root_test_index.png",
},
{
desc: "異常系: 空文字列は不正",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_empty_string.png"
c.Writer = nil
return c
}(),
args: []string{""},
envs: config.EnvVars{},
wantErr: true,
},
{
desc: "異常系: 改行文字のみは不正",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_only_line.png"
c.Writer = nil
return c
}(),
args: []string{"\n\n\n"},
envs: config.EnvVars{},
wantErr: true,
},
{
desc: "異常系: 色文字列が不正",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_numbering.png"
c.Writer = nil
c.Foreground = "ggg"
return c
}(),
args: []string{"ggg"},
envs: config.EnvVars{},
wantErr: true,
},
{
desc: "異常系: 背景色が不正",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_numbering.png"
c.Writer = nil
c.Background = "ggg"
return c
}(),
args: []string{"ggg"},
envs: config.EnvVars{},
wantErr: true,
},
{
desc: "異常系: 不正なフォント指定",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_numbering.png"
c.Writer = nil
c.FontFile = inDir + "/illegal_font.ttc"
return c
}(),
args: []string{"ggg"},
envs: config.EnvVars{},
wantErr: true,
},
{
desc: "異常系: 不正な絵文字フォント指定",
c: func() config.Config {
c := newDefaultConfig()
c.Outpath = outDir + "/root_test_numbering.png"
c.Writer = nil
c.EmojiFontFile = inDir + "/illegal_font.ttc"
return c
}(),
args: []string{"ggg"},
envs: config.EnvVars{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
assert := assert.New(t)
err := RunRootCommand(tt.c, tt.args, tt.envs)
if tt.wantErr {
assert.Error(err)
return
}
assert.NoError(err)
if tt.existsFile != "" {
_, err := os.Stat(tt.existsFile)
got := os.IsNotExist(err)
assert.False(got)
}
})
}
}
func TestComplementWidthHeight(t *testing.T) {
type TestData struct {
desc string
x, y, w, h int
wantWidth int
wantHeight int
}
tds := []TestData{
{
desc: "正常系: wが0のときはwidthが調整される",
x: 200,
y: 100,
w: 0,
h: 200,
wantWidth: 400,
wantHeight: 200,
},
{
desc: "正常系: hが0のときはheightが調整される",
x: 200,
y: 100,
w: 100,
h: 0,
wantWidth: 100,
wantHeight: 50,
},
{
desc: "正常系: hが0のときはheightが調整される",
x: 200,
y: 100,
w: 100,
h: 0,
wantWidth: 100,
wantHeight: 50,
},
{
desc: "正常系: wとhが0出ないときはwとhが返る",
x: 200,
y: 100,
w: 400,
h: 300,
wantWidth: 400,
wantHeight: 300,
},
}
for _, v := range tds {
t.Run(v.desc, func(t *testing.T) {
a := assert.New(t)
w, h := complementWidthHeight(v.x, v.y, v.w, v.h)
a.Equal(v.wantWidth, w)
a.Equal(v.wantHeight, h)
})
}
}