-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_union_test.go
More file actions
244 lines (232 loc) · 5.94 KB
/
types_union_test.go
File metadata and controls
244 lines (232 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package cloudlayer
import (
"encoding/json"
"testing"
)
func TestLayoutDimension_MarshalJSON(t *testing.T) {
tests := []struct {
name string
dim *LayoutDimension
want string
}{
{"string value", NewLayoutDimensionString("10in"), `"10in"`},
{"int value", NewLayoutDimensionInt(1920), `1920`},
{"zero int", NewLayoutDimensionInt(0), `0`},
{"empty string", NewLayoutDimensionString(""), `""`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := json.Marshal(tt.dim)
if err != nil {
t.Fatalf("MarshalJSON() error = %v", err)
}
if string(got) != tt.want {
t.Errorf("MarshalJSON() = %s, want %s", got, tt.want)
}
})
}
}
func TestLayoutDimension_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
input string
wantStr *string
wantInt *int
}{
{"string value", `"25cm"`, stringPtr("25cm"), nil},
{"int value", `1080`, nil, intPtr(1080)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var d LayoutDimension
if err := json.Unmarshal([]byte(tt.input), &d); err != nil {
t.Fatalf("UnmarshalJSON() error = %v", err)
}
if tt.wantStr != nil && (d.strVal == nil || *d.strVal != *tt.wantStr) {
t.Errorf("expected string %q, got %v", *tt.wantStr, d.strVal)
}
if tt.wantInt != nil && (d.intVal == nil || *d.intVal != *tt.wantInt) {
t.Errorf("expected int %d, got %v", *tt.wantInt, d.intVal)
}
})
}
}
func TestLayoutDimension_RoundTrip(t *testing.T) {
original := NewLayoutDimensionString("10in")
data, err := json.Marshal(original)
if err != nil {
t.Fatal(err)
}
var decoded LayoutDimension
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatal(err)
}
reencoded, err := json.Marshal(&decoded)
if err != nil {
t.Fatal(err)
}
if string(data) != string(reencoded) {
t.Errorf("round-trip mismatch: %s != %s", data, reencoded)
}
}
func TestGeneratePreviewOption_MarshalJSON(t *testing.T) {
tests := []struct {
name string
opt *GeneratePreviewOption
want string
}{
{"bool true", NewGeneratePreviewBool(true), `true`},
{"bool false", NewGeneratePreviewBool(false), `false`},
{
"options object",
NewGeneratePreviewOptions(&PreviewOptions{
Quality: 80,
Width: intPtr(200),
}),
`{"width":200,"quality":80}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := json.Marshal(tt.opt)
if err != nil {
t.Fatalf("MarshalJSON() error = %v", err)
}
if string(got) != tt.want {
t.Errorf("MarshalJSON() = %s, want %s", got, tt.want)
}
})
}
}
func TestGeneratePreviewOption_UnmarshalJSON(t *testing.T) {
t.Run("bool", func(t *testing.T) {
var g GeneratePreviewOption
if err := json.Unmarshal([]byte(`true`), &g); err != nil {
t.Fatal(err)
}
if g.boolVal == nil || *g.boolVal != true {
t.Error("expected bool true")
}
})
t.Run("object", func(t *testing.T) {
var g GeneratePreviewOption
if err := json.Unmarshal([]byte(`{"quality":90}`), &g); err != nil {
t.Fatal(err)
}
if g.optsVal == nil || g.optsVal.Quality != 90 {
t.Error("expected PreviewOptions with quality 90")
}
})
}
func TestStorageOption_MarshalJSON(t *testing.T) {
tests := []struct {
name string
opt *StorageOption
want string
}{
{"bool true", NewStorageBool(true), `true`},
{"bool false", NewStorageBool(false), `false`},
{"storage id", NewStorageID("abc-123"), `{"id":"abc-123"}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := json.Marshal(tt.opt)
if err != nil {
t.Fatalf("MarshalJSON() error = %v", err)
}
if string(got) != tt.want {
t.Errorf("MarshalJSON() = %s, want %s", got, tt.want)
}
})
}
}
func TestStorageOption_UnmarshalJSON(t *testing.T) {
t.Run("bool", func(t *testing.T) {
var s StorageOption
if err := json.Unmarshal([]byte(`false`), &s); err != nil {
t.Fatal(err)
}
if s.boolVal == nil || *s.boolVal != false {
t.Error("expected bool false")
}
})
t.Run("object", func(t *testing.T) {
var s StorageOption
if err := json.Unmarshal([]byte(`{"id":"xyz"}`), &s); err != nil {
t.Fatal(err)
}
if s.idVal == nil || *s.idVal != "xyz" {
t.Error("expected id xyz")
}
})
}
func TestNullableString_MarshalJSON(t *testing.T) {
tests := []struct {
name string
ns *NullableString
want string
}{
{"screen", EmulateScreen(), `"screen"`},
{"print", EmulatePrint(), `"print"`},
{"null", EmulateNone(), `null`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := json.Marshal(tt.ns)
if err != nil {
t.Fatalf("MarshalJSON() error = %v", err)
}
if string(got) != tt.want {
t.Errorf("MarshalJSON() = %s, want %s", got, tt.want)
}
})
}
}
func TestNullableString_Omitted(t *testing.T) {
// When NullableString pointer is nil, it should be omitted from JSON
type wrapper struct {
Field *NullableString `json:"field,omitempty"`
}
w := wrapper{Field: nil}
data, err := json.Marshal(w)
if err != nil {
t.Fatal(err)
}
if string(data) != `{}` {
t.Errorf("nil NullableString should be omitted, got: %s", data)
}
}
func TestNullableString_NullInJSON(t *testing.T) {
// When NullableString is EmulateNone(), it should serialize as null in the parent
type wrapper struct {
Field *NullableString `json:"field,omitempty"`
}
w := wrapper{Field: EmulateNone()}
data, err := json.Marshal(w)
if err != nil {
t.Fatal(err)
}
if string(data) != `{"field":null}` {
t.Errorf("EmulateNone should serialize as null, got: %s", data)
}
}
func TestNullableString_UnmarshalJSON(t *testing.T) {
t.Run("string value", func(t *testing.T) {
var ns NullableString
if err := json.Unmarshal([]byte(`"screen"`), &ns); err != nil {
t.Fatal(err)
}
if ns.IsNull || ns.Value != "screen" {
t.Error("expected value screen")
}
})
t.Run("null value", func(t *testing.T) {
var ns NullableString
if err := json.Unmarshal([]byte(`null`), &ns); err != nil {
t.Fatal(err)
}
if !ns.IsNull {
t.Error("expected IsNull=true")
}
})
}