2017-07-28 00:11:37 +02:00
|
|
|
package values
|
2017-06-28 03:30:47 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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}))
|
|
|
|
}
|
2017-07-02 14:35:06 +02:00
|
|
|
|
|
|
|
func TestSort(t *testing.T) {
|
|
|
|
array := []interface{}{2, 1}
|
|
|
|
Sort(array)
|
|
|
|
require.Equal(t, []interface{}{1, 2}, array)
|
|
|
|
|
|
|
|
array = []interface{}{"b", "a"}
|
|
|
|
Sort(array)
|
|
|
|
require.Equal(t, []interface{}{"a", "b"}, array)
|
|
|
|
|
|
|
|
array = []interface{}{
|
|
|
|
map[string]interface{}{"key": 20},
|
|
|
|
map[string]interface{}{"key": 10},
|
|
|
|
map[string]interface{}{},
|
|
|
|
}
|
|
|
|
SortByProperty(array, "key", true)
|
|
|
|
require.Equal(t, nil, array[0].(map[string]interface{})["key"])
|
|
|
|
require.Equal(t, 10, array[1].(map[string]interface{})["key"])
|
|
|
|
require.Equal(t, 20, array[2].(map[string]interface{})["key"])
|
|
|
|
}
|