Adds tests for invalid statements, tokens

This commit is contained in:
Tom Hudson 2016-09-04 12:26:27 +01:00
parent fe49bfda0c
commit 52117da204
2 changed files with 36 additions and 2 deletions

View file

@ -250,7 +250,7 @@ func BenchmarkMakeStatements(b *testing.B) {
}
}
func TestUngronStatements(t *testing.T) {
func TestUngronStatementsSimple(t *testing.T) {
in := statements{
`json.contact = {};`,
`json.contact["e-mail"][0] = "mail@tomnomnom.com";`,
@ -286,3 +286,18 @@ func TestUngronStatements(t *testing.T) {
t.Errorf("have and want are not equal")
}
}
func TestUngronStatementsInvalid(t *testing.T) {
cases := []statements{
{``},
{`this isn't a statement at all`},
{`json[0] = 1;`, `json.bar = 1;`},
}
for _, c := range cases {
_, err := c.ungron()
if err == nil {
t.Errorf("want non-nil error; have nil")
}
}
}

View file

@ -130,7 +130,7 @@ func TestLex(t *testing.T) {
}
}
func TestUngronTokens(t *testing.T) {
func TestUngronTokensSimple(t *testing.T) {
in := `json.contact["e-mail"][0] = "mail@tomnomnom.com";`
want := map[string]interface{}{
"json": map[string]interface{}{
@ -159,6 +159,25 @@ func TestUngronTokens(t *testing.T) {
}
}
func TestUngronTokensInvalid(t *testing.T) {
cases := []struct {
in []token
}{
{[]token{{``, typError}}}, // Error token
{[]token{{`foo`, typValue}}}, // Invalid value
{[]token{{`"foo`, typQuoted}, {"1", typValue}}}, // Invalid quoted key
{[]token{{`foo`, typNumeric}, {"1", typValue}}}, // Invalid numeric key
{[]token{{``, -255}, {"1", typValue}}}, // Invalid token type
}
for _, c := range cases {
_, err := ungronTokens(c.in)
if err == nil {
t.Errorf("want non-nil error for %#v; have nil", c.in)
}
}
}
func TestMerge(t *testing.T) {
a := map[string]interface{}{
"json": map[string]interface{}{