]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: add test for EncodeToken
authorShawn Smith <shawn.p.smith@gmail.com>
Wed, 5 Mar 2014 19:49:33 +0000 (14:49 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 5 Mar 2014 19:49:33 +0000 (14:49 -0500)
LGTM=rsc
R=golang-codereviews, josharian, dave, iant, bradfitz, rsc
CC=golang-codereviews
https://golang.org/cl/47870043

src/pkg/encoding/xml/marshal_test.go

index d34118a3d8b7f7cf9708387e4936eec3fa891dd5..8bd589644cc1b537080f0acfe7ba47ac524dbe70 100644 (file)
@@ -1149,3 +1149,43 @@ func TestStructPointerMarshal(t *testing.T) {
                t.Fatal(err)
        }
 }
+
+var encodeTokenTests = []struct {
+       tok  Token
+       want string
+       ok   bool
+}{
+       {StartElement{Name{"space", "local"}, nil}, "<local xmlns=\"space\">", true},
+       {StartElement{Name{"space", ""}, nil}, "", false},
+       {EndElement{Name{"space", ""}}, "", false},
+       {CharData("foo"), "foo", true},
+       {Comment("foo"), "<!--foo-->", true},
+       {Comment("foo-->"), "", false},
+       {ProcInst{"Target", []byte("Instruction")}, "<?Target Instruction?>", true},
+       {ProcInst{"xml", []byte("Instruction")}, "", false},
+       {ProcInst{"Target", []byte("Instruction?>")}, "", false},
+       {Directive("foo"), "<!foo>", true},
+       {Directive("foo>"), "", false},
+}
+
+func TestEncodeToken(t *testing.T) {
+       for _, tt := range encodeTokenTests {
+               var buf bytes.Buffer
+               enc := NewEncoder(&buf)
+               err := enc.EncodeToken(tt.tok)
+               switch {
+               case !tt.ok && err == nil:
+                       t.Errorf("enc.EncodeToken(%#v): expected error; got none", tt.tok)
+               case tt.ok && err != nil:
+                       t.Fatalf("enc.EncodeToken: %v", err)
+               case !tt.ok && err != nil:
+                       // expected error, got one
+               }
+               if err := enc.Flush(); err != nil {
+                       t.Fatalf("enc.EncodeToken: %v", err)
+               }
+               if got := buf.String(); got != tt.want {
+                       t.Errorf("enc.EncodeToken = %s; want: %s", got, tt.want)
+               }
+       }
+}