summaryrefslogtreecommitdiff
path: root/compat/json/compat_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'compat/json/compat_test.go')
-rw-r--r--compat/json/compat_test.go78
1 files changed, 78 insertions, 0 deletions
diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go
index 29a8b37..df9d387 100644
--- a/compat/json/compat_test.go
+++ b/compat/json/compat_test.go
@@ -6,6 +6,8 @@ package json
import (
"bytes"
+ "reflect"
+ "strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -161,3 +163,79 @@ func TestCompatMarshal(t *testing.T) {
})
}
}
+
+func TestCompatUnmarshal(t *testing.T) {
+ t.Parallel()
+ type testcase struct {
+ In string
+ InPtr any
+ ExpOut any
+ ExpErr string
+ }
+ testcases := map[string]testcase{
+ "empty-obj": {In: `{}`, ExpOut: map[string]any{}},
+ "partial-obj": {In: `{"foo":"bar",`, ExpOut: nil, ExpErr: `unexpected end of JSON input`},
+ "existing-obj": {In: `{"baz":"quz"}`, InPtr: &map[string]string{"foo": "bar"}, ExpOut: map[string]string{"foo": "bar", "baz": "quz"}},
+ "existing-obj-partial": {In: `{"baz":"quz"`, InPtr: &map[string]string{"foo": "bar"}, ExpOut: map[string]string{"foo": "bar"}, ExpErr: "unexpected end of JSON input"},
+ "empty-ary": {In: `[]`, ExpOut: []any{}},
+ "two-objs": {In: `{} {}`, ExpOut: nil, ExpErr: `invalid character '{' after top-level value`},
+ "two-numbers1": {In: `00`, ExpOut: nil, ExpErr: `invalid character '0' after top-level value`},
+ "two-numbers2": {In: `1 2`, ExpOut: nil, ExpErr: `invalid character '2' after top-level value`},
+ }
+ for tcName, tc := range testcases {
+ tc := tc
+ t.Run(tcName, func(t *testing.T) {
+ t.Parallel()
+ ptr := tc.InPtr
+ if ptr == nil {
+ var out any
+ ptr = &out
+ }
+ err := Unmarshal([]byte(tc.In), ptr)
+ assert.Equal(t, tc.ExpOut, reflect.ValueOf(ptr).Elem().Interface())
+ if tc.ExpErr == "" {
+ assert.NoError(t, err)
+ } else {
+ assert.EqualError(t, err, tc.ExpErr)
+ }
+ })
+ }
+}
+
+func TestCompatDecode(t *testing.T) {
+ t.Parallel()
+ type testcase struct {
+ In string
+ InPtr any
+ ExpOut any
+ ExpErr string
+ }
+ testcases := map[string]testcase{
+ "empty-obj": {In: `{}`, ExpOut: map[string]any{}},
+ "partial-obj": {In: `{"foo":"bar",`, ExpOut: nil, ExpErr: `unexpected EOF`},
+ "existing-obj": {In: `{"baz":"quz"}`, InPtr: &map[string]string{"foo": "bar"}, ExpOut: map[string]string{"foo": "bar", "baz": "quz"}},
+ "existing-obj-partial": {In: `{"baz":"quz"`, InPtr: &map[string]string{"foo": "bar"}, ExpOut: map[string]string{"foo": "bar"}, ExpErr: "unexpected EOF"},
+ "empty-ary": {In: `[]`, ExpOut: []any{}},
+ "two-objs": {In: `{} {}`, ExpOut: map[string]any{}},
+ "two-numbers1": {In: `00`, ExpOut: float64(0)},
+ "two-numbers2": {In: `1 2`, ExpOut: float64(1)},
+ }
+ for tcName, tc := range testcases {
+ tc := tc
+ t.Run(tcName, func(t *testing.T) {
+ t.Parallel()
+ ptr := tc.InPtr
+ if ptr == nil {
+ var out any
+ ptr = &out
+ }
+ err := NewDecoder(strings.NewReader(tc.In)).Decode(ptr)
+ assert.Equal(t, tc.ExpOut, reflect.ValueOf(ptr).Elem().Interface())
+ if tc.ExpErr == "" {
+ assert.NoError(t, err)
+ } else {
+ assert.EqualError(t, err, tc.ExpErr)
+ }
+ })
+ }
+}