-
Notifications
You must be signed in to change notification settings - Fork 4
/
default_test.go
63 lines (57 loc) · 1.48 KB
/
default_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
package gommand
import (
"fmt"
"reflect"
"testing"
)
func TestDefault(t *testing.T) {
tables := []struct {
rawArgs string
expected []interface{}
}{
{"42 \"test string\"", []interface{}{[]interface{}{42}, "test string", "string 2"}},
{"5 \"test string\"", []interface{}{[]interface{}{5}, "test string", "string 2"}},
{"1 \"test string\"", []interface{}{[]interface{}{1}, "test string", "string 2"}},
{"1 2 3 4 hello", []interface{}{[]interface{}{1, 2, 3, 4}, "hello", "string 2"}},
{"1", []interface{}{[]interface{}{1}, "test string", "string 2"}},
{"1 hello \"replace string 2\"", []interface{}{[]interface{}{1}, "hello", "replace string 2"}},
}
test := 0
r := NewRouter(&RouterConfig{
PrefixCheck: StaticPrefix("%"),
})
r.SetCommand(&Command{
Name: "default",
ArgTransformers: []ArgTransformer{
{
Function: IntTransformer,
Greedy: true,
},
{
Function: StringTransformer,
Default: "test string",
},
{
Function: StringTransformer,
Default: "string 2",
},
},
Function: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.Args, tables[test].expected) {
// reflect.DeepEqual is slow but should be fine for test cases
t.Log(fmt.Sprintf("test %d failed", test))
t.FailNow()
}
return nil
},
})
r.AddErrorHandler(func(_ *Context, err error) bool {
t.Log(err)
t.FailNow()
return true
})
for _, j := range tables {
r.CommandProcessor(nil, 0, mockMessage("%default "+j.rawArgs), true)
test++
}
}