-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_test.go
More file actions
147 lines (133 loc) · 3.19 KB
/
type_test.go
File metadata and controls
147 lines (133 loc) · 3.19 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
//go:build go1.18
// Copyright (c) 2020 gomoni contributors
// Under BSD 3-Clause license, see LICENSE file
package null_test
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"testing"
. "github.com/gomoni/null"
)
func TestJSON(t *testing.T) {
t.Parallel()
var testCases = []struct {
name string
inp string
wantValue Type[int]
wantJSON string
}{
{
name: "int value",
inp: `{"key": 42}`,
wantValue: New(42),
wantJSON: `{"key":42}`,
},
{
name: "null value",
inp: `{"key": null}`,
wantValue: NewNullType[int](),
wantJSON: `{"key":null}`,
},
{
name: "undefined value",
inp: `{}`,
wantValue: NewUndefined[int](),
wantJSON: `{}`,
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
var s struct {
Key Type[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)
}
if _, err := s.Key.Value(); errors.Is(err, ErrUndefined) {
_, err = json.Marshal(s)
if !errors.Is(err, ErrUndefined) {
t.Fatalf("expected err when marshaling undefined: got %+v, want ErrUndefined", err)
}
} else {
out, err := json.Marshal(s)
if err != nil {
t.Fatalf("unexpected err when marshaling: %s", err)
}
if string(out) != tt.wantJSON {
t.Fatalf("unexpected json: got %s, want: %s", string(out), tt.wantJSON)
}
}
})
}
}
func TestSprintf(t *testing.T) {
t.Parallel()
type v struct {
Int int
Str string
}
type s struct {
Key Type[string]
Value Type[v]
}
var testCases = []struct {
name string
inp s
want map[string]string
}{
{
name: "undefined",
inp: s{Key: NewUndefined[string](), Value: NewUndefined[v]()},
want: map[string]string{
"%s": "{undefined undefined}",
"%q": "{undefined undefined}",
"%v": "{undefined undefined}",
"%+v": "{Key:undefined Value:undefined}",
"%#v": "null_test.s{Key:NewUndefined[string](), Value:NewUndefined[null_test.v]()}",
},
},
{
name: "null",
inp: s{Key: NewNullType[string](), Value: NewNullType[v]()},
want: map[string]string{
"%s": "{null null}",
"%q": "{null null}",
"%v": "{null null}",
"%+v": "{Key:null Value:null}",
"%#v": "null_test.s{Key:NewNull[string](), Value:NewNull[null_test.v]()}",
},
},
{
name: "some",
inp: s{Key: New("some"), Value: New(v{42, "string"})},
want: map[string]string{
"%s": "{some {42 string}}",
"%q": `{"some" {'*' "string"}}`,
"%v": "{some {42 string}}",
"%+v": "{Key:some Value:{42 string}}",
"%#v": `null_test.s{Key:New[string]("some"), Value:New[null_test.v](null_test.v{Int:42, Str:"string"})}`,
},
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
for format, want := range tt.want {
t.Run(format, func(t *testing.T) {
got := fmt.Sprintf(format, tt.inp)
if got != want {
t.Errorf("format failed %q: got %q, want %q", format, got, want)
}
})
}
})
}
}