Add tests for errors (fixes #5)

This commit is contained in:
Thomas Pelletier
2013-03-22 15:23:53 +01:00
parent 183bbe2aa9
commit 2bd1eb4f21
3 changed files with 18 additions and 3 deletions
+2 -2
View File
@@ -106,7 +106,7 @@ func parseAssign(p *parser) parserStateFn {
func parseRvalue(p *parser) interface{} { func parseRvalue(p *parser) interface{} {
tok := p.getToken() tok := p.getToken()
if tok == nil { if tok == nil || tok.typ == tokenEOF {
panic("expecting a value") panic("expecting a value")
} }
@@ -148,7 +148,7 @@ func parseArray(p *parser) []interface{} {
array := make([]interface{}, 0) array := make([]interface{}, 0)
for { for {
follow := p.peek() follow := p.peek()
if follow == nil { if follow == nil || follow.typ == tokenEOF {
panic("unterminated array") panic("unterminated array")
} }
if follow.typ == tokenRightBracket { if follow.typ == tokenRightBracket {
+14
View File
@@ -101,3 +101,17 @@ func TestArrayNested(t *testing.T) {
"a": [][]int64{[]int64{int64(42), int64(21)}, []int64{int64(10)}}, "a": [][]int64{[]int64{int64(42), int64(21)}, []int64{int64(10)}},
}) })
} }
func TestMissingValue(t *testing.T) {
_, err := Load("a = ")
if (err.Error() != "expecting a value") {
t.Error("Bad error message:", err.Error());
}
}
func TestUnterminatedArray(t *testing.T) {
_, err := Load("a = [1,")
if (err.Error() != "unterminated array") {
t.Error("Bad error message:", err.Error());
}
}
+2 -1
View File
@@ -5,6 +5,7 @@
package toml package toml
import ( import (
"errors"
"runtime" "runtime"
"strings" "strings"
) )
@@ -81,7 +82,7 @@ func Load(content string) (tree *TomlTree, err error) {
if _, ok := r.(runtime.Error); ok { if _, ok := r.(runtime.Error); ok {
panic(r) panic(r)
} }
err = r.(error) err = errors.New(r.(string))
} }
}() }()
_, flow := lex(content) _, flow := lex(content)