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