aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/xml/read_test.go
diff options
context:
space:
mode:
authorRoger Peppe <rogpeppe@gmail.com>2015-01-10 14:00:21 +0000
committerroger peppe <rogpeppe@gmail.com>2015-02-13 17:28:45 +0000
commit3be158d6ab73090a74df6bc3b7cfa062d896483a (patch)
tree102d0ed12be30c3bbd83f908e32d746d26ce0b46 /src/encoding/xml/read_test.go
parentf59f9b85276b6f80b6e55ea747a5df64bb83ecce (diff)
downloadgo-3be158d6ab73090a74df6bc3b7cfa062d896483a.tar.gz
go-3be158d6ab73090a74df6bc3b7cfa062d896483a.zip
encoding/xml: encoding name spaces correctly
The current XML printer does not understand the xmlns attribute. This change changes it so that it interprets the xmlns attributes in the tokens being printed, and uses appropriate prefixes. Fixes #7535. Change-Id: I20fae291d20602d37deb41ed42fab4c9a50ec85d Reviewed-on: https://go-review.googlesource.com/2660 Reviewed-by: Nigel Tao <nigeltao@golang.org>
Diffstat (limited to 'src/encoding/xml/read_test.go')
-rw-r--r--src/encoding/xml/read_test.go32
1 files changed, 31 insertions, 1 deletions
diff --git a/src/encoding/xml/read_test.go b/src/encoding/xml/read_test.go
index 7d004dc488..02f1e10c33 100644
--- a/src/encoding/xml/read_test.go
+++ b/src/encoding/xml/read_test.go
@@ -5,6 +5,8 @@
package xml
import (
+ "bytes"
+ "fmt"
"io"
"reflect"
"strings"
@@ -484,6 +486,34 @@ func TestUnmarshalNS(t *testing.T) {
}
}
+func TestRoundTrip(t *testing.T) {
+ // From issue 7535
+ const s = `<ex:element xmlns:ex="http://example.com/schema"></ex:element>`
+ in := bytes.NewBufferString(s)
+ for i := 0; i < 10; i++ {
+ out := &bytes.Buffer{}
+ d := NewDecoder(in)
+ e := NewEncoder(out)
+
+ for {
+ t, err := d.Token()
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ fmt.Println("failed:", err)
+ return
+ }
+ e.EncodeToken(t)
+ }
+ e.Flush()
+ in = out
+ }
+ if got := in.String(); got != s {
+ t.Errorf("have: %q\nwant: %q\n", got, s)
+ }
+}
+
func TestMarshalNS(t *testing.T) {
dst := Tables{"hello", "world"}
data, err := Marshal(&dst)
@@ -607,7 +637,7 @@ func TestMarshalNSAttr(t *testing.T) {
if err != nil {
t.Fatalf("Marshal: %v", err)
}
- want := `<TableAttrs><TAttr xmlns:html4="http://www.w3.org/TR/html4/" html4:table="hello" xmlns:furniture="http://www.w3schools.com/furniture" furniture:table="world" xml:lang="en_US" xmlns:_xml="http://golang.org/xml/" _xml:other="other1" xmlns:_xmlfoo="http://golang.org/xmlfoo/" _xmlfoo:other="other2" xmlns:json="http://golang.org/json/" json:other="other3" xmlns:json_1="http://golang.org/2/json/" json_1:other="other4"></TAttr></TableAttrs>`
+ want := `<TableAttrs><TAttr xmlns:json_1="http://golang.org/2/json/" xmlns:json="http://golang.org/json/" xmlns:_xmlfoo="http://golang.org/xmlfoo/" xmlns:_xml="http://golang.org/xml/" xmlns:furniture="http://www.w3schools.com/furniture" xmlns:html4="http://www.w3.org/TR/html4/" html4:table="hello" furniture:table="world" xml:lang="en_US" _xml:other="other1" _xmlfoo:other="other2" json:other="other3" json_1:other="other4"></TAttr></TableAttrs>`
str := string(data)
if str != want {
t.Errorf("Marshal:\nhave: %#q\nwant: %#q\n", str, want)