parser: add TestObjectType

This commit is contained in:
Fatih Arslan 2015-10-18 23:19:56 +03:00
parent 1f47d675b9
commit 393af546c0
2 changed files with 76 additions and 32 deletions

View File

@ -210,8 +210,8 @@ func (p *Parser) parseListType() (*ast.ListType, error) {
// TODO(arslan) should we support? not supported by HCL yet // TODO(arslan) should we support? not supported by HCL yet
case token.LBRACK: case token.LBRACK:
// TODO(arslan) should we support nested lists? Even though it's // TODO(arslan) should we support nested lists? Even though it's
// written in README of HCL, it's not a parse of the grammar // written in README of HCL, it's not a part of the grammar
// (defined in parse.y) // (not defined in parse.y)
case token.RBRACK: case token.RBRACK:
// finished // finished
l.Rbrack = p.tok.Pos l.Rbrack = p.tok.Pos

View File

@ -31,7 +31,7 @@ func TestType(t *testing.T) {
lit, ok := item.Val.(*ast.LiteralType) lit, ok := item.Val.(*ast.LiteralType)
if !ok { if !ok {
t.Errorf("node should be of type LiteralType, got: %+v", item.Val) t.Errorf("node should be of type LiteralType, got: %T", item.Val)
} }
if lit.Token.Type != l.typ { if lit.Token.Type != l.typ {
@ -72,7 +72,7 @@ func TestListType(t *testing.T) {
list, ok := item.Val.(*ast.ListType) list, ok := item.Val.(*ast.ListType)
if !ok { if !ok {
t.Errorf("node should be of type LiteralType, got: %+v", item.Val) t.Errorf("node should be of type LiteralType, got: %T", item.Val)
} }
tokens := []token.Type{} tokens := []token.Type{}
@ -83,39 +83,83 @@ func TestListType(t *testing.T) {
} }
equals(t, l.tokens, tokens) equals(t, l.tokens, tokens)
} }
} }
func TestObjectType(t *testing.T) { func TestObjectType(t *testing.T) {
} var literals = []struct {
src string
nodeType []ast.Node
itemLen int
}{
{
`foo = {}`,
nil,
0,
},
{
`foo = {
bar = "fatih"
}`,
[]ast.Node{&ast.LiteralType{}},
1,
},
{
`foo = {
bar = "fatih"
baz = ["arslan"]
}`,
[]ast.Node{
&ast.LiteralType{},
&ast.ListType{},
},
2,
},
{
`foo = {
bar {}
}`,
[]ast.Node{
&ast.ObjectType{},
},
1,
},
{
`foo {
bar {}
foo = true
}`,
[]ast.Node{
&ast.ObjectType{},
&ast.LiteralType{},
},
2,
},
}
// func TestParseType(t *testing.T) { for _, l := range literals {
// src := `foo { p := New([]byte(l.src))
// fatih = "true" item, err := p.parseObjectItem()
// }` if err != nil {
// t.Error(err)
// p := New([]byte(src)) }
// p.enableTrace = true
// // we know that the ObjectKey name is foo for all cases, what matters
// node, err := p.Parse() // is the object
// if err != nil { obj, ok := item.Val.(*ast.ObjectType)
// t.Fatal(err) if !ok {
// } t.Errorf("node should be of type LiteralType, got: %T", item.Val)
// }
// ast.Walk(node, func(n ast.Node) bool {
// if list, ok := n.(*ast.ObjectList); ok { // check if the total length of items are correct
// for _, l := range list.Items { equals(t, l.itemLen, len(obj.List.Items))
// fmt.Printf("l = %+v\n", l)
// for _, k := range l.Keys { // check if the types are correct
// fmt.Printf("key = %+v\n", k) for i, item := range obj.List.Items {
// } equals(t, reflect.TypeOf(l.nodeType[i]), reflect.TypeOf(item.Val))
// fmt.Printf("val = %+v\n", l.Val) }
// } }
// } }
// return true
// })
// }
func TestObjectKey(t *testing.T) { func TestObjectKey(t *testing.T) {
keys := []struct { keys := []struct {