Skip to content

Commit

Permalink
Update tests a little bit and add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Oct 22, 2022
1 parent 8bbca55 commit 8de7f4a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 6 additions & 0 deletions encode.go
Expand Up @@ -657,6 +657,12 @@ func (enc *Encoder) isEmpty(rv reflect.Value) bool {
if rv.Type().Comparable() {
return reflect.Zero(rv.Type()).Interface() == rv.Interface()
}
// Need to also check if all the fields are empty, otherwise something
// like this with uncomparable types will always return true:
//
// type a struct{ field b }
// type b struct{ s []string }
// s := a{field: b{s: []string{"AAA"}}}
for i := 0; i < rv.NumField(); i++ {
if !enc.isEmpty(rv.Field(i)) {
return false
Expand Down
10 changes: 6 additions & 4 deletions encode_test.go
Expand Up @@ -181,7 +181,7 @@ func TestEncodeOmitEmptyStruct(t *testing.T) {
}
}

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

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

func TestEncodeOmitemptyWithEmptyName(t *testing.T) {
func TestEncodeOmitemptyEmptyName(t *testing.T) {
type simple struct {
S []int `toml:",omitempty"`
}
Expand Down Expand Up @@ -1187,7 +1187,9 @@ func encodeExpected(t *testing.T, label string, val interface{}, want string, wa
have := strings.TrimSpace(buf.String())
want = strings.TrimSpace(want)
if want != have {
t.Errorf("\nhave:\n%s\nwant:\n%s\n", have, want)
t.Errorf("\nhave:\n%s\nwant:\n%s\n",
"\t"+strings.ReplaceAll(have, "\n", "\n\t"),
"\t"+strings.ReplaceAll(want, "\n", "\n\t"))
}
})
}

0 comments on commit 8de7f4a

Please sign in to comment.