forked from basgys/goxml2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder_test.go
85 lines (73 loc) · 2.7 KB
/
decoder_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
package xml2json
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var s = `<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.0.2">
<bounds minlat="54.0889580" minlon="12.2487570" maxlat="54.0913900" maxlon="12.2524800"/>
<node id="298884269" lat="54.0901746" lon="12.2482632" user="SvenHRO" uid="46882" visible="true" version="1" changeset="676636" timestamp="2008-09-21T21:37:45Z"/>
<node id="261728686" lat="54.0906309" lon="12.2441924" user="PikoWinter" uid="36744" visible="true" version="1" changeset="323878" timestamp="2008-05-03T13:39:23Z"/>
<node id="1831881213" version="1" changeset="12370172" lat="54.0900666" lon="12.2539381" user="lafkor" uid="75625" visible="true" timestamp="2012-07-20T09:43:19Z">
<tag k="name" v="Neu Broderstorf"/>
<tag k="traffic_sign" v="city_limit"/>
</node>
<foo>bar</foo>
</osm>`
// TestDecode ensures that decode does not return any errors (not that useful)
func TestDecode(t *testing.T) {
assert := assert.New(t)
// Decode XML document
root := &Node{}
var err error
var dec *Decoder
dec = NewDecoder(strings.NewReader(s))
err = dec.Decode(root)
assert.NoError(err)
dec.SetAttributePrefix("test")
dec.SetContentPrefix("test2")
err = dec.DecodeWithCustomPrefixes(root, "test3", "test4")
assert.NoError(err)
}
func TestDecodeWithoutDefaultsAndExcludeAttributes(t *testing.T) {
assert := assert.New(t)
// Decode XML document
root := &Node{}
var err error
var dec *Decoder
dec = NewDecoder(strings.NewReader(s), WithAttrPrefix(""), ExcludeAttributes([]string{"version", "generator"}))
err = dec.Decode(root)
assert.NoError(err)
// Check that some attribute`s name has no prefix and has expected value
assert.Exactly(root.Children["osm"][0].Children["bounds"][0].Children["minlat"][0].Data, "54.0889580")
// Check that some attributes are not present
_, exists := root.Children["osm"][0].Children["version"]
assert.False(exists)
_, exists = root.Children["osm"][0].Children["generator"]
assert.False(exists)
}
func TestTrim(t *testing.T) {
table := []struct {
in string
expected string
}{
{in: "foo", expected: "foo"},
{in: " foo", expected: "foo"},
{in: "foo ", expected: "foo"},
{in: " foo ", expected: "foo"},
{in: " foo ", expected: "foo"},
{in: "foo bar", expected: "foo bar"},
{in: "\n\tfoo\n\t", expected: "foo"},
{in: "\n\tfoo\n\tbar\n\t", expected: "foo\n\tbar"},
{in: "", expected: ""},
{in: "\n", expected: ""},
{in: "\n\v", expected: ""},
{in: "ending with ä", expected: "ending with ä"},
{in: "ä and ä", expected: "ä and ä"},
}
for _, scenario := range table {
got := trimNonGraphic(scenario.in)
assert.Equal(t, scenario.expected, got)
}
}