-
Notifications
You must be signed in to change notification settings - Fork 1
/
options_test.go
92 lines (83 loc) · 3.2 KB
/
options_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
package main
import (
"net/url"
"reflect"
"strings"
"testing"
)
type parseTestReulst struct {
version bool
exitCode int
sOpt *SearchOpt
}
func TestOption_Parse(t *testing.T) {
assert := func(result interface{}, want interface{}) {
if !reflect.DeepEqual(result, want) {
t.Errorf("Returned %+v, want %+v", result, want)
}
}
// want :exit, exitCode , sOpt, url, token
assert(testParse("ghs -v"), &parseTestReulst{true, ExitCodeOK, nil})
assert(testParse("ghs -h"), &parseTestReulst{false, ExitCodeError, nil})
defaultOpt := SearchOpt{
sort: "best match",
order: "desc",
query: "SEARCH_WORD",
max: 100,
perPage: 100,
baseURL: nil,
token: "",
}
// normal query test
wantOpt := defaultOpt
assert(testParse("ghs SEARCH_WORD"), &parseTestReulst{false, ExitCodeOK, &wantOpt})
wantOpt = defaultOpt
wantOpt.sort = "stars"
assert(testParse("ghs -s stars SEARCH_WORD"), &parseTestReulst{false, ExitCodeOK, &wantOpt})
wantOpt = defaultOpt
wantOpt.order = "asc"
assert(testParse("ghs -o asc SEARCH_WORD"), &parseTestReulst{false, ExitCodeOK, &wantOpt})
wantOpt = defaultOpt
wantOpt.max = 1000
assert(testParse("ghs -m 1000 SEARCH_WORD"), &parseTestReulst{false, ExitCodeOK, &wantOpt})
wantOpt = defaultOpt
wantOpt.baseURL, _ = url.Parse("http://test.exmaple/")
assert(testParse("ghs -e http://test.exmaple/ SEARCH_WORD"), &parseTestReulst{false, ExitCodeOK, &wantOpt})
wantOpt = defaultOpt
wantOpt.token = "abcdefg"
assert(testParse("ghs -t abcdefg SEARCH_WORD"), &parseTestReulst{false, ExitCodeOK, &wantOpt})
wantOpt = defaultOpt
wantOpt.query = "in:name SEARCH_WORD"
assert(testParse("ghs -f name SEARCH_WORD"), &parseTestReulst{false, ExitCodeOK, &wantOpt})
wantOpt = defaultOpt
wantOpt.query = "user:sona-tar"
assert(testParse("ghs -u sona-tar"), &parseTestReulst{false, ExitCodeOK, &wantOpt})
wantOpt = defaultOpt
wantOpt.query = "repo:sona-tar/ghs"
assert(testParse("ghs -r sona-tar/ghs"), &parseTestReulst{false, ExitCodeOK, &wantOpt})
wantOpt = defaultOpt
wantOpt.query = "language:golang"
assert(testParse("ghs -l golang"), &parseTestReulst{false, ExitCodeOK, &wantOpt})
// no args test
assert(testParse("ghs"), &parseTestReulst{false, ExitCodeError, nil})
assert(testParse("ghs -o asc"), &parseTestReulst{false, ExitCodeError, nil})
wantOpt = defaultOpt
wantOpt.query = "user:sona-tar"
assert(testParse("ghs -u sona-tar"), &parseTestReulst{false, ExitCodeOK, &wantOpt})
wantOpt = defaultOpt
wantOpt.query = "repo:sona-tar/ghs"
assert(testParse("ghs -r sona-tar/ghs"), &parseTestReulst{false, ExitCodeOK, &wantOpt})
wantOpt = defaultOpt
wantOpt.query = "language:golang"
assert(testParse("ghs -l golang"), &parseTestReulst{false, ExitCodeOK, &wantOpt})
// invalid option value test
assert(testParse("ghs -m 1001 SEARCH_WORD"), &parseTestReulst{false, ExitCodeError, nil})
assert(testParse("ghs -m 0 SEARCH_WORD"), &parseTestReulst{false, ExitCodeError, nil})
assert(testParse("ghs -e : SEARCH_WORD"), &parseTestReulst{false, ExitCodeError, nil})
}
func testParse(args_string string) *parseTestReulst {
args := strings.Split(args_string, " ")[1:]
flags, _ := NewFlags(args)
version, exitCode, sOpt := flags.ParseOption()
return &parseTestReulst{version, exitCode, sOpt}
}