value_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. for _, testCase := range marshalCases {
  32. var name string
  33. if testCase != nil {
  34. name = reflect.TypeOf(testCase).String()
  35. }
  36. t.Run(name, func(t *testing.T) {
  37. should := require.New(t)
  38. output1, err1 := json.Marshal(testCase)
  39. should.NoError(err1)
  40. output2, err2 := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(testCase)
  41. should.NoError(err2)
  42. should.Equal(string(output1), string(output2))
  43. })
  44. }
  45. }