2021-07-09 16:56:01 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestIsEmpty(t *testing.T) {
|
|
|
|
|
testCases := [...][2]interface{}{
|
|
|
|
|
{"", true},
|
|
|
|
|
{' ', true},
|
|
|
|
|
{'\t', true},
|
|
|
|
|
{'\n', true},
|
|
|
|
|
{" ", true},
|
|
|
|
|
{"\n", true},
|
|
|
|
|
{"\t", true},
|
2021-07-12 17:20:01 +03:00
|
|
|
{0, true},
|
2021-07-09 16:56:01 +03:00
|
|
|
{[]string{}, true},
|
|
|
|
|
{[0]string{}, true},
|
|
|
|
|
{[...]string{}, true},
|
|
|
|
|
{[]int{}, true},
|
|
|
|
|
{[0]int{}, true},
|
|
|
|
|
{[...]int{}, true},
|
|
|
|
|
{interface{}(nil), true},
|
|
|
|
|
{[]struct{}(nil), true},
|
|
|
|
|
{[]interface{}(nil), true},
|
2021-07-13 11:12:03 +03:00
|
|
|
{map[string]interface{}{}, true},
|
2021-07-09 16:56:01 +03:00
|
|
|
{nil, true},
|
|
|
|
|
|
|
|
|
|
{'a', false},
|
|
|
|
|
{1, false},
|
|
|
|
|
{3.14, false},
|
|
|
|
|
{" test ", false},
|
|
|
|
|
{[]string{"a"}, false},
|
|
|
|
|
{[...]string{"a"}, false},
|
|
|
|
|
{[2]string{"a", "b"}, false},
|
|
|
|
|
{[]int{1, 2}, false},
|
|
|
|
|
{[...]int{1, 2}, false},
|
|
|
|
|
{struct{ a string }{"a"}, false},
|
|
|
|
|
{&struct{ a string }{"a"}, false},
|
|
|
|
|
{[]struct{ a string }{{"b"}, {"b"}}, false},
|
2021-07-13 11:12:03 +03:00
|
|
|
{map[string]interface{}{"a": 13}, false},
|
2021-07-09 16:56:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for index, testCase := range testCases {
|
|
|
|
|
t.Run(fmt.Sprintf("%v # %d", testCase[0], index), func(t *testing.T) {
|
|
|
|
|
assert.Equal(t, testCase[1], IsEmpty(testCase[0]))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-12 17:20:01 +03:00
|
|
|
|
2021-07-13 11:12:03 +03:00
|
|
|
func TestVariadicIsEmpty(t *testing.T) {
|
|
|
|
|
testVariadicIsEmpty := func(expected bool, value ...interface{}) {
|
|
|
|
|
t.Run(fmt.Sprintf("%v", value), func(testCase *testing.T) {
|
|
|
|
|
assert.Equal(testCase, expected, IsEmpty(value...))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testVariadicIsEmpty(false, [2]int{1, 2}, [0]int{})
|
|
|
|
|
testVariadicIsEmpty(false, [0]int{}, [2]int{1, 2})
|
|
|
|
|
testVariadicIsEmpty(false, [0]int{}, " abc ")
|
|
|
|
|
testVariadicIsEmpty(false, [0]int{}, []string{}, 123)
|
|
|
|
|
testVariadicIsEmpty(false, [0]int{}, []string{}, []string{"a"})
|
|
|
|
|
testVariadicIsEmpty(false, [0]int{}, map[string]int{"a": 123}, map[string]interface{}{"b": "c"})
|
2021-07-12 17:20:01 +03:00
|
|
|
|
2021-07-13 11:12:03 +03:00
|
|
|
testVariadicIsEmpty(true, [0]int{}, "")
|
|
|
|
|
testVariadicIsEmpty(true, [0]int{}, []string{})
|
|
|
|
|
testVariadicIsEmpty(true, [0]int{}, []string{}, 0)
|
2021-07-12 17:20:01 +03:00
|
|
|
}
|