OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 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 "fmt" |
8 "io" | 10 "io" |
9 "reflect" | 11 "reflect" |
10 "strings" | 12 "strings" |
11 "testing" | 13 "testing" |
12 "time" | 14 "time" |
13 ) | 15 ) |
14 | 16 |
15 // Stripped down Atom feed data structures. | 17 // Stripped down Atom feed data structures. |
16 | 18 |
17 func TestUnmarshalFeed(t *testing.T) { | 19 func TestUnmarshalFeed(t *testing.T) { |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 t.Errorf("#%d: Unmarshal: %v", i, err) | 479 t.Errorf("#%d: Unmarshal: %v", i, err) |
478 continue | 480 continue |
479 } | 481 } |
480 want := tt.tab | 482 want := tt.tab |
481 if dst != want { | 483 if dst != want { |
482 t.Errorf("#%d: dst=%+v, want %+v", i, dst, want) | 484 t.Errorf("#%d: dst=%+v, want %+v", i, dst, want) |
483 } | 485 } |
484 } | 486 } |
485 } | 487 } |
486 | 488 |
| 489 func TestMarshalUnmarshalRoundTrip(t *testing.T) { |
| 490 // From issue 7535 |
| 491 source := `<ex:element xmlns:ex="http://example.com/schema"></ex:element
>` |
| 492 in := bytes.NewBufferString(source) |
| 493 for i := 0; i < 10; i++ { |
| 494 out := &bytes.Buffer{} |
| 495 d := NewDecoder(in) |
| 496 e := NewEncoder(out) |
| 497 |
| 498 for { |
| 499 t, err := d.Token() |
| 500 if err == io.EOF { |
| 501 break |
| 502 } |
| 503 if err != nil { |
| 504 fmt.Println("failed:", err) |
| 505 return |
| 506 } |
| 507 e.EncodeToken(t) |
| 508 } |
| 509 e.Flush() |
| 510 in = out |
| 511 } |
| 512 if in.String() != source { |
| 513 t.Errorf("have %s; want %s", in.String(), source) |
| 514 } |
| 515 } |
| 516 |
487 func TestMarshalNS(t *testing.T) { | 517 func TestMarshalNS(t *testing.T) { |
488 dst := Tables{"hello", "world"} | 518 dst := Tables{"hello", "world"} |
489 data, err := Marshal(&dst) | 519 data, err := Marshal(&dst) |
490 if err != nil { | 520 if err != nil { |
491 t.Fatalf("Marshal: %v", err) | 521 t.Fatalf("Marshal: %v", err) |
492 } | 522 } |
493 want := `<Tables><table xmlns="http://www.w3.org/TR/html4/">hello</table
><table xmlns="http://www.w3schools.com/furniture">world</table></Tables>` | 523 want := `<Tables><table xmlns="http://www.w3.org/TR/html4/">hello</table
><table xmlns="http://www.w3schools.com/furniture">world</table></Tables>` |
494 str := string(data) | 524 str := string(data) |
495 if str != want { | 525 if str != want { |
496 t.Errorf("have: %q\nwant: %q\n", str, want) | 526 t.Errorf("have: %q\nwant: %q\n", str, want) |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 } | 630 } |
601 } | 631 } |
602 } | 632 } |
603 | 633 |
604 func TestMarshalNSAttr(t *testing.T) { | 634 func TestMarshalNSAttr(t *testing.T) { |
605 src := TableAttrs{TAttr{"hello", "world", "en_US", "other1", "other2", "
other3", "other4"}} | 635 src := TableAttrs{TAttr{"hello", "world", "en_US", "other1", "other2", "
other3", "other4"}} |
606 data, err := Marshal(&src) | 636 data, err := Marshal(&src) |
607 if err != nil { | 637 if err != nil { |
608 t.Fatalf("Marshal: %v", err) | 638 t.Fatalf("Marshal: %v", err) |
609 } | 639 } |
610 » want := `<TableAttrs><TAttr xmlns:html4="http://www.w3.org/TR/html4/" ht
ml4: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:j
son="http://golang.org/json/" json:other="other3" xmlns:json_1="http://golang.or
g/2/json/" json_1:other="other4"></TAttr></TableAttrs>` | 640 » want := `<TableAttrs><TAttr xmlns:json_1="http://golang.org/2/json/" xml
ns:json="http://golang.org/json/" xmlns:_xmlfoo="http://golang.org/xmlfoo/" xmln
s:_xml="http://golang.org/xml/" xmlns:furniture="http://www.w3schools.com/furnit
ure" xmlns:html4="http://www.w3.org/TR/html4/" html4:table="hello" furniture:tab
le="world" xml:lang="en_US" _xml:other="other1" _xmlfoo:other="other2" json:othe
r="other3" json_1:other="other4"></TAttr></TableAttrs>` |
611 str := string(data) | 641 str := string(data) |
612 if str != want { | 642 if str != want { |
613 t.Errorf("Marshal:\nhave: %#q\nwant: %#q\n", str, want) | 643 t.Errorf("Marshal:\nhave: %#q\nwant: %#q\n", str, want) |
614 } | 644 } |
615 | 645 |
616 var dst TableAttrs | 646 var dst TableAttrs |
617 if err := Unmarshal(data, &dst); err != nil { | 647 if err := Unmarshal(data, &dst); err != nil { |
618 t.Errorf("Unmarshal: %v", err) | 648 t.Errorf("Unmarshal: %v", err) |
619 } | 649 } |
620 | 650 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 } | 735 } |
706 pea, ok := pod.Pea.(*Pea) | 736 pea, ok := pod.Pea.(*Pea) |
707 if !ok { | 737 if !ok { |
708 t.Fatalf("unmarshalled into wrong type: have %T want *Pea", pod.
Pea) | 738 t.Fatalf("unmarshalled into wrong type: have %T want *Pea", pod.
Pea) |
709 } | 739 } |
710 have, want := pea.Cotelydon, "Green stuff" | 740 have, want := pea.Cotelydon, "Green stuff" |
711 if have != want { | 741 if have != want { |
712 t.Errorf("failed to unmarshal into interface, have %q want %q",
have, want) | 742 t.Errorf("failed to unmarshal into interface, have %q want %q",
have, want) |
713 } | 743 } |
714 } | 744 } |
OLD | NEW |