forked from basgys/goxml2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
112 lines (96 loc) · 3.02 KB
/
parse_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
package xml2json_test
import (
"encoding/json"
"strings"
"testing"
"github.com/stretchr/testify/suite"
xml2json "github.com/txix-open/goxml2json"
)
func TestParse_Suite(t *testing.T) {
t.Parallel()
suite.Run(t, &TestParse{})
}
type TestParse struct {
suite.Suite
}
func (t *TestParse) SetupSuite() {}
type Product struct {
ID []int `json:"id"`
Price []float64 `json:"price"`
Deleted []bool `json:"deleted"`
Nullable []any `json:"nullable"`
}
type StringProduct struct {
ID []string `json:"id"`
Price []string `json:"price"`
Deleted []string `json:"deleted"`
Nullable []string `json:"nullable"`
}
type MixedProduct struct {
ID []string `json:"id"`
Price []float64 `json:"price"`
Deleted []string `json:"deleted"`
Nullable []string `json:"nullable"`
}
const (
productString = `
<?xml version="1.0" encoding="UTF-8"?>
<id>42</id>
<price>13.32</price>
<deleted>true</deleted>
<nullable>null</nullable>
`
)
func (t *TestParse) TestAllJSTypeParsing() {
converter := xml2json.NewConverter(
xml2json.WithAttrPrefix("-"),
xml2json.WithContentPrefix("#"),
xml2json.WithTypeConverter(xml2json.Bool, xml2json.Int, xml2json.Float, xml2json.Null),
xml2json.AllAttrToArray(),
)
xml := strings.NewReader(productString)
jsBuf, err := converter.Convert(xml)
t.NoError(err, "could not parse test xml")
product := Product{}
err = json.Unmarshal(jsBuf.Bytes(), &product)
t.NoError(err, "could not unmarshal test json")
t.Equal(42, product.ID[0], "ID should match")
t.Equal(13.32, product.Price[0], "price should match")
t.Equal(true, product.Deleted[0], "deleted should match")
t.Equal(nil, product.Nullable[0], "nullable should match")
}
func (t *TestParse) TestStringParsing() {
converter := xml2json.NewConverter(
xml2json.WithAttrPrefix("-"),
xml2json.WithContentPrefix("#"),
xml2json.AllAttrToArray(),
)
xml := strings.NewReader(productString)
jsBuf, err := converter.Convert(xml)
t.NoError(err, "could not parse test xml")
product := StringProduct{}
err = json.Unmarshal(jsBuf.Bytes(), &product)
t.NoError(err, "could not unmarshal test json")
t.Equal("42", product.ID[0], "ID should match")
t.Equal("13.32", product.Price[0], "price should match")
t.Equal("true", product.Deleted[0], "deleted should match")
t.Equal("null", product.Nullable[0], "nullable should match")
}
func (t *TestParse) TestMixedParsing() {
converter := xml2json.NewConverter(
xml2json.WithAttrPrefix("-"),
xml2json.WithContentPrefix("#"),
xml2json.WithTypeConverter(xml2json.Float),
xml2json.AllAttrToArray(),
)
xml := strings.NewReader(productString)
jsBuf, err := converter.Convert(xml)
t.NoError(err, "could not parse test xml")
product := MixedProduct{}
err = json.Unmarshal(jsBuf.Bytes(), &product)
t.NoError(err, "could not unmarshal test json")
t.Equal("42", product.ID[0], "ID should match")
t.Equal(13.32, product.Price[0], "price should match")
t.Equal("true", product.Deleted[0], "deleted should match")
t.Equal("null", product.Nullable[0], "nullable should match")
}