Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3052)

Side by Side Diff: src/pkg/encoding/xml/marshal_test.go

Issue 5574053: code review 5574053: encoding/xml: bring API closer to other packages (Closed)
Patch Set: diff -r bf7fe7752318 https://go.googlecode.com/hg/ Created 13 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/pkg/encoding/xml/marshal.go ('k') | src/pkg/encoding/xml/read.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 The Go Authors. All rights reserved. 1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package xml 5 package xml
6 6
7 import ( 7 import (
8 "bytes"
9 "reflect" 8 "reflect"
10 "strconv" 9 "strconv"
11 "strings" 10 "strings"
12 "testing" 11 "testing"
13 ) 12 )
14 13
15 type DriveType int 14 type DriveType int
16 15
17 const ( 16 const (
18 HyperDrive DriveType = iota 17 HyperDrive DriveType = iota
(...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 Value: &IgnoreTest{}, 611 Value: &IgnoreTest{},
613 UnmarshalOnly: true, 612 UnmarshalOnly: true,
614 }, 613 },
615 } 614 }
616 615
617 func TestMarshal(t *testing.T) { 616 func TestMarshal(t *testing.T) {
618 for idx, test := range marshalTests { 617 for idx, test := range marshalTests {
619 if test.UnmarshalOnly { 618 if test.UnmarshalOnly {
620 continue 619 continue
621 } 620 }
622 » » buf := bytes.NewBuffer(nil) 621 » » data, err := Marshal(test.Value)
623 » » err := Marshal(buf, test.Value)
624 if err != nil { 622 if err != nil {
625 t.Errorf("#%d: Error: %s", idx, err) 623 t.Errorf("#%d: Error: %s", idx, err)
626 continue 624 continue
627 } 625 }
628 » » if got, want := buf.String(), test.ExpectXML; got != want { 626 » » if got, want := string(data), test.ExpectXML; got != want {
629 if strings.Contains(want, "\n") { 627 if strings.Contains(want, "\n") {
630 t.Errorf("#%d: marshal(%#v):\nHAVE:\n%s\nWANT:\n %s", idx, test.Value, got, want) 628 t.Errorf("#%d: marshal(%#v):\nHAVE:\n%s\nWANT:\n %s", idx, test.Value, got, want)
631 } else { 629 } else {
632 t.Errorf("#%d: marshal(%#v):\nhave %#q\nwant %#q ", idx, test.Value, got, want) 630 t.Errorf("#%d: marshal(%#v):\nhave %#q\nwant %#q ", idx, test.Value, got, want)
633 } 631 }
634 } 632 }
635 } 633 }
636 } 634 }
637 635
638 var marshalErrorTests = []struct { 636 var marshalErrorTests = []struct {
(...skipping 20 matching lines...) Expand all
659 Kind: reflect.Map, 657 Kind: reflect.Map,
660 }, 658 },
661 { 659 {
662 Value: &Domain{Comment: []byte("f--bar")}, 660 Value: &Domain{Comment: []byte("f--bar")},
663 Err: `xml: comments must not contain "--"`, 661 Err: `xml: comments must not contain "--"`,
664 }, 662 },
665 } 663 }
666 664
667 func TestMarshalErrors(t *testing.T) { 665 func TestMarshalErrors(t *testing.T) {
668 for idx, test := range marshalErrorTests { 666 for idx, test := range marshalErrorTests {
669 » » buf := bytes.NewBuffer(nil) 667 » » _, err := Marshal(test.Value)
670 » » err := Marshal(buf, test.Value)
671 if err == nil || err.Error() != test.Err { 668 if err == nil || err.Error() != test.Err {
672 t.Errorf("#%d: marshal(%#v) = [error] %v, want %v", idx, test.Value, err, test.Err) 669 t.Errorf("#%d: marshal(%#v) = [error] %v, want %v", idx, test.Value, err, test.Err)
673 } 670 }
674 if test.Kind != reflect.Invalid { 671 if test.Kind != reflect.Invalid {
675 if kind := err.(*UnsupportedTypeError).Type.Kind(); kind != test.Kind { 672 if kind := err.(*UnsupportedTypeError).Type.Kind(); kind != test.Kind {
676 t.Errorf("#%d: marshal(%#v) = [error kind] %s, w ant %s", idx, test.Value, kind, test.Kind) 673 t.Errorf("#%d: marshal(%#v) = [error kind] %s, w ant %s", idx, test.Value, kind, test.Kind)
677 } 674 }
678 } 675 }
679 } 676 }
680 } 677 }
681 678
682 // Do invertibility testing on the various structures that we test 679 // Do invertibility testing on the various structures that we test
683 func TestUnmarshal(t *testing.T) { 680 func TestUnmarshal(t *testing.T) {
684 for i, test := range marshalTests { 681 for i, test := range marshalTests {
685 if test.MarshalOnly { 682 if test.MarshalOnly {
686 continue 683 continue
687 } 684 }
688 if _, ok := test.Value.(*Plain); ok { 685 if _, ok := test.Value.(*Plain); ok {
689 continue 686 continue
690 } 687 }
691 688
692 vt := reflect.TypeOf(test.Value) 689 vt := reflect.TypeOf(test.Value)
693 dest := reflect.New(vt.Elem()).Interface() 690 dest := reflect.New(vt.Elem()).Interface()
694 » » buffer := bytes.NewBufferString(test.ExpectXML) 691 » » err := Unmarshal([]byte(test.ExpectXML), dest)
695 » » err := Unmarshal(buffer, dest)
696 692
697 switch fix := dest.(type) { 693 switch fix := dest.(type) {
698 case *Feed: 694 case *Feed:
699 fix.Author.InnerXML = "" 695 fix.Author.InnerXML = ""
700 for i := range fix.Entry { 696 for i := range fix.Entry {
701 fix.Entry[i].Author.InnerXML = "" 697 fix.Entry[i].Author.InnerXML = ""
702 } 698 }
703 } 699 }
704 700
705 if err != nil { 701 if err != nil {
706 t.Errorf("#%d: unexpected error: %#v", i, err) 702 t.Errorf("#%d: unexpected error: %#v", i, err)
707 } else if got, want := dest, test.Value; !reflect.DeepEqual(got, want) { 703 } else if got, want := dest, test.Value; !reflect.DeepEqual(got, want) {
708 t.Errorf("#%d: unmarshal(%q):\nhave %#v\nwant %#v", i, t est.ExpectXML, got, want) 704 t.Errorf("#%d: unmarshal(%q):\nhave %#v\nwant %#v", i, t est.ExpectXML, got, want)
709 } 705 }
710 } 706 }
711 } 707 }
712 708
713 func BenchmarkMarshal(b *testing.B) { 709 func BenchmarkMarshal(b *testing.B) {
714 buf := bytes.NewBuffer(nil)
715 for i := 0; i < b.N; i++ { 710 for i := 0; i < b.N; i++ {
716 » » Marshal(buf, atomValue) 711 » » Marshal(atomValue)
717 » » buf.Truncate(0)
718 } 712 }
719 } 713 }
720 714
721 func BenchmarkUnmarshal(b *testing.B) { 715 func BenchmarkUnmarshal(b *testing.B) {
722 xml := []byte(atomXml) 716 xml := []byte(atomXml)
723 for i := 0; i < b.N; i++ { 717 for i := 0; i < b.N; i++ {
724 » » buffer := bytes.NewBuffer(xml) 718 » » Unmarshal(xml, &Feed{})
725 » » Unmarshal(buffer, &Feed{})
726 } 719 }
727 } 720 }
OLDNEW
« no previous file with comments | « src/pkg/encoding/xml/marshal.go ('k') | src/pkg/encoding/xml/read.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b