2014-08-02 18:38:41 +00:00
|
|
|
package ast
|
2014-08-01 15:31:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAssignmentNode_accept(t *testing.T) {
|
|
|
|
n := AssignmentNode{
|
2014-08-02 22:44:45 +00:00
|
|
|
K: "foo",
|
2014-08-01 15:31:17 +00:00
|
|
|
Value: LiteralNode{Value: "foo"},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []Node{
|
|
|
|
n,
|
2014-08-02 22:44:45 +00:00
|
|
|
n.Value,
|
2014-08-01 15:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2014-08-01 15:31:17 +00:00
|
|
|
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",
|
|
|
|
Elem: []KeyedNode{
|
|
|
|
AssignmentNode{K: "foo", Value: LiteralNode{Value: "foo"}},
|
|
|
|
AssignmentNode{K: "bar", Value: LiteralNode{Value: "bar"}},
|
2014-08-01 15:31:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []Node{
|
2014-08-02 22:44:45 +00:00
|
|
|
n,
|
2014-08-01 15:31:17 +00:00
|
|
|
n.Elem[0],
|
2014-08-02 22:44:45 +00:00
|
|
|
n.Elem[0].(AssignmentNode).Value,
|
2014-08-01 15:31:17 +00:00
|
|
|
n.Elem[1],
|
2014-08-02 22:44:45 +00:00
|
|
|
n.Elem[1].(AssignmentNode).Value,
|
2014-08-01 15:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
Elem: []KeyedNode{
|
|
|
|
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"},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := n.Get("foo")
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|