forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeconv_test.go
137 lines (121 loc) · 3.08 KB
/
typeconv_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
package main
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestBool(t *testing.T) {
ty := &TypeConv{}
assert.False(t, ty.Bool(""))
assert.False(t, ty.Bool("asdf"))
assert.False(t, ty.Bool("1234"))
assert.False(t, ty.Bool("False"))
assert.False(t, ty.Bool("0"))
assert.False(t, ty.Bool("false"))
assert.False(t, ty.Bool("F"))
assert.False(t, ty.Bool("f"))
assert.True(t, ty.Bool("true"))
assert.True(t, ty.Bool("True"))
assert.True(t, ty.Bool("t"))
assert.True(t, ty.Bool("T"))
assert.True(t, ty.Bool("1"))
}
func TestUnmarshalObj(t *testing.T) {
ty := new(TypeConv)
expected := map[string]interface{}{
"foo": "bar",
"one": 1.0,
"true": true,
}
test := func(actual map[string]interface{}) {
assert.Equal(t, expected["foo"], actual["foo"])
assert.Equal(t, expected["one"], actual["one"])
assert.Equal(t, expected["true"], actual["true"])
}
test(ty.JSON(`{"foo":"bar","one":1.0,"true":true}`))
test(ty.YAML(`foo: bar
one: 1.0
true: true
`))
}
func TestUnmarshalArray(t *testing.T) {
ty := new(TypeConv)
expected := []string{"foo", "bar"}
test := func(actual []interface{}) {
assert.Equal(t, expected[0], actual[0])
assert.Equal(t, expected[1], actual[1])
}
test(ty.JSONArray(`["foo","bar"]`))
test(ty.YAMLArray(`
- foo
- bar
`))
}
func TestToJSON(t *testing.T) {
ty := new(TypeConv)
expected := `{"down":{"the":{"rabbit":{"hole":true}}},"foo":"bar","one":1,"true":true}`
in := map[string]interface{}{
"foo": "bar",
"one": 1,
"true": true,
"down": map[string]interface{}{
"the": map[string]interface{}{
"rabbit": map[string]interface{}{
"hole": true,
},
},
},
}
assert.Equal(t, expected, ty.ToJSON(in))
}
func TestToYAML(t *testing.T) {
ty := new(TypeConv)
expected := `d: 2006-01-02T15:04:05.999999999-07:00
foo: bar
? |-
multi
line
key
: hello: world
one: 1
"true": true
`
mst, _ := time.LoadLocation("MST")
in := map[string]interface{}{
"foo": "bar",
"one": 1,
"true": true,
`multi
line
key`: map[string]interface{}{
"hello": "world",
},
"d": time.Date(2006, time.January, 2, 15, 4, 5, 999999999, mst),
}
assert.Equal(t, expected, ty.ToYAML(in))
}
func TestSlice(t *testing.T) {
ty := new(TypeConv)
expected := []string{"foo", "bar"}
actual := ty.Slice("foo", "bar")
assert.Equal(t, expected[0], actual[0])
assert.Equal(t, expected[1], actual[1])
}
func TestJoin(t *testing.T) {
ty := new(TypeConv)
assert.Equal(t, "foo,bar", ty.Join([]interface{}{"foo", "bar"}, ","))
assert.Equal(t, "foo,\nbar", ty.Join([]interface{}{"foo", "bar"}, ",\n"))
// Join handles all kinds of scalar types too...
assert.Equal(t, "42-18446744073709551615", ty.Join([]interface{}{42, uint64(18446744073709551615)}, "-"))
assert.Equal(t, "1,,true,3.14,foo,nil", ty.Join([]interface{}{1, "", true, 3.14, "foo", nil}, ","))
// and best-effort with weird types
assert.Equal(t, "[foo],bar", ty.Join([]interface{}{[]string{"foo"}, "bar"}, ","))
}
func TestHas(t *testing.T) {
ty := new(TypeConv)
in := map[string]interface{}{
"foo": "bar",
}
assert.True(t, ty.Has(in, "foo"))
assert.False(t, ty.Has(in, "bar"))
}