1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 type tokenTest
struct {
14 // A short description of the test case.
18 // The string representations of the expected tokens.
22 var tokenTests
= []tokenTest
{
23 // A single text node. The tokenizer should not break text nodes on whitespace,
24 // nor should it normalize whitespace within a text node.
40 // A start, self-closing and end tag. The tokenizer does not care if the start
41 // and end tokens don't match; that is the job of the parser.
53 // An attribute with a backslash.
61 // Entities, tag name and attribute key lower-casing, and whitespace
62 // normalization within a tag.
65 "<p \t\n iD=\"a"B\" foo=\"bar\"><EM>te<&;xt</em></p>",
67 `<p id="a"B" foo="bar">`,
74 // A non-existant entity. Tokenizing and converting back to a string should
75 // escape the "&" to become "&".
78 `<a b="c&noSuchEntity;d"><&alsoDoesntExist;&`,
80 `<a b="c&noSuchEntity;d">`,
81 "<&alsoDoesntExist;&",
86 func TestTokenizer(t
*testing
.T
) {
88 for _
, tt
:= range tokenTests
{
89 z
:= NewTokenizer(bytes
.NewBuffer([]byte(tt
.html
)))
90 for i
, s
:= range tt
.tokens
{
91 if z
.Next() == Error
{
92 t
.Errorf("%s token %d: want %q got error %v", tt
.desc
, i
, s
, z
.Error())
95 actual
:= z
.Token().String()
97 t
.Errorf("%s token %d: want %q got %q", tt
.desc
, i
, s
, actual
)
102 if z
.Error() != os
.EOF
{
103 t
.Errorf("%s: want EOF got %q", tt
.desc
, z
.Token().String())
108 func TestUnescapeEscape(t
*testing
.T
) {
118 `"<&>"`,
119 `3&5==1 && 0<1, "0<1", a+acute=á`,
121 for _
, s
:= range ss
{
122 if s
!= UnescapeString(EscapeString(s
)) {
123 t
.Errorf("s != UnescapeString(EscapeString(s)), s=%q", s
)
128 func TestBufAPI(t
*testing
.T
) {
129 s
:= "0<a>1</a>2<b>3<a>4<a>5</a>6</b>7</a>8<a/>9"
130 z
:= NewTokenizer(bytes
.NewBuffer([]byte(s
)))
131 result
:= bytes
.NewBuffer(nil)
138 if z
.Error() != os
.EOF
{
144 result
.Write(z
.Text())
146 case StartTag
, EndTag
:
148 if len(tn
) == 1 && tn
[0] == 'a' {
158 v
:= string(result
.Bytes())
160 t
.Errorf("TestBufAPI: want %q got %q", u
, v
)