aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/encoding/xml/map_test.go
blob: 022165bd6d5d03b7157692a29db1143adea4c314 (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
package xml

import (
	"bytes"
	"testing"
)

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

	func() {
		m := newMap(buffer, &scratch)

		key := StartElement{Name: Name{Local: "key"}}
		value := StartElement{Name: Name{Local: "value"}}

		// map entry
		e := m.Entry()
		e.MemberElement(key).String("example-key1")
		e.MemberElement(value).String("example1")
		e.Close()

		// map entry
		e = m.Entry()
		e.MemberElement(key).String("example-key2")
		e.MemberElement(value).String("example2")
		e.Close()

		// map entry
		e = m.Entry()
		e.MemberElement(key).String("example-key3")
		e.MemberElement(value).String("example3")
		e.Close()
	}()

	ex := []byte(`<entry><key>example-key1</key><value>example1</value></entry><entry><key>example-key2</key><value>example2</value></entry><entry><key>example-key3</key><value>example3</value></entry>`)
	if a := buffer.Bytes(); bytes.Compare(ex, a) != 0 {
		t.Errorf("expected %+q, but got %+q", ex, a)
	}
}

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

	func() {
		root := StartElement{Name: Name{Local: "flatMap"}}
		m := newFlattenedMap(buffer, &scratch, root)

		key := StartElement{Name: Name{Local: "key"}}
		value := StartElement{Name: Name{Local: "value"}}

		// map entry
		e := m.Entry()
		e.MemberElement(key).String("example-key1")
		e.MemberElement(value).String("example1")
		e.Close()

		// map entry
		e = m.Entry()
		e.MemberElement(key).String("example-key2")
		e.MemberElement(value).String("example2")
		e.Close()

		// map entry
		e = m.Entry()
		e.MemberElement(key).String("example-key3")
		e.MemberElement(value).String("example3")
		e.Close()
	}()

	ex := []byte(`<flatMap><key>example-key1</key><value>example1</value></flatMap><flatMap><key>example-key2</key><value>example2</value></flatMap><flatMap><key>example-key3</key><value>example3</value></flatMap>`)
	if a := buffer.Bytes(); bytes.Compare(ex, a) != 0 {
		t.Errorf("expected %+q, but got %+q", ex, a)
	}
}