forked from bettercap/bettercap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_handler_test.go
58 lines (52 loc) · 1.13 KB
/
command_handler_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
package session
import (
"testing"
)
func sameStrings(a []string, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
func assertPanic(t *testing.T, msg string, f func()) {
defer func() {
if r := recover(); r == nil {
t.Fatal(msg)
}
}()
f()
}
func TestSessionCommandHandler(t *testing.T) {
var units = []struct {
expr string
panic bool
parsed []string
}{
{"notvali(d", true, nil},
{`simple\s+(\d+)`, false, []string{"123"}},
}
for _, u := range units {
if u.panic {
assertPanic(t, "", func() {
_ = NewCommandHandler("", u.expr, "", nil)
t.Fatal("panic expected")
})
} else {
c := NewCommandHandler("", u.expr, "", nil)
shouldNotParse := "simple123"
shouldParse := "simple 123"
if parsed, _ := c.Parse(shouldNotParse); parsed {
t.Fatalf("should not parse '%s'", shouldNotParse)
} else if parsed, parts := c.Parse(shouldParse); !parsed {
t.Fatalf("should parse '%s'", shouldParse)
} else if !sameStrings(parts, u.parsed) {
t.Fatalf("expected '%v', got '%v'", u.parsed, parts)
}
}
}
}