2017-06-28 03:30:47 +02:00
|
|
|
package generics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-28 19:28:55 +02:00
|
|
|
"reflect"
|
2017-06-28 03:30:47 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-06-28 19:28:55 +02:00
|
|
|
var convertTests = []struct {
|
|
|
|
value, proto, expected interface{}
|
|
|
|
}{
|
|
|
|
{1, 1.0, float64(1)},
|
|
|
|
{"2", 1, int(2)},
|
|
|
|
{"1.2", 1.0, float64(1.2)},
|
2017-06-29 18:20:16 +02:00
|
|
|
{true, 1, 1},
|
|
|
|
{false, 1, 0},
|
2017-07-02 13:51:24 +02:00
|
|
|
{1, "", "1"},
|
|
|
|
{false, "", "false"},
|
|
|
|
{true, "", "true"},
|
|
|
|
{"string", "", "string"},
|
2017-06-28 19:28:55 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 04:54:27 +02:00
|
|
|
var eqTests = []struct {
|
|
|
|
a, b interface{}
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{nil, nil, true},
|
|
|
|
{nil, 1, false},
|
|
|
|
{1, nil, false},
|
|
|
|
{false, false, true},
|
|
|
|
{false, true, false},
|
2017-06-28 15:36:39 +02:00
|
|
|
{0, 1, false},
|
|
|
|
{1, 1, true},
|
|
|
|
{1.0, 1.0, true},
|
|
|
|
{1, 1.0, true},
|
|
|
|
{1, 2.0, false},
|
|
|
|
{1.0, 1, true},
|
|
|
|
{"a", "b", false},
|
|
|
|
{"a", "a", true},
|
|
|
|
{[]string{"a"}, []string{"a"}, true},
|
|
|
|
{[]string{"a"}, []string{"a", "b"}, false},
|
|
|
|
{[]string{"a", "b"}, []string{"a"}, false},
|
|
|
|
{[]string{"a", "b"}, []string{"a", "b"}, true},
|
|
|
|
{[]string{"a", "b"}, []string{"a", "c"}, false},
|
|
|
|
{[]interface{}{1.0, 2}, []interface{}{1, 2.0}, true},
|
2017-06-28 04:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var lessTests = []struct {
|
2017-06-28 03:30:47 +02:00
|
|
|
a, b interface{}
|
|
|
|
expected bool
|
|
|
|
}{
|
2017-06-28 15:36:39 +02:00
|
|
|
{nil, nil, false},
|
|
|
|
{false, true, true},
|
|
|
|
{false, false, false},
|
|
|
|
{false, nil, false},
|
|
|
|
{nil, false, false},
|
2017-06-28 03:30:47 +02:00
|
|
|
{0, 1, true},
|
|
|
|
{1, 0, false},
|
|
|
|
{1, 1, false},
|
2017-06-28 15:36:39 +02:00
|
|
|
{1, 2.1, true},
|
|
|
|
{1.1, 2, true},
|
|
|
|
{2.1, 1, false},
|
2017-06-28 03:30:47 +02:00
|
|
|
{"a", "b", true},
|
|
|
|
{"b", "a", false},
|
2017-06-28 15:36:39 +02:00
|
|
|
{[]string{"a"}, []string{"a"}, false},
|
2017-06-28 03:30:47 +02:00
|
|
|
}
|
|
|
|
|
2017-07-02 13:51:24 +02:00
|
|
|
func TestCall(t *testing.T) {
|
|
|
|
fn := func(a, b string) string {
|
|
|
|
return a + "," + b + "."
|
|
|
|
}
|
|
|
|
args := []interface{}{5, 10}
|
|
|
|
value, err := Call(reflect.ValueOf(fn), args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "5,10.", value)
|
|
|
|
}
|
2017-06-28 19:28:55 +02:00
|
|
|
func TestConvert(t *testing.T) {
|
|
|
|
for i, test := range convertTests {
|
|
|
|
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
|
|
|
|
typ := reflect.TypeOf(test.proto)
|
2017-06-29 14:42:24 +02:00
|
|
|
value, err := Convert(test.value, typ)
|
2017-07-02 13:51:24 +02:00
|
|
|
name := fmt.Sprintf("Convert %#v -> %v", test.value, typ)
|
|
|
|
require.NoErrorf(t, err, name)
|
|
|
|
require.Equalf(t, test.expected, value, name)
|
2017-06-28 19:28:55 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 14:03:55 +02:00
|
|
|
func TestConvertMap(t *testing.T) {
|
|
|
|
m := map[interface{}]interface{}{"key": "value"}
|
|
|
|
typ := reflect.TypeOf(map[string]string{})
|
|
|
|
a, err := Convert(m, typ)
|
|
|
|
require.NoError(t, err)
|
|
|
|
switch a := a.(type) {
|
|
|
|
case map[string]string:
|
|
|
|
require.Len(t, a, 1)
|
|
|
|
require.Equal(t, "value", a["key"])
|
|
|
|
default:
|
|
|
|
require.Equal(t, typ.String(), reflect.TypeOf(a).String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertMapSynonym(t *testing.T) {
|
|
|
|
type VariableMap map[interface{}]interface{}
|
|
|
|
m := VariableMap{"key": "value"}
|
|
|
|
typ := reflect.TypeOf(map[string]string{})
|
|
|
|
a, err := Convert(m, typ)
|
|
|
|
require.NoError(t, err)
|
|
|
|
switch a := a.(type) {
|
|
|
|
case map[string]string:
|
|
|
|
require.Len(t, a, 1)
|
|
|
|
require.Equal(t, "value", a["key"])
|
|
|
|
default:
|
|
|
|
require.Equal(t, typ.String(), reflect.TypeOf(a).String())
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 19:28:55 +02:00
|
|
|
|
2017-06-28 04:54:27 +02:00
|
|
|
func TestEqual(t *testing.T) {
|
|
|
|
for i, test := range eqTests {
|
2017-06-28 15:36:39 +02:00
|
|
|
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
|
2017-06-28 04:54:27 +02:00
|
|
|
value := Equal(test.a, test.b)
|
2017-06-28 15:36:39 +02:00
|
|
|
require.Equalf(t, test.expected, value, "%#v == %#v", test.a, test.b)
|
2017-06-28 04:54:27 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 03:30:47 +02:00
|
|
|
func TestLess(t *testing.T) {
|
2017-06-28 04:54:27 +02:00
|
|
|
for i, test := range lessTests {
|
2017-06-28 15:36:39 +02:00
|
|
|
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
|
2017-06-28 03:30:47 +02:00
|
|
|
value := Less(test.a, test.b)
|
2017-06-28 15:36:39 +02:00
|
|
|
require.Equalf(t, test.expected, value, "%#v < %#v", test.a, test.b)
|
2017-06-28 03:30:47 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 20:41:46 +02:00
|
|
|
|
|
|
|
func TestLength(t *testing.T) {
|
|
|
|
require.Equal(t, 3, Length([]int{1, 2, 3}))
|
|
|
|
require.Equal(t, 3, Length("abc"))
|
|
|
|
require.Equal(t, 0, Length(map[string]int{"a": 1}))
|
|
|
|
}
|