forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_eval_test.go
55 lines (41 loc) · 993 Bytes
/
example_eval_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
package tick
import (
"fmt"
"github.com/influxdata/kapacitor/tick/stateful"
)
type Process struct {
Name string
Children []*Process
}
func (p *Process) Spawn() *Process {
child := &Process{}
p.Children = append(p.Children, child)
return child
}
func (p *Process) String() string {
return fmt.Sprintf("{%q %s}", p.Name, p.Children)
}
func ExampleEvaluate() {
//Run a test that evaluates the DSL against the Process struct.
script := `
//Name the parent
parent.name('parent')
// Spawn a first child
var child1 = parent|spawn()
// Name the first child
child1.name('child1')
//Spawn a grandchild and name it
child1|spawn().name('grandchild')
//Spawn a second child and name it
parent|spawn().name('child2')
`
scope := stateful.NewScope()
parent := &Process{}
scope.Set("parent", parent)
_, err := Evaluate(script, scope, nil, false)
if err != nil {
fmt.Println(err)
}
fmt.Println(parent)
// Output: {"parent" [{"child1" [{"grandchild" []}]} {"child2" []}]}
}