Added arrays tests

This commit is contained in:
Thomas Pelletier
2013-02-24 22:59:52 +01:00
parent 820938471d
commit e0796931d0
2 changed files with 29 additions and 1 deletions
+8
View File
@@ -135,6 +135,8 @@ func parseRvalue(p *parser) interface{} {
return parseArray(p)
}
println(tok.typ)
println(tok.val)
panic("never reached")
return nil
@@ -152,10 +154,16 @@ func parseArray(p *parser) []interface{} {
val := parseRvalue(p)
array = append(array, val)
follow = p.peek()
fmt.Println("Added to array:", val)
fmt.Println("Follow", follow)
fmt.Println("Follow", follow.typ)
if follow == nil { panic("unterminated array") }
if follow.typ != tokenRightBracket && follow.typ != tokenComma {
panic("missing comma")
}
if follow.typ == tokenComma {
p.getToken()
}
}
return array
}
+21 -1
View File
@@ -1,6 +1,7 @@
package toml
import (
"fmt"
"testing"
"time"
)
@@ -8,7 +9,7 @@ import (
func assertTree(t *testing.T, tree *TomlTree, ref map[string]interface{}) {
for k, v := range ref {
if tree.Get(k) != v {
if fmt.Sprintf("%v", tree.Get(k)) != fmt.Sprintf("%v", v) {
t.Log("was expecting", v, "at", k, "but got", tree.Get(k))
t.Fail()
}
@@ -76,3 +77,22 @@ func TestNestedKeys(t *testing.T) {
"a.b.c.d": int64(42),
})
}
func TestArraySimple(t *testing.T) {
tree := Load("a = [42, 21, 10]")
assertTree(t, tree, map[string]interface{}{
"a": []int64{int64(42), int64(21), int64(10),},
})
tree = Load("a = [42, 21, 10,]")
assertTree(t, tree, map[string]interface{}{
"a": []int64{int64(42), int64(21), int64(10),},
})
}
func TestArrayNested(t *testing.T) {
tree := Load("a = [[42, 21], [10]]")
assertTree(t, tree, map[string]interface{}{
"a": [][]int64{[]int64{int64(42), int64(21),}, []int64{int64(10),},},
})
}