forked from recursecenter/pairing-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_cmd_test.go
147 lines (133 loc) · 4.61 KB
/
parse_cmd_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
package main
import (
"testing"
)
var daysList = map[string]struct{}{
"monday": {},
"tuesday": {},
"wednesday": {},
"thursday": {},
"friday": {},
"saturday": {},
"sunday": {},
}
// due to the difficulties around comparing error return values (and because we don't want to compare error messages),
// the struct contains expectErr to indicate whether an error is expected, instead of an actual error value
var tableNoArgs = []struct {
testName string
inputStr string
wantedCmd string
wantedArgs []string
expectErr bool
}{
{"subscribe_correct_usage", "subscribe", "subscribe", nil, false},
{"subscribe_wrong_usage", "subscribe mon", "help", nil, true},
{"unsubscribe_correct_usage", "unsubscribe", "unsubscribe", nil, false},
{"unsubscribe_wrong_usage", "unsubscribe tuesday", "help", nil, true},
{"help_correct_usage", "help", "help", nil, false},
{"help_wrong_usage", "help me", "help", nil, true},
{"status_correct_usage", "status", "status", nil, false},
{"status_wrong_usage", "status me", "help", nil, true},
}
func TestParseCmdNoArgs(t *testing.T) {
for _, tt := range tableNoArgs {
t.Run(tt.testName, func(t *testing.T) {
gotCmd, gotArgs, gotErr := parseCmd(tt.inputStr)
if gotCmd != tt.wantedCmd {
t.Errorf("got %v, %v\n", gotCmd, gotArgs)
}
_, ok := gotErr.(*parsingErr)
if tt.expectErr && !ok {
t.Errorf("Expected parsingErr but didn't get one\n")
} else if !tt.expectErr && ok {
t.Errorf("Got unexpected parsingError\n")
}
})
}
}
var tableWithArgs = []struct {
testName string
inputStr string
wantedCmd string
wantedArgs []string
expectErr bool
}{
{"schedule_1_arg", "schedule monday", "schedule", []string{"monday"}, false},
{"schedule_2_args", "schedule monday friday", "schedule", []string{"monday", "friday"}, false},
{"schedule_3_args", "schedule monday wednesday friday", "schedule", []string{"monday", "wednesday", "friday"}, false},
{"schedule_4_args", "schedule monday wednesday friday sunday", "schedule", []string{"monday", "wednesday", "friday", "sunday"}, false},
{"schedule_weekend_only", "schedule sunday", "schedule", []string{"sunday"}, false},
{"schedule_wrong_usage", "schedule", "help", nil, true},
{"skip_correct_usage", "skip tomorrow", "skip", []string{"tomorrow"}, false},
{"skip_wrong_usage", "skip monday", "help", nil, true},
{"skip_wrong_usage", "skip whenever", "help", nil, true},
{"skip_wrong_usage", "skip", "help", nil, true},
{"unskip_correct_usage", "unskip tomorrow", "unskip", []string{"tomorrow"}, false},
{"unskip_wrong_usage", "unskip today", "help", nil, true},
{"unskip_wrong_usage", "unskip friday", "help", nil, true},
{"unskip_wrong_usage", "unskip", "help", nil, true},
}
func TestParseCmdWithArgs(t *testing.T) {
for _, tt := range tableWithArgs {
t.Run(tt.testName, func(t *testing.T) {
gotCmd, gotArgs, gotErr := parseCmd(tt.inputStr)
if gotCmd != tt.wantedCmd || len(gotArgs) != len(tt.wantedArgs) {
t.Errorf("got %v, %v, wanted %v, %v\n", gotCmd, gotArgs, tt.wantedCmd, tt.wantedArgs)
}
switch gotCmd {
case "schedule":
for i, arg := range gotArgs {
if _, ok := daysList[arg]; !ok {
t.Errorf("Wrong argument %v for command %v\n", gotArgs[i], gotCmd)
}
}
case "skip":
if gotArgs[0] != "tomorrow" {
t.Errorf("Wrong argument %v for command %v\n", gotArgs[0], gotCmd)
}
case "unskip":
if gotArgs[0] != "tomorrow" {
t.Errorf("Wrong argument %v for command %v\n", gotArgs[0], gotCmd)
}
default:
if gotCmd != "help" {
t.Errorf("unknown command %v\n", gotCmd)
}
}
_, ok := gotErr.(*parsingErr)
if tt.expectErr && !ok {
t.Errorf("Expected parsingErr but didn't get one\n")
} else if !tt.expectErr && ok {
t.Errorf("Got unexpected parsingError\n")
}
})
}
}
var tableMisc = []struct {
testName string
inputStr string
wantedCmd string
wantedArgs []string
expectErr bool
}{
{"command_is_superstring", "scheduleing monday", "help", nil, true},
{"command_is_substring", "schedul monday", "help", nil, true},
{"command_is_undefined", "mooh", "help", nil, true},
{"command_is_capitalized", "Help", "help", nil, false},
}
func TestParseCmdMisc(t *testing.T) {
for _, tt := range tableMisc {
t.Run(tt.testName, func(t *testing.T) {
gotCmd, gotArgs, gotErr := parseCmd(tt.inputStr)
if gotCmd != tt.wantedCmd {
t.Errorf("got %v, %v, wanted %v, %v\n", gotCmd, gotArgs, tt.wantedCmd, tt.wantedArgs)
}
_, ok := gotErr.(*parsingErr)
if tt.expectErr && !ok {
t.Errorf("Expected parsingErr but didn't get one\n")
} else if !tt.expectErr && ok {
t.Errorf("Got unexpected parsingError\n")
}
})
}
}