forked from binhonglee/kdlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal_test.go
58 lines (46 loc) · 1.17 KB
/
marshal_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
package kdl
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestStructIntoChildren(t *testing.T) {
s := struct {
Foo string
Bar string `kdl:"baz"`
}{}
c := marshalContext{}
doc := NewDocument()
structToChildren(&c, reflect.ValueOf(s), &doc)
assert.Len(t, doc.Nodes, 2)
assert.EqualValues(t, "foo", doc.Nodes[0].Name)
assert.EqualValues(t, "baz", doc.Nodes[1].Name)
}
func TestStructIntoNode(t *testing.T) {
s := struct {
Foo int `kdl:"-"`
Bar int
Baz float32 `kdl:",argument"`
Quox string `kdl:"hello,property"`
}{}
c := marshalContext{}
n := NewNode("")
structIntoNode(&c, reflect.ValueOf(s), &n)
if assert.Len(t, n.Args, 1) {
assert.EqualValues(t, TypeFloat, n.Args[0].Type)
}
if assert.Len(t, n.Props, 2) {
assert.Equal(t, TypeInteger, n.GetProp("bar").Type)
assert.Equal(t, TypeString, n.GetProp("hello").Type)
}
}
func TestValueConverts(t *testing.T) {
n := 3
v, err := valueToKDLValue(reflect.ValueOf(n))
assert.NoError(t, err)
assert.EqualValues(t, n, v.IntegerValue().Int64())
s := "foo"
v, err = valueToKDLValue(reflect.ValueOf(s))
assert.NoError(t, err)
assert.EqualValues(t, s, v.StringValue())
}