1
2
3
4
5
6
7 package bsoncodec
8
9 import (
10 "reflect"
11 "testing"
12 "time"
13
14 "go.mongodb.org/mongo-driver/internal/assert"
15 )
16
17 var _ Zeroer = zeroer{}
18
19 type zeroer struct {
20 val int
21 }
22
23 func (z zeroer) IsZero() bool {
24 return z.val != 0
25 }
26
27 func TestIsZero(t *testing.T) {
28 t.Parallel()
29 testCases := []struct {
30 description string
31 value interface{}
32 omitZeroStruct bool
33 want bool
34 }{
35 {
36 description: "false",
37 value: false,
38 want: true,
39 },
40 {
41 description: "0",
42 value: 0,
43 want: true,
44 },
45 {
46 description: "nil pointer to int",
47 value: (*int)(nil),
48 want: true,
49 },
50 {
51 description: "time.Time",
52 value: time.Unix(1682123781, 0),
53 want: false,
54 },
55 {
56 description: "empty time.Time",
57 value: time.Time{},
58 want: true,
59 },
60 {
61 description: "nil pointer to time.Time",
62 value: (*time.Time)(nil),
63 want: true,
64 },
65 {
66 description: "zero struct",
67 value: struct{ Val bool }{},
68 want: false,
69 },
70 {
71 description: "non-zero struct",
72 value: struct{ Val bool }{Val: true},
73 want: false,
74 },
75 {
76 description: "nil pointer to struct",
77 value: (*struct{ Val bool })(nil),
78 want: true,
79 },
80 {
81 description: "pointer to struct",
82 value: &struct{ Val bool }{},
83 want: false,
84 },
85 {
86 description: "zero struct that implements Zeroer",
87 value: zeroer{},
88 want: false,
89 },
90 {
91 description: "non-zero struct that implements Zeroer",
92 value: &zeroer{val: 1},
93 want: true,
94 },
95 {
96 description: "pointer to zero struct that implements Zeroer",
97 value: &zeroer{},
98 want: false,
99 },
100 {
101 description: "pointer to non-zero struct that implements Zeroer",
102 value: zeroer{val: 1},
103 want: true,
104 },
105 {
106 description: "zero struct with omitZeroStruct",
107 value: struct{ Val bool }{},
108 omitZeroStruct: true,
109 want: true,
110 },
111 {
112 description: "non-zero struct with omitZeroStruct",
113 value: struct{ Val bool }{Val: true},
114 omitZeroStruct: true,
115 want: false,
116 },
117 {
118 description: "zero struct with only private fields omitZeroStruct",
119 value: struct{ val bool }{},
120 omitZeroStruct: true,
121 want: true,
122 },
123
124
125 {
126 description: "non-zero struct with only private fields with omitZeroStruct",
127 value: struct{ val bool }{val: true},
128 omitZeroStruct: true,
129 want: true,
130 },
131 {
132 description: "pointer to zero struct with omitZeroStruct",
133 value: &struct{ Val bool }{},
134 omitZeroStruct: true,
135 want: false,
136 },
137 {
138 description: "pointer to non-zero struct with omitZeroStruct",
139 value: &struct{ Val bool }{Val: true},
140 omitZeroStruct: true,
141 want: false,
142 },
143 {
144 description: "empty map",
145 value: map[string]string{},
146 want: true,
147 },
148 {
149 description: "empty slice",
150 value: []struct{}{},
151 want: true,
152 },
153 {
154 description: "empty string",
155 value: "",
156 want: true,
157 },
158 }
159
160 for _, tc := range testCases {
161 tc := tc
162
163 t.Run(tc.description, func(t *testing.T) {
164 t.Parallel()
165
166 got := isEmpty(reflect.ValueOf(tc.value), tc.omitZeroStruct)
167 assert.Equal(t, tc.want, got, "expected and actual isEmpty return are different")
168 })
169 }
170 }
171
View as plain text