-
Notifications
You must be signed in to change notification settings - Fork 75
/
value_test.go
143 lines (117 loc) · 3.54 KB
/
value_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package objx_test
import (
"testing"
"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
)
func TestStringTypeString(t *testing.T) {
m := objx.Map{
"string": "foo",
}
assert.Equal(t, "foo", m.Get("string").String())
}
func TestStringTypeBool(t *testing.T) {
m := objx.Map{
"bool": true,
}
assert.Equal(t, "true", m.Get("bool").String())
}
func TestStringTypeInt(t *testing.T) {
m := objx.Map{
"int": int(1),
"int8": int8(8),
"int16": int16(16),
"int32": int32(32),
"int64": int64(64),
}
assert.Equal(t, "1", m.Get("int").String())
assert.Equal(t, "8", m.Get("int8").String())
assert.Equal(t, "16", m.Get("int16").String())
assert.Equal(t, "32", m.Get("int32").String())
assert.Equal(t, "64", m.Get("int64").String())
}
func TestStringTypeUint(t *testing.T) {
m := objx.Map{
"uint": uint(1),
"uint8": uint8(8),
"uint16": uint16(16),
"uint32": uint32(32),
"uint64": uint64(64),
}
assert.Equal(t, "1", m.Get("uint").String())
assert.Equal(t, "8", m.Get("uint8").String())
assert.Equal(t, "16", m.Get("uint16").String())
assert.Equal(t, "32", m.Get("uint32").String())
assert.Equal(t, "64", m.Get("uint64").String())
}
func TestStringTypeFloat(t *testing.T) {
m := objx.Map{
"float32": float32(32.32),
"float64": float64(64.64),
}
assert.Equal(t, "32.32", m.Get("float32").String())
assert.Equal(t, "64.64", m.Get("float64").String())
}
func TestStringTypeOther(t *testing.T) {
m := objx.Map{
"other": []string{"foo", "bar"},
"nilValue": nil,
}
assert.Equal(t, "[]string{\"foo\", \"bar\"}", m.Get("other").String())
assert.Equal(t, "", m.Get("nilValue").String())
}
func TestStringSliceTypeString(t *testing.T) {
m := objx.Map{
"string": []string{"foo", "bar"},
}
assert.Equal(t, []string{"foo", "bar"}, m.Get("string").StringSlice())
}
func TestStringSliceTypeBool(t *testing.T) {
m := objx.Map{
"bool": []bool{true, false},
}
assert.Equal(t, []string{"true", "false"}, m.Get("bool").StringSlice())
}
func TestStringSliceTypeInt(t *testing.T) {
m := objx.Map{
"int": []int{1, 2},
"int8": []int8{8, 9},
"int16": []int16{16, 17},
"int32": []int32{32, 33},
"int64": []int64{64, 65},
}
assert.Equal(t, []string{"1", "2"}, m.Get("int").StringSlice())
assert.Equal(t, []string{"8", "9"}, m.Get("int8").StringSlice())
assert.Equal(t, []string{"16", "17"}, m.Get("int16").StringSlice())
assert.Equal(t, []string{"32", "33"}, m.Get("int32").StringSlice())
assert.Equal(t, []string{"64", "65"}, m.Get("int64").StringSlice())
}
func TestStringSliceTypeUint(t *testing.T) {
m := objx.Map{
"uint": []uint{1, 2},
"uint8": []uint8{8, 9},
"uint16": []uint16{16, 17},
"uint32": []uint32{32, 33},
"uint64": []uint64{64, 65},
}
assert.Equal(t, []string{"1", "2"}, m.Get("uint").StringSlice())
assert.Equal(t, []string{"8", "9"}, m.Get("uint8").StringSlice())
assert.Equal(t, []string{"16", "17"}, m.Get("uint16").StringSlice())
assert.Equal(t, []string{"32", "33"}, m.Get("uint32").StringSlice())
assert.Equal(t, []string{"64", "65"}, m.Get("uint64").StringSlice())
}
func TestStringSliceTypeFloat(t *testing.T) {
m := objx.Map{
"float32": []float32{32.32, 33.33},
"float64": []float64{64.64, 65.65},
}
assert.Equal(t, []string{"32.32", "33.33"}, m.Get("float32").StringSlice())
assert.Equal(t, []string{"64.64", "65.65"}, m.Get("float64").StringSlice())
}
func TestStringSliceTypeOther(t *testing.T) {
m := objx.Map{
"other": "foo",
}
assert.Equal(t, []string{}, m.Get("other").StringSlice())
assert.Equal(t, []string{"bar"}, m.Get("other").StringSlice([]string{"bar"}))
}