Skip to content

Commit

Permalink
Added tests for anyToString (#1382)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobht authored Nov 5, 2024
1 parent 6b2bc0d commit 2801925
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions internal/common/util/stringer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,80 @@ import (
"github.com/stretchr/testify/require"
)

func Test_anyToString(t *testing.T) {
testCases := []struct {
name string
thingToSerialize interface{}
expected string
}{
{
name: "nil",
thingToSerialize: nil,
expected: "<nil>",
},
{
name: "int",
thingToSerialize: 1,
expected: "1",
},
{
name: "string",
thingToSerialize: "test",
expected: "test",
},
{
name: "struct",
thingToSerialize: struct {
A string
B int
C bool
}{A: "test", B: 1, C: true},
expected: "(A:test, B:1, C:true)",
},
{
name: "pointer",
thingToSerialize: &struct {
A string
B int
C bool
}{A: "test", B: 1, C: true},
expected: "(A:test, B:1, C:true)",
},
{
name: "slice",
thingToSerialize: []int{1, 2, 3},
expected: "[1 2 3]",
},
{
name: "struct with slice",
thingToSerialize: struct{ A []int }{A: []int{1, 2, 3}},
expected: "(A:[len=3])",
},
{
name: "struct with blob",
thingToSerialize: struct{ A []byte }{A: []byte("blob-data")},
expected: "(A:[blob-data])",
},
{
name: "struct with struct",
thingToSerialize: struct{ A struct{ B int } }{A: struct{ B int }{B: 1}},
expected: "(A:(B:1))",
},
{
name: "struct with pointer",
thingToSerialize: struct{ A *int }{A: new(int)},
expected: "(A:0)",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := anyToString(tc.thingToSerialize)
require.Equal(t, tc.expected, actual)
})
}
}

func Test_byteSliceToString(t *testing.T) {
data := []byte("blob-data")
v := reflect.ValueOf(data)
Expand Down

0 comments on commit 2801925

Please sign in to comment.