forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisitor_test.go
53 lines (43 loc) · 1.1 KB
/
visitor_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 ast_test
import (
"testing"
"github.com/antonmedv/expr/ast"
"github.com/stretchr/testify/assert"
)
type visitor struct {
identifiers []string
}
func (v *visitor) Visit(node *ast.Node) {
if n, ok := (*node).(*ast.IdentifierNode); ok {
v.identifiers = append(v.identifiers, n.Value)
}
}
func TestWalk(t *testing.T) {
var node ast.Node
node = &ast.BinaryNode{
Operator: "+",
Left: &ast.IdentifierNode{Value: "foo"},
Right: &ast.IdentifierNode{Value: "bar"},
}
visitor := &visitor{}
ast.Walk(&node, visitor)
assert.Equal(t, []string{"foo", "bar"}, visitor.identifiers)
}
type patcher struct{}
func (p *patcher) Visit(node *ast.Node) {
if _, ok := (*node).(*ast.IdentifierNode); ok {
*node = &ast.NilNode{}
}
}
func TestWalk_patch(t *testing.T) {
var node ast.Node
node = &ast.BinaryNode{
Operator: "+",
Left: &ast.IdentifierNode{Value: "foo"},
Right: &ast.IdentifierNode{Value: "bar"},
}
patcher := &patcher{}
ast.Walk(&node, patcher)
assert.IsType(t, &ast.NilNode{}, node.(*ast.BinaryNode).Left)
assert.IsType(t, &ast.NilNode{}, node.(*ast.BinaryNode).Right)
}