unknown_fields_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package tests
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestUnknownFieldsProxy(t *testing.T) {
  7. baseJson := `{"Field1":"123","Field2":"321"}`
  8. s := StructWithUnknownsProxy{}
  9. err := s.UnmarshalJSON([]byte(baseJson))
  10. if err != nil {
  11. t.Errorf("UnmarshalJSON didn't expect error: %v", err)
  12. }
  13. if s.Field1 != "123" {
  14. t.Errorf("UnmarshalJSON expected to parse Field1 as \"123\". got: %v", s.Field1)
  15. }
  16. data, err := s.MarshalJSON()
  17. if err != nil {
  18. t.Errorf("MarshalJSON didn't expect error: %v", err)
  19. }
  20. if !reflect.DeepEqual(baseJson, string(data)) {
  21. t.Errorf("MarshalJSON expected to gen: %v. got: %v", baseJson, string(data))
  22. }
  23. }
  24. func TestUnknownFieldsProxyWithOmitempty(t *testing.T) {
  25. baseJson := `{"Field1":"123","Field2":"321"}`
  26. s := StructWithUnknownsProxyWithOmitempty{}
  27. err := s.UnmarshalJSON([]byte(baseJson))
  28. if err != nil {
  29. t.Errorf("UnmarshalJSON didn't expect error: %v", err)
  30. }
  31. if s.Field1 != "123" {
  32. t.Errorf("UnmarshalJSON expected to parse Field1 as \"123\". got: %v", s.Field1)
  33. }
  34. data, err := s.MarshalJSON()
  35. if err != nil {
  36. t.Errorf("MarshalJSON didn't expect error: %v", err)
  37. }
  38. if !reflect.DeepEqual(baseJson, string(data)) {
  39. t.Errorf("MarshalJSON expected to gen: %v. got: %v", baseJson, string(data))
  40. }
  41. }