-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
71 lines (67 loc) · 1.82 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
package main
import (
"io"
"os"
"strings"
"testing"
s "github.com/palsmo/comic-epub/src"
)
func initArgs(arg string) {
os.Args = []string{s.Cmd}
if len(arg) > 0 {
os.Args = append(os.Args, strings.FieldsFunc(arg, func(r rune) bool {
return r == ' ' || r == '\n'
})...)
}
}
func TestMainArgs(t *testing.T) {
w := io.Discard // shorthand
t.Parallel()
// test cases
tcs := []struct {
arg string
code int
}{
// basic
{"", 0}, // no args -> valid
{".", 0}, // current path -> valid
{"~", 1}, // invalid path -> error
{"-~", 1}, // unknown flag -> error
// single argument option
{"-t", 1}, // too few args -> error
{"-t ~", 1}, // missing path -> error
{"-t ~ .", 0}, // has arg and path -> valid
// // language format
{"-l ~ .", 1}, // bad parse -> error
{"-l eng .", 0}, // ok parse -> valid
// background color format
{"-bg ~ .", 1}, // bad parse -> error
{"-bg #000 .", 0}, // ok parse -> valid
// output path
{"-out ~ .", 1}, // invalid path -> error
{"-out . .", 0}, // ok path -> valid
// multi argument option & chapter format
{"-c", 1}, // too few args -> error
{"-c ~", 1}, // invalid format on arg -> error
{"-c N:~", 1}, // invalid page number -> error
{"-c 7:~", 1}, // missing path -> error
{"-c 7:~ 7:~", 1}, // page number already set -> error
{"-c 7:~ 42:~", 1}, // no stop, missing path -> error
{"-c 7:~ 42:~ ,", 1}, // with stop, missing path -> error
{"-c 7:~ 42:~ , .", 0}, // valid arg(s), stop and path -> valid
// full command
{"-t ~ -a ~ -d ~ -c 7:~ 42:~ , -l eng -bg #000 -out . .", 0},
}
// run each test case
for _, tc := range tcs {
t.Run(tc.arg, func(t *testing.T) {
// init
initArgs(tc.arg)
s.Setup(w)
// try run
if tc.code != s.Run(w) {
t.Fail()
}
})
}
}