1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
package parser
import (
"bytes"
"reflect"
"testing"
)
func TestScannerTagNameWithDotAndEqual(t *testing.T) {
testScannerSuccess(t, "{%foo.bar.34 baz aaa%} awer{% aa= %}",
[]tt{
{ID: tagName, Value: "foo.bar.34"},
{ID: tagContents, Value: "baz aaa"},
{ID: text, Value: " awer"},
{ID: tagName, Value: "aa="},
{ID: tagContents, Value: ""},
})
}
func TestScannerEndTagWithMinus(t *testing.T) {
testScannerSuccess(t, "{%foo baz aaa-%} awer{% aa= -%}",
[]tt{
{ID: tagName, Value: "foo"},
{ID: tagContents, Value: "baz aaa"},
{ID: text, Value: "awer"},
{ID: tagName, Value: "aa="},
{ID: tagContents, Value: ""},
})
testScannerSuccess(t, "{%foo baz aaa- %} awer{% aa= -% %}",
[]tt{
{ID: tagName, Value: "foo"},
{ID: tagContents, Value: "baz aaa-"},
{ID: text, Value: " awer"},
{ID: tagName, Value: "aa="},
{ID: tagContents, Value: "-%"},
})
testScannerSuccess(t, "{%foo -%} \t\r\r\n\n\tawer{%aa= - %} \nbb",
[]tt{
{ID: tagName, Value: "foo"},
{ID: tagContents, Value: ""},
{ID: text, Value: "\n\tawer"},
{ID: tagName, Value: "aa="},
{ID: tagContents, Value: "-"},
{ID: text, Value: " \nbb"},
})
testScannerSuccess(t, "{%foo-%}\n awer{%bar -%}\n",
[]tt{
{ID: tagName, Value: "foo"},
{ID: tagContents, Value: ""},
{ID: text, Value: " awer"},
{ID: tagName, Value: "bar"},
{ID: tagContents, Value: ""},
})
testScannerSuccess(t, "{%foo-%}{%bar%} \n {%baz%}",
[]tt{
{ID: tagName, Value: "foo"},
{ID: tagContents, Value: ""},
{ID: tagName, Value: "bar"},
{ID: tagContents, Value: ""},
{ID: text, Value: " \n "},
{ID: tagName, Value: "baz"},
{ID: tagContents, Value: ""},
})
}
func TestScannerBeginTagWithMinus(t *testing.T) {
testScannerSuccess(t, "x\n {%-foo%} {%- bar%}",
[]tt{
{ID: text, Value: "x\n"},
{ID: tagName, Value: "foo"},
{ID: tagContents, Value: ""},
{ID: tagName, Value: "bar"},
{ID: tagContents, Value: ""},
})
testScannerSuccess(t, "{%- foo%}",
[]tt{
{ID: tagName, Value: "foo"},
{ID: tagContents, Value: ""},
})
testScannerSuccess(t, "{%-foo baz aaa%} awer\n\t {%-aa= %}",
[]tt{
{ID: tagName, Value: "foo"},
{ID: tagContents, Value: "baz aaa"},
{ID: text, Value: " awer\n"},
{ID: tagName, Value: "aa="},
{ID: tagContents, Value: ""},
})
testScannerSuccess(t, "{%-foo baz aaa%} awer {- % aa= %}",
[]tt{
{ID: tagName, Value: "foo"},
{ID: tagContents, Value: "baz aaa"},
{ID: text, Value: " awer {- % aa= %}"},
})
testScannerSuccess(t, "\n\n {%- foo baz aaa -%} awer {-{%- aa= xxx%}",
[]tt{
{ID: text, Value: "\n\n"},
{ID: tagName, Value: "foo"},
{ID: tagContents, Value: "baz aaa"},
{ID: text, Value: "awer {-"},
{ID: tagName, Value: "aa="},
{ID: tagContents, Value: "xxx"},
})
testScannerSuccess(t, "{%-foo%}{%bar%} \n {%-baz%}",
[]tt{
{ID: tagName, Value: "foo"},
{ID: tagContents, Value: ""},
{ID: tagName, Value: "bar"},
{ID: tagContents, Value: ""},
{ID: text, Value: " \n"},
{ID: tagName, Value: "baz"},
{ID: tagContents, Value: ""},
})
}
func TestScannerStripspaceSuccess(t *testing.T) {
testScannerSuccess(t, " aa\n\t {%stripspace%} \t\n f\too \n b ar \n\r\t {% bar baz asd %}\n\nbaz \n\t \taaa \n{%endstripspace%} bb ", []tt{
{ID: text, Value: " aa\n\t "},
{ID: text, Value: "f\toob ar"},
{ID: tagName, Value: "bar"},
{ID: tagContents, Value: "baz asd"},
{ID: text, Value: "bazaaa"},
{ID: text, Value: " bb "},
})
testScannerSuccess(t, "{%stripspace %}{% stripspace fobar %} {%space%} a\taa\n\r\t bb b {%endstripspace %} {%endstripspace baz%}", []tt{
{ID: text, Value: " "},
{ID: text, Value: "a\taabb b"},
})
// sripspace wins over collapsespace
testScannerSuccess(t, "{%stripspace%} {%collapsespace%}foo\n\t bar{%endcollapsespace%} \r\n\t {%endstripspace%}", []tt{
{ID: text, Value: "foobar"},
})
}
func TestScannerStripspaceFailure(t *testing.T) {
// incomplete stripspace tag
testScannerFailure(t, "{%stripspace ")
// incomplete endstripspace tag
testScannerFailure(t, "{%stripspace%}aaa{%endstripspace")
// missing endstripspace
testScannerFailure(t, "{%stripspace%} foobar")
// missing stripspace
testScannerFailure(t, "aaa{%endstripspace%}")
// missing the second endstripspace
testScannerFailure(t, "{%stripspace%}{%stripspace%}aaaa{%endstripspace%}")
}
func TestScannerCollapsespaceSuccess(t *testing.T) {
testScannerSuccess(t, " aa\n\t {%collapsespace%} \t\n foo \n bar{% bar baz asd %}\n\nbaz \n \n{%endcollapsespace%} bb ", []tt{
{ID: text, Value: " aa\n\t "},
{ID: text, Value: " foo bar"},
{ID: tagName, Value: "bar"},
{ID: tagContents, Value: "baz asd"},
{ID: text, Value: " baz "},
{ID: text, Value: " bb "},
})
testScannerSuccess(t, "{%collapsespace %}{% collapsespace fobar %} {%space%} aaa\n\r\t bbb {%endcollapsespace %} {%endcollapsespace baz%}", []tt{
{ID: text, Value: " "},
{ID: text, Value: " "},
{ID: text, Value: " aaa bbb "},
{ID: text, Value: " "},
})
}
func TestScannerCollapsespaceFailure(t *testing.T) {
// incomplete collapsespace tag
testScannerFailure(t, "{%collapsespace ")
// incomplete endcollapsespace tag
testScannerFailure(t, "{%collapsespace%}aaa{%endcollapsespace")
// missing endcollapsespace
testScannerFailure(t, "{%collapsespace%} foobar")
// missing collapsespace
testScannerFailure(t, "aaa{%endcollapsespace%}")
// missing the second endcollapsespace
testScannerFailure(t, "{%collapsespace%}{%collapsespace%}aaaa{%endcollapsespace%}")
}
func TestScannerPlainSuccess(t *testing.T) {
testScannerSuccess(t, "{%plain%}{%endplain%}", nil)
testScannerSuccess(t, "{%plain%}{%foo bar%}asdf{%endplain%}", []tt{
{ID: text, Value: "{%foo bar%}asdf"},
})
testScannerSuccess(t, "{%plain%}{%foo{%endplain%}", []tt{
{ID: text, Value: "{%foo"},
})
testScannerSuccess(t, "aa{%plain%}bbb{%cc%}{%endplain%}{%plain%}dsff{%endplain%}", []tt{
{ID: text, Value: "aa"},
{ID: text, Value: "bbb{%cc%}"},
{ID: text, Value: "dsff"},
})
testScannerSuccess(t, "mmm{%plain%}aa{% bar {%%% }baz{%endplain%}nnn", []tt{
{ID: text, Value: "mmm"},
{ID: text, Value: "aa{% bar {%%% }baz"},
{ID: text, Value: "nnn"},
})
testScannerSuccess(t, "{% plain dsd %}0{%comment%}123{%endcomment%}45{% endplain aaa %}", []tt{
{ID: text, Value: "0{%comment%}123{%endcomment%}45"},
})
}
func TestScannerPlainFailure(t *testing.T) {
testScannerFailure(t, "{%plain%}sdfds")
testScannerFailure(t, "{%plain%}aaaa%{%endplain")
testScannerFailure(t, "{%plain%}{%endplain%")
}
func TestScannerCommentSuccess(t *testing.T) {
testScannerSuccess(t, "{%comment%}{%endcomment%}", nil)
testScannerSuccess(t, "{%comment%}foo{%endcomment%}", nil)
testScannerSuccess(t, "{%comment%}foo{%endcomment%}{%comment%}sss{%endcomment%}", nil)
testScannerSuccess(t, "{%comment%}foo{%bar%}{%endcomment%}", nil)
testScannerSuccess(t, "{%comment%}foo{%bar {%endcomment%}", nil)
testScannerSuccess(t, "{%comment%}foo{%bar&^{%endcomment%}", nil)
testScannerSuccess(t, "{%comment%}foo{% bar\n\rs%{%endcomment%}", nil)
testScannerSuccess(t, "xx{%x%}www{% comment aux data %}aaa{% comment %}{% endcomment %}yy", []tt{
{ID: text, Value: "xx"},
{ID: tagName, Value: "x"},
{ID: tagContents, Value: ""},
{ID: text, Value: "www"},
{ID: text, Value: "yy"},
})
}
func TestScannerCommentFailure(t *testing.T) {
testScannerFailure(t, "{%comment%}...no endcomment")
testScannerFailure(t, "{% comment %}foobar{% endcomment")
}
func TestScannerSuccess(t *testing.T) {
testScannerSuccess(t, "", nil)
testScannerSuccess(t, "a%}{foo}bar", []tt{
{ID: text, Value: "a%}{foo}bar"},
})
testScannerSuccess(t, "{% foo bar baz(a, b, 123) %}", []tt{
{ID: tagName, Value: "foo"},
{ID: tagContents, Value: "bar baz(a, b, 123)"},
})
testScannerSuccess(t, "foo{%bar%}baz", []tt{
{ID: text, Value: "foo"},
{ID: tagName, Value: "bar"},
{ID: tagContents, Value: ""},
{ID: text, Value: "baz"},
})
testScannerSuccess(t, "{{{%\n\r\tfoo bar\n\rbaz%%\n \r %}}", []tt{
{ID: text, Value: "{{"},
{ID: tagName, Value: "foo"},
{ID: tagContents, Value: "bar\n\rbaz%%"},
{ID: text, Value: "}"},
})
testScannerSuccess(t, "{%%}", []tt{
{ID: tagName, Value: ""},
{ID: tagContents, Value: ""},
})
testScannerSuccess(t, "{%%aaa bb%}", []tt{
{ID: tagName, Value: ""},
{ID: tagContents, Value: "%aaa bb"},
})
testScannerSuccess(t, "foo{% bar %}{% baz aa (123)%}321", []tt{
{ID: text, Value: "foo"},
{ID: tagName, Value: "bar"},
{ID: tagContents, Value: ""},
{ID: tagName, Value: "baz"},
{ID: tagContents, Value: "aa (123)"},
{ID: text, Value: "321"},
})
}
func TestScannerFailure(t *testing.T) {
testScannerFailure(t, "a{%")
testScannerFailure(t, "a{%foo")
testScannerFailure(t, "a{%% }foo")
testScannerFailure(t, "a{% foo %")
testScannerFailure(t, "b{% fo() %}bar")
testScannerFailure(t, "aa{% foo bar")
testScannerFailure(t, "a{%-")
testScannerFailure(t, "a{%-foo")
testScannerFailure(t, "a{%-% }foo")
testScannerFailure(t, "a{%- foo %")
testScannerFailure(t, "b{%- fo() %}bar")
testScannerFailure(t, "aa{%- foo bar")
}
func testScannerFailure(t *testing.T, str string) {
r := bytes.NewBufferString(str)
s := newScanner(r, "memory")
var tokens []tt
for s.Next() {
tokens = append(tokens, tt{
ID: s.Token().ID,
Value: string(s.Token().Value),
})
}
if err := s.LastError(); err == nil {
t.Fatalf("expecting error when scanning %q. got tokens %v", str, tokens)
}
}
func testScannerSuccess(t *testing.T, str string, expectedTokens []tt) {
r := bytes.NewBufferString(str)
s := newScanner(r, "memory")
var tokens []tt
for s.Next() {
tokens = append(tokens, tt{
ID: s.Token().ID,
Value: string(s.Token().Value),
})
}
if err := s.LastError(); err != nil {
t.Fatalf("unexpected error: %s. str=%q", err, str)
}
if !reflect.DeepEqual(tokens, expectedTokens) {
t.Fatalf("unexpected tokens %v. Expecting %v. str=%q", tokens, expectedTokens, str)
}
}
type tt struct {
ID int
Value string
}
|