forked from erinpentecost/gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coerce_test.go
55 lines (44 loc) · 2.62 KB
/
coerce_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
package gou
import (
"testing"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type aliasedStringSlice []string
type aliasedIntSlice []int
type aliasedFloatSlice []float64
func TestCoerce(t *testing.T) {
data := map[string]interface{}{
"int": 4,
"float": 45.3,
"string": "22",
"stringf": "22.2",
"boolt": true,
"emptystring": "",
"stringslice": []string{"one", "two", "three"},
"aliasedstringslice": aliasedStringSlice{"one", "two", "three"},
"mongostringslice": primitive.A{"one", "two", "three"},
"aliasedintslice": aliasedIntSlice{1, 2, 3},
"aliasedfloatslice": aliasedFloatSlice{1.1, 2.2, 3.3},
}
assert.True(t, CoerceStringShort(data["int"]) == "4", "get int as string")
assert.True(t, CoerceStringShort(data["float"]) == "45.3", "get float as string: %v", data["float"])
assert.True(t, CoerceStringShort(data["string"]) == "22", "get string as string: %v", data["string"])
assert.True(t, CoerceStringShort(data["stringf"]) == "22.2", "get stringf as string: %v", data["stringf"])
assert.Equal(t, "true", CoerceStringShort(data["boolt"]))
assert.True(t, CoerceIntShort(data["int"]) == 4, "get int as int: %v", data["int"])
assert.EqualValues(t, []int{1, 2, 3}, CoerceInts(data["aliasedintslice"]))
assert.True(t, CoerceIntShort(data["float"]) == 45, "get float as int: %v", data["float"])
assert.True(t, CoerceIntShort(data["string"]) == 22, "get string as int: %v", data["string"])
assert.True(t, CoerceIntShort(data["stringf"]) == 22, "get stringf as int: %v", data["stringf"])
assert.Equal(t, 0, len(CoerceStrings(data["emptystring"])), "get emptystring as []string: %v", data["emptystring"])
assert.Equal(t, []string{"22"}, CoerceStrings(data["string"]), "get string as []string: %v", data["string"])
assert.Equal(t, []string{"4"}, CoerceStrings(data["int"]), "get int as []string: %v", data["int"])
assert.EqualValues(t, []string{"one", "two", "three"}, CoerceStrings(data["stringslice"]))
assert.EqualValues(t, []string{"one", "two", "three"}, CoerceStrings(data["aliasedstringslice"]))
assert.EqualValues(t, []string{"one", "two", "three"}, CoerceStrings(data["mongostringslice"]))
assert.Equal(t, []string{"4"}, CoerceStrings(data["int"]), "get int as []string: %v", data["int"])
assert.Equal(t, []float64{float64(4)}, CoerceFloats(data["int"]), "get int as []float64: %v", data["int"])
assert.Equal(t, []float64{float64(45.3)}, CoerceFloats(data["float"]), "get float as []float64: %v", data["float"])
assert.EqualValues(t, []float64{1.1, 2.2, 3.3}, CoerceFloats(data["aliasedfloatslice"]))
}