forked from sirikothe/gotextfsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule_test.go
179 lines (175 loc) · 4.04 KB
/
rule_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package gotextfsm
import (
"regexp"
"testing"
)
type ruleTestCase struct {
input string
match string
line_op string
record_op string
new_state string
err *regexp.Regexp
}
func TestRuleParse(t *testing.T) {
for _, tc := range ruleTestCases {
v := TextFSMRule{}
err := v.Parse(tc.input, 1, nil, false)
if tc.err != nil {
if err == nil {
t.Errorf("'%s' failed. Expected error, but none found", tc.input)
}
continue
}
if err != nil {
t.Errorf("'%s' failed. Expected no error. But found error %s", tc.input, err)
continue
}
if tc.line_op != v.LineOp {
t.Errorf("'%s' failed. line_op dont match ('%s', '%s')", tc.input, tc.line_op, v.LineOp)
}
if tc.record_op != v.RecordOp {
t.Errorf("'%s' failed. record_op dont match ('%s', '%s')", tc.input, tc.record_op, v.RecordOp)
}
if tc.new_state != v.NewState {
t.Errorf("'%s' failed. new_state dont match ('%s', '%s')", tc.input, tc.new_state, v.NewState)
}
}
t.Logf("Executed %d test cases", len(ruleTestCases))
}
var ruleTestCases = []ruleTestCase{
{
input: ` `,
err: regexp.MustCompile(`.+`),
},
{
input: " ^A beer called ${beer}",
match: "^A beer called ${beer}",
line_op: "",
record_op: "",
new_state: "",
},
{
input: " ^A $hi called ${beer}",
match: "^A $hi called ${beer}",
line_op: "",
record_op: "",
new_state: "",
},
{
input: " ^A beer called ${beer} -> Next",
match: "^A beer called ${beer}",
line_op: "Next",
record_op: "",
new_state: "",
},
{
input: " ^A beer called ${beer} -> Continue.Record",
match: "^A beer called ${beer}",
line_op: "Continue",
record_op: "Record",
new_state: "",
},
{
input: " ^A beer called ${beer} -> Next.NoRecord End",
match: "^A beer called ${beer}",
line_op: "Next",
record_op: "NoRecord",
new_state: "End",
},
{
input: " ^A beer called ${beer} -> Next Next Next",
err: regexp.MustCompile(`.+`),
},
{
input: " ^A beer called ${beer} -> Boo.hoo",
err: regexp.MustCompile(`.+`),
},
{
input: " ^A beer called ${beer} -> Continue.Record $Hi",
err: regexp.MustCompile(`.+`),
},
{
input: " ^A beer called ${beer} -> Record End",
match: "^A beer called ${beer}",
record_op: "Record",
new_state: "End",
},
{
input: " ^A beer called ${beer} -> End",
match: "^A beer called ${beer}",
record_op: "",
new_state: "End",
},
{
input: " ^A beer called ${beer} -> Next.NoRecord End",
match: "^A beer called ${beer}",
line_op: "Next",
record_op: "NoRecord",
new_state: "End",
},
{
input: " ^A beer called ${beer} -> Clear End",
match: "^A beer called ${beer}",
record_op: "Clear",
new_state: "End",
},
{
input: ` ^A beer called ${beer} -> Error "Hello World"`,
match: "^A beer called ${beer}",
line_op: "Error",
new_state: `"Hello World"`,
},
{
input: ` ^A beer called ${beer} -> Next "Hello World`,
err: regexp.MustCompile(`.+`),
},
{
input: ` ^A beer called ${beer} -> Record.Next`,
err: regexp.MustCompile(`.+`),
},
{
input: ` ^A beer called ${beer} -> Continue End`,
err: regexp.MustCompile(`.+`),
},
{
input: ` ^A beer called ${beer} -> Beer End`,
err: regexp.MustCompile(`.+`),
},
{
input: ` ^Hello World -> Boo`,
match: "^Hello World",
new_state: "Boo",
},
{
input: ` ^Hello World -> Boo`,
match: "^Hello World",
new_state: "Boo",
},
{
input: ` ^Hello World -> Boo`,
match: "^Hello World",
new_state: "Boo",
},
// A '->' without a leading space is considered part of the matching line.
{
input: ` A simple line-> Boo -> Next`,
match: " A simple line-> Boo",
line_op: "Next",
},
// Alpha numaric characters only in state names
{
input: ` A simple line -> Next +`,
err: regexp.MustCompile(`.+`),
},
// Unicode characters
{
input: ` ^A beer called ${beer}Δ`,
match: `^A beer called ${beer}Δ`,
},
// Invalid regular expression
{
input: ` ^A beer called .+++ ${beer}Δ`,
err: regexp.MustCompile(`.+`),
},
}