cast/cast_test.go

154 lines
6.2 KiB
Go
Raw Normal View History

2014-04-05 13:21:52 +08: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 (
"testing"
2014-12-09 21:17:55 +08:00
"html/template"
"github.com/stretchr/testify/assert"
2014-04-05 13:21:52 +08:00
)
func TestToInt(t *testing.T) {
var eight interface{} = 8
assert.Equal(t, ToInt(8), 8)
assert.Equal(t, ToInt(8.31), 8)
2014-04-05 13:21:52 +08: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)
}
2014-04-07 23:43:25 +08: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)
}
func TestToString(t *testing.T) {
var foo interface{} = "one more time"
assert.Equal(t, ToString(8), "8")
assert.Equal(t, ToString(8.12), "8.12")
assert.Equal(t, ToString([]byte("one time")), "one time")
2014-12-09 21:17:55 +08:00
assert.Equal(t, ToString(template.HTML("one time")), "one time")
2015-06-08 00:35:28 +08:00
assert.Equal(t, ToString(template.URL("http://somehost.foo")), "http://somehost.foo")
assert.Equal(t, ToString(foo), "one more time")
assert.Equal(t, ToString(nil), "")
}
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 13:21:52 +08:00
func TestMaps(t *testing.T) {
var taxonomies = map[interface{}]interface{}{"tag": "tags", "group": "groups"}
var stringMapBool = map[interface{}]interface{}{"v1": true, "v2": false}
// 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"}}
2014-04-05 13:21:52 +08:00
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)
2014-04-05 13:21:52 +08:00
}
2015-02-24 18:58:16 +08: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}))
assert.Equal(t, []int{2, 3}, ToIntSlice([]string{"2", "3"}))
assert.Equal(t, []int{2, 3}, ToIntSlice([2]string{"2", "3"}))
2015-02-24 18:58:16 +08: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)
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)
}
func TestIndirectPointers(t *testing.T) {
x := 13
y := &x
z := &y
assert.Equal(t, ToInt(y), 13)
assert.Equal(t, ToInt(z), 13)
}