forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_scope_test.go
53 lines (40 loc) · 919 Bytes
/
example_scope_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
package tick
import (
"fmt"
)
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 := NewScope()
parent := &Process{}
scope.Set("parent", parent)
err := Evaluate(script, scope)
if err != nil {
fmt.Println(err)
}
fmt.Println(parent)
// Output: {"parent" [{"child1" [{"grandchild" []}]} {"child2" []}]}
}