Skip to content

Commit

Permalink
"complex" test for slice reflectutil
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxNode committed Apr 22, 2024
1 parent 127a78c commit 56887ff
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion util/reflectutil/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestUnsafeSliceClone(t *testing.T) {
func TestUnsafeSliceCloneSimple(t *testing.T) {
testCases := []struct {
name string
given []int
Expand Down Expand Up @@ -39,3 +39,40 @@ func TestUnsafeSliceClone(t *testing.T) {
})
}
}

func TestUnsafeSliceCloneComplex(t *testing.T) {
type foo struct {
value string
}

testCases := []struct {
name string
given []foo
}{
{
name: "empty",
given: []foo{},
},
{
name: "one",
given: []foo{{value: "a"}},
},
{
name: "many",
given: []foo{{value: "a"}, {value: "b"}},
},
}

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
original := test.given
clonePtr := UnsafeSliceClone(reflect2.PtrOf(test.given), reflect2.TypeOf([]foo{}).(*reflect2.UnsafeSliceType))
clone := *(*[]foo)(clonePtr)

assert.NotSame(t, original, clone, "reference")
assert.Equal(t, original, clone, "equality")
assert.Equal(t, len(original), len(clone), "len")
assert.Equal(t, cap(original), cap(clone), "cap")
})
}
}

0 comments on commit 56887ff

Please sign in to comment.