-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added handling for non-zero values excluding
false
(#35)
* Added handling for non-zero values excluding `false` * Resolving comments.
- Loading branch information
1 parent
05fa0cd
commit 2490e7b
Showing
2 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package zap | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type testStruct struct { | ||
Field string | ||
} | ||
|
||
func Test_appendFilledFieldsOnly(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
fields []any | ||
value any | ||
want []any | ||
}{ | ||
{ | ||
name: "nil value", | ||
fields: []any{}, | ||
value: nil, | ||
want: []any{}, | ||
}, | ||
{ | ||
name: "nil pointer", | ||
fields: []any{}, | ||
value: (*string)(nil), | ||
want: []any{}, | ||
}, | ||
{ | ||
name: "empty string value", | ||
fields: []any{}, | ||
value: "", | ||
want: []any{}, | ||
}, | ||
{ | ||
name: "string with spaces", | ||
fields: []any{}, | ||
value: " ", | ||
want: []any{"key", " "}, | ||
}, | ||
{ | ||
name: "string with special characters", | ||
fields: []any{}, | ||
value: "!@#$%", | ||
want: []any{"key", "!@#$%"}, | ||
}, | ||
{ | ||
name: "zero int value", | ||
fields: []any{}, | ||
value: 0, | ||
want: []any{}, | ||
}, | ||
{ | ||
name: "zero float value", | ||
fields: []any{}, | ||
value: 0.0, | ||
want: []any{}, | ||
}, | ||
{ | ||
name: "negative number", | ||
fields: []any{}, | ||
value: -1, | ||
want: []any{"key", -1}, | ||
}, | ||
{ | ||
name: "positive float value", | ||
fields: []any{}, | ||
value: 3.14, | ||
want: []any{"key", 3.14}, | ||
}, | ||
{ | ||
name: "boolean false value", | ||
fields: []any{}, | ||
value: false, | ||
want: []any{"key", false}, | ||
}, | ||
{ | ||
name: "boolean true value", | ||
fields: []any{}, | ||
value: true, | ||
want: []any{"key", true}, | ||
}, | ||
{ | ||
name: "empty struct", | ||
fields: []any{}, | ||
value: testStruct{}, | ||
want: []any{}, | ||
}, | ||
{ | ||
name: "non-empty struct", | ||
fields: []any{}, | ||
value: testStruct{Field: "value"}, | ||
want: []any{"key", testStruct{Field: "value"}}, | ||
}, | ||
{ | ||
name: "empty slice", | ||
fields: []any{}, | ||
value: []string{}, | ||
want: []any{}, | ||
}, | ||
{ | ||
name: "non-empty slice", | ||
fields: []any{}, | ||
value: []string{"item"}, | ||
want: []any{"key", []string{"item"}}, | ||
}, | ||
{ | ||
name: "empty map", | ||
fields: []any{}, | ||
value: map[string]string{}, | ||
want: []any{}, | ||
}, | ||
{ | ||
name: "non-empty map", | ||
fields: []any{}, | ||
value: map[string]string{"key": "value"}, | ||
want: []any{"key", map[string]string{"key": "value"}}, | ||
}, | ||
{ | ||
name: "existing fields", | ||
fields: []any{"existing", 1}, | ||
value: "value", | ||
want: []any{"existing", 1, "key", "value"}, | ||
}, | ||
{ | ||
name: "boolean true with existing fields", | ||
fields: []any{"existing", 1}, | ||
value: true, | ||
want: []any{"existing", 1, "key", true}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
appendFilledFieldsOnly(&tt.fields, "key", tt.value) | ||
assert.Equal(t, tt.want, tt.fields) | ||
}) | ||
} | ||
} |