xml: handle unexpected EOF while parsing and fix a bug in name

mustgetc reports unexpected EOF as SyntaxError.  using
mustgetc seems to be a better approach than letting the
caller handle unexpected EOF every time.

name: the second if statement should explicitly return
ok==false.

R=rsc
https://golang.org/cl/174083
This commit is contained in:
Arvindh Rajesh Tamilmani 2009-12-14 19:28:36 -08:00 committed by Russ Cox
parent 19c18358ca
commit dec5bb7882
2 changed files with 104 additions and 27 deletions

View file

@ -94,6 +94,57 @@ var cookedTokens = []Token{
Comment(strings.Bytes(" missing final newline ")),
}
var xmlInput = []string{
// unexpected EOF cases
"<",
"<t",
"<t ",
"<t/",
"<t/>c",
"<!",
"<!-",
"<!--",
"<!--c-",
"<!--c--",
"<!d",
"<t></",
"<t></t",
"<?",
"<?p",
"<t a",
"<t a=",
"<t a='",
"<t a=''",
"<t/><![",
"<t/><![C",
"<t/><![CDATA[d",
"<t/><![CDATA[d]",
"<t/><![CDATA[d]]",
// other Syntax errors
" ",
">",
"<>",
"<t/a",
"<0 />",
"<?0 >",
// "<!0 >", // let the Token() caller handle
"</0>",
"<t 0=''>",
"<t a='&'>",
"<t a='<'>",
"<t>&nbspc;</t>",
"<t a>",
"<t a=>",
"<t a=v>",
// "<![CDATA[d]]>", // let the Token() caller handle
"cdata",
"<t></e>",
"<t></>",
"<t></t!",
"<t>cdata]]></t>",
}
type stringReader struct {
s string;
off int;
@ -149,3 +200,15 @@ func TestToken(t *testing.T) {
}
}
}
func TestSyntax(t *testing.T) {
for i := range xmlInput {
p := NewParser(StringReader(xmlInput[i]));
var err os.Error;
for _, err = p.Token(); err == nil; _, err = p.Token() {
}
if _, ok := err.(SyntaxError); !ok {
t.Fatalf(`xmlInput "%s": expected SyntaxError not received`, xmlInput[i])
}
}
}