cast/cast_test.go

1435 lines
38 KiB
Go
Raw Permalink Normal View History

2014-04-05 09:21:52 +04:00
// Copyright © 2014 Steve Francia <spf@spf13.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package cast
import (
"fmt"
2014-12-09 16:17:55 +03:00
"html/template"
"path"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2014-04-05 09:21:52 +04:00
)
func TestToUintE(t *testing.T) {
tests := []struct {
input interface{}
expect uint
iserr bool
}{
{int(8), 8, false},
{int8(8), 8, false},
{int16(8), 8, false},
{int32(8), 8, false},
{int64(8), 8, false},
{uint(8), 8, false},
{uint8(8), 8, false},
{uint16(8), 8, false},
{uint32(8), 8, false},
{uint64(8), 8, false},
{float32(8.31), 8, false},
{float64(8.31), 8, false},
{true, 1, false},
{false, 0, false},
{"8", 8, false},
{nil, 0, false},
// errors
{int(-8), 0, true},
{int8(-8), 0, true},
{int16(-8), 0, true},
{int32(-8), 0, true},
{int64(-8), 0, true},
{float32(-8.31), 0, true},
{float64(-8.31), 0, true},
{"-8", 0, true},
{"test", 0, true},
{testing.T{}, 0, true},
}
for i, test := range tests {
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
v, err := ToUintE(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 = ToUint(test.input)
assert.Equal(t, test.expect, v, errmsg)
}
}
func TestToUint64E(t *testing.T) {
tests := []struct {
input interface{}
expect uint64
iserr bool
}{
{int(8), 8, false},
{int8(8), 8, false},
{int16(8), 8, false},
{int32(8), 8, false},
{int64(8), 8, false},
{uint(8), 8, false},
{uint8(8), 8, false},
{uint16(8), 8, false},
{uint32(8), 8, false},
{uint64(8), 8, false},
{float32(8.31), 8, false},
{float64(8.31), 8, false},
{true, 1, false},
{false, 0, false},
{"8", 8, false},
{nil, 0, false},
// errors
{int(-8), 0, true},
{int8(-8), 0, true},
{int16(-8), 0, true},
{int32(-8), 0, true},
{int64(-8), 0, true},
{float32(-8.31), 0, true},
{float64(-8.31), 0, true},
{"-8", 0, true},
{"test", 0, true},
{testing.T{}, 0, true},
}
for i, test := range tests {
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
v, err := ToUint64E(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 = ToUint64(test.input)
assert.Equal(t, test.expect, v, errmsg)
}
}
func TestToUint32E(t *testing.T) {
tests := []struct {
input interface{}
expect uint32
iserr bool
}{
{int(8), 8, false},
{int8(8), 8, false},
{int16(8), 8, false},
{int32(8), 8, false},
{int64(8), 8, false},
{uint(8), 8, false},
{uint8(8), 8, false},
{uint16(8), 8, false},
{uint32(8), 8, false},
{uint64(8), 8, false},
{float32(8.31), 8, false},
{float64(8.31), 8, false},
{true, 1, false},
{false, 0, false},
{"8", 8, false},
{nil, 0, false},
{int(-8), 0, true},
{int8(-8), 0, true},
{int16(-8), 0, true},
{int32(-8), 0, true},
{int64(-8), 0, true},
{float32(-8.31), 0, true},
{float64(-8.31), 0, true},
{"-8", 0, true},
// errors
{"test", 0, true},
{testing.T{}, 0, true},
}
for i, test := range tests {
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
v, err := ToUint32E(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 = ToUint32(test.input)
assert.Equal(t, test.expect, v, errmsg)
}
}
func TestToUint16E(t *testing.T) {
tests := []struct {
input interface{}
expect uint16
iserr bool
}{
{int(8), 8, false},
{int8(8), 8, false},
{int16(8), 8, false},
{int32(8), 8, false},
{int64(8), 8, false},
{uint(8), 8, false},
{uint8(8), 8, false},
{uint16(8), 8, false},
{uint32(8), 8, false},
{uint64(8), 8, false},
{float32(8.31), 8, false},
{float64(8.31), 8, false},
{true, 1, false},
{false, 0, false},
{"8", 8, false},
{nil, 0, false},
// errors
{int(-8), 0, true},
{int8(-8), 0, true},
{int16(-8), 0, true},
{int32(-8), 0, true},
{int64(-8), 0, true},
{float32(-8.31), 0, true},
{float64(-8.31), 0, true},
{"-8", 0, true},
{"test", 0, true},
{testing.T{}, 0, true},
}
for i, test := range tests {
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
v, err := ToUint16E(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 = ToUint16(test.input)
assert.Equal(t, test.expect, v, errmsg)
}
}
func TestToUint8E(t *testing.T) {
tests := []struct {
input interface{}
expect uint8
iserr bool
}{
{int(8), 8, false},
{int8(8), 8, false},
{int16(8), 8, false},
{int32(8), 8, false},
{int64(8), 8, false},
{uint(8), 8, false},
{uint8(8), 8, false},
{uint16(8), 8, false},
{uint32(8), 8, false},
{uint64(8), 8, false},
{float32(8.31), 8, false},
{float64(8.31), 8, false},
{true, 1, false},
{false, 0, false},
{"8", 8, false},
{nil, 0, false},
// errors
{int(-8), 0, true},
{int8(-8), 0, true},
{int16(-8), 0, true},
{int32(-8), 0, true},
{int64(-8), 0, true},
{float32(-8.31), 0, true},
{float64(-8.31), 0, true},
{"-8", 0, true},
{"test", 0, true},
{testing.T{}, 0, true},
}
for i, test := range tests {
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
v, err := ToUint8E(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 = ToUint8(test.input)
assert.Equal(t, test.expect, v, errmsg)
}
}
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},
{uint(8), 8, false},
{uint8(8), 8, false},
{uint16(8), 8, false},
{uint32(8), 8, false},
{uint64(8), 8, false},
{float32(8.31), 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 := ToIntE(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 = ToInt(test.input)
assert.Equal(t, test.expect, v, errmsg)
}
2014-04-05 09:21:52 +04:00
}
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},
{uint(8), 8, false},
{uint8(8), 8, false},
{uint16(8), 8, false},
{uint32(8), 8, false},
{uint64(8), 8, false},
{float32(8.31), 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 := ToInt64E(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 = ToInt64(test.input)
assert.Equal(t, test.expect, v, errmsg)
}
2016-01-28 21:41:34 +03:00
}
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},
{uint(8), 8, false},
{uint8(8), 8, false},
{uint16(8), 8, false},
{uint32(8), 8, false},
{uint64(8), 8, false},
{float32(8.31), 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)
}
2017-02-10 10:00:46 +03:00
}
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},
{uint(8), 8, false},
{uint8(8), 8, false},
{uint16(8), 8, false},
{uint32(8), 8, false},
{uint64(8), 8, false},
{float32(8.31), 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)
}
2017-02-10 10:00:46 +03:00
}
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},
{uint(8), 8, false},
{uint8(8), 8, false},
{uint16(8), 8, false},
{uint32(8), 8, false},
{uint64(8), 8, false},
{float32(8.31), 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)
}
2017-02-10 10:00:46 +03:00
}
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},
{uint(8), 8, false},
{uint8(8), 8, false},
{uint16(8), 8, false},
{uint32(8), 8, false},
{uint64(8), 8, false},
{float32(8), 8, false},
{float64(8.31), 8.31, false},
{"8", 8, false},
{true, 1, false},
{false, 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 := 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)
}
2014-04-07 19:43:25 +04:00
}
func TestToFloat32E(t *testing.T) {
tests := []struct {
input interface{}
expect float32
iserr bool
}{
{int(8), 8, false},
{int8(8), 8, false},
{int16(8), 8, false},
{int32(8), 8, false},
{int64(8), 8, false},
{uint(8), 8, false},
{uint8(8), 8, false},
{uint16(8), 8, false},
{uint32(8), 8, false},
{uint64(8), 8, false},
{float32(8.31), 8.31, false},
{float64(8.31), 8.31, false},
{"8", 8, false},
{true, 1, false},
{false, 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 := ToFloat32E(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 = ToFloat32(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},
{int8(8), "8", false},
{int16(8), "8", false},
{int32(8), "8", false},
{int64(8), "8", false},
{uint(8), "8", false},
{uint8(8), "8", false},
{uint16(8), "8", false},
{uint32(8), "8", false},
{uint64(8), "8", false},
{float32(8.31), "8.31", 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 {
val string
}
func (x foo) String() string {
return x.val
}
func TestStringerToString(t *testing.T) {
var x foo
x.val = "bar"
assert.Equal(t, "bar", ToString(x))
}
type fu struct {
val string
}
func (x fu) Error() string {
return x.val
}
func TestErrorToString(t *testing.T) {
var x fu
x.val = "bar"
assert.Equal(t, "bar", ToString(x))
}
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"}
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"}
// ToStringMapStringSlice inputs/outputs
2017-03-03 15:53:30 +03:00
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"}}
2017-03-03 15:53:30 +03:00
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"}}
2017-03-03 15:53:30 +03:00
var stringMapStringSliceMultiple = 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 stringMapStringSliceSingle = map[string][]string{"key 1": {"value 1"}, "key 2": {"value 2"}, "key 3": {"value 3"}}
var stringMapInterface1 = map[string]interface{}{"key 1": []string{"value 1"}, "key 2": []string{"value 2"}}
2017-03-03 15:53:30 +03:00
var stringMapInterfaceResult1 = map[string][]string{"key 1": {"value 1"}, "key 2": {"value 2"}}
var jsonStringMapString = `{"key 1": "value 1", "key 2": "value 2"}`
var jsonStringMapStringArray = `{"key 1": ["value 1"], "key 2": ["value 2", "value 3"]}`
var jsonStringMapStringArrayResult = map[string][]string{"key 1": {"value 1"}, "key 2": {"value 2", "value 3"}}
type Key struct {
k string
}
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},
{jsonStringMapStringArray, jsonStringMapStringArrayResult, 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
{jsonStringMapString, nil, true},
{"", nil, true},
}
for i, test := range tests {
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
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)
}
2014-04-05 09:21:52 +04:00
}
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},
{`{"tag": "tags", "group": "groups"}`, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
{`{"tag": "tags", "group": true}`, map[string]interface{}{"tag": "tags", "group": true}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{"", 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)
}
2015-02-24 13:58:16 +03:00
}
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},
{`{"v1": true, "v2": false}`, map[string]bool{"v1": true, "v2": false}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{"", 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 TestToStringMapIntE(t *testing.T) {
tests := []struct {
input interface{}
expect map[string]int
iserr bool
}{
{map[interface{}]interface{}{"v1": 1, "v2": 222}, map[string]int{"v1": 1, "v2": 222}, false},
{map[string]interface{}{"v1": 342, "v2": 5141}, map[string]int{"v1": 342, "v2": 5141}, false},
{map[string]int{"v1": 33, "v2": 88}, map[string]int{"v1": 33, "v2": 88}, false},
{map[string]int32{"v1": int32(33), "v2": int32(88)}, map[string]int{"v1": 33, "v2": 88}, false},
{map[string]uint16{"v1": uint16(33), "v2": uint16(88)}, map[string]int{"v1": 33, "v2": 88}, false},
{map[string]float64{"v1": float64(8.22), "v2": float64(43.32)}, map[string]int{"v1": 8, "v2": 43}, false},
{`{"v1": 67, "v2": 56}`, map[string]int{"v1": 67, "v2": 56}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{"", nil, true},
}
for i, test := range tests {
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
v, err := ToStringMapIntE(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 = ToStringMapInt(test.input)
assert.Equal(t, test.expect, v, errmsg)
}
}
func TestToStringMapInt64E(t *testing.T) {
tests := []struct {
input interface{}
expect map[string]int64
iserr bool
}{
{map[interface{}]interface{}{"v1": int32(8), "v2": int32(888)}, map[string]int64{"v1": int64(8), "v2": int64(888)}, false},
{map[string]interface{}{"v1": int64(45), "v2": int64(67)}, map[string]int64{"v1": 45, "v2": 67}, false},
{map[string]int64{"v1": 33, "v2": 88}, map[string]int64{"v1": 33, "v2": 88}, false},
{map[string]int{"v1": 33, "v2": 88}, map[string]int64{"v1": 33, "v2": 88}, false},
{map[string]int32{"v1": int32(33), "v2": int32(88)}, map[string]int64{"v1": 33, "v2": 88}, false},
{map[string]uint16{"v1": uint16(33), "v2": uint16(88)}, map[string]int64{"v1": 33, "v2": 88}, false},
{map[string]float64{"v1": float64(8.22), "v2": float64(43.32)}, map[string]int64{"v1": 8, "v2": 43}, false},
2018-10-25 01:49:29 +03:00
{`{"v1": 67, "v2": 56}`, map[string]int64{"v1": 67, "v2": 56}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{"", nil, true},
}
for i, test := range tests {
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
v, err := ToStringMapInt64E(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 = ToStringMapInt64(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"}
var jsonString = `{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}`
var invalidJsonString = `{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"`
var emptyString = ""
tests := []struct {
input interface{}
expect map[string]string
iserr bool
}{
{stringMapString, stringMapString, false},
{stringMapInterface, stringMapString, false},
{interfaceMapString, stringMapString, false},
{interfaceMapInterface, stringMapString, false},
{jsonString, stringMapString, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{invalidJsonString, nil, true},
{emptyString, 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)
}
}
2017-04-13 11:50:28 +03:00
func TestToDurationSliceE(t *testing.T) {
tests := []struct {
input interface{}
expect []time.Duration
iserr bool
}{
{[]string{"1s", "1m"}, []time.Duration{time.Second, time.Minute}, false},
{[]int{1, 2}, []time.Duration{1, 2}, false},
{[]interface{}{1, 3}, []time.Duration{1, 3}, false},
{[]time.Duration{1, 3}, []time.Duration{1, 3}, false},
2017-04-13 11:50:28 +03:00
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{[]string{"invalid"}, nil, true},
2017-04-13 11:50:28 +03:00
}
for i, test := range tests {
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
v, err := ToDurationSliceE(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 = ToDurationSlice(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) {
for i := 0; i < b.N; i++ {
if !ToBool(true) {
b.Fatal("ToBool returned false")
}
}
}
func TestIndirectPointers(t *testing.T) {
x := 13
y := &x
z := &y
assert.Equal(t, ToInt(y), 13)
assert.Equal(t, ToInt(z), 13)
}
func TestToTime(t *testing.T) {
tests := []struct {
input interface{}
expect time.Time
iserr bool
2016-12-24 20:25:03 +03:00
}{
{"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
{"2018-10-21T23:21:29+0200", time.Date(2018, 10, 21, 21, 21, 29, 0, time.UTC), false}, // RFC3339 without timezone hh:mm colon
{"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-0000", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false}, // RFC3339 without T or timezone hh:mm colon
{"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},
{uint(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, time.UTC), false},
{uint64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
{uint32(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 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)
2016-12-24 20:25:03 +03:00
}
}
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},
{uint(5), td, false},
{uint64(5), td, false},
{uint32(5), td, false},
{uint16(5), td, false},
{uint8(5), td, false},
{float64(5), td, false},
{float32(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)
}
}
func TestToTimeWithTimezones(t *testing.T) {
est, err := time.LoadLocation("EST")
require.NoError(t, err)
irn, err := time.LoadLocation("Iran")
require.NoError(t, err)
require.NoError(t, err)
// Test same local time in different timezones
utc2016 := time.Date(2016, time.January, 1, 3, 1, 0, 0, time.UTC)
est2016 := time.Date(2016, time.January, 1, 3, 1, 0, 0, est)
irn2016 := time.Date(2016, time.January, 1, 3, 1, 0, 0, irn)
loc2016 := time.Date(2016, time.January, 1, 3, 1, 0, 0, time.Local)
for i, format := range timeFormats {
format := format
if format.typ == timeFormatShort {
continue
}
nameBase := fmt.Sprintf("%d;timeFormatType=%d;%s", i, format.typ, format.format)
t.Run(path.Join(nameBase), func(t *testing.T) {
est2016str := est2016.Format(format.format)
loc2016str := loc2016.Format(format.format)
t.Run("without default location", func(t *testing.T) {
assert := require.New(t)
converted, err := ToTimeE(est2016str)
assert.NoError(err)
if format.hasNumericTimezone() {
assertTimeEqual(t, est2016, converted)
assertLocationEqual(t, est, converted.Location())
} else {
assertTimeEqual(t, utc2016, converted)
assertLocationEqual(t, time.UTC, converted.Location())
}
})
t.Run("local timezone without a default location", func(t *testing.T) {
assert := require.New(t)
converted, err := ToTimeE(loc2016str)
assert.NoError(err)
if format.hasAnyTimezone() {
// Local timezone strings can be either named or numeric and
// time.Parse connects the dots.
assertTimeEqual(t, loc2016, converted)
assertLocationEqual(t, time.Local, converted.Location())
} else {
assertTimeEqual(t, utc2016, converted)
assertLocationEqual(t, time.UTC, converted.Location())
}
})
t.Run("nil default location", func(t *testing.T) {
assert := require.New(t)
converted, err := ToTimeInDefaultLocationE(est2016str, nil)
assert.NoError(err)
if format.hasNumericTimezone() {
assertTimeEqual(t, est2016, converted)
assertLocationEqual(t, est, converted.Location())
} else {
assertTimeEqual(t, utc2016, converted)
assertLocationEqual(t, time.UTC, converted.Location())
}
})
t.Run("default location not UTC", func(t *testing.T) {
assert := require.New(t)
converted, err := ToTimeInDefaultLocationE(est2016str, irn)
assert.NoError(err)
if format.hasNumericTimezone() {
assertTimeEqual(t, est2016, converted)
assertLocationEqual(t, est, converted.Location())
} else {
assertTimeEqual(t, irn2016, converted)
assertLocationEqual(t, irn, converted.Location())
}
})
t.Run("time in the local timezone default location not UTC", func(t *testing.T) {
assert := require.New(t)
converted, err := ToTimeInDefaultLocationE(loc2016str, irn)
assert.NoError(err)
if format.hasNumericTimezone() {
assertTimeEqual(t, loc2016, converted)
assertLocationEqual(t, time.Local, converted.Location())
} else {
assertTimeEqual(t, irn2016, converted)
assertLocationEqual(t, irn, converted.Location())
}
})
})
}
}
func assertTimeEqual(t *testing.T, expected, actual time.Time) {
t.Helper()
require.True(t, expected.Equal(actual), fmt.Sprintf("expected\n%s\ngot\n%s", expected, actual))
format := "2006-01-02 15:04:05.999999999 -0700"
require.Equal(t, expected.Format(format), actual.Format(format))
}
func assertLocationEqual(t *testing.T, expected, actual *time.Location) {
t.Helper()
require.True(t, locationEqual(expected, actual), fmt.Sprintf("Expected location '%s', got '%s'", expected, actual))
}
func locationEqual(a, b *time.Location) bool {
// A note about comparing time.Locations:
// - can't only compare pointers
// - can't compare loc.String() because locations with the same
// name can have different offsets
// - can't use reflect.DeepEqual because time.Location has internal
// caches
if a == b {
return true
} else if a == nil || b == nil {
return false
}
// Check if they're equal by parsing times with a format that doesn't
// include a timezone, which will interpret it as being a local time in
// the given zone, and comparing the resulting local times.
tA, err := time.ParseInLocation("2006-01-02", "2016-01-01", a)
if err != nil {
return false
}
tB, err := time.ParseInLocation("2006-01-02", "2016-01-01", b)
if err != nil {
return false
}
return tA.Equal(tB)
}