mirror of
https://github.com/bettercap/bettercap
synced 2025-07-07 05:22:04 -07:00
more unit tests
This commit is contained in:
parent
5a60e8897e
commit
735d074cd8
3 changed files with 73 additions and 15 deletions
57
session/command_handler_test.go
Normal file
57
session/command_handler_test.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
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)
|
||||
})
|
||||
} else {
|
||||
c := NewCommandHandler("", u.expr, "", nil)
|
||||
shouldNotParse := "simple123"
|
||||
shouldParse := "simple 123"
|
||||
|
||||
if parsed, parts := 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue