hcl/ast/ast_test.go

95 lines
1.5 KiB
Go
Raw Normal View History

2014-08-02 18:38:41 +00:00
package ast
import (
"reflect"
"testing"
)
func TestAssignmentNode_accept(t *testing.T) {
n := AssignmentNode{
2014-08-02 22:44:45 +00:00
K: "foo",
Value: LiteralNode{Value: "foo"},
}
expected := []Node{
n,
2014-08-02 22:44:45 +00:00
n.Value,
}
v := new(MockVisitor)
n.Accept(v)
if !reflect.DeepEqual(v.Nodes, expected) {
t.Fatalf("bad: %#v", v.Nodes)
}
}
func TestListNode_accept(t *testing.T) {
n := ListNode{
Elem: []Node{
LiteralNode{Value: "foo"},
LiteralNode{Value: "bar"},
},
}
expected := []Node{
2014-08-02 22:44:45 +00:00
n,
n.Elem[0],
n.Elem[1],
}
v := new(MockVisitor)
n.Accept(v)
if !reflect.DeepEqual(v.Nodes, expected) {
t.Fatalf("bad: %#v", v.Nodes)
}
}
func TestObjectNode_accept(t *testing.T) {
n := ObjectNode{
2014-08-02 22:44:45 +00:00
K: "foo",
2014-08-04 04:56:50 +00:00
Elem: []AssignmentNode{
2014-08-02 22:44:45 +00:00
AssignmentNode{K: "foo", Value: LiteralNode{Value: "foo"}},
AssignmentNode{K: "bar", Value: LiteralNode{Value: "bar"}},
},
}
expected := []Node{
2014-08-02 22:44:45 +00:00
n,
n.Elem[0],
2014-08-04 04:56:50 +00:00
n.Elem[0].Value,
n.Elem[1],
2014-08-04 04:56:50 +00:00
n.Elem[1].Value,
}
v := new(MockVisitor)
n.Accept(v)
if !reflect.DeepEqual(v.Nodes, expected) {
t.Fatalf("bad: %#v", v.Nodes)
}
}
2014-08-04 00:17:17 +00:00
func TestObjectNodeGet(t *testing.T) {
n := ObjectNode{
K: "foo",
2014-08-04 04:56:50 +00:00
Elem: []AssignmentNode{
2014-08-04 00:17:17 +00:00
AssignmentNode{K: "foo", Value: LiteralNode{Value: "foo"}},
AssignmentNode{K: "bar", Value: LiteralNode{Value: "bar"}},
AssignmentNode{K: "foo", Value: LiteralNode{Value: "baz"}},
},
}
expected := []Node{
LiteralNode{Value: "foo"},
LiteralNode{Value: "baz"},
}
2014-08-04 00:57:54 +00:00
actual := n.Get("foo", false)
2014-08-04 00:17:17 +00:00
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}