-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbool_test.go
288 lines (211 loc) · 5.48 KB
/
bool_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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package schemer
import (
"bytes"
"fmt"
"reflect"
"strings"
"testing"
)
// base case, just make sure we can encode a boolean value, and subsequently read it back out
// from an empty interface
func TestDecodeBool1(t *testing.T) {
bSchema := BoolSchema{SchemaOptions{nullable: false}}
var buf bytes.Buffer
var err error
var valueToEncode bool = false
buf.Reset()
fmt.Println("bool to bool")
bSchema.Encode(&buf, valueToEncode)
if err != nil {
t.Error(err)
}
r := bytes.NewReader(buf.Bytes())
var decodedValue1 interface{}
err = bSchema.Decode(r, &decodedValue1)
if err != nil {
t.Error(err)
}
if valueToEncode != reflect.ValueOf(decodedValue1).Elem().Bool() {
t.Errorf("Expected value")
}
}
// make sure we can decode a boolean to the integer types
func TestDecodeBool2(t *testing.T) {
bSchema := BoolSchema{SchemaOptions{weakDecoding: true}}
var buf bytes.Buffer
var err error
var valueToEncode bool = true
buf.Reset()
bSchema.Encode(&buf, valueToEncode)
if err != nil {
t.Error(err)
}
fmt.Println("bool to int")
r := bytes.NewReader(buf.Bytes())
var decodedValue1 int
err = bSchema.Decode(r, &decodedValue1)
if err != nil {
t.Error(err)
}
if decodedValue1 != 1 {
t.Errorf("Expected value")
}
fmt.Println("bool to uint")
r = bytes.NewReader(buf.Bytes())
var decodedValue2 uint
err = bSchema.Decode(r, &decodedValue2)
if err != nil {
t.Error(err)
}
if decodedValue2 != 1 {
t.Errorf("Expected value")
}
}
// make sure we can decode a boolean value to a string, as long as weak decoding is on
func TestDecodeBool3(t *testing.T) {
bSchema := BoolSchema{SchemaOptions{weakDecoding: true}}
var buf bytes.Buffer
var err error
var valueToEncode bool = true
buf.Reset()
bSchema.Encode(&buf, valueToEncode)
if err != nil {
t.Error(err)
}
fmt.Println("bool to string")
r := bytes.NewReader(buf.Bytes())
var decodedValue1 string
err = bSchema.Decode(r, &decodedValue1)
if err != nil {
t.Error(err)
}
if strings.ToUpper(decodedValue1) != "TRUE" {
t.Errorf("Expected value")
}
}
// corner case:
// make sure library refuses to decode to an int when weak decoding is not enabled
func TestDecodeBool4(t *testing.T) {
bSchema := BoolSchema{SchemaOptions{weakDecoding: false}}
var buf bytes.Buffer
var err error
var valueToEncode bool = true
buf.Reset()
bSchema.Encode(&buf, valueToEncode)
if err != nil {
t.Error(err)
}
fmt.Println("bool to int (w/o weak decoding enabled)")
r := bytes.NewReader(buf.Bytes())
var decodedValue1 int
err = bSchema.Decode(r, &decodedValue1)
if err == nil {
t.Error("schemer library decoding failure; decoding bool to int should not be allowed w/o weak decoding")
}
}
// corner case:
// make sure library refuses to decode to string when weak decoding is not enabled
func TestDecodeBool5(t *testing.T) {
bSchema := BoolSchema{SchemaOptions{weakDecoding: false}}
var buf bytes.Buffer
var err error
var valueToEncode bool = true
buf.Reset()
bSchema.Encode(&buf, valueToEncode)
if err != nil {
t.Error(err)
}
fmt.Println("bool to string (w/o weak decoding enabled)")
r := bytes.NewReader(buf.Bytes())
var decodedValue1 string
err = bSchema.Decode(r, &decodedValue1)
if err == nil {
t.Error("schemer library decoding failure; decoding bool to string should not be allowed w/o weak decoding")
}
}
// TestDecodeBool6 makes sure we can encode and decode boolean schemas
func TestDecodeBool6(t *testing.T) {
// setup an example schema
schema := BoolSchema{SchemaOptions{nullable: false}}
// encode it
b, err := schema.MarshalSchemer()
if err != nil {
t.Error("cannot marshall schemer")
}
// make sure we can successfully decode it
tmp, err := DecodeSchema(bytes.NewReader(b))
decodedBoolSchema := tmp.(*BoolSchema)
if err != nil {
t.Error("cannot decode binary encoded bool")
}
if decodedBoolSchema.Nullable() != schema.Nullable() {
t.Error("unexpected value for BoolSchema")
}
}
func TestDecodeBool7(t *testing.T) {
boolSchema := BoolSchema{SchemaOptions{nullable: true}}
fmt.Println("decode nil bool")
var buf bytes.Buffer
var err error
var boolPtr *bool
buf.Reset()
err = boolSchema.Encode(&buf, boolPtr) // pass in nil pointer
if err != nil {
t.Error(err)
}
//------------
r := bytes.NewReader(buf.Bytes())
var boolToDecode bool
var boolPtr2 *bool = &boolToDecode
err = boolSchema.Decode(r, &boolPtr2)
if err != nil {
t.Error(err)
}
if boolPtr2 != nil {
t.Error("unexpected value decoding null boolean")
}
}
// make sure we can decode a nil bool into an interface
func TestDecodeBool7A(t *testing.T) {
boolSchema := BoolSchema{SchemaOptions{
nullable: true,
weakDecoding: false,
}}
fmt.Println("decode nil bool")
var buf bytes.Buffer
var err error
var boolPtr *bool
buf.Reset()
err = boolSchema.Encode(&buf, boolPtr) // pass in nil pointer
if err != nil {
t.Error(err)
return
}
// encode it
b, err := boolSchema.MarshalSchemer()
if err != nil {
t.Error(err)
return
}
//------------
// make sure we can successfully decode it
decodedSchema, err := DecodeSchema(bytes.NewReader(b))
if err != nil {
t.Error(err)
return
}
r := bytes.NewReader(buf.Bytes())
var decodedValue interface{} = "A" // just some random value
err = decodedSchema.Decode(r, &decodedValue)
if err != nil {
t.Error(err)
}
if decodedValue != nil {
t.Error("unexpected value decoding null boolean")
}
}
func TestDecodeBool8(t *testing.T) {
boolSchema := BoolSchema{SchemaOptions{nullable: true}}
b, _ := boolSchema.MarshalJSON()
fmt.Print(string(b))
}