From 22edcf6a68a057ed04368d5f78c8ba3ddfee8d57 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 25 Feb 2023 11:11:36 -0700 Subject: reencode: Improve the error messages for trailing partial-UTF-8 --- reencode_test.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'reencode_test.go') diff --git a/reencode_test.go b/reencode_test.go index feabde5..60180c8 100644 --- a/reencode_test.go +++ b/reencode_test.go @@ -15,7 +15,7 @@ import ( "git.lukeshu.com/go/lowmemjson/internal/fastio" ) -func TestReEncode(t *testing.T) { +func TestEncodeReEncode(t *testing.T) { t.Parallel() type testcase struct { enc ReEncoderConfig @@ -168,6 +168,43 @@ func TestReEncode(t *testing.T) { } } +func TestReEncode(t *testing.T) { + t.Parallel() + type testcase struct { + Cfg ReEncoderConfig + In string + ExpOut string + ExpWriteErr string + ExpCloseErr string + } + testcases := map[string]testcase{ + "partial-utf8-replace": {Cfg: ReEncoderConfig{InvalidUTF8: InvalidUTF8Replace}, In: "\xf0\xbf", ExpOut: ``, ExpCloseErr: "json: syntax error at input byte 0: invalid character '\uFFFD' looking for beginning of value"}, + "partial-utf8-preserve": {Cfg: ReEncoderConfig{InvalidUTF8: InvalidUTF8Preserve}, In: "\xf0\xbf", ExpOut: ``, ExpCloseErr: `json: syntax error at input byte 0: invalid character '\xf0' looking for beginning of value`}, + "partial-utf8-error": {Cfg: ReEncoderConfig{InvalidUTF8: InvalidUTF8Error}, In: "\xf0\xbf", ExpOut: ``, ExpCloseErr: `json: syntax error at input byte 0: truncated UTF-8: "\xf0\xbf"`}, + } + for tcName, tc := range testcases { + tc := tc + t.Run(tcName, func(t *testing.T) { + t.Parallel() + var out strings.Builder + enc := NewReEncoder(&out, tc.Cfg) + _, err := enc.WriteString(tc.In) + assert.Equal(t, tc.ExpOut, out.String()) + if tc.ExpWriteErr == "" { + assert.NoError(t, err) + } else { + assert.EqualError(t, err, tc.ExpWriteErr) + } + err = enc.Close() + if tc.ExpCloseErr == "" { + assert.NoError(t, err) + } else { + assert.EqualError(t, err, tc.ExpCloseErr) + } + }) + } +} + func TestReEncodeWriteSize(t *testing.T) { t.Parallel() -- cgit v1.2.3-2-g168b