forked from clbanning/mxj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leafnode_test.go
140 lines (127 loc) · 2.69 KB
/
leafnode_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
package mxj
import (
"fmt"
"testing"
)
func TestLNHeader(t *testing.T) {
fmt.Println("\n---------------- leafnode_test.go ...")
}
func TestLeafNodes(t *testing.T) {
json1 := []byte(`{
"friends": [
{
"skills": [
44, 12
]
}
]
}`)
json2 := []byte(`{
"friends":
{
"skills": [
44, 12
]
}
}`)
m, _ := NewMapJson(json1)
ln := m.LeafNodes()
fmt.Println("\njson1-LeafNodes:")
for _, v := range ln {
fmt.Printf("%#v\n", v)
}
p := m.LeafPaths()
fmt.Println("\njson1-LeafPaths:")
for _, v := range p {
fmt.Printf("%#v\n", v)
}
m, _ = NewMapJson(json2)
ln = m.LeafNodes()
fmt.Println("\njson2-LeafNodes:")
for _, v := range ln {
fmt.Printf("%#v\n", v)
}
v := m.LeafValues()
fmt.Println("\njson1-LeafValues:")
for _, v := range v {
fmt.Printf("%#v\n", v)
}
json3 := []byte(`{ "a":"list", "of":["data", "of", 3, "types", true]}`)
m, _ = NewMapJson(json3)
ln = m.LeafNodes()
fmt.Println("\njson3-LeafNodes:")
for _, v := range ln {
fmt.Printf("%#v\n", v)
}
v = m.LeafValues()
fmt.Println("\njson3-LeafValues:")
for _, v := range v {
fmt.Printf("%#v\n", v)
}
p = m.LeafPaths()
fmt.Println("\njson3-LeafPaths:")
for _, v := range p {
fmt.Printf("%#v\n", v)
}
xmldata2 := []byte(`
<doc>
<item num="2" color="blue">Item 2 is blue</item>
<item num="3" color="green">
<arm side="left" length="3.5"/>
<arm side="right" length="3.6"/>
</item>
</doc>`)
m, err := NewMapXml(xmldata2)
if err != nil {
t.Fatal(err.Error())
}
fmt.Println("\nxml2data2-LeafValues:")
ln = m.LeafNodes()
for _, v := range ln {
fmt.Printf("%#v\n", v)
}
fmt.Println("\nxml2data2-LeafValues(NoAttributes):")
ln = m.LeafNodes(NoAttributes)
for _, v := range ln {
fmt.Printf("%#v\n", v)
}
// no-hyphen
PrependAttrWithHyphen(false)
m, err = NewMapXml(xmldata2)
if err != nil {
t.Fatal(err.Error())
}
fmt.Println("\nno-hyphen-xml2data2-LeafValues:")
ln = m.LeafNodes()
for _, v := range ln {
fmt.Printf("%#v\n", v)
}
fmt.Println("\nno-hyphen-xml2data2-LeafValues(NoAttributes):")
ln = m.LeafNodes(NoAttributes)
for _, v := range ln {
fmt.Printf("%#v\n", v)
}
// restore default
PrependAttrWithHyphen(true)
}
func TestLeafDotNotation(t *testing.T) {
xmldata2 := []byte(`
<doc>
<item num="2" color="blue">Item 2 is blue</item>
<item num="3" color="green">
<arm side="left" length="3.5"/>
<arm side="right" length="3.6"/>
</item>
</doc>`)
m, err := NewMapXml(xmldata2)
if err != nil {
t.Fatal(err.Error())
}
fmt.Println("\nDotNotation-LeafValues:")
LeafUseDotNotation()
defer LeafUseDotNotation()
ln := m.LeafNodes()
for _, v := range ln {
fmt.Printf("%#v\n", v)
}
}