aboutsummaryrefslogtreecommitdiff
path: root/src/common/golang/marshal_test.go
blob: 7e4d04b56462636221dcd50b5797d9e9a2a5bcc9 (plain)
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
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package xml2

import (
	"bufio"
	"bytes"
	"encoding/xml"
	"io"
	"strings"
	"testing"
)

func TestEncoderEncodeToken(t *testing.T) {
	tests := []struct {
		name    string
		in      string
		want    string
		wantErr string
	}{
		{
			name: "xmlnsPrefixForElement",
			in:   "<foo:bar xmlns:foo=\"baz\"></foo:bar>",
			want: "<foo:bar xmlns:foo=\"baz\"></foo:bar>",
		},
		{
			name: "xmlnsPrefixForAttribute",
			in:   "<foo bar:baz=\"qux\" xmlns:bar=\"quux\"></foo>",
			want: "<foo bar:baz=\"qux\" xmlns:bar=\"quux\"></foo>",
		},
		{
			name: "defaultXmlnsAttribute",
			in:   "<foo xmlns=\"bar\"></foo>",
			want: "<foo xmlns=\"bar\"></foo>",
		},
		{
			// The return value of Decoder.Token() makes it
			// impossible for a decode then encode of an xml file
			// be isomorphic. This is mainly due to the fact that
			// xml.Name.Space contains the uri, and xml.Name does
			// not store the prefix. Instead, make sure that the
			// behavior remains consistent.
			//
			// That is, the last prefix defined for the space is the
			// one applied when encoding the token.
			name: "multipleDefsXmlnsPrefixesSameUri",
			in: `
<foo xmlns:bar="bar">
  <bar:baz xmlns:qux="bar">
    <qux:quux></qux:quux>
  </bar:baz>
</foo>`,
			want: `
<foo xmlns:bar="bar">
  <qux:baz xmlns:qux="bar">
    <qux:quux></qux:quux>
  </qux:baz>
</foo>`,
		},
		{
			name:    "xmlnsPrefixUsedOnElementButNotDefined",
			in:      "<foo:bar></foo:bar>",
			wantErr: "unknown namespace: foo",
		},
		{
			name:    "xmlnsPrefixUsedOnAttrButNotDefined",
			in:      "<foo bar:baz=\"qux\"></foo>",
			wantErr: "unknown namespace: bar",
		},
		{
			name: "xmlnsPrefixUsedOutsideOfDefiningTag",
			in: `
<foo xmlns:bar="baz" bar:qux="quux">corge</foo>
<grault bar:garply="waldo"></grault>`,
			wantErr: "unknown namespace: bar",
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			var b bytes.Buffer
			e := NewEncoder(bufio.NewWriter(&b))
			d := xml.NewDecoder(strings.NewReader(test.in))
			for {
				tkn, err := d.Token()
				if err != nil {
					if err == io.EOF {
						break
					}
					t.Fatalf("Unexpected error got: %v while reading: %s", err, test.in)
				}
				if err := e.EncodeToken(tkn); err != nil {
					if test.wantErr != "" && strings.Contains(err.Error(), test.wantErr) {
						// Do nothing, error is expected.
					} else {
						t.Errorf("Unexpected error during encode: %v", err)
					}
					return
				}
			}
			e.Flush()
			if b.String() != test.want {
				t.Errorf("got: <%s> expected: <%s>", b.String(), test.want)
			}
		})
	}
}

func TestChildEncoder(t *testing.T) {
	// Setup the parent Encoder with the namespace "bar".
	d := xml.NewDecoder(strings.NewReader("<foo xmlns:bar=\"bar\"><bar:baz>Hello World</bar:baz></foo>"))
	tkn, err := d.Token()
	if err != nil {
		t.Fatalf("Error occurred during decoding, got: %v", err)
	}
	parentEnc := NewEncoder(&bytes.Buffer{})
	if err := parentEnc.EncodeToken(tkn); err != nil {
		t.Fatalf("Error occurred while the parent encoder was encoding token %q got: %v", tkn, err)
	}

	// Without instantiating the Encoder as a child, the "bar" namespace will be unknown and cause an
	// error to occur when trying to encode the "bar" namespaced element "<bar:baz>".
	tkn, err = d.Token()
	if err != nil {
		t.Fatalf("Error occurred during decoding, got: %v", err)
	}
	b := &bytes.Buffer{}
	childEnc := ChildEncoder(b, parentEnc)
	if err := childEnc.EncodeToken(tkn); err != nil {
		t.Fatalf("Error occurred while the child encoder was encoding token %q got: %v", tkn, err)
	}
	childEnc.Flush()

	// Verify that the token is not mangled.
	if want := "<bar:baz>"; b.String() != want {
		t.Errorf("Error, got %q, wanted %q", b.String(), want)
	}
}