forked from spf13/cast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcast_test.go
117 lines (97 loc) · 3.01 KB
/
cast_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright © 2014 Steve Francia <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package cast
import (
"testing"
"html/template"
"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 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")
assert.Equal(t, ToString(template.HTML("one time")), "one time")
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))
}
func TestMaps(t *testing.T) {
var taxonomies = map[interface{}]interface{}{"tag": "tags", "group": "groups"}
var stringMapBool = map[interface{}]interface{}{"v1": true, "v2": false}
assert.Equal(t, ToStringMap(taxonomies), map[string]interface{}{"tag": "tags", "group": "groups"})
assert.Equal(t, ToStringMapBool(stringMapBool), map[string]bool{"v1": true, "v2": false})
}
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"}))
}
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)
}