-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposite_test.go
More file actions
91 lines (83 loc) · 1.72 KB
/
composite_test.go
File metadata and controls
91 lines (83 loc) · 1.72 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
package null_test
import (
"encoding/json"
"reflect"
"testing"
. "github.com/gomoni/null"
)
func TestNull(t *testing.T) {
var testCases = []struct {
name string
inp string
wantValue Null[int]
}{
{
name: "int value",
inp: `{"key": 42}`,
wantValue: NewNull(42),
},
{
name: "null value",
inp: `{"key": null}`,
wantValue: NewNull[int](),
},
{
name: "empty value",
inp: `{}`,
wantValue: NewNull[int](0),
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
var s struct {
Key Null[int] `json:"key"`
}
err := json.Unmarshal([]byte(tt.inp), &s)
if err != nil {
t.Fatalf("unexpected err when unmarshaling: %s", err)
}
if !reflect.DeepEqual(s.Key, tt.wantValue) {
t.Fatalf("unexpected value: got %+v, want: %+v", s.Key, tt.wantValue)
}
})
}
}
func TestComposite(t *testing.T) {
var testCases = []struct {
name string
inp string
wantValue Option[Null[int]]
}{
{
name: "int value",
inp: `{"key": 42}`,
wantValue: NewOption(NewNull(42)),
},
{
name: "null value",
inp: `{"key": null}`,
wantValue: NewOption(NewNull[int]()),
},
{
name: "empty value",
inp: `{}`,
wantValue: NewOption[Null[int]](),
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
var s struct {
Key Option[Null[int]] `json:"key"`
}
err := json.Unmarshal([]byte(tt.inp), &s)
if err != nil {
t.Fatalf("unexpected err when unmarshaling: %s", err)
}
if !reflect.DeepEqual(s.Key, tt.wantValue) {
t.Fatalf("unexpected value: got %+v, want: %+v", s.Key, tt.wantValue)
}
})
}
}