forked from mirror/cast
Refactor tests to get full coverage
Test coverage goes from 75.6% to 100%
This commit is contained in:
parent
4f1683a224
commit
df9611b942
2
Makefile
2
Makefile
|
@ -32,7 +32,7 @@ vet: ## Run go vet linter
|
|||
|
||||
test-cover-html: ## Generate test coverage report
|
||||
go test -coverprofile=coverage.out -covermode=count
|
||||
go tool cover -func=coverage-all.out
|
||||
go tool cover -func=coverage.out
|
||||
|
||||
help:
|
||||
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
||||
|
|
861
cast_test.go
861
cast_test.go
|
@ -6,6 +6,7 @@
|
|||
package cast
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -13,83 +14,283 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestToInt(t *testing.T) {
|
||||
var eight interface{} = 8
|
||||
assert.Equal(t, ToInt(8), 8)
|
||||
assert.Equal(t, ToInt(8.31), 8)
|
||||
assert.Equal(t, ToInt("8"), 8)
|
||||
assert.Equal(t, ToInt(true), 1)
|
||||
assert.Equal(t, ToInt(false), 0)
|
||||
assert.Equal(t, ToInt(eight), 8)
|
||||
func TestToIntE(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect int
|
||||
iserr bool
|
||||
}{
|
||||
{int(8), 8, false},
|
||||
{int8(8), 8, false},
|
||||
{int16(8), 8, false},
|
||||
{int32(8), 8, false},
|
||||
{int64(8), 8, false},
|
||||
{float64(8.31), 8, false},
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
func TestToInt64(t *testing.T) {
|
||||
var eight interface{} = 8
|
||||
assert.Equal(t, ToInt64(int64(8)), int64(8))
|
||||
assert.Equal(t, ToInt64(8), int64(8))
|
||||
assert.Equal(t, ToInt64(8.31), int64(8))
|
||||
assert.Equal(t, ToInt64("8"), int64(8))
|
||||
assert.Equal(t, ToInt64(true), int64(1))
|
||||
assert.Equal(t, ToInt64(false), int64(0))
|
||||
assert.Equal(t, ToInt64(eight), int64(8))
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToIntE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
func TestToInt32(t *testing.T) {
|
||||
var eight interface{} = 8
|
||||
assert.Equal(t, ToInt32(int32(8)), int32(8))
|
||||
assert.Equal(t, ToInt32(8), int32(8))
|
||||
assert.Equal(t, ToInt32(8.31), int32(8))
|
||||
assert.Equal(t, ToInt32("8"), int32(8))
|
||||
assert.Equal(t, ToInt32(true), int32(1))
|
||||
assert.Equal(t, ToInt32(false), int32(0))
|
||||
assert.Equal(t, ToInt32(eight), int32(8))
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToInt(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToInt16(t *testing.T) {
|
||||
var eight interface{} = 8
|
||||
assert.Equal(t, ToInt16(int16(8)), int16(8))
|
||||
assert.Equal(t, ToInt16(8), int16(8))
|
||||
assert.Equal(t, ToInt16(8.31), int16(8))
|
||||
assert.Equal(t, ToInt16("8"), int16(8))
|
||||
assert.Equal(t, ToInt16(true), int16(1))
|
||||
assert.Equal(t, ToInt16(false), int16(0))
|
||||
assert.Equal(t, ToInt16(eight), int16(8))
|
||||
func TestToInt64E(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect int64
|
||||
iserr bool
|
||||
}{
|
||||
{int(8), 8, false},
|
||||
{int8(8), 8, false},
|
||||
{int16(8), 8, false},
|
||||
{int32(8), 8, false},
|
||||
{int64(8), 8, false},
|
||||
{float64(8.31), 8, false},
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
func TestToInt8(t *testing.T) {
|
||||
var eight interface{} = 8
|
||||
assert.Equal(t, ToInt8(int8(8)), int8(8))
|
||||
assert.Equal(t, ToInt8(8), int8(8))
|
||||
assert.Equal(t, ToInt8(8.31), int8(8))
|
||||
assert.Equal(t, ToInt8("8"), int8(8))
|
||||
assert.Equal(t, ToInt8(true), int8(1))
|
||||
assert.Equal(t, ToInt8(false), int8(0))
|
||||
assert.Equal(t, ToInt8(eight), int8(8))
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToInt64E(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
func TestToFloat64(t *testing.T) {
|
||||
var eight interface{} = 8
|
||||
assert.Equal(t, ToFloat64(8), 8.00)
|
||||
assert.Equal(t, ToFloat64(8.31), 8.31)
|
||||
assert.Equal(t, ToFloat64("8.31"), 8.31)
|
||||
assert.Equal(t, ToFloat64(eight), 8.0)
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToInt64(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToString(t *testing.T) {
|
||||
var foo interface{} = "one more time"
|
||||
assert.Equal(t, ToString(8), "8")
|
||||
assert.Equal(t, ToString(int64(16)), "16")
|
||||
assert.Equal(t, ToString(8.12), "8.12")
|
||||
assert.Equal(t, ToString([]byte("one time")), "one time")
|
||||
assert.Equal(t, ToString(template.HTML("one time")), "one time")
|
||||
assert.Equal(t, ToString(template.URL("http://somehost.foo")), "http://somehost.foo")
|
||||
assert.Equal(t, ToString(template.JS("(1+2)")), "(1+2)")
|
||||
assert.Equal(t, ToString(template.CSS("a")), "a")
|
||||
assert.Equal(t, ToString(template.HTMLAttr("a")), "a")
|
||||
assert.Equal(t, ToString(foo), "one more time")
|
||||
assert.Equal(t, ToString(nil), "")
|
||||
assert.Equal(t, ToString(true), "true")
|
||||
assert.Equal(t, ToString(false), "false")
|
||||
func TestToInt32E(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect int32
|
||||
iserr bool
|
||||
}{
|
||||
{int(8), 8, false},
|
||||
{int8(8), 8, false},
|
||||
{int16(8), 8, false},
|
||||
{int32(8), 8, false},
|
||||
{int64(8), 8, false},
|
||||
{float64(8.31), 8, false},
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToInt32E(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToInt32(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToInt16E(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect int16
|
||||
iserr bool
|
||||
}{
|
||||
{int(8), 8, false},
|
||||
{int8(8), 8, false},
|
||||
{int16(8), 8, false},
|
||||
{int32(8), 8, false},
|
||||
{int64(8), 8, false},
|
||||
{float64(8.31), 8, false},
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToInt16E(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToInt16(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToInt8E(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect int8
|
||||
iserr bool
|
||||
}{
|
||||
{int(8), 8, false},
|
||||
{int8(8), 8, false},
|
||||
{int16(8), 8, false},
|
||||
{int32(8), 8, false},
|
||||
{int64(8), 8, false},
|
||||
{float64(8.31), 8, false},
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToInt8E(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToInt8(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToFloat64E(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect float64
|
||||
iserr bool
|
||||
}{
|
||||
{int(8), 8, false},
|
||||
{int8(8), 8, false},
|
||||
{int16(8), 8, false},
|
||||
{int32(8), 8, false},
|
||||
{int64(8), 8, false},
|
||||
{float32(8), 8, false},
|
||||
{float64(8.31), 8.31, false},
|
||||
{"8", 8, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToFloat64E(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToFloat64(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToStringE(t *testing.T) {
|
||||
type Key struct {
|
||||
k string
|
||||
}
|
||||
key := &Key{"foo"}
|
||||
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect string
|
||||
iserr bool
|
||||
}{
|
||||
{int(8), "8", false},
|
||||
{int64(8), "8", false},
|
||||
{float64(8.31), "8.31", false},
|
||||
{true, "true", false},
|
||||
{false, "false", false},
|
||||
{nil, "", false},
|
||||
{[]byte("one time"), "one time", false},
|
||||
{"one more time", "one more time", false},
|
||||
{template.HTML("one time"), "one time", false},
|
||||
{template.URL("http://somehost.foo"), "http://somehost.foo", false},
|
||||
{template.JS("(1+2)"), "(1+2)", false},
|
||||
{template.CSS("a"), "a", false},
|
||||
{template.HTMLAttr("a"), "a", false},
|
||||
// errors
|
||||
{testing.T{}, "", true},
|
||||
{key, "", true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToStringE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToString(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
type foo struct {
|
||||
|
@ -101,7 +302,6 @@ func (x foo) String() string {
|
|||
}
|
||||
|
||||
func TestStringerToString(t *testing.T) {
|
||||
|
||||
var x foo
|
||||
x.val = "bar"
|
||||
assert.Equal(t, "bar", ToString(x))
|
||||
|
@ -121,10 +321,7 @@ func TestErrorToString(t *testing.T) {
|
|||
assert.Equal(t, "bar", ToString(x))
|
||||
}
|
||||
|
||||
func TestMaps(t *testing.T) {
|
||||
var taxonomies = map[interface{}]interface{}{"tag": "tags", "group": "groups"}
|
||||
var stringMapBool = map[interface{}]interface{}{"v1": true, "v2": false}
|
||||
|
||||
func TestStringMapStringSliceE(t *testing.T) {
|
||||
// ToStringMapString inputs/outputs
|
||||
var stringMapString = map[string]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
||||
var stringMapInterface = map[string]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
||||
|
@ -134,6 +331,7 @@ func TestMaps(t *testing.T) {
|
|||
// ToStringMapStringSlice inputs/outputs
|
||||
var stringMapStringSlice = map[string][]string{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
||||
var stringMapInterfaceSlice = map[string][]interface{}{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
||||
var stringMapInterfaceInterfaceSlice = map[string]interface{}{"key 1": []interface{}{"value 1", "value 2", "value 3"}, "key 2": []interface{}{"value 1", "value 2", "value 3"}, "key 3": []interface{}{"value 1", "value 2", "value 3"}}
|
||||
var stringMapStringSingleSliceFieldsResult = map[string][]string{"key 1": {"value", "1"}, "key 2": {"value", "2"}, "key 3": {"value", "3"}}
|
||||
var interfaceMapStringSlice = map[interface{}][]string{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
||||
var interfaceMapInterfaceSlice = map[interface{}][]interface{}{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
||||
|
@ -144,61 +342,329 @@ func TestMaps(t *testing.T) {
|
|||
var stringMapInterface1 = map[string]interface{}{"key 1": []string{"value 1"}, "key 2": []string{"value 2"}}
|
||||
var stringMapInterfaceResult1 = map[string][]string{"key 1": {"value 1"}, "key 2": {"value 2"}}
|
||||
|
||||
assert.Equal(t, ToStringMap(taxonomies), map[string]interface{}{"tag": "tags", "group": "groups"})
|
||||
assert.Equal(t, ToStringMapBool(stringMapBool), map[string]bool{"v1": true, "v2": false})
|
||||
|
||||
// ToStringMapString tests
|
||||
assert.Equal(t, ToStringMapString(stringMapString), stringMapString)
|
||||
assert.Equal(t, ToStringMapString(stringMapInterface), stringMapString)
|
||||
assert.Equal(t, ToStringMapString(interfaceMapString), stringMapString)
|
||||
assert.Equal(t, ToStringMapString(interfaceMapInterface), stringMapString)
|
||||
|
||||
// ToStringMapStringSlice tests
|
||||
assert.Equal(t, ToStringMapStringSlice(stringMapStringSlice), stringMapStringSlice)
|
||||
assert.Equal(t, ToStringMapStringSlice(stringMapInterfaceSlice), stringMapStringSlice)
|
||||
assert.Equal(t, ToStringMapStringSlice(stringMapStringSliceMultiple), stringMapStringSlice)
|
||||
assert.Equal(t, ToStringMapStringSlice(stringMapStringSliceMultiple), stringMapStringSlice)
|
||||
assert.Equal(t, ToStringMapStringSlice(stringMapString), stringMapStringSliceSingle)
|
||||
assert.Equal(t, ToStringMapStringSlice(stringMapInterface), stringMapStringSliceSingle)
|
||||
assert.Equal(t, ToStringMapStringSlice(interfaceMapStringSlice), stringMapStringSlice)
|
||||
assert.Equal(t, ToStringMapStringSlice(interfaceMapInterfaceSlice), stringMapStringSlice)
|
||||
assert.Equal(t, ToStringMapStringSlice(interfaceMapString), stringMapStringSingleSliceFieldsResult)
|
||||
assert.Equal(t, ToStringMapStringSlice(interfaceMapInterface), stringMapStringSingleSliceFieldsResult)
|
||||
assert.Equal(t, ToStringMapStringSlice(stringMapInterface1), stringMapInterfaceResult1)
|
||||
type Key struct {
|
||||
k string
|
||||
}
|
||||
|
||||
func TestSlices(t *testing.T) {
|
||||
assert.Equal(t, []string{"a", "b"}, ToStringSlice([]string{"a", "b"}))
|
||||
assert.Equal(t, []string{"1", "3"}, ToStringSlice([]interface{}{1, 3}))
|
||||
assert.Equal(t, []int{1, 3}, ToIntSlice([]int{1, 3}))
|
||||
assert.Equal(t, []int{1, 3}, ToIntSlice([]interface{}{1.2, 3.2}))
|
||||
assert.Equal(t, []int{2, 3}, ToIntSlice([]string{"2", "3"}))
|
||||
assert.Equal(t, []int{2, 3}, ToIntSlice([2]string{"2", "3"}))
|
||||
assert.Equal(t, []bool{true, false, true}, ToBoolSlice([]bool{true, false, true}))
|
||||
assert.Equal(t, []bool{true, false, true}, ToBoolSlice([]interface{}{true, false, true}))
|
||||
assert.Equal(t, []bool{true, false, true}, ToBoolSlice([]int{1, 0, 1}))
|
||||
assert.Equal(t, []bool{true, false, true}, ToBoolSlice([]string{"true", "false", "true"}))
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect map[string][]string
|
||||
iserr bool
|
||||
}{
|
||||
{stringMapStringSlice, stringMapStringSlice, false},
|
||||
{stringMapInterfaceSlice, stringMapStringSlice, false},
|
||||
{stringMapInterfaceInterfaceSlice, stringMapStringSlice, false},
|
||||
{stringMapStringSliceMultiple, stringMapStringSlice, false},
|
||||
{stringMapStringSliceMultiple, stringMapStringSlice, false},
|
||||
{stringMapString, stringMapStringSliceSingle, false},
|
||||
{stringMapInterface, stringMapStringSliceSingle, false},
|
||||
{stringMapInterface1, stringMapInterfaceResult1, false},
|
||||
{interfaceMapStringSlice, stringMapStringSlice, false},
|
||||
{interfaceMapInterfaceSlice, stringMapStringSlice, false},
|
||||
{interfaceMapString, stringMapStringSingleSliceFieldsResult, false},
|
||||
{interfaceMapInterface, stringMapStringSingleSliceFieldsResult, false},
|
||||
// errors
|
||||
{nil, nil, true},
|
||||
{testing.T{}, nil, true},
|
||||
{map[interface{}]interface{}{"foo": testing.T{}}, nil, true},
|
||||
{map[interface{}]interface{}{Key{"foo"}: "bar"}, nil, true}, // ToStringE(Key{"foo"}) should fail
|
||||
}
|
||||
|
||||
func TestToBool(t *testing.T) {
|
||||
assert.Equal(t, ToBool(0), false)
|
||||
assert.Equal(t, ToBool(nil), false)
|
||||
assert.Equal(t, ToBool("false"), false)
|
||||
assert.Equal(t, ToBool("FALSE"), false)
|
||||
assert.Equal(t, ToBool("False"), false)
|
||||
assert.Equal(t, ToBool("f"), false)
|
||||
assert.Equal(t, ToBool("F"), false)
|
||||
assert.Equal(t, ToBool(false), false)
|
||||
assert.Equal(t, ToBool("foo"), false)
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
assert.Equal(t, ToBool("true"), true)
|
||||
assert.Equal(t, ToBool("TRUE"), true)
|
||||
assert.Equal(t, ToBool("True"), true)
|
||||
assert.Equal(t, ToBool("t"), true)
|
||||
assert.Equal(t, ToBool("T"), true)
|
||||
assert.Equal(t, ToBool(1), true)
|
||||
assert.Equal(t, ToBool(true), true)
|
||||
assert.Equal(t, ToBool(-1), true)
|
||||
v, err := ToStringMapStringSliceE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToStringMapStringSlice(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToStringMapE(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect map[string]interface{}
|
||||
iserr bool
|
||||
}{
|
||||
{map[interface{}]interface{}{"tag": "tags", "group": "groups"}, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
|
||||
{map[string]interface{}{"tag": "tags", "group": "groups"}, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
|
||||
// errors
|
||||
{nil, nil, true},
|
||||
{testing.T{}, nil, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToStringMapE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToStringMap(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToStringMapBoolE(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect map[string]bool
|
||||
iserr bool
|
||||
}{
|
||||
{map[interface{}]interface{}{"v1": true, "v2": false}, map[string]bool{"v1": true, "v2": false}, false},
|
||||
{map[string]interface{}{"v1": true, "v2": false}, map[string]bool{"v1": true, "v2": false}, false},
|
||||
{map[string]bool{"v1": true, "v2": false}, map[string]bool{"v1": true, "v2": false}, false},
|
||||
// errors
|
||||
{nil, nil, true},
|
||||
{testing.T{}, nil, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToStringMapBoolE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToStringMapBool(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToStringMapStringE(t *testing.T) {
|
||||
var stringMapString = map[string]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
||||
var stringMapInterface = map[string]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
||||
var interfaceMapString = map[interface{}]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
||||
var interfaceMapInterface = map[interface{}]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
||||
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect map[string]string
|
||||
iserr bool
|
||||
}{
|
||||
{stringMapString, stringMapString, false},
|
||||
{stringMapInterface, stringMapString, false},
|
||||
{interfaceMapString, stringMapString, false},
|
||||
{interfaceMapInterface, stringMapString, false},
|
||||
// errors
|
||||
{nil, nil, true},
|
||||
{testing.T{}, nil, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToStringMapStringE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToStringMapString(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToBoolSliceE(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect []bool
|
||||
iserr bool
|
||||
}{
|
||||
{[]bool{true, false, true}, []bool{true, false, true}, false},
|
||||
{[]interface{}{true, false, true}, []bool{true, false, true}, false},
|
||||
{[]int{1, 0, 1}, []bool{true, false, true}, false},
|
||||
{[]string{"true", "false", "true"}, []bool{true, false, true}, false},
|
||||
// errors
|
||||
{nil, nil, true},
|
||||
{testing.T{}, nil, true},
|
||||
{[]string{"foo", "bar"}, nil, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToBoolSliceE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToBoolSlice(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToIntSliceE(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect []int
|
||||
iserr bool
|
||||
}{
|
||||
{[]int{1, 3}, []int{1, 3}, false},
|
||||
{[]interface{}{1.2, 3.2}, []int{1, 3}, false},
|
||||
{[]string{"2", "3"}, []int{2, 3}, false},
|
||||
{[2]string{"2", "3"}, []int{2, 3}, false},
|
||||
// errors
|
||||
{nil, nil, true},
|
||||
{testing.T{}, nil, true},
|
||||
{[]string{"foo", "bar"}, nil, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToIntSliceE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToIntSlice(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToSliceE(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect []interface{}
|
||||
iserr bool
|
||||
}{
|
||||
{[]interface{}{1, 3}, []interface{}{1, 3}, false},
|
||||
{[]map[string]interface{}{{"k1": 1}, {"k2": 2}}, []interface{}{map[string]interface{}{"k1": 1}, map[string]interface{}{"k2": 2}}, false},
|
||||
// errors
|
||||
{nil, nil, true},
|
||||
{testing.T{}, nil, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToSliceE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToSlice(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToStringSliceE(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect []string
|
||||
iserr bool
|
||||
}{
|
||||
{[]string{"a", "b"}, []string{"a", "b"}, false},
|
||||
{[]interface{}{1, 3}, []string{"1", "3"}, false},
|
||||
{interface{}(1), []string{"1"}, false},
|
||||
// errors
|
||||
{nil, nil, true},
|
||||
{testing.T{}, nil, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToStringSliceE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToStringSlice(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToBoolE(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect bool
|
||||
iserr bool
|
||||
}{
|
||||
{0, false, false},
|
||||
{nil, false, false},
|
||||
{"false", false, false},
|
||||
{"FALSE", false, false},
|
||||
{"False", false, false},
|
||||
{"f", false, false},
|
||||
{"F", false, false},
|
||||
{false, false, false},
|
||||
|
||||
{"true", true, false},
|
||||
{"TRUE", true, false},
|
||||
{"True", true, false},
|
||||
{"t", true, false},
|
||||
{"T", true, false},
|
||||
{1, true, false},
|
||||
{true, true, false},
|
||||
{-1, true, false},
|
||||
|
||||
// errors
|
||||
{"test", false, true},
|
||||
{testing.T{}, false, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToBoolE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToBool(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkTooBool(b *testing.B) {
|
||||
|
@ -218,69 +684,104 @@ func TestIndirectPointers(t *testing.T) {
|
|||
assert.Equal(t, ToInt(z), 13)
|
||||
}
|
||||
|
||||
func TestToTimeE(t *testing.T) {
|
||||
cases := []struct {
|
||||
input interface{}
|
||||
want time.Time
|
||||
}{
|
||||
{"2009-11-10 23:00:00 +0000 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)}, // Time.String()
|
||||
{"Tue Nov 10 23:00:00 2009", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)}, // ANSIC
|
||||
{"Tue Nov 10 23:00:00 UTC 2009", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)}, // UnixDate
|
||||
{"Tue Nov 10 23:00:00 +0000 2009", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)}, // RubyDate
|
||||
{"10 Nov 09 23:00 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)}, // RFC822
|
||||
{"10 Nov 09 23:00 +0000", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)}, // RFC822Z
|
||||
{"Tuesday, 10-Nov-09 23:00:00 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)}, // RFC850
|
||||
{"Tue, 10 Nov 2009 23:00:00 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)}, // RFC1123
|
||||
{"Tue, 10 Nov 2009 23:00:00 +0000", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)}, // RFC1123Z
|
||||
{"2009-11-10T23:00:00Z", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)}, // RFC3339
|
||||
{"2009-11-10T23:00:00Z", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)}, // RFC3339Nano
|
||||
{"11:00PM", time.Date(0, 1, 1, 23, 0, 0, 0, time.UTC)}, // Kitchen
|
||||
{"Nov 10 23:00:00", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC)}, // Stamp
|
||||
{"Nov 10 23:00:00.000", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC)}, // StampMilli
|
||||
{"Nov 10 23:00:00.000000", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC)}, // StampMicro
|
||||
{"Nov 10 23:00:00.000000000", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC)}, // StampNano
|
||||
{"2016-03-06 15:28:01-00:00", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC)}, // RFC3339 without T
|
||||
{"2016-03-06 15:28:01", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC)},
|
||||
{"2016-03-06 15:28:01 -0000", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC)},
|
||||
{"2016-03-06 15:28:01 -00:00", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC)},
|
||||
{"2006-01-02", time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC)},
|
||||
{"02 Jan 2006", time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC)},
|
||||
{1472574600, time.Date(2016, 8, 30, 16, 30, 0, 0, time.UTC)},
|
||||
{int(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, time.UTC)},
|
||||
{int32(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC)},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
v, err := ToTimeE(c.input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, v.UTC(), c.want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToDuration(t *testing.T) {
|
||||
var td time.Duration = 5
|
||||
func TestToTimeEE(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expected time.Duration
|
||||
expect time.Time
|
||||
iserr bool
|
||||
}{
|
||||
{time.Duration(5), td},
|
||||
{int64(5), td},
|
||||
{int32(5), td},
|
||||
{int16(5), td},
|
||||
{int8(5), td},
|
||||
{int(5), td},
|
||||
{float64(5), td},
|
||||
{float32(5), td},
|
||||
{string("5"), td},
|
||||
{string("5ns"), td},
|
||||
{string("5us"), time.Microsecond * td},
|
||||
{string("5µs"), time.Microsecond * td},
|
||||
{string("5ms"), time.Millisecond * td},
|
||||
{string("5s"), time.Second * td},
|
||||
{string("5m"), time.Minute * td},
|
||||
{string("5h"), time.Hour * td},
|
||||
{"2009-11-10 23:00:00 +0000 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // Time.String()
|
||||
{"Tue Nov 10 23:00:00 2009", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // ANSIC
|
||||
{"Tue Nov 10 23:00:00 UTC 2009", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // UnixDate
|
||||
{"Tue Nov 10 23:00:00 +0000 2009", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RubyDate
|
||||
{"10 Nov 09 23:00 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC822
|
||||
{"10 Nov 09 23:00 +0000", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC822Z
|
||||
{"Tuesday, 10-Nov-09 23:00:00 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC850
|
||||
{"Tue, 10 Nov 2009 23:00:00 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC1123
|
||||
{"Tue, 10 Nov 2009 23:00:00 +0000", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC1123Z
|
||||
{"2009-11-10T23:00:00Z", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC3339
|
||||
{"2009-11-10T23:00:00Z", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC3339Nano
|
||||
{"11:00PM", time.Date(0, 1, 1, 23, 0, 0, 0, time.UTC), false}, // Kitchen
|
||||
{"Nov 10 23:00:00", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC), false}, // Stamp
|
||||
{"Nov 10 23:00:00.000", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC), false}, // StampMilli
|
||||
{"Nov 10 23:00:00.000000", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC), false}, // StampMicro
|
||||
{"Nov 10 23:00:00.000000000", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC), false}, // StampNano
|
||||
{"2016-03-06 15:28:01-00:00", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false}, // RFC3339 without T
|
||||
{"2016-03-06 15:28:01", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false},
|
||||
{"2016-03-06 15:28:01 -0000", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false},
|
||||
{"2016-03-06 15:28:01 -00:00", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false},
|
||||
{"2006-01-02", time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC), false},
|
||||
{"02 Jan 2006", time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC), false},
|
||||
{1472574600, time.Date(2016, 8, 30, 16, 30, 0, 0, time.UTC), false},
|
||||
{int(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, time.UTC), false},
|
||||
{int64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
|
||||
{int32(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
|
||||
{time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
|
||||
// errors
|
||||
{"2006", time.Time{}, true},
|
||||
{testing.T{}, time.Time{}, true},
|
||||
}
|
||||
for _, v := range tests {
|
||||
assert.Equal(t, v.expected, ToDuration(v.input))
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToTimeE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v.UTC(), errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToTime(test.input)
|
||||
assert.Equal(t, test.expect, v.UTC(), errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToDurationE(t *testing.T) {
|
||||
var td time.Duration = 5
|
||||
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect time.Duration
|
||||
iserr bool
|
||||
}{
|
||||
{time.Duration(5), td, false},
|
||||
{int(5), td, false},
|
||||
{int64(5), td, false},
|
||||
{int32(5), td, false},
|
||||
{int16(5), td, false},
|
||||
{int8(5), td, false},
|
||||
{float64(5), td, false},
|
||||
{string("5"), td, false},
|
||||
{string("5ns"), td, false},
|
||||
{string("5us"), time.Microsecond * td, false},
|
||||
{string("5µs"), time.Microsecond * td, false},
|
||||
{string("5ms"), time.Millisecond * td, false},
|
||||
{string("5s"), time.Second * td, false},
|
||||
{string("5m"), time.Minute * td, false},
|
||||
{string("5h"), time.Hour * td, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToDurationE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToDuration(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue