-
Notifications
You must be signed in to change notification settings - Fork 4
/
subcommand_test.go
54 lines (52 loc) · 1.31 KB
/
subcommand_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
package gommand
import "testing"
// TestSubcommand is used to test that subcommands work properly.
func TestSubcommand(t *testing.T) {
r := NewRouter(&RouterConfig{
PrefixCheck: StaticPrefix("%"),
})
r.SetCommand(&CommandGroup{
Name: "a",
NoCommandSpecified: &Command{
Function: func(ctx *Context) error {
return nil
},
},
})
shouldFail := false
failed := false
r.AddErrorHandler(func(_ *Context, err error) bool {
if shouldFail {
failed = true
} else {
t.Fatal(err)
}
return true
})
r.CommandProcessor(nil, 0, mockMessage("%a"), true)
r.SetCommand(&CommandGroup{
Name: "b",
subcommands: map[string]CommandInterface{
"test": &Command{Function: func(ctx *Context) error {
return nil
}},
"arg_expected": &Command{ArgTransformers: []ArgTransformer{{Function: StringTransformer}}, Function: func(ctx *Context) error {
_ = ctx.Args[0].(string)
return nil
}},
},
})
shouldFail = true
r.CommandProcessor(nil, 0, mockMessage("%b"), true)
if !failed {
t.Fatal("command did not fail")
}
shouldFail = false
failed = false
r.CommandProcessor(nil, 0, mockMessage("%b test"), true)
shouldFail = true
r.CommandProcessor(nil, 0, mockMessage("%b arg_expected"), true)
shouldFail = false
failed = false
r.CommandProcessor(nil, 0, mockMessage("%b arg_expected test"), true)
}