From e79c39f004769fc55e60c2fb052155486295d533 Mon Sep 17 00:00:00 2001 From: Iskander Sharipov Date: Wed, 17 Nov 2021 17:46:22 +0300 Subject: encoding/xml: improve the test coverage, fix minor bugs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Improve the test coverage of encoding/xml package by adding the test cases for the execution paths that were not covered before. Since it reveals a couple of issues, fix them as well while we're at it. As I used an `strings.EqualFold` instead of adding one more `strings.ToLower`, our fix to `autoClose()` tends to run faster as well as a result. name old time/op new time/op delta HTMLAutoClose-8 5.93µs ± 2% 5.75µs ± 3% -3.16% (p=0.000 n=10+10) name old alloc/op new alloc/op delta HTMLAutoClose-8 2.60kB ± 0% 2.58kB ± 0% -0.46% (p=0.000 n=10+10) name old allocs/op new allocs/op delta HTMLAutoClose-8 72.0 ± 0% 67.0 ± 0% -6.94% (p=0.000 n=10+10) The overall `encoding/xml` test coverage increase is `88.1% -> 89.9%`; although it may look insignificant, this CL covers some important corner cases, like `autoClose()` functionality (that was not tested at all). Fixes #49635 Fixes #49636 Change-Id: I50b2769896c197eb285672313b7148f4fe8bdb38 Reviewed-on: https://go-review.googlesource.com/c/go/+/364734 Trust: Bryan Mills Reviewed-by: Daniel Martí Trust: Daniel Martí Run-TryBot: Daniel Martí TryBot-Result: Gopher Robot --- src/encoding/xml/xml.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'src/encoding/xml/xml.go') diff --git a/src/encoding/xml/xml.go b/src/encoding/xml/xml.go index 8a0a9c253a..ef51252dcb 100644 --- a/src/encoding/xml/xml.go +++ b/src/encoding/xml/xml.go @@ -10,9 +10,6 @@ package xml // Annotated XML spec: https://www.xml.com/axml/testaxml.htm // XML name spaces: https://www.w3.org/TR/REC-xml-names/ -// TODO(rsc): -// Test error handling. - import ( "bufio" "bytes" @@ -499,7 +496,7 @@ func (d *Decoder) popElement(t *EndElement) bool { return false case s.name.Space != name.Space: d.err = d.syntaxError("element <" + s.name.Local + "> in space " + s.name.Space + - "closed by in space " + name.Space) + " closed by in space " + name.Space) return false } @@ -523,12 +520,11 @@ func (d *Decoder) autoClose(t Token) (Token, bool) { if d.stk == nil || d.stk.kind != stkStart { return nil, false } - name := strings.ToLower(d.stk.name.Local) for _, s := range d.AutoClose { - if strings.ToLower(s) == name { + if strings.EqualFold(s, d.stk.name.Local) { // This one should be auto closed if t doesn't close it. et, ok := t.(EndElement) - if !ok || et.Name.Local != name { + if !ok || !strings.EqualFold(et.Name.Local, d.stk.name.Local) { return EndElement{d.stk.name}, true } break -- cgit v1.3