|
7 | 7 | "testing" |
8 | 8 |
|
9 | 9 | "github.com/moby/moby/v2/daemon/internal/compat" |
| 10 | + "gotest.tools/v3/assert" |
| 11 | + is "gotest.tools/v3/assert/cmp" |
10 | 12 | ) |
11 | 13 |
|
12 | 14 | type Info struct { |
@@ -80,6 +82,49 @@ func TestWrap(t *testing.T) { |
80 | 82 | } |
81 | 83 | } |
82 | 84 |
|
| 85 | +func TestWrapNilPtrField(t *testing.T) { |
| 86 | + type bStruct struct { |
| 87 | + StringField string `json:"stringfield"` |
| 88 | + } |
| 89 | + type aStruct struct { |
| 90 | + IntField *int `json:"intfield"` |
| 91 | + StructField *bStruct `json:"structfield"` |
| 92 | + } |
| 93 | + info := &aStruct{} |
| 94 | + |
| 95 | + tests := []struct { |
| 96 | + name string |
| 97 | + options []compat.Option |
| 98 | + expected string |
| 99 | + }{ |
| 100 | + { |
| 101 | + name: "none", |
| 102 | + expected: `{"intfield":null,"structfield":null}`, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "replace nil int", |
| 106 | + options: []compat.Option{compat.WithExtraFields(map[string]any{"intfield": 42})}, |
| 107 | + expected: `{"intfield":42,"structfield":null}`, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "replace nil struct", |
| 111 | + options: []compat.Option{compat.WithExtraFields(map[string]any{ |
| 112 | + "structfield": map[string]any{"stringfield": "hello"}, |
| 113 | + })}, |
| 114 | + expected: `{"intfield":null,"structfield":{"stringfield":"hello"}}`, |
| 115 | + }, |
| 116 | + } |
| 117 | + for _, tc := range tests { |
| 118 | + t.Run(tc.name, func(t *testing.T) { |
| 119 | + resp := compat.Wrap(info, tc.options...) |
| 120 | + data, err := json.Marshal(resp) |
| 121 | + if assert.Check(t, err) { |
| 122 | + assert.Check(t, is.Equal(string(data), tc.expected)) |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
83 | 128 | func TestNestedCompat(t *testing.T) { |
84 | 129 | info := &Info{ |
85 | 130 | Name: "daemon", |
|
0 commit comments