From 995f3cbcfb67d284e01dcf4e9b60a586a84ca57a Mon Sep 17 00:00:00 2001 From: Aleksandr Petrukhin Date: Tue, 20 Dec 2016 17:49:43 +0300 Subject: [PATCH] fixed tests for semantic errors --- jlexer/lexer_test.go | 4 ++-- tests/basic_test.go | 38 -------------------------------------- tests/errors.go | 6 ++++++ tests/errors_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 40 deletions(-) create mode 100644 tests/errors_test.go diff --git a/jlexer/lexer_test.go b/jlexer/lexer_test.go index f8ec84a..c974edd 100644 --- a/jlexer/lexer_test.go +++ b/jlexer/lexer_test.go @@ -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) } } diff --git a/tests/basic_test.go b/tests/basic_test.go index ea7fbbd..25b1bfc 100644 --- a/tests/basic_test.go +++ b/tests/basic_test.go @@ -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)) - } - } -} diff --git a/tests/errors.go b/tests/errors.go index cc4eb93..a0180ad 100644 --- a/tests/errors.go +++ b/tests/errors.go @@ -2,3 +2,9 @@ package tests //easyjson:json type ErrorIntSlice []int + +//easyjson:json +type ErrorBoolSlice []bool + +//easyjson:json +type ErrorUintSlice []uint diff --git a/tests/errors_test.go b/tests/errors_test.go new file mode 100644 index 0000000..a434f65 --- /dev/null +++ b/tests/errors_test.go @@ -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)) + } + } +}