parser: add list tests

This commit is contained in:
Fatih Arslan 2015-10-17 02:16:33 +03:00
parent 39f76a99eb
commit 0cf842255d
2 changed files with 104 additions and 23 deletions

View File

@ -11,33 +11,112 @@ import (
"github.com/fatih/hcl/token" "github.com/fatih/hcl/token"
) )
func TestParseType(t *testing.T) { func TestType(t *testing.T) {
src := `foo { var literals = []struct {
fatih = "true" typ token.Type
}` src string
}{
p := New([]byte(src)) {token.STRING, `foo = "foo"`},
p.enableTrace = true {token.NUMBER, `foo = 123`},
{token.FLOAT, `foo = 123.12`},
node, err := p.Parse() {token.BOOL, `foo = true`},
if err != nil {
t.Fatal(err)
} }
ast.Walk(node, func(n ast.Node) bool { for _, l := range literals {
if list, ok := n.(*ast.ObjectList); ok { p := New([]byte(l.src))
for _, l := range list.Items { item, err := p.parseObjectItem()
fmt.Printf("l = %+v\n", l) if err != nil {
for _, k := range l.Keys { t.Error(err)
fmt.Printf("key = %+v\n", k) }
}
fmt.Printf("val = %+v\n", l.Val) lit, ok := item.Val.(*ast.LiteralType)
if !ok {
t.Errorf("node should be of type LiteralType, got: %+v", item.Val)
}
if lit.Token.Type != l.typ {
t.Errorf("want: %s, got: %s", l.typ, lit.Token.Type)
}
}
}
func TestListType(t *testing.T) {
var literals = []struct {
src string
tokens []token.Type
}{
{
`foo = ["123", 123]`,
[]token.Type{token.STRING, token.NUMBER},
},
{
`foo = [123, "123",]`,
[]token.Type{token.NUMBER, token.STRING},
},
{
`foo = []`,
[]token.Type{},
},
{
`foo = ["123", 123]`,
[]token.Type{token.STRING, token.NUMBER},
},
}
for _, l := range literals {
p := New([]byte(l.src))
item, err := p.parseObjectItem()
if err != nil {
t.Error(err)
}
list, ok := item.Val.(*ast.ListType)
if !ok {
t.Errorf("node should be of type LiteralType, got: %+v", item.Val)
}
var tokens []token.Type
for _, li := range list.List {
if tp, ok := li.(*ast.LiteralType); ok {
tokens = append(tokens, tp.Token.Type)
} }
} }
return true
}) equals(t, l.tokens, tokens)
}
} }
func TestObjectType(t *testing.T) {
}
// func TestParseType(t *testing.T) {
// src := `foo {
// fatih = "true"
// }`
//
// p := New([]byte(src))
// p.enableTrace = true
//
// node, err := p.Parse()
// if err != nil {
// t.Fatal(err)
// }
//
// ast.Walk(node, func(n ast.Node) bool {
// if list, ok := n.(*ast.ObjectList); ok {
// for _, l := range list.Items {
// fmt.Printf("l = %+v\n", l)
// for _, k := range l.Keys {
// fmt.Printf("key = %+v\n", k)
// }
// fmt.Printf("val = %+v\n", l.Val)
// }
// }
// return true
// })
// }
func TestObjectKey(t *testing.T) { func TestObjectKey(t *testing.T) {
keys := []struct { keys := []struct {
exp []token.Type exp []token.Type

View File

@ -325,7 +325,9 @@ func (s *Scanner) scanNumber(ch rune) token.Type {
return token.FLOAT return token.FLOAT
} }
s.unread() if ch != eof {
s.unread()
}
return token.NUMBER return token.NUMBER
} }
@ -338,7 +340,7 @@ func (s *Scanner) scanMantissa(ch rune) rune {
scanned = true scanned = true
} }
if scanned { if scanned && ch != eof {
s.unread() s.unread()
} }
return ch return ch