fixed tests for semantic errors

This commit is contained in:
Aleksandr Petrukhin
2016-12-20 17:49:43 +03:00
parent 3c64f7b0b1
commit 995f3cbcfb
4 changed files with 52 additions and 40 deletions
+2 -2
View File
@@ -98,9 +98,9 @@ func TestNumber(t *testing.T) {
if err == nil && len(l.SemanticErrors) != 0 {
err = l.SemanticErrors[0]
}
if err != nil && !test.wantError {
if (err != nil || (len(l.SemanticErrors) != 0 && *UseSemanticErrors)) && !test.wantError {
t.Errorf("[%d, %q] number() error: %v", i, test.toParse, err)
} else if err == nil && test.wantError {
} else if (err == nil || (len(l.SemanticErrors) == 0 && *UseSemanticErrors)) && test.wantError {
t.Errorf("[%d, %q] number() ok; want error", i, test.toParse)
}
}
-38
View File
@@ -7,7 +7,6 @@ import (
"encoding/json"
"github.com/mailru/easyjson"
"github.com/mailru/easyjson/jlexer"
"github.com/mailru/easyjson/jwriter"
)
@@ -207,40 +206,3 @@ func TestNestedEasyJsonMarshal(t *testing.T) {
}
}
}
func TestSemanticErrors(t *testing.T) {
if !*jlexer.UseSemanticErrors {
return
}
for i, test := range []struct {
Data []byte
ErrorNum int
}{
{
Data: []byte(`[1, 2, 3, "4", "5"]`),
ErrorNum: 2,
},
{
Data: []byte(`[1, {"2" : "3"}, 3, "4"`),
ErrorNum: 2,
},
{
Data: []byte(`[1, "2", "3", "4", "5", "6"]`),
ErrorNum: 5,
},
{
Data: []byte(`[1, 2, 3, 4, "5"]`),
ErrorNum: 1,
},
} {
l := jlexer.Lexer{Data: test.Data}
var v ErrorIntSlice
v.UnmarshalEasyJSON(&l)
if len(l.SemanticErrors) != test.ErrorNum {
t.Errorf("[%d] TestSemanticErrors(): errornum: want: %d, got %d", i, test.ErrorNum, len(l.SemanticErrors))
}
}
}
+6
View File
@@ -2,3 +2,9 @@ package tests
//easyjson:json
type ErrorIntSlice []int
//easyjson:json
type ErrorBoolSlice []bool
//easyjson:json
type ErrorUintSlice []uint
+44
View File
@@ -0,0 +1,44 @@
package tests
import (
"testing"
"github.com/mailru/easyjson/jlexer"
)
func TestSemanticErrorsInt(t *testing.T) {
if !*jlexer.UseSemanticErrors {
return
}
for i, test := range []struct {
Data []byte
ErrorNum int
}{
{
Data: []byte(`[1, 2, 3, "4", "5"]`),
ErrorNum: 2,
},
{
Data: []byte(`[1, {"2" : "3"}, 3, "4"`),
ErrorNum: 2,
},
{
Data: []byte(`[1, "2", "3", "4", "5", "6"]`),
ErrorNum: 5,
},
{
Data: []byte(`[1, 2, 3, 4, "5"]`),
ErrorNum: 1,
},
} {
l := jlexer.Lexer{Data: test.Data}
var v ErrorIntSlice
v.UnmarshalEasyJSON(&l)
if len(l.SemanticErrors) != test.ErrorNum {
t.Errorf("[%d] TestSemanticErrors(): errornum: want: %d, got %d", i, test.ErrorNum, len(l.SemanticErrors))
}
}
}