Add tests for errors (fixes #5)
This commit is contained in:
@@ -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 {
|
||||||
|
|||||||
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user