-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcookoo_test.go
109 lines (95 loc) · 2.52 KB
/
cookoo_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
package cookoo
import (
"fmt"
"testing"
)
func Example() {
// This is an admittedly contrived example in which we first store a
// "Hello World" message, and then tell the logger to get that stored
// message and write it to the log.
reg, router, cxt := Cookoo()
reg.AddRoute(Route{
Name: "hello",
Help: "Sends the log message 'Hello World'",
Does: Tasks{
// First, store the message "Hello World" in the context.
Cmd{
Name: "message",
Fn: AddToContext,
Using: []Param{
Param{
Name: "hello",
DefaultValue: "Hello World",
},
},
},
// Now get that message and write it to the log.
Cmd{
Name: "log",
Fn: LogMessage,
Using: []Param{
Param{
Name: "msg",
From: "cxt:message",
},
},
},
},
})
router.HandleRequest("hello", cxt, false)
}
func ExampleCookoo() {
reg, router, cxt := Cookoo()
reg.AddRoute(Route{
// The name of the route. You execute routes by name. (See router.HandleRequest below)
Name: "hello",
// This is for documentation/help tools.
Help: "Print a message on standard output",
// This is a list of things you want this route to do. When executed,
// it will run these commands in order.
Does: Tasks{
// Declare a new command.
Cmd{
// Give the command a name. Programs reference command output
// by this name.
Name: "print",
// Tell Cookoo what function to execute when we get to this
// step.
//
// Usually we define functions elsewhere so we can re-use them.
Fn: func(c Context, p *Params) (interface{}, Interrupt) {
// Print whatever the content of the 'msg' parameter is.
fmt.Println(p.Get("msg", "").(string))
return nil, nil
},
// Send some parameters into Fn. Here we define the 'msg'
// parameter that Fn prints. While we just use a default
// value here, Cookoo can get that information from another
// source and then send it into Fn.
Using: []Param{
Param{
Name: "msg",
DefaultValue: "Hello World",
},
},
},
},
})
// Now we execute the "hello" chain of commands.
router.HandleRequest("hello", cxt, false)
// Output:
// Hello World
}
func TestCookooForCoCo(t *testing.T) {
registry, router, cxt := Cookoo()
cxt.Put("Answer", 42)
lifeUniverseEverything := cxt.Get("Answer", nil)
if lifeUniverseEverything != 42 {
t.Error("! Context is not working.")
}
registry.Route("foo", "test")
ok := router.HasRoute("foo")
if !ok {
t.Error("! Router does not have 'foo' route.")
}
}