commands: Wrote tests for command option validation
Matt Bell committed
Oct 8, 2014 at 14:30 UTC
dd682963a2f14535b77ce78c243f83052122eaaf
1 file changed
+60
commands/command_test.go
new
+60
@@ -0,0 +1,60 @@
1
+package commands
2
+
3
+import "testing"
4
+
5
+func TestOptionValidation(t *testing.T) {
6
+ cmd := Command{
7
+ Options: []Option{
8
+ Option{ []string{ "b", "beep" }, Int },
9
+ Option{ []string{ "B", "boop" }, String },
10
+ },
11
+ f: func(req *Request) (interface{}, error) {
12
+ return nil, nil
13
+ },
14
+ }
15
+
16
+ req := NewRequest()
17
+ req.options["foo"] = 5
18
+ _, err := cmd.Call(nil, req)
19
+ if err == nil {
20
+ t.Error("Should have failed (unrecognized command)")
21
+ }
22
+
23
+ req = NewRequest()
24
+ req.options["beep"] = 5
25
+ req.options["b"] = 10
26
+ _, err = cmd.Call(nil, req)
27
+ if err == nil {
28
+ t.Error("Should have failed (duplicate options)")
29
+ }
30
+
31
+ req = NewRequest()
32
+ req.options["beep"] = "foo"
33
+ _, err = cmd.Call(nil, req)
34
+ if err == nil {
35
+ t.Error("Should have failed (incorrect type)")
36
+ }
37
+
38
+ req = NewRequest()
39
+ req.options["beep"] = 5
40
+ _, err = cmd.Call(nil, req)
41
+ if err != nil {
42
+ t.Error("Should have passed")
43
+ }
44
+
45
+ req = NewRequest()
46
+ req.options["beep"] = 5
47
+ req.options["boop"] = "test"
48
+ _, err = cmd.Call(nil, req)
49
+ if err != nil {
50
+ t.Error("Should have passed")
51
+ }
52
+
53
+ req = NewRequest()
54
+ req.options["b"] = 5
55
+ req.options["B"] = "test"
56
+ _, err = cmd.Call(nil, req)
57
+ if err != nil {
58
+ t.Error("Should have passed")
59
+ }
60
+}