diff options
| author | Shawn Smith <shawn.p.smith@gmail.com> | 2014-03-05 14:49:33 -0500 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2014-03-05 14:49:33 -0500 |
| commit | 58980821c7f8f5412da418365b717eeef4078846 (patch) | |
| tree | 116cb1ce2a0ff614784bf5d2c75c8f1aff5f9f7d /src/pkg/encoding | |
| parent | 37e195cf727eb61d352f244230d988eb444a9b13 (diff) | |
| download | go-58980821c7f8f5412da418365b717eeef4078846.tar.xz | |
encoding/xml: add test for EncodeToken
LGTM=rsc
R=golang-codereviews, josharian, dave, iant, bradfitz, rsc
CC=golang-codereviews
https://golang.org/cl/47870043
Diffstat (limited to 'src/pkg/encoding')
| -rw-r--r-- | src/pkg/encoding/xml/marshal_test.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/pkg/encoding/xml/marshal_test.go b/src/pkg/encoding/xml/marshal_test.go index d34118a3d8..8bd589644c 100644 --- a/src/pkg/encoding/xml/marshal_test.go +++ b/src/pkg/encoding/xml/marshal_test.go @@ -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) + } + } +} |
