File: value_test.go

package info (click to toggle)
golang-github-json-iterator-go 1.1.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 916 kB
  • sloc: sh: 19; makefile: 3
file content (80 lines) | stat: -rw-r--r-- 1,955 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package test

import (
	"encoding/json"
	"fmt"
	"github.com/json-iterator/go"
	"github.com/modern-go/reflect2"
	"github.com/stretchr/testify/require"
	"testing"
)

type unmarshalCase struct {
	obj      func() interface{}
	ptr      interface{}
	input    string
	selected bool
}

var unmarshalCases []unmarshalCase

var marshalCases = []interface{}{
	nil,
}

type selectedMarshalCase struct {
	marshalCase interface{}
}

func Test_unmarshal(t *testing.T) {
	for _, testCase := range unmarshalCases {
		if testCase.selected {
			unmarshalCases = []unmarshalCase{testCase}
			break
		}
	}
	for i, testCase := range unmarshalCases {
		t.Run(fmt.Sprintf("[%v]%s", i, testCase.input), func(t *testing.T) {
			should := require.New(t)
			var obj1 interface{}
			var obj2 interface{}
			if testCase.obj != nil {
				obj1 = testCase.obj()
				obj2 = testCase.obj()
			} else {
				valType := reflect2.TypeOfPtr(testCase.ptr).Elem()
				obj1 = valType.New()
				obj2 = valType.New()
			}
			err1 := json.Unmarshal([]byte(testCase.input), obj1)
			should.NoError(err1, "json")
			err2 := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(testCase.input), obj2)
			should.NoError(err2, "jsoniter")
			should.Equal(obj1, obj2)
		})
	}
}

func Test_marshal(t *testing.T) {
	for _, testCase := range marshalCases {
		selectedMarshalCase, found := testCase.(selectedMarshalCase)
		if found {
			marshalCases = []interface{}{selectedMarshalCase.marshalCase}
			break
		}
	}
	for i, testCase := range marshalCases {
		var name string
		if testCase != nil {
			name = fmt.Sprintf("[%v]%v/%s", i, testCase, reflect2.TypeOf(testCase).String())
		}
		t.Run(name, func(t *testing.T) {
			should := require.New(t)
			output1, err1 := json.Marshal(testCase)
			should.NoError(err1, "json")
			output2, err2 := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(testCase)
			should.NoError(err2, "jsoniter")
			should.Equal(string(output1), string(output2))
		})
	}
}