Skip to content

Commit 8de7f4a

Browse files
committed
Update tests a little bit and add comment
1 parent 8bbca55 commit 8de7f4a

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

encode.go

+6
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,12 @@ func (enc *Encoder) isEmpty(rv reflect.Value) bool {
657657
if rv.Type().Comparable() {
658658
return reflect.Zero(rv.Type()).Interface() == rv.Interface()
659659
}
660+
// Need to also check if all the fields are empty, otherwise something
661+
// like this with uncomparable types will always return true:
662+
//
663+
// type a struct{ field b }
664+
// type b struct{ s []string }
665+
// s := a{field: b{s: []string{"AAA"}}}
660666
for i := 0; i < rv.NumField(); i++ {
661667
if !enc.isEmpty(rv.Field(i)) {
662668
return false

encode_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func TestEncodeOmitEmptyStruct(t *testing.T) {
181181
}
182182
}
183183

184-
func TestEncodeWithOmitEmpty(t *testing.T) {
184+
func TestEncodeOmitEmpty(t *testing.T) {
185185
type compareable struct {
186186
Bool bool `toml:"bool,omitempty"`
187187
}
@@ -245,7 +245,7 @@ time = 1985-06-18T15:16:17Z
245245
v, expected, nil)
246246
}
247247

248-
func TestEncodeWithOmitZero(t *testing.T) {
248+
func TestEncodeOmitZero(t *testing.T) {
249249
type simple struct {
250250
Number int `toml:"number,omitzero"`
251251
Real float64 `toml:"real,omitzero"`
@@ -267,7 +267,7 @@ unsigned = 5
267267
encodeExpected(t, "simple with omitzero, non-zero", value, expected, nil)
268268
}
269269

270-
func TestEncodeOmitemptyWithEmptyName(t *testing.T) {
270+
func TestEncodeOmitemptyEmptyName(t *testing.T) {
271271
type simple struct {
272272
S []int `toml:",omitempty"`
273273
}
@@ -1187,7 +1187,9 @@ func encodeExpected(t *testing.T, label string, val interface{}, want string, wa
11871187
have := strings.TrimSpace(buf.String())
11881188
want = strings.TrimSpace(want)
11891189
if want != have {
1190-
t.Errorf("\nhave:\n%s\nwant:\n%s\n", have, want)
1190+
t.Errorf("\nhave:\n%s\nwant:\n%s\n",
1191+
"\t"+strings.ReplaceAll(have, "\n", "\n\t"),
1192+
"\t"+strings.ReplaceAll(want, "\n", "\n\t"))
11911193
}
11921194
})
11931195
}

0 commit comments

Comments
 (0)