"
p := NewParser(StringReader(data))
p.Strict = false
token, err := p.Token()
if _, ok := err.(*SyntaxError); ok {
t.Errorf("Unexpected error: %v", err)
}
if token.(StartElement).Name.Local != "tag" {
t.Errorf("Unexpected tag name: %v", token.(StartElement).Name.Local)
}
attr := token.(StartElement).Attr[0]
if attr.Value != "azAZ09:-_" {
t.Errorf("Unexpected attribute value: %v", attr.Value)
}
if attr.Name.Local != "attr" {
t.Errorf("Unexpected attribute name: %v", attr.Name.Local)
}
}
func TestCopyTokenCharData(t *testing.T) {
data := []byte("same data")
var tok1 Token = CharData(data)
tok2 := CopyToken(tok1)
if !reflect.DeepEqual(tok1, tok2) {
t.Error("CopyToken(CharData) != CharData")
}
data[1] = 'o'
if reflect.DeepEqual(tok1, tok2) {
t.Error("CopyToken(CharData) uses same buffer.")
}
}
func TestCopyTokenStartElement(t *testing.T) {
elt := StartElement{Name{"", "hello"}, []Attr{{Name{"", "lang"}, "en"}}}
var tok1 Token = elt
tok2 := CopyToken(tok1)
if !reflect.DeepEqual(tok1, tok2) {
t.Error("CopyToken(StartElement) != StartElement")
}
elt.Attr[0] = Attr{Name{"", "lang"}, "de"}
if reflect.DeepEqual(tok1, tok2) {
t.Error("CopyToken(CharData) uses same buffer.")
}
}
func TestSyntaxErrorLineNum(t *testing.T) {
testInput := "Foo
\n\n
Bar>\n"
p := NewParser(StringReader(testInput))
var err os.Error
for _, err = p.Token(); err == nil; _, err = p.Token() {
}
synerr, ok := err.(*SyntaxError)
if !ok {
t.Error("Expected SyntaxError.")
}
if synerr.Line != 3 {
t.Error("SyntaxError didn't have correct line number.")
}
}
func TestTrailingRawToken(t *testing.T) {
input := ` `
p := NewParser(StringReader(input))
var err os.Error
for _, err = p.RawToken(); err == nil; _, err = p.RawToken() {
}
if err != os.EOF {
t.Fatalf("p.RawToken() = _, %v, want _, os.EOF", err)
}
}
func TestTrailingToken(t *testing.T) {
input := ` `
p := NewParser(StringReader(input))
var err os.Error
for _, err = p.Token(); err == nil; _, err = p.Token() {
}
if err != os.EOF {
t.Fatalf("p.Token() = _, %v, want _, os.EOF", err)
}
}
func TestEntityInsideCDATA(t *testing.T) {
input := ``
p := NewParser(StringReader(input))
var err os.Error
for _, err = p.Token(); err == nil; _, err = p.Token() {
}
if err != os.EOF {
t.Fatalf("p.Token() = _, %v, want _, os.EOF", err)
}
}
// The last three tests (respectively one for characters in attribute
// names and two for character entities) pass not because of code
// changed for issue 1259, but instead pass with the given messages
// from other parts of xml.Parser. I provide these to note the
// current behavior of situations where one might think that character
// range checking would detect the error, but it does not in fact.
var characterTests = []struct {
in string
err string
}{
{"\x12", "illegal character code U+0012"},
{"\x0b", "illegal character code U+000B"},
{"\xef\xbf\xbe", "illegal character code U+FFFE"},
{"\r\n\x07", "illegal character code U+0007"},
{"what's up", "expected attribute name in element"},
{"&\x01;", "invalid character entity &;"},
{"&\xef\xbf\xbe;", "invalid character entity &;"},
}
func TestDisallowedCharacters(t *testing.T) {
for i, tt := range characterTests {
p := NewParser(StringReader(tt.in))
var err os.Error
for err == nil {
_, err = p.Token()
}
synerr, ok := err.(*SyntaxError)
if !ok {
t.Fatalf("input %d p.Token() = _, %v, want _, *SyntaxError", i, err)
}
if synerr.Msg != tt.err {
t.Fatalf("input %d synerr.Msg wrong: want '%s', got '%s'", i, tt.err, synerr.Msg)
}
}
}