aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/encoding/xml/value_test.go
blob: cbce967bd91a688b8edfcd3aac28dad2bd420ff5 (plain) (blame)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package xml

import (
	"bytes"
	"fmt"
	"math"
	"math/big"
	"strconv"
	"testing"
)

var (
	oneInt   = new(big.Int).SetInt64(1)
	oneFloat = new(big.Float).SetFloat64(1.0)
)

func TestValue(t *testing.T) {
	nested := StartElement{Name: Name{Local: "nested"}}

	cases := map[string]struct {
		setter   func(Value)
		expected string
	}{
		"string value": {
			setter: func(value Value) {
				value.String("foo")
			},
			expected: `foo`,
		},
		"string escaped": {
			setter: func(value Value) {
				value.String("{\"foo\":\"bar\"}")
			},
			expected: fmt.Sprintf("{%sfoo%s:%sbar%s}", escQuot, escQuot, escQuot, escQuot),
		},
		"integer": {
			setter: func(value Value) {
				value.Long(1024)
			},
			expected: `1024`,
		},
		"float": {
			setter: func(value Value) {
				value.Double(1e20)
			},
			expected: `100000000000000000000`,
		},
		"float exponent component": {
			setter: func(value Value) {
				value.Double(3e22)
			},
			expected: `3e+22`,
		},
		"boolean true": {
			setter: func(value Value) {
				value.Boolean(true)
			},
			expected: `true`,
		},
		"boolean false": {
			setter: func(value Value) {
				value.Boolean(false)
			},
			expected: `false`,
		},
		"encode bytes": {
			setter: func(value Value) {
				value.Base64EncodeBytes([]byte("foo bar"))
			},
			expected: `Zm9vIGJhcg==`,
		},
		"encode bytes nil": {
			setter: func(value Value) {
				value.Base64EncodeBytes(nil)
			},
			expected: ``,
		},
		"object": {
			setter: func(value Value) {
				defer value.Close()
				value.MemberElement(nested).String("value")
			},
			expected: `<nested>value</nested>`,
		},
		"null": {
			setter: func(value Value) {
				value.Close()
			},
			expected: ``,
		},
		"nullWithRoot": {
			setter: func(value Value) {
				defer value.Close()
				o := value.MemberElement(nested)
				defer o.Close()
			},
			expected: `<nested></nested>`,
		},
		"write text": {
			setter: func(value Value) {
				defer value.Close()
				o := value.MemberElement(nested)
				o.Write([]byte(`{"nested":"value"}`), false)
			},
			expected: `<nested>{"nested":"value"}</nested>`,
		},
		"write escaped text": {
			setter: func(value Value) {
				defer value.Close()
				o := value.MemberElement(nested)
				o.Write([]byte(`{"nested":"value"}`), true)
			},
			expected: fmt.Sprintf("<nested>{%snested%s:%svalue%s}</nested>", escQuot, escQuot, escQuot, escQuot),
		},
		"bigInteger": {
			setter: func(value Value) {
				v := new(big.Int).SetInt64(math.MaxInt64)
				value.BigInteger(v.Sub(v, oneInt))
			},
			expected: strconv.FormatInt(math.MaxInt64-1, 10),
		},
		"bigInteger > int64": {
			setter: func(value Value) {
				v := new(big.Int).SetInt64(math.MaxInt64)
				value.BigInteger(v.Add(v, oneInt))
			},
			expected: "9223372036854775808",
		},
		"bigInteger < int64": {
			setter: func(value Value) {
				v := new(big.Int).SetInt64(math.MinInt64)
				value.BigInteger(v.Sub(v, oneInt))
			},
			expected: "-9223372036854775809",
		},
		"bigFloat": {
			setter: func(value Value) {
				v := new(big.Float).SetFloat64(math.MaxFloat64)
				value.BigDecimal(v.Sub(v, oneFloat))
			},
			expected: strconv.FormatFloat(math.MaxFloat64-1, 'e', -1, 64),
		},
		"bigFloat fits in int64": {
			setter: func(value Value) {
				v := new(big.Float).SetInt64(math.MaxInt64)
				value.BigDecimal(v)
			},
			expected: "9223372036854775807",
		},
	}
	scratch := make([]byte, 64)

	for name, tt := range cases {
		t.Run(name, func(t *testing.T) {
			b := bytes.NewBuffer(nil)
			root := StartElement{Name: Name{Local: "root"}}
			value := newValue(b, &scratch, root)
			tt.setter(value)

			if e, a := []byte("<root>"+tt.expected+"</root>"), b.Bytes(); bytes.Compare(e, a) != 0 {
				t.Errorf("expected %+q, but got %+q", e, a)
			}
		})
	}
}

func TestWrappedValue(t *testing.T) {
	buffer := bytes.NewBuffer(nil)
	scratch := make([]byte, 64)

	func() {
		root := StartElement{Name: Name{Local: "root"}}
		object := newValue(buffer, &scratch, root)
		defer object.Close()

		foo := StartElement{Name: Name{Local: "foo"}}
		faz := StartElement{Name: Name{Local: "faz"}}

		object.MemberElement(foo).String("bar")
		object.MemberElement(faz).String("baz")
	}()

	e := []byte(`<root><foo>bar</foo><faz>baz</faz></root>`)
	if a := buffer.Bytes(); bytes.Compare(e, a) != 0 {
		t.Errorf("expected %+q, but got %+q", e, a)
	}
}

func TestWrappedValueWithNameSpaceAndAttributes(t *testing.T) {
	buffer := bytes.NewBuffer(nil)
	scratch := make([]byte, 64)

	func() {
		root := StartElement{Name: Name{Local: "root"}}
		object := newValue(buffer, &scratch, root)
		defer object.Close()

		foo := StartElement{Name: Name{Local: "foo"}, Attr: []Attr{
			NewNamespaceAttribute("newspace", "https://endpoint.com"),
			NewAttribute("attrName", "attrValue"),
		}}
		faz := StartElement{Name: Name{Local: "faz"}}

		object.MemberElement(foo).String("bar")
		object.MemberElement(faz).String("baz")
	}()

	e := []byte(`<root><foo xmlns:newspace="https://endpoint.com" attrName="attrValue">bar</foo><faz>baz</faz></root>`)
	if a := buffer.Bytes(); bytes.Compare(e, a) != 0 {
		t.Errorf("expected %+q, but got %+q", e, a)
	}
}