-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_test.go
106 lines (96 loc) · 3.14 KB
/
utils_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
package qstring
import (
"reflect"
"testing"
"time"
)
func TestIsEmptyValue(t *testing.T) {
var ts *TestStruct
ts = nil
testIO := []struct {
inp reflect.Value
expected bool
}{
{inp: reflect.ValueOf([]int{0}), expected: false},
{inp: reflect.ValueOf([]int{}), expected: true},
{inp: reflect.ValueOf(map[string]int{"a": 0}), expected: false},
{inp: reflect.ValueOf(map[string]int{}), expected: true},
{inp: reflect.ValueOf(false), expected: true},
{inp: reflect.ValueOf(true), expected: false},
{inp: reflect.ValueOf(5), expected: false},
{inp: reflect.ValueOf(0), expected: true},
{inp: reflect.ValueOf(uint(5)), expected: false},
{inp: reflect.ValueOf(uint(0)), expected: true},
{inp: reflect.ValueOf(float32(5)), expected: false},
{inp: reflect.ValueOf(float32(0)), expected: true},
{inp: reflect.ValueOf(&TestStruct{}), expected: false},
{inp: reflect.ValueOf(ts), expected: true},
{inp: reflect.ValueOf(nil), expected: false},
{inp: reflect.ValueOf(time.Time{}), expected: true},
{inp: reflect.ValueOf(time.Now()), expected: false},
{inp: reflect.ValueOf(*NewComparativeTime()), expected: true},
{inp: reflect.ValueOf(ComparativeTime{Operator: "=", Time: time.Now()}),
expected: false},
}
var result bool
for _, test := range testIO {
result = isEmptyValue(test.inp)
if result != test.expected {
t.Errorf("Expected %t for input %s, got %t", test.expected, test.inp, result)
}
}
}
func TestTagParsing(t *testing.T) {
testio := []struct {
inp string
output string
omit bool
comma bool
}{
{inp: "name,omitempty", output: "name", omit: true, comma: false},
{inp: "name", output: "name", omit: false, comma: false},
{inp: "name,", output: "name", omit: false, comma: false},
{inp: "name", output: "name", omit: false, comma: false},
{inp: "", output: "", omit: false, comma: false},
{inp: ",omitempty", output: "", omit: true, comma: false},
{inp: "-", output: "-", omit: false, comma: false},
{inp: "name,omitempty,comma", output: "name", omit: true, comma: true},
{inp: "name,comma", output: "name", omit: false, comma: true},
{inp: "name,comma,", output: "name", omit: false, comma: true},
}
var name string
var omit bool
var comma bool
for _, test := range testio {
name, omit, comma = parseTag(test.inp)
if name != test.output {
t.Errorf("Expected tag name to be %q, got %q instead", test.output, name)
}
if omit != test.omit {
t.Errorf("Expected omitempty to be %t, got %t instead", test.omit, omit)
}
if comma != test.comma {
t.Errorf("Expected comma to be %t, got %t instead", test.comma, comma)
}
}
}
func TestOmitEmpty(t *testing.T) {
vis := 5
testio := []struct {
Visible int
Conditional int `qstring:"conditional,omitempty"`
omit bool
}{
{Visible: vis, Conditional: 5, omit: false},
{Visible: vis, Conditional: 0, omit: true},
}
for _, test := range testio {
values, _ := Marshal(&test)
_, found := values["conditional"]
if found && test.omit {
t.Errorf("%d was unexpectedly marshaled", test.Conditional)
} else if !found && !test.omit {
t.Errorf("%d was not marshaled, but should have been", test.Conditional)
}
}
}