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 (
|
2014-12-09 16:17:55 +03:00
|
|
|
"html/template"
|
2015-11-22 23:21:13 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-01-01 07:48:55 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2014-04-05 09:21:52 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestToInt(t *testing.T) {
|
|
|
|
var eight interface{} = 8
|
|
|
|
assert.Equal(t, ToInt(8), 8)
|
2014-04-05 23:18:23 +04:00
|
|
|
assert.Equal(t, ToInt(8.31), 8)
|
2014-04-05 09:21:52 +04:00
|
|
|
assert.Equal(t, ToInt("8"), 8)
|
|
|
|
assert.Equal(t, ToInt(true), 1)
|
|
|
|
assert.Equal(t, ToInt(false), 0)
|
|
|
|
assert.Equal(t, ToInt(eight), 8)
|
|
|
|
}
|
|
|
|
|
2016-01-28 21:41:34 +03:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2014-04-07 19:43:25 +04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-04-05 23:18:23 +04:00
|
|
|
func TestToString(t *testing.T) {
|
|
|
|
var foo interface{} = "one more time"
|
|
|
|
assert.Equal(t, ToString(8), "8")
|
2016-01-28 21:33:21 +03:00
|
|
|
assert.Equal(t, ToString(int64(16)), "16")
|
2014-04-05 23:18:23 +04:00
|
|
|
assert.Equal(t, ToString(8.12), "8.12")
|
|
|
|
assert.Equal(t, ToString([]byte("one time")), "one time")
|
2014-12-09 16:17:55 +03:00
|
|
|
assert.Equal(t, ToString(template.HTML("one time")), "one time")
|
2015-06-07 19:35:28 +03:00
|
|
|
assert.Equal(t, ToString(template.URL("http://somehost.foo")), "http://somehost.foo")
|
2016-03-03 22:15:55 +03:00
|
|
|
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")
|
2014-04-05 23:18:23 +04:00
|
|
|
assert.Equal(t, ToString(foo), "one more time")
|
|
|
|
assert.Equal(t, ToString(nil), "")
|
2015-11-26 09:42:19 +03:00
|
|
|
assert.Equal(t, ToString(true), "true")
|
|
|
|
assert.Equal(t, ToString(false), "false")
|
2014-04-05 23:18:23 +04:00
|
|
|
}
|
|
|
|
|
2015-01-01 07:48:55 +03:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2014-04-05 09:21:52 +04:00
|
|
|
func TestMaps(t *testing.T) {
|
|
|
|
var taxonomies = map[interface{}]interface{}{"tag": "tags", "group": "groups"}
|
2014-11-06 13:32:02 +03:00
|
|
|
var stringMapBool = map[interface{}]interface{}{"v1": true, "v2": false}
|
2015-07-31 21:44:36 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
var stringMapStringSlice = map[string][]string{"key 1": []string{"value 1", "value 2", "value 3"}, "key 2": []string{"value 1", "value 2", "value 3"}, "key 3": []string{"value 1", "value 2", "value 3"}}
|
|
|
|
var stringMapInterfaceSlice = 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": []string{"value", "1"}, "key 2": []string{"value", "2"}, "key 3": []string{"value", "3"}}
|
|
|
|
var interfaceMapStringSlice = map[interface{}][]string{"key 1": []string{"value 1", "value 2", "value 3"}, "key 2": []string{"value 1", "value 2", "value 3"}, "key 3": []string{"value 1", "value 2", "value 3"}}
|
|
|
|
var interfaceMapInterfaceSlice = map[interface{}][]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 stringMapStringSliceMultiple = map[string][]string{"key 1": []string{"value 1", "value 2", "value 3"}, "key 2": []string{"value 1", "value 2", "value 3"}, "key 3": []string{"value 1", "value 2", "value 3"}}
|
|
|
|
var stringMapStringSliceSingle = map[string][]string{"key 1": []string{"value 1"}, "key 2": []string{"value 2"}, "key 3": []string{"value 3"}}
|
|
|
|
|
2016-09-19 23:22:46 +03:00
|
|
|
var stringMapInterface1 = map[string]interface{}{"key 1": []string{"value 1"}, "key 2": []string{"value 2"}}
|
|
|
|
var stringMapInterfaceResult1 = map[string][]string{"key 1": []string{"value 1"}, "key 2": []string{"value 2"}}
|
|
|
|
|
2014-04-05 09:21:52 +04:00
|
|
|
assert.Equal(t, ToStringMap(taxonomies), map[string]interface{}{"tag": "tags", "group": "groups"})
|
2014-11-06 13:32:02 +03:00
|
|
|
assert.Equal(t, ToStringMapBool(stringMapBool), map[string]bool{"v1": true, "v2": false})
|
2015-07-31 21:44:36 +03:00
|
|
|
|
|
|
|
// 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)
|
2016-09-19 23:22:46 +03:00
|
|
|
assert.Equal(t, ToStringMapStringSlice(stringMapInterface1), stringMapInterfaceResult1)
|
2014-04-05 09:21:52 +04:00
|
|
|
}
|
2014-08-31 05:16:09 +04:00
|
|
|
|
2015-02-24 13:58:16 +03:00
|
|
|
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}))
|
2015-02-25 16:50:44 +03:00
|
|
|
assert.Equal(t, []int{2, 3}, ToIntSlice([]string{"2", "3"}))
|
|
|
|
assert.Equal(t, []int{2, 3}, ToIntSlice([2]string{"2", "3"}))
|
2016-09-19 23:26:41 +03:00
|
|
|
assert.Equal(t, []bool{true, false, true}, ToBoolSlice([]bool{true, false, true}))
|
|
|
|
assert.Equal(t, []bool{true, false, true}, ToBoolSlice([]interface{}{true, false, true}))
|
2016-09-26 11:25:08 +03:00
|
|
|
assert.Equal(t, []bool{true, false, true}, ToBoolSlice([]int{1, 0, 1}))
|
|
|
|
assert.Equal(t, []bool{true, false, true}, ToBoolSlice([]string{"true", "false", "true"}))
|
2015-02-24 13:58:16 +03:00
|
|
|
}
|
|
|
|
|
2014-08-31 05:16:09 +04:00
|
|
|
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)
|
2014-11-06 13:32:02 +03:00
|
|
|
|
2014-08-31 05:16:09 +04:00
|
|
|
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)
|
2014-11-19 21:08:49 +03:00
|
|
|
assert.Equal(t, ToBool(-1), true)
|
2014-08-31 05:16:09 +04:00
|
|
|
}
|
2015-01-01 08:23:46 +03:00
|
|
|
|
2016-09-26 11:25:08 +03:00
|
|
|
func BenchmarkTooBool(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if !ToBool(true) {
|
|
|
|
b.Fatal("ToBool returned false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 08:23:46 +03:00
|
|
|
func TestIndirectPointers(t *testing.T) {
|
|
|
|
x := 13
|
|
|
|
y := &x
|
|
|
|
z := &y
|
|
|
|
|
|
|
|
assert.Equal(t, ToInt(y), 13)
|
|
|
|
assert.Equal(t, ToInt(z), 13)
|
|
|
|
}
|
2015-11-22 23:21:13 +03:00
|
|
|
|
|
|
|
func TestToDuration(t *testing.T) {
|
2016-09-19 23:24:24 +03:00
|
|
|
var td time.Duration = 5
|
|
|
|
tests := []struct {
|
|
|
|
input interface{}
|
|
|
|
expected time.Duration
|
|
|
|
}{
|
|
|
|
{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},
|
|
|
|
}
|
|
|
|
for _, v := range tests {
|
|
|
|
assert.Equal(t, v.expected, ToDuration(v.input))
|
|
|
|
}
|
2015-11-22 23:21:13 +03:00
|
|
|
}
|
2016-11-11 21:01:40 +03:00
|
|
|
|
|
|
|
func TestStringToDate(t *testing.T) {
|
|
|
|
dateString := "2016-12-01 15:05:04"
|
|
|
|
tv, _ := time.Parse("2006-01-02 15:04:05", dateString)
|
|
|
|
assert.Equal(t, ToTime(dateString), tv)
|
|
|
|
}
|