summaryrefslogtreecommitdiff
path: root/internal/conv/conv_test.go
blob: f25423835a1796299e2f627b7b10400a31551fae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package conv

import (
	"fmt"
	"testing"

	"gotest.tools/v3/assert"
)

func TestInterfaceSlice(t *testing.T) {
	data := []struct {
		in, expected any
	}{
		{[]int{1, 2, 3}, []any{1, 2, 3}},
		{[3]int{1, 2, 3}, []any{1, 2, 3}},
		{[]string{"foo", "bar", "baz"}, []any{"foo", "bar", "baz"}},
		{[3]string{"foo", "bar", "baz"}, []any{"foo", "bar", "baz"}},
		{[]any{[]string{}, []int{1, 2}, 3}, []any{[]string{}, []int{1, 2}, 3}},
		{[3]any{[]string{}, []int{1, 2}, 3}, []any{[]string{}, []int{1, 2}, 3}},
	}

	for _, d := range data {
		out, err := InterfaceSlice(d.in)
		assert.NilError(t, err)
		assert.DeepEqual(t, d.expected, out)
	}

	_, err := InterfaceSlice(42)
	assert.ErrorContains(t, err, "")
}

func BenchmarkInterfaceSlice(b *testing.B) {
	data := []any{
		[]int{1, 2, 3},
		[3]int{1, 2, 3},
		[]string{"foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz"},
		[12]string{"foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz"},
		[]any{[]string{}, []int{1, 2}, 3},
		[3]any{[]string{}, []int{1, 2}, 3},
	}

	for _, d := range data {
		b.Run(fmt.Sprintf("%T(%v)", d, d), func(b *testing.B) {
			for b.Loop() {
				InterfaceSlice(d)
			}
		})
	}
}