-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapto_test.go
56 lines (48 loc) · 1.2 KB
/
mapto_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
package mapto
import (
"time"
"testing"
"github.com/magiconair/properties/assert"
)
type TestCommonStruct struct {
Dur time.Duration
Val string
Num int
}
type TestInterface interface{
String() string
}
type TestNestedStruct struct {
NestedVal string
}
func (tns * TestNestedStruct) String() string {
return "TestNestedStruct:"+tns.NestedVal
}
type TestStruct struct {
TestCommonStruct `mapstructure:",squash"`
Dur time.Duration
Val2 string
Nested TestInterface
}
func TestDecode(t *testing.T) {
m := map[string]interface{} {
"dur": "2s",
"val": "testvalue",
"num": 42,
"val2": "testvalue2",
"nested": map[string]interface{}{ "nestedVal": "testNestedValue", "@type": "teststruct"},
}
RegisterConstructor("teststruct", StructConstructor(&TestNestedStruct{}))
teststruct := TestStruct{}
err := Decode(m, &teststruct)
if err != nil {
t.Error(err)
return
}
assert.Equal(t, 2 * time.Second, teststruct.Dur)
assert.Equal(t, 2 * time.Second, teststruct.TestCommonStruct.Dur)
assert.Equal(t, "testvalue", teststruct.Val)
assert.Equal(t, 42, teststruct.Num)
assert.Equal(t, "testvalue2", teststruct.Val2)
assert.Equal(t, "TestNestedStruct:testNestedValue", teststruct.Nested.String())
}