diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-14 11:02:09 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-18 22:45:54 -0700 |
commit | 49319198500729fd65bd6d69071f45f2d7ae2aa7 (patch) | |
tree | 95ca5950c0abd8f2ad8e8770799e7802454c62f0 /compat/json/compat_test.go | |
parent | eaaf7bc29d43b4470623c75e6e409a049b3083af (diff) |
compat/json: Compact, Indent: Clear the output if there's an error
Diffstat (limited to 'compat/json/compat_test.go')
-rw-r--r-- | compat/json/compat_test.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go index 5c8f3ee..d513c27 100644 --- a/compat/json/compat_test.go +++ b/compat/json/compat_test.go @@ -5,6 +5,7 @@ package json import ( + "bytes" "testing" "github.com/stretchr/testify/assert" @@ -32,3 +33,59 @@ func TestCompatValid(t *testing.T) { }) } } + +func TestCompatCompact(t *testing.T) { + t.Parallel() + type testcase struct { + In string + Out string + Err string + } + testcases := map[string]testcase{ + "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, + "object": {In: `{}`, Out: `{}`}, + } + for tcName, tc := range testcases { + tc := tc + t.Run(tcName, func(t *testing.T) { + t.Parallel() + t.Logf("in=%q", tc.In) + var out bytes.Buffer + err := Compact(&out, []byte(tc.In)) + assert.Equal(t, tc.Out, out.String()) + if tc.Err == "" { + assert.NoError(t, err) + } else { + assert.EqualError(t, err, tc.Err) + } + }) + } +} + +func TestCompatIndent(t *testing.T) { + t.Parallel() + type testcase struct { + In string + Out string + Err string + } + testcases := map[string]testcase{ + "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, + "object": {In: `{}`, Out: `{}`}, + } + for tcName, tc := range testcases { + tc := tc + t.Run(tcName, func(t *testing.T) { + t.Parallel() + t.Logf("in=%q", tc.In) + var out bytes.Buffer + err := Indent(&out, []byte(tc.In), ">", ".") + assert.Equal(t, tc.Out, out.String()) + if tc.Err == "" { + assert.NoError(t, err) + } else { + assert.EqualError(t, err, tc.Err) + } + }) + } +} |