github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/internal/conv/conv_test.go (about) 1 package conv 2 3 import ( 4 "fmt" 5 "testing" 6 7 "gotest.tools/v3/assert" 8 ) 9 10 func TestInterfaceSlice(t *testing.T) { 11 data := []struct { 12 in, expected interface{} 13 }{ 14 {[]int{1, 2, 3}, []interface{}{1, 2, 3}}, 15 {[3]int{1, 2, 3}, []interface{}{1, 2, 3}}, 16 {[]string{"foo", "bar", "baz"}, []interface{}{"foo", "bar", "baz"}}, 17 {[3]string{"foo", "bar", "baz"}, []interface{}{"foo", "bar", "baz"}}, 18 {[]interface{}{[]string{}, []int{1, 2}, 3}, []interface{}{[]string{}, []int{1, 2}, 3}}, 19 {[3]interface{}{[]string{}, []int{1, 2}, 3}, []interface{}{[]string{}, []int{1, 2}, 3}}, 20 } 21 22 for _, d := range data { 23 out, err := InterfaceSlice(d.in) 24 assert.NilError(t, err) 25 assert.DeepEqual(t, d.expected, out) 26 } 27 28 _, err := InterfaceSlice(42) 29 assert.ErrorContains(t, err, "") 30 } 31 32 func BenchmarkInterfaceSlice(b *testing.B) { 33 data := []interface{}{ 34 []int{1, 2, 3}, 35 [3]int{1, 2, 3}, 36 []string{"foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz"}, 37 [12]string{"foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz"}, 38 []interface{}{[]string{}, []int{1, 2}, 3}, 39 [3]interface{}{[]string{}, []int{1, 2}, 3}, 40 } 41 42 for _, d := range data { 43 d := d 44 b.Run(fmt.Sprintf("%T(%v)", d, d), func(b *testing.B) { 45 for i := 0; i < b.N; i++ { 46 InterfaceSlice(d) 47 } 48 }) 49 } 50 }