-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
100 lines (90 loc) · 2.32 KB
/
config_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
package switchboard_test
import (
"strings"
"testing"
"github.com/vanstee/switchboard"
)
var (
helloCommand = &switchboard.Command{
Name: "hello",
Command: "echo hello",
Driver: switchboard.LocalDriver{},
Image: "",
Inline: "",
}
configTests = []struct {
body string
commands map[string]*switchboard.Command
routes map[string]switchboard.Route
}{
{
body: `
commands:
hello:
command: "echo hello"
routes:
"/hello":
command: hello`,
commands: map[string]*switchboard.Command{
"hello": helloCommand,
},
routes: map[string]switchboard.Route{
"/hello": &switchboard.BasicRoute{
Path: "/hello",
Command: helloCommand,
Methods: []string{"GET"},
Routes: nil,
},
},
},
}
)
func TestParseConfig(t *testing.T) {
for _, test := range configTests {
body := strings.Replace(test.body, "\t", " ", -1)
config, err := switchboard.ParseConfig(strings.NewReader(body))
if err != nil {
t.Fatalf("ParseConfig returned an error: %s", err)
}
commands := config.Commands
if len(commands) != len(test.commands) {
t.Fatalf("expected %d commands, got %d commands", len(test.commands), len(commands))
}
for name, tcommand := range test.commands {
command, ok := commands[name]
if !ok {
t.Fatalf("command %s not found", name)
}
if command.Name != tcommand.Name {
t.Errorf("expected command named %s, got command named %s", tcommand.Name, command.Name)
}
if command.Command != tcommand.Command {
t.Errorf("expected command %s, got command %s", tcommand.Command, command.Command)
}
}
routes := config.Routes
if len(routes) != 1 {
t.Fatalf("expected %d routes, got %d routes", 1, len(routes))
}
for path, tiroute := range test.routes {
iroute, ok := routes[path]
if !ok {
t.Fatal("route %s not found", path)
}
route, ok := iroute.(*switchboard.BasicRoute)
if !ok {
t.Fatal("iroute is not a basic route")
}
troute, ok := tiroute.(*switchboard.BasicRoute)
if !ok {
t.Fatal("tiroute is not a basic route")
}
if route.Path != troute.Path {
t.Errorf("expected route %s, got route %s", troute.Path, route.Path)
}
if route.Command.Name != troute.Command.Name {
t.Errorf("expected route to have command %s, got command %s", troute.Command.Name, route.Command.Name)
}
}
}
}