forked from sbstjn/allot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_test.go
105 lines (86 loc) · 3.79 KB
/
match_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
package allot
import "testing"
func TestMatch(t *testing.T) {
var data = []struct {
command string
request string
position int
value string
}{
{"command <param1:integer>", "command 1234", 0, "1234"},
{"revert from <project:string> last <commits:integer> commits", "revert from example last 51 commits", 1, "51"},
{"revert from <project:string> last <commits:integer> commits on (stage|prod)", "revert from example last 51 commits on stage", 2, "stage"},
{"revert from <project:string> last <commits:integer> commits on (stage|prod)", "revert from example last 51 commits on prod", 2, "prod"},
}
for _, set := range data {
match, err := New(set.command).Match(set.request)
if err != nil {
t.Errorf("Request [%s] does not match Command [%s]\n => %s", set.request, set.command, New(set.command).Expression().String())
}
value, err := match.Match(set.position)
if err != nil {
t.Errorf("Parsign parameter returned error: %v", err)
}
if value != set.value {
t.Errorf("GetString() returned incorrect value. Got \"%v\", expected \"%v\"", value, set.value)
}
}
}
func TestMatchAndInteger(t *testing.T) {
var data = []struct {
command string
request string
parameter string
value int
}{
{"command <param1:integer>", "command 1234", "param1", 1234},
{"revert from <project:string> last <commits:integer> commits", "revert from example last 51 commits", "commits", 51},
}
for _, set := range data {
match, err := New(set.command).Match(set.request)
if err != nil {
t.Errorf("Request [%s] does not match Command [%s]\n => %s", set.request, set.command, New(set.command).Expression().String())
}
value, err := match.Integer(set.parameter)
if err != nil {
t.Errorf("Parsign parameter returned error: %v", err)
}
if value != set.value {
t.Errorf("GetString() returned incorrect value. Got \"%d\", expected \"%d\"", value, set.value)
}
}
}
func TestMatchAndString(t *testing.T) {
var data = []struct {
command string
request string
parameter string
value string
}{
{"command <param1>", "command lorem", "param1", "lorem"},
{"deploy <project:string> to <environment:string>", "deploy example to stage", "project", "example"},
{"deploy <project:string> to <environment:string>", "deploy example to stage", "environment", "stage"},
{"deploy <project:string> to <environment:string> at <host>", "deploy example to stage at api.exa!@#$%^&*()_mple.com", "host", "api.exa!@#$%^&*()_mple.com"},
{"deploy <project:string> to <environment:string> at <host>", "deploy exam/ple to stage at api-prod.example.com", "host", "api-prod.example.com"},
{"deploy <project:string> to <environment:string> at <host>", "deploy @klaus to stage at api-<test>.example.com", "project", "@klaus"},
{"deploy <project:string> to <environment:string> at <host>", "deploy \"klaus\" to stage at api-<test>.example.com", "project", "\"klaus\""},
{"deploy <project:string>-<stage:string> to <host>", "deploy klaus-prod to example", "project", "klaus"},
{"deploy <project:string>-<stage:string> to <host>", "deploy klaus-prod to example", "stage", "prod"},
{"deploy <project:string> to (stage|prod)", "deploy klaus to stage", "project", "klaus"},
{"deploy <project:string> to (stage|prod)+", "deploy klaus to prod", "project", "klaus"},
{"deploy <project:string> to (stage|prod)*", "deploy klaus to ", "project", "klaus"},
}
for _, set := range data {
match, err := New(set.command).Match(set.request)
if err != nil {
t.Errorf("Request [%s] does not match Command [%s]", set.request, set.command)
}
value, err := match.String(set.parameter)
if err != nil {
t.Errorf("Parsign parameter returned error: %v", err)
}
if value != set.value {
t.Errorf("GetString() returned incorrect value. Got \"%s\", expected \"%s\"", value, set.value)
}
}
}