-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommand_test.go
123 lines (105 loc) · 4.61 KB
/
command_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
package allot
import (
"testing"
)
var resultCommand bool
func BenchmarkMatches(b *testing.B) {
var r bool
for n := 0; n < b.N; n++ {
r = New("command <lorem:integer> <ipsum:string>").Matches("command 12345 abcdef")
}
resultCommand = r
}
func TestMatches(t *testing.T) {
var data = []struct {
command string
request string
matches bool
}{
{"command", "example", false},
{"command", "command", true},
{"command", "command example", false},
{"command <lorem>", "command", false},
{"command <lorem>", "command example", true},
{"command <lorem>", "command 1234567", true},
{"command <lorem>", "command command", true},
{"command <lorem>", "example command", false},
{"command <lorem:integer>", "command example", false},
{"command <lorem:integer>", "command 1234567", true},
{"command <lorem>", "command command command", false},
}
for _, set := range data {
cmd := New(set.command)
if cmd.Matches(set.request) != set.matches {
t.Errorf("Matches() returns unexpected values. Got \"%v\", expected \"%v\"\nExpression: \"%s\" not matching \"%s\"", cmd.Matches(set.request), set.matches, cmd.Expression().String(), set.request)
}
}
}
func TestPosition(t *testing.T) {
var data = []struct {
command string
param Parameter
position int
}{
{"command <lorem>", NewParameterWithType("lorem", "string"), 0},
{"command <lorem> <ipsum> <dolor> <sit> <amet>", NewParameterWithType("dolor", "string"), 2},
{"command <lorem> <ipsum> <dolor:integer> <sit> <amet>", NewParameterWithType("dolor", "string"), -1},
{"command <lorem:integer> <ipsum> <dolor:integer> <sit> <amet>", NewParameterWithType("lorem", "string"), -1},
{"command <lorem:integer> <ipsum> <dolor:integer> <sit> <amet>", NewParameterWithType("lorem", "integer"), 0},
{"command <lorem:integer> <ipsum> <lorem:string> <sit> <amet>", NewParameterWithType("lorem", "integer"), 0},
{"command <lorem:integer> <ipsum> <lorem:string> <sit> <amet>", NewParameterWithType("lorem", "string"), 2},
}
var cmd Command
for _, set := range data {
cmd = New(set.command)
if cmd.Position(set.param) != set.position {
t.Errorf("Position() should be \"%d\", but is \"%d\"", set.position, cmd.Position(set.param))
}
}
}
func TestHas(t *testing.T) {
var data = []struct {
command string
parameter Parameter
has bool
}{
{"command <lorem>", NewParameterWithType("lorem", "string"), true},
{"command <lorem>", NewParameterWithType("lorem", "integer"), false},
}
var cmd CommandInterface
for _, set := range data {
cmd = New(set.command)
if cmd.Has(set.parameter) != set.has {
t.Errorf("HasParameter is \"%v\", expected \"%v\"", cmd.Has(set.parameter), set.has)
}
}
}
func TestParameters(t *testing.T) {
var data = []struct {
command string
parameters []Parameter
}{
{"command <lorem>", []Parameter{NewParameterWithType("lorem", "string")}},
{"cmd <lorem:string>", []Parameter{NewParameterWithType("lorem", "string")}},
{"command <lorem:integer>", []Parameter{NewParameterWithType("lorem", "integer")}},
{"example <lorem> <ipsum> <dolor>", []Parameter{NewParameterWithType("lorem", "string"), NewParameterWithType("ipsum", "string"), NewParameterWithType("dolor", "string")}},
{"command <lorem> <ipsum> <dolor:string>", []Parameter{NewParameterWithType("lorem", "string"), NewParameterWithType("ipsum", "string"), NewParameterWithType("dolor", "string")}},
{"command <lorem> <ipsum:string> <dolor>", []Parameter{NewParameterWithType("lorem", "string"), NewParameterWithType("ipsum", "string"), NewParameterWithType("dolor", "string")}},
{"command <lorem:string> <ipsum> <dolor>", []Parameter{NewParameterWithType("lorem", "string"), NewParameterWithType("ipsum", "string"), NewParameterWithType("dolor", "string")}},
{"command <lorem:string> <ipsum> <dolor:string>", []Parameter{NewParameterWithType("lorem", "string"), NewParameterWithType("ipsum", "string"), NewParameterWithType("dolor", "string")}},
{"command <lorem:string> <ipsum> <dolor:integer>", []Parameter{NewParameterWithType("lorem", "string"), NewParameterWithType("ipsum", "string"), NewParameterWithType("dolor", "integer")}},
{"command <lorem:integer> <ipsum:string> <dolor:integer>", []Parameter{NewParameterWithType("lorem", "integer"), NewParameterWithType("ipsum", "string"), NewParameterWithType("dolor", "integer")}},
}
var cmd Command
for _, set := range data {
cmd = New(set.command)
if cmd.Text() != set.command {
t.Errorf("cmd.Text() must be \"%s\", but is \"%s\"", set.command, cmd.Text())
}
for _, param := range set.parameters {
if !cmd.Has(param) {
t.Errorf("\"%s\" missing parameter.Item \"%s\"", cmd.Text(), param)
}
}
}
}