]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: add docs and tests for bool whitespace
authorLeigh McCulloch <leighmcc@gmail.com>
Fri, 27 Oct 2017 07:04:49 +0000 (07:04 +0000)
committerIan Lance Taylor <iant@golang.org>
Wed, 1 Nov 2017 20:35:59 +0000 (20:35 +0000)
Whitespace is ignored in bool values and attrs, but there are no tests
capturing this behavior.

Change-Id: I7a7249de4886f510869e91de937e69b83c3254c8
Reviewed-on: https://go-review.googlesource.com/73890
Reviewed-by: Sam Whited <sam@samwhited.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Sam Whited <sam@samwhited.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/encoding/xml/read.go
src/encoding/xml/read_test.go

index 2fd6e06688ae29be5d9949d6ea8656583d719cf3..6709d5aeba5a1eb1ef56d13271b07cc8223131b3 100644 (file)
@@ -107,7 +107,8 @@ import (
 // to the newly created value.
 //
 // Unmarshal maps an XML element or attribute value to a bool by
-// setting it to the boolean value represented by the string.
+// setting it to the boolean value represented by the string. Whitespace
+// is trimmed and ignored.
 //
 // Unmarshal maps an XML element or attribute value to an integer or
 // floating-point field by setting the field to the result of
index bd6260d6d42c5854ed2f2d3a63ed625380d973ab..08c3e3b4fe2081a0ffccd5cb121947044ec91f7d 100644 (file)
@@ -908,3 +908,60 @@ func TestUnmarshalEmptyValues(t *testing.T) {
                t.Fatalf("populated: Unmarshal:\nhave:  %#+v\nwant: %#+v", v, want)
        }
 }
+
+type WhitespaceValuesParent struct {
+       BFalse bool
+       BTrue  bool
+}
+
+const whitespaceValuesXML = `
+<WhitespaceValuesParent>
+    <BFalse>   false   </BFalse>
+    <BTrue>   true   </BTrue>
+</WhitespaceValuesParent>
+`
+
+// golang.org/issues/22146
+func TestUnmarshalWhitespaceValues(t *testing.T) {
+       v := WhitespaceValuesParent{}
+       if err := Unmarshal([]byte(whitespaceValuesXML), &v); err != nil {
+               t.Fatalf("whitespace values: Unmarshal failed: got %v", err)
+       }
+
+       want := WhitespaceValuesParent{
+               BFalse: false,
+               BTrue:  true,
+       }
+       if v != want {
+               t.Fatalf("whitespace values: Unmarshal:\nhave: %#+v\nwant: %#+v", v, want)
+       }
+}
+
+type WhitespaceAttrsParent struct {
+       BFalse bool `xml:",attr"`
+       BTrue  bool `xml:",attr"`
+}
+
+const whitespaceAttrsXML = `
+<WhitespaceAttrsParent
+    BFalse="  false  "
+    BTrue="  true  "
+>
+</WhitespaceAttrsParent>
+`
+
+// golang.org/issues/22146
+func TestUnmarshalWhitespaceAttrs(t *testing.T) {
+       v := WhitespaceAttrsParent{}
+       if err := Unmarshal([]byte(whitespaceAttrsXML), &v); err != nil {
+               t.Fatalf("whitespace attrs: Unmarshal failed: got %v", err)
+       }
+
+       want := WhitespaceAttrsParent{
+               BFalse: false,
+               BTrue:  true,
+       }
+       if v != want {
+               t.Fatalf("whitespace attrs: Unmarshal:\nhave: %#+v\nwant: %#+v", v, want)
+       }
+}