gojay

high performance JSON encoder/decoder with stream API for Golang
git clone git://git.lair.cx/gojay
Log | Files | Refs | README | LICENSE

commit 9925951c87287380fbfbce341b7bba9c48851d7d
parent eed09b19a08e9b57937cdcc74938a545991a845f
Author: francoispqt <francois@parquet.ninja>
Date:   Tue, 15 May 2018 00:09:49 +0800

clean decode string tests

Diffstat:
Mdecode_string_test.go | 197+++++++++++++++++++++++++++++--------------------------------------------------
1 file changed, 73 insertions(+), 124 deletions(-)

diff --git a/decode_string_test.go b/decode_string_test.go @@ -25,6 +25,18 @@ func TestDecoderString(t *testing.T) { err: false, }, { + name: "basic-string", + json: ``, + expectedResult: "", + err: false, + }, + { + name: "basic-string", + json: `""`, + expectedResult: "", + err: false, + }, + { name: "basic-string2", json: `"hello world!"`, expectedResult: "hello world!", @@ -175,11 +187,72 @@ func TestDecoderString(t *testing.T) { err: true, }, { + name: "null", + json: `null`, + expectedResult: "", + }, + { name: "null-err", json: `nall`, expectedResult: "", err: true, }, + { + name: "escape quote err", + json: `"test string \" escaped"`, + expectedResult: `test string " escaped`, + err: false, + }, + { + name: "escape quote err2", + json: `"test string \\t escaped"`, + expectedResult: "test string \t escaped", + err: false, + }, + { + name: "escape quote err2", + json: `"test string \\r escaped"`, + expectedResult: "test string \r escaped", + err: false, + }, + { + name: "escape quote err2", + json: `"test string \\b escaped"`, + expectedResult: "test string \b escaped", + err: false, + }, + { + name: "escape quote err", + json: `"test string \\n escaped"`, + expectedResult: "test string \n escaped", + err: false, + }, + { + name: "escape quote err", + json: `"test string \\" escaped"`, + expectedResult: ``, + err: true, + errType: InvalidJSONError(""), + }, + { + name: "escape quote err", + json: `"test string \\\l escaped"`, + expectedResult: ``, + err: true, + errType: InvalidJSONError(""), + }, + { + name: "invalid-json", + json: `invalid`, + expectedResult: ``, + err: true, + errType: InvalidJSONError(""), + }, + { + name: "string-complex", + json: ` "string with spaces and \"escape\"d \"quotes\" and escaped line returns \\n and escaped \\\\ escaped char"`, + expectedResult: "string with spaces and \"escape\"d \"quotes\" and escaped line returns \n and escaped \\\\ escaped char", + }, } for _, testCase := range testCases { @@ -199,56 +272,6 @@ func TestDecoderString(t *testing.T) { }) } } - -func TestDecoderStringBasic(t *testing.T) { - json := []byte(`"string"`) - var v string - err := Unmarshal(json, &v) - assert.Nil(t, err, "Err must be nil") - assert.Equal(t, "string", v, "v must be equal to 'string'") -} - -func TestDecoderStringEmpty(t *testing.T) { - json := []byte(``) - var v string - err := Unmarshal(json, &v) - assert.Nil(t, err, "Err must be nil") - assert.Equal(t, "", v, "v must be equal to 'string'") -} - -func TestDecoderStringNullInvalid(t *testing.T) { - json := []byte(`nall`) - var v string - err := Unmarshal(json, &v) - assert.NotNil(t, err, "Err must be nil") - assert.IsType(t, InvalidJSONError(""), err, "Err must be nil") - assert.Equal(t, "", v, "v must be equal to 'string'") -} - -func TestDecoderStringComplex(t *testing.T) { - json := []byte(` "string with spaces and \"escape\"d \"quotes\" and escaped line returns \\n and escaped \\\\ escaped char"`) - var v string - err := Unmarshal(json, &v) - assert.Nil(t, err, "Err must be nil") - assert.Equal(t, "string with spaces and \"escape\"d \"quotes\" and escaped line returns \n and escaped \\\\ escaped char", v, "v is not equal to the value expected") -} - -func TestDecoderStringNull(t *testing.T) { - json := []byte(`null`) - var v string - err := Unmarshal(json, &v) - assert.Nil(t, err, "Err must be nil") - assert.Equal(t, "", v, "v must be equal to ''") -} - -func TestDecoderStringInvalidJSON(t *testing.T) { - json := []byte(`"invalid JSONs`) - var v string - err := Unmarshal(json, &v) - assert.NotNil(t, err, "Err must not be nil as JSON is invalid") - assert.IsType(t, InvalidJSONError(""), err, "err message must be 'Invalid JSON'") -} - func TestDecoderStringInvalidType(t *testing.T) { json := []byte(`1`) var v string @@ -317,80 +340,6 @@ func TestDecoderSkipStringError(t *testing.T) { assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError") } -func TestParseEscapedString(t *testing.T) { - testCases := []struct { - name string - json string - expectedResult string - err bool - errType interface{} - }{ - { - name: "escape quote err", - json: `"test string \" escaped"`, - expectedResult: `test string " escaped`, - err: false, - }, - { - name: "escape quote err2", - json: `"test string \\t escaped"`, - expectedResult: "test string \t escaped", - err: false, - }, - { - name: "escape quote err2", - json: `"test string \\r escaped"`, - expectedResult: "test string \r escaped", - err: false, - }, - { - name: "escape quote err2", - json: `"test string \\b escaped"`, - expectedResult: "test string \b escaped", - err: false, - }, - { - name: "escape quote err", - json: `"test string \\n escaped"`, - expectedResult: "test string \n escaped", - err: false, - }, - { - name: "escape quote err", - json: `"test string \\" escaped"`, - expectedResult: ``, - err: true, - errType: InvalidJSONError(""), - }, - { - name: "escape quote err", - json: `"test string \\\l escaped"`, - expectedResult: ``, - err: true, - errType: InvalidJSONError(""), - }, - } - - for _, testCase := range testCases { - t.Run(testCase.name, func(t *testing.T) { - str := "" - dec := NewDecoder(strings.NewReader(testCase.json)) - err := dec.Decode(&str) - if testCase.err { - assert.NotNil(t, err, "err should not be nil") - if testCase.errType != nil { - assert.IsType(t, testCase.errType, err, "err should be of expected type") - } - log.Print(err) - } else { - assert.Nil(t, err, "err should be nil") - } - assert.Equal(t, testCase.expectedResult, str, fmt.Sprintf("str should be equal to '%s'", testCase.expectedResult)) - }) - } - -} - func TestSkipString(t *testing.T) { testCases := []struct { name string