-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper-string_test.go
210 lines (196 loc) · 4.57 KB
/
wrapper-string_test.go
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
package wrappers
import (
"encoding/json"
"testing"
)
func TestWrapperString_Wrap(t *testing.T) {
tests := []struct {
name string
input any
discard bool
want string
wantError bool
wantDiscard bool
}{
{
name: "Wrap string",
input: "hello",
discard: false,
want: "hello",
wantError: false,
wantDiscard: false,
},
{
name: "Wrap integer",
input: 123,
discard: false,
want: "123",
wantError: false,
wantDiscard: false,
},
{
name: "Wrap float",
input: 3.14,
discard: false,
want: "3.14",
wantError: false,
wantDiscard: false,
},
{
name: "Wrap nested wrapper discarding",
input: func() *WrapperString { w := New[*WrapperString](); w.Wrap("nested", true); return w }(),
discard: false,
want: "nested",
wantError: false,
wantDiscard: false,
},
{
name: "Wrap nested wrapper discarding to true",
input: func() *WrapperString { w := New[*WrapperString](); w.Discard(); return w }(),
discard: false,
want: "",
wantError: false,
wantDiscard: false,
},
{
name: "Wrap unsupported type",
input: []int{1, 2, 3},
discard: false,
want: "",
wantError: true,
wantDiscard: true,
},
{
name: "Wrap unsupported type discard",
input: []int{1, 2, 3},
discard: true,
want: "",
wantError: false,
wantDiscard: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wrapper := New[*WrapperString]()
err := wrapper.Wrap(tt.input, tt.discard)
if tt.wantError && err == nil {
t.Fatalf("Expected error but got none")
}
if !tt.wantError && err != nil {
t.Fatalf("Did not expect error but got: %v", err)
}
if tt.wantDiscard && !wrapper.IsDiscarded() {
t.Errorf("Expected wrapper to be discarded")
}
unwrapped := wrapper.Unwrap()
if unwrapped != tt.want {
t.Errorf("Unwrapped value = %v, want %v", unwrapped, tt.want)
}
})
}
}
// TestWrapperString_JSONMarshal tests JSON marshalling of WrapperString.
func TestWrapperString_JSONMarshal(t *testing.T) {
tests := []struct {
name string
wrapper *WrapperString
expectedJSON string
}{
{
name: "Marshal non-empty string",
wrapper: func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("test", false)
return w
}(),
expectedJSON: `"test"`,
},
{
name: "Marshal empty string",
wrapper: func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("", false)
return w
}(),
expectedJSON: `null`,
},
{
name: "Marshal discarded string",
wrapper: func() *WrapperString {
w := New[*WrapperString]()
w.Discard()
return w
}(),
expectedJSON: "null",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
jsonData, err := json.Marshal(tt.wrapper)
if err != nil {
t.Fatalf("Failed to marshal WrapperString: %v", err)
}
if string(jsonData) != tt.expectedJSON {
t.Errorf("Marshalled JSON = %v, want %v", string(jsonData), tt.expectedJSON)
}
})
}
}
// TestWrapperString_JSONUnmarshal tests JSON unmarshalling of WrapperString.
func TestWrapperString_JSONUnmarshal(t *testing.T) {
tests := []struct {
name string
jsonInput string
want string
wantDiscard bool
wantError bool
}{
{
name: "Unmarshal valid string",
jsonInput: `"hello world"`,
want: "hello world",
wantDiscard: false,
wantError: false,
},
{
name: "Unmarshal numeric string",
jsonInput: `"12345"`,
want: "12345",
wantDiscard: false,
wantError: false,
},
{
name: "Unmarshal null",
jsonInput: `null`,
want: "",
wantDiscard: true,
wantError: true,
},
{
name: "Unmarshal numeric type",
jsonInput: `123`,
want: "123",
wantDiscard: false,
wantError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wrapper := New[*WrapperString]()
err := json.Unmarshal([]byte(tt.jsonInput), wrapper)
if tt.wantError && err == nil {
t.Fatalf("Expected error but got none")
}
if !tt.wantError && err != nil {
t.Fatalf("Did not expect error but got: %v", err)
}
if tt.wantDiscard && !wrapper.IsDiscarded() {
t.Errorf("Expected wrapper to be discarded")
}
unwrapped := wrapper.Unwrap()
if unwrapped != tt.want {
t.Errorf("Unwrapped value = %v, want %v", unwrapped, tt.want)
}
})
}
}