...
1
16
17 package v1
18
19 import (
20 gojson "encoding/json"
21 "reflect"
22 "testing"
23
24 utiljson "k8s.io/apimachinery/pkg/util/json"
25 )
26
27 type GroupVersionHolder struct {
28 GV GroupVersion `json:"val"`
29 }
30
31 func TestGroupVersionUnmarshalJSON(t *testing.T) {
32 cases := []struct {
33 input []byte
34 expect GroupVersion
35 }{
36 {[]byte(`{"val": "v1"}`), GroupVersion{"", "v1"}},
37 {[]byte(`{"val": "apps/v1"}`), GroupVersion{"apps", "v1"}},
38 }
39
40 for _, c := range cases {
41 var result GroupVersionHolder
42
43 if err := gojson.Unmarshal([]byte(c.input), &result); err != nil {
44 t.Errorf("JSON codec failed to unmarshal input '%v': %v", c.input, err)
45 }
46 if !reflect.DeepEqual(result.GV, c.expect) {
47 t.Errorf("JSON codec failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)
48 }
49
50 if err := utiljson.Unmarshal(c.input, &result); err != nil {
51 t.Errorf("util/json codec failed to unmarshal input '%v': %v", c.input, err)
52 }
53 if !reflect.DeepEqual(result.GV, c.expect) {
54 t.Errorf("util/json codec failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)
55 }
56 }
57 }
58
59 func TestGroupVersionMarshalJSON(t *testing.T) {
60 cases := []struct {
61 input GroupVersion
62 expect []byte
63 }{
64 {GroupVersion{"", "v1"}, []byte(`{"val":"v1"}`)},
65 {GroupVersion{"apps", "v1"}, []byte(`{"val":"apps/v1"}`)},
66 }
67
68 for _, c := range cases {
69 input := GroupVersionHolder{c.input}
70 result, err := gojson.Marshal(&input)
71 if err != nil {
72 t.Errorf("Failed to marshal input '%v': %v", input, err)
73 }
74 if !reflect.DeepEqual(result, c.expect) {
75 t.Errorf("Failed to marshal input '%+v': expected: %s, got: %s", input, c.expect, result)
76 }
77 }
78 }
79
View as plain text