value_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package test
  2. import (
  3. "testing"
  4. "reflect"
  5. "encoding/json"
  6. "github.com/stretchr/testify/require"
  7. "github.com/json-iterator/go"
  8. )
  9. type unmarshalCase struct {
  10. ptr interface{}
  11. input string
  12. }
  13. var unmarshalCases []unmarshalCase
  14. var marshalCases = []interface{}{
  15. nil,
  16. }
  17. func Test_unmarshal(t *testing.T) {
  18. should := require.New(t)
  19. for _, testCase := range unmarshalCases {
  20. valType := reflect.TypeOf(testCase.ptr).Elem()
  21. ptr1Val := reflect.New(valType)
  22. err1 := json.Unmarshal([]byte(testCase.input), ptr1Val.Interface())
  23. should.NoError(err1)
  24. ptr2Val := reflect.New(valType)
  25. err2 := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(testCase.input), ptr2Val.Interface())
  26. should.NoError(err2)
  27. should.Equal(ptr1Val.Interface(), ptr2Val.Interface())
  28. }
  29. }
  30. func Test_marshal(t *testing.T) {
  31. should := require.New(t)
  32. for _, testCase := range marshalCases {
  33. output1, err1 := json.Marshal(testCase)
  34. should.NoError(err1)
  35. output2, err2 := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(testCase)
  36. should.NoError(err2)
  37. should.Equal(string(output1), string(output2))
  38. }
  39. }