value_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. func Test_unmarshal(t *testing.T) {
  16. should := require.New(t)
  17. for _, testCase := range unmarshalCases {
  18. valType := reflect.TypeOf(testCase.ptr).Elem()
  19. ptr1Val := reflect.New(valType)
  20. err1 := json.Unmarshal([]byte(testCase.input), ptr1Val.Interface())
  21. should.NoError(err1)
  22. ptr2Val := reflect.New(valType)
  23. err2 := jsoniter.Unmarshal([]byte(testCase.input), ptr2Val.Interface())
  24. should.NoError(err2)
  25. should.Equal(ptr1Val.Interface(), ptr2Val.Interface())
  26. }
  27. }
  28. func Test_marshal(t *testing.T) {
  29. should := require.New(t)
  30. for _, testCase := range marshalCases {
  31. output1, err1 := json.Marshal(testCase)
  32. should.NoError(err1)
  33. output2, err2 := jsoniter.Marshal(testCase)
  34. should.NoError(err2)
  35. should.Equal(string(output1), string(output2))
  36. }
  37. }