-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder_test.go
65 lines (58 loc) · 1.48 KB
/
builder_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
package bifurcate
import (
"fmt"
"log"
"testing"
)
func TestBuilder(t *testing.T) {
builder := NewBuilder()
handler := func(ctx *Context, next *Handler) error {
fmt.Println(ctx.Data)
return nil
}
// Test case 1: Parse a valid command
err := builder.Parse("foo bar {id:literal}", handler)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
// Test case 2: Parse an invalid command
err = builder.Parse("foo bar {id}", handler)
if err == nil {
t.Error("Expected an error, but got nil")
}
// Test case 3: Parse without a handler
err = builder.Parse("foo bar {id:literal}", nil)
if err == nil {
t.Error("Expected an error, but got nil")
}
}
func TestNode_Match(t *testing.T) {
builder := NewBuilder()
log.SetFlags(log.LstdFlags | log.Lshortfile)
handler := func(ctx *Context, next *Handler) error {
return nil
}
err := builder.Parse("foo bar {id:literal}", handler)
if err != nil {
t.Log(err)
}
root := builder.Build()
//fmt.Println(root)
root.PrintTree("", true)
// Test case 1: Match a valid command
ctx, h, err := root.Match(nil, NewCommandHelper("foo bar 123"))
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if ctx.GetData("id") != "123" {
t.Errorf("Expected id to be '123', but got '%v'", ctx.GetData("id"))
}
if h == nil {
t.Error("Expected a handler, but got nil")
}
// Test case 2: Match an invalid command
ctx, h, err = root.Match(nil, NewCommandHelper("foo baz"))
if err == nil {
t.Error("Expected an error, but got nil")
}
}