| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
import groovy.text.Template |
| 20 |
import groovy.text.markup.MarkupTemplateEngine |
| 21 |
import groovy.text.markup.TemplateConfiguration |
| 22 |
|
| 23 |
import java.nio.charset.Charset |
| 24 |
|
| 25 |
|
| 26 |
class MarkupTemplateEngineSpecTest extends GroovyTestCase { |
| 27 |
private MarkupTemplateEngine engine |
| 28 |
private TemplateConfiguration config |
| 29 |
private Map model |
| 30 |
private Map modelTypes |
| 31 |
private String templateContents |
| 32 |
private String expectedRendered |
| 33 |
|
| 34 |
@Override |
| 35 |
void setUp() { |
| 36 |
super.setUp(); |
| 37 |
config = new TemplateConfiguration() |
| 38 |
model = null |
| 39 |
modelTypes = null |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
void tearDown() { |
| 44 |
super.tearDown(); |
| 45 |
config = null |
| 46 |
engine = null |
| 47 |
} |
| 48 |
|
| 49 |
private void assertRendered() { |
| 50 |
engine = new MarkupTemplateEngine(this.class.classLoader, config) |
| 51 |
def template = modelTypes ? |
| 52 |
engine.createTypeCheckedModelTemplate(templateContents, modelTypes) : |
| 53 |
engine.createTemplate(templateContents); |
| 54 |
def ready = template.make(model) |
| 55 |
def sw = new StringWriter() |
| 56 |
ready.writeTo(sw) |
| 57 |
assert sw.toString() == expectedRendered |
| 58 |
} |
| 59 |
|
| 60 |
private void assertError(String error) { |
| 61 |
def msg = shouldFail { |
| 62 |
assertRendered() |
| 63 |
} |
| 64 |
assert msg.contains(error) |
| 65 |
} |
| 66 |
|
| 67 |
private String stripAsciidocMarkup(String string) { |
| 68 |
boolean inside = false |
| 69 |
StringBuilder sb = new StringBuilder() |
| 70 |
new StringReader(string).eachLine { line -> |
| 71 |
if (line =~ 'tag::') { |
| 72 |
inside = true |
| 73 |
} else if (line =~ 'end::') { |
| 74 |
inside = false |
| 75 |
} else if (inside) { |
| 76 |
if (sb.length() > 0) { |
| 77 |
sb.append(config.newLineString) |
| 78 |
} |
| 79 |
sb.append(line) |
| 80 |
} |
| 81 |
} |
| 82 |
sb |
| 83 |
} |
| 84 |
|
| 85 |
void testSimpleTemplate() { |
| 86 |
templateContents = ''' |
| 87 |
// tag::example1_template[] |
| 88 |
xmlDeclaration() |
| 89 |
cars { |
| 90 |
cars.each { |
| 91 |
car(make: it.make, model: it.model) |
| 92 |
} |
| 93 |
} |
| 94 |
// end::example1_template[] |
| 95 |
''' |
| 96 |
expectedRendered = stripAsciidocMarkup ''' |
| 97 |
// tag::example1_expected[] |
| 98 |
<?xml version='1.0'?> |
| 99 |
<cars><car make='Peugeot' model='508'/><car make='Toyota' model='Prius'/></cars> |
| 100 |
// end::example1_expected[] |
| 101 |
''' |
| 102 |
// tag::example1_model[] |
| 103 |
model = [cars: [new Car(make: 'Peugeot', model: '508'), new Car(make: 'Toyota', model: 'Prius')]] |
| 104 |
// end::example1_model[] |
| 105 |
assertRendered() |
| 106 |
|
| 107 |
/* |
| 108 |
// tag::example1_template_with_bullets[] |
| 109 |
xmlDeclaration() <1> |
| 110 |
cars { <2> |
| 111 |
cars.each { <3> |
| 112 |
car(make: it.make, model: it.model) <4> |
| 113 |
} <5> |
| 114 |
} |
| 115 |
// end::example1_template_with_bullets[] |
| 116 |
*/ |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
void testSimpleHtmlTemplate() { |
| 121 |
templateContents = ''' |
| 122 |
// tag::example2_template[] |
| 123 |
yieldUnescaped '<!DOCTYPE html>' // <1> |
| 124 |
html(lang:'en') { // <2> |
| 125 |
head { // <3> |
| 126 |
meta('http-equiv':'"Content-Type" content="text/html; charset=utf-8"') // <4> |
| 127 |
title('My page') // <5> |
| 128 |
} // <6> |
| 129 |
body { // <7> |
| 130 |
p('This is an example of HTML contents') // <8> |
| 131 |
} // <9> |
| 132 |
} // <10> |
| 133 |
// end::example2_template[] |
| 134 |
''' |
| 135 |
expectedRendered = stripAsciidocMarkup ''' |
| 136 |
// tag::example2_expected[] |
| 137 |
<!DOCTYPE html><html lang='en'><head><meta http-equiv='"Content-Type" content="text/html; charset=utf-8"'/><title>My page</title></head><body><p>This is an example of HTML contents</p></body></html> |
| 138 |
// end::example2_expected[] |
| 139 |
''' |
| 140 |
assertRendered() |
| 141 |
} |
| 142 |
|
| 143 |
void testYield() { |
| 144 |
templateContents = ''' |
| 145 |
// tag::yield[] |
| 146 |
yield 'Some text with <angle brackets>' |
| 147 |
// end::yield[] |
| 148 |
''' |
| 149 |
expectedRendered = stripAsciidocMarkup ''' |
| 150 |
// tag::yield_expected[] |
| 151 |
Some text with <angle brackets> |
| 152 |
// end::yield_expected[] |
| 153 |
''' |
| 154 |
assertRendered() |
| 155 |
} |
| 156 |
|
| 157 |
void testYieldUnescaped() { |
| 158 |
templateContents = ''' |
| 159 |
// tag::yieldUnescaped[] |
| 160 |
yieldUnescaped 'Some text with <angle brackets>' |
| 161 |
// end::yieldUnescaped[] |
| 162 |
''' |
| 163 |
expectedRendered = stripAsciidocMarkup ''' |
| 164 |
// tag::yieldUnescaped_expected[] |
| 165 |
Some text with <angle brackets> |
| 166 |
// end::yieldUnescaped_expected[] |
| 167 |
''' |
| 168 |
assertRendered() |
| 169 |
} |
| 170 |
|
| 171 |
void testXMLDeclaration() { |
| 172 |
config.newLineString = '' |
| 173 |
templateContents = ''' |
| 174 |
// tag::xmlDeclaration[] |
| 175 |
xmlDeclaration() |
| 176 |
// end::xmlDeclaration[] |
| 177 |
''' |
| 178 |
expectedRendered = stripAsciidocMarkup ''' |
| 179 |
// tag::xmlDeclaration_expected[] |
| 180 |
<?xml version='1.0'?> |
| 181 |
// end::xmlDeclaration_expected[] |
| 182 |
''' |
| 183 |
assertRendered() |
| 184 |
} |
| 185 |
|
| 186 |
void testXMLDeclarationWithEncoding() { |
| 187 |
config.newLineString = '' |
| 188 |
config.declarationEncoding = 'UTF-8' |
| 189 |
templateContents = ''' |
| 190 |
// tag::xmlDeclaration_encoding[] |
| 191 |
xmlDeclaration() |
| 192 |
// end::xmlDeclaration_encoding[] |
| 193 |
''' |
| 194 |
expectedRendered = stripAsciidocMarkup ''' |
| 195 |
// tag::xmlDeclaration_encoding_expected[] |
| 196 |
<?xml version='1.0' encoding='UTF-8'?> |
| 197 |
// end::xmlDeclaration_encoding_expected[] |
| 198 |
''' |
| 199 |
assertRendered() |
| 200 |
} |
| 201 |
|
| 202 |
void testComment() { |
| 203 |
templateContents = ''' |
| 204 |
// tag::comment[] |
| 205 |
comment 'This is <a href="foo.html">commented out</a>' |
| 206 |
// end::comment[] |
| 207 |
''' |
| 208 |
expectedRendered = stripAsciidocMarkup ''' |
| 209 |
// tag::comment_expected[] |
| 210 |
<!--This is <a href="foo.html">commented out</a>--> |
| 211 |
// end::comment_expected[] |
| 212 |
''' |
| 213 |
assertRendered() |
| 214 |
} |
| 215 |
|
| 216 |
void testNewLine() { |
| 217 |
config.newLineString = '\n' |
| 218 |
templateContents = ''' |
| 219 |
// tag::newline[] |
| 220 |
p('text') |
| 221 |
newLine() |
| 222 |
p('text on new line') |
| 223 |
// end::newline[] |
| 224 |
''' |
| 225 |
expectedRendered = stripAsciidocMarkup ''' |
| 226 |
// tag::newline_expected[] |
| 227 |
<p>text</p> |
| 228 |
<p>text on new line</p> |
| 229 |
// end::newline_expected[] |
| 230 |
''' |
| 231 |
assertRendered() |
| 232 |
} |
| 233 |
|
| 234 |
void testPI() { |
| 235 |
config.newLineString = '' |
| 236 |
templateContents = ''' |
| 237 |
// tag::pi[] |
| 238 |
pi("xml-stylesheet":[href:"mystyle.css", type:"text/css"]) |
| 239 |
// end::pi[] |
| 240 |
''' |
| 241 |
expectedRendered = stripAsciidocMarkup ''' |
| 242 |
// tag::pi_expected[] |
| 243 |
<?xml-stylesheet href='mystyle.css' type='text/css'?> |
| 244 |
// end::pi_expected[] |
| 245 |
''' |
| 246 |
assertRendered() |
| 247 |
} |
| 248 |
|
| 249 |
void testTryEscape() { |
| 250 |
templateContents = ''' |
| 251 |
// tag::tryEscape[] |
| 252 |
yieldUnescaped tryEscape('Some text with <angle brackets>') |
| 253 |
// end::tryEscape[] |
| 254 |
''' |
| 255 |
expectedRendered = stripAsciidocMarkup ''' |
| 256 |
// tag::tryEscape_expected[] |
| 257 |
Some text with <angle brackets> |
| 258 |
// end::tryEscape_expected[] |
| 259 |
''' |
| 260 |
assertRendered() |
| 261 |
} |
| 262 |
|
| 263 |
void testIncludeTemplate() { |
| 264 |
templateContents = ''' |
| 265 |
// tag::include_template[] |
| 266 |
include template: 'other_template.tpl' |
| 267 |
// end::include_template[] |
| 268 |
''' |
| 269 |
expectedRendered = stripAsciidocMarkup ''' |
| 270 |
// tag::include_template_expected[] |
| 271 |
included as a template |
| 272 |
// end::include_template_expected[] |
| 273 |
''' |
| 274 |
assertRendered() |
| 275 |
} |
| 276 |
|
| 277 |
void testIncludeRaw() { |
| 278 |
templateContents = ''' |
| 279 |
// tag::include_raw[] |
| 280 |
include unescaped: 'raw.txt' |
| 281 |
// end::include_raw[] |
| 282 |
''' |
| 283 |
expectedRendered = stripAsciidocMarkup ''' |
| 284 |
// tag::include_raw_expected[] |
| 285 |
This is raw contents that may include <tags>. |
| 286 |
// end::include_raw_expected[] |
| 287 |
''' |
| 288 |
assertRendered() |
| 289 |
} |
| 290 |
|
| 291 |
void testIncludeEscaped() { |
| 292 |
templateContents = ''' |
| 293 |
// tag::include_escaped[] |
| 294 |
include escaped: 'to_be_escaped.txt' |
| 295 |
// end::include_escaped[] |
| 296 |
''' |
| 297 |
expectedRendered = stripAsciidocMarkup ''' |
| 298 |
// tag::include_escaped_expected[] |
| 299 |
This is contents that may include <tags>. |
| 300 |
// end::iinclude_escaped_expected[] |
| 301 |
''' |
| 302 |
assertRendered() |
| 303 |
} |
| 304 |
|
| 305 |
void testExpandEmptyElements() { |
| 306 |
templateContents = ''' |
| 307 |
// tag::expandEmptyElements[] |
| 308 |
p() |
| 309 |
// end::expandEmptyElements[] |
| 310 |
''' |
| 311 |
expectedRendered = stripAsciidocMarkup ''' |
| 312 |
// tag::expandEmptyElements_false[] |
| 313 |
<p/> |
| 314 |
// end::expandEmptyElements_false[] |
| 315 |
''' |
| 316 |
assertRendered() |
| 317 |
config.expandEmptyElements = true |
| 318 |
expectedRendered = stripAsciidocMarkup ''' |
| 319 |
// tag::expandEmptyElements_true[] |
| 320 |
<p></p> |
| 321 |
// end::expandEmptyElements_true[] |
| 322 |
''' |
| 323 |
assertRendered() |
| 324 |
} |
| 325 |
|
| 326 |
void testUseDoubleQuotes() { |
| 327 |
templateContents = ''' |
| 328 |
// tag::useDoubleQuotes[] |
| 329 |
tag(attr:'value') |
| 330 |
// end::useDoubleQuotes[] |
| 331 |
''' |
| 332 |
expectedRendered = stripAsciidocMarkup ''' |
| 333 |
// tag::useDoubleQuotes_false[] |
| 334 |
<tag attr='value'/> |
| 335 |
// end::useDoubleQuotes_false[] |
| 336 |
''' |
| 337 |
assertRendered() |
| 338 |
config.useDoubleQuotes = true |
| 339 |
expectedRendered = stripAsciidocMarkup ''' |
| 340 |
// tag::useDoubleQuotes_true[] |
| 341 |
<tag attr="value"/> |
| 342 |
// end::useDoubleQuotes_true[] |
| 343 |
''' |
| 344 |
assertRendered() |
| 345 |
} |
| 346 |
|
| 347 |
void testNewLineString() { |
| 348 |
config.newLineString = 'BAR' |
| 349 |
templateContents = ''' |
| 350 |
// tag::newLineString[] |
| 351 |
p('foo') |
| 352 |
newLine() |
| 353 |
p('baz') |
| 354 |
// end::newLineString[] |
| 355 |
''' |
| 356 |
expectedRendered = stripAsciidocMarkup ''' |
| 357 |
// tag::newLineString_expected[] |
| 358 |
<p>foo</p>BAR<p>baz</p> |
| 359 |
// end::newLineString_expected[] |
| 360 |
''' |
| 361 |
assertRendered() |
| 362 |
} |
| 363 |
|
| 364 |
void testAutoformat() { |
| 365 |
config.newLineString = '\n' |
| 366 |
|
| 367 |
// do NOT change the Java-style of the code below! Used in documentation! |
| 368 |
// tag::autoformat_setup[] |
| 369 |
config.setAutoNewLine(true); |
| 370 |
config.setAutoIndent(true); |
| 371 |
// end::autoformat_setup[] |
| 372 |
|
| 373 |
templateContents = ''' |
| 374 |
// tag::autoformat_template[] |
| 375 |
html { |
| 376 |
head { |
| 377 |
title('Title') |
| 378 |
} |
| 379 |
} |
| 380 |
// end::autoformat_template[] |
| 381 |
''' |
| 382 |
expectedRendered = stripAsciidocMarkup ''' |
| 383 |
// tag::autoformat_template_expected[] |
| 384 |
<html> |
| 385 |
<head> |
| 386 |
<title>Title</title> |
| 387 |
</head> |
| 388 |
</html> |
| 389 |
// end::autoformat_template_expected[] |
| 390 |
''' |
| 391 |
assertRendered() |
| 392 |
|
| 393 |
templateContents = ''' |
| 394 |
// tag::autoformat_template2[] |
| 395 |
html { |
| 396 |
head { title('Title') |
| 397 |
} |
| 398 |
} |
| 399 |
// end::autoformat_template2[] |
| 400 |
''' |
| 401 |
expectedRendered = stripAsciidocMarkup ''' |
| 402 |
// tag::autoformat_template2_expected[] |
| 403 |
<html> |
| 404 |
<head><title>Title</title> |
| 405 |
</head> |
| 406 |
</html> |
| 407 |
// end::autoformat_template2_expected[] |
| 408 |
''' |
| 409 |
assertRendered() |
| 410 |
|
| 411 |
templateContents = ''' |
| 412 |
// tag::autoformat_template3[] |
| 413 |
html { |
| 414 |
head { |
| 415 |
meta(attr:'value') // <1> |
| 416 |
title('Title') // <2> |
| 417 |
newLine() // <3> |
| 418 |
meta(attr:'value2') // <4> |
| 419 |
} |
| 420 |
} |
| 421 |
// end::autoformat_template3[] |
| 422 |
''' |
| 423 |
expectedRendered = stripAsciidocMarkup ''' |
| 424 |
// tag::autoformat_template3_expected[] |
| 425 |
<html> |
| 426 |
<head> |
| 427 |
<meta attr='value'/><title>Title</title> |
| 428 |
<meta attr='value2'/> |
| 429 |
</head> |
| 430 |
</html> |
| 431 |
// end::autoformat_template3_expected[] |
| 432 |
''' |
| 433 |
assertRendered() |
| 434 |
} |
| 435 |
|
| 436 |
void testAutoEscape() { |
| 437 |
config.newLineString = '\n' |
| 438 |
|
| 439 |
// do NOT change the Java-style of the code below! Used in documentation! |
| 440 |
// tag::autoescape_setup[] |
| 441 |
config.setAutoEscape(false); |
| 442 |
model = new HashMap<String,Object>(); |
| 443 |
model.put("unsafeContents", "I am an <html> hacker."); |
| 444 |
// end::autoescape_setup[] |
| 445 |
|
| 446 |
templateContents = ''' |
| 447 |
// tag::autoescape_template[] |
| 448 |
html { |
| 449 |
body { |
| 450 |
div(unsafeContents) |
| 451 |
} |
| 452 |
} |
| 453 |
// end::autoescape_template[] |
| 454 |
''' |
| 455 |
expectedRendered = stripAsciidocMarkup ''' |
| 456 |
// tag::autoescape_template_expected[] |
| 457 |
<html><body><div>I am an <html> hacker.</div></body></html> |
| 458 |
// end::autoescape_template_expected[] |
| 459 |
''' |
| 460 |
assertRendered() |
| 461 |
|
| 462 |
// tag::autoescape_setup_fixed[] |
| 463 |
config.setAutoEscape(true); |
| 464 |
// end::autoescape_setup_fixed[] |
| 465 |
|
| 466 |
expectedRendered = stripAsciidocMarkup ''' |
| 467 |
// tag::autoescape_template_fixed_expected[] |
| 468 |
<html><body><div>I am an <html> hacker.</div></body></html> |
| 469 |
// end::autoescape_template_fixed_expected[] |
| 470 |
''' |
| 471 |
assertRendered() |
| 472 |
|
| 473 |
templateContents = ''' |
| 474 |
// tag::autoescape_template_unescaped[] |
| 475 |
html { |
| 476 |
body { |
| 477 |
div(unescaped.unsafeContents) |
| 478 |
} |
| 479 |
} |
| 480 |
// end::autoescape_template_unescaped[] |
| 481 |
''' |
| 482 |
|
| 483 |
expectedRendered = stripAsciidocMarkup ''' |
| 484 |
// tag::autoescape_template_expected2[] |
| 485 |
<html><body><div>I am an <html> hacker.</div></body></html> |
| 486 |
// end::autoescape_template_expected2[] |
| 487 |
''' |
| 488 |
assertRendered() |
| 489 |
|
| 490 |
} |
| 491 |
|
| 492 |
void testLocale() { |
| 493 |
// TODO shouldn't this (or a better) test pass in all environments?? |
| 494 |
if (!System.getProperty("os.name").toLowerCase().contains("windows")) { |
| 495 |
config.locale = Locale.ENGLISH |
| 496 |
templateContents = ''' |
| 497 |
// tag::locale_explicit_import[] |
| 498 |
include template: 'locale_include_fr_FR.tpl' |
| 499 |
// end::locale_explicit_import[] |
| 500 |
''' |
| 501 |
expectedRendered = stripAsciidocMarkup ''' |
| 502 |
// tag::locale_explicit_import_expected[] |
| 503 |
Texte en français |
| 504 |
// end::locale_explicit_import_expected[] |
| 505 |
''' |
| 506 |
assertRendered() |
| 507 |
|
| 508 |
templateContents = ''' |
| 509 |
// tag::locale_implicit_import[] |
| 510 |
include template: 'locale_include.tpl' |
| 511 |
// end::locale_implicit_import[] |
| 512 |
''' |
| 513 |
|
| 514 |
expectedRendered = stripAsciidocMarkup ''' |
| 515 |
// tag::locale_implicit_import_expected[] |
| 516 |
Default text |
| 517 |
// end::locale_implicit_import_expected[] |
| 518 |
''' |
| 519 |
assertRendered() |
| 520 |
|
| 521 |
config.locale = Locale.FRANCE |
| 522 |
expectedRendered = stripAsciidocMarkup ''' |
| 523 |
// tag::locale_implicit_import_expected2[] |
| 524 |
Texte en français |
| 525 |
// end::locale_implicit_import_expected2[] |
| 526 |
''' |
| 527 |
assertRendered() |
| 528 |
|
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
void testRenderingSetup() { |
| 533 |
// NOTE: please keep the Java style syntax here, it is used in the docs! |
| 534 |
def writer = new StringWriter() |
| 535 |
// tag::rendering_setup[] |
| 536 |
TemplateConfiguration config = new TemplateConfiguration(); // <1> |
| 537 |
MarkupTemplateEngine engine = new MarkupTemplateEngine(config); // <2> |
| 538 |
Template template = engine.createTemplate("p('test template')"); // <3> |
| 539 |
Map<String, Object> model = new HashMap<>(); // <4> |
| 540 |
Writable output = template.make(model); // <5> |
| 541 |
output.writeTo(writer); // <6> |
| 542 |
// end::rendering_setup[] |
| 543 |
assert writer.toString() == '<p>test template</p>' |
| 544 |
} |
| 545 |
|
| 546 |
void testRenderingByTemplateName() { |
| 547 |
// NOTE: please keep the Java style syntax here, it is used in the docs! |
| 548 |
def writer = new StringWriter() |
| 549 |
TemplateConfiguration config = new TemplateConfiguration(); |
| 550 |
MarkupTemplateEngine engine = new MarkupTemplateEngine(config); |
| 551 |
Map<String, Object> model = new HashMap<>(); |
| 552 |
// tag::rendering_by_name[] |
| 553 |
Template template = engine.createTemplateByPath("main.tpl"); |
| 554 |
Writable output = template.make(model); |
| 555 |
output.writeTo(writer); |
| 556 |
// end::rendering_by_name[] |
| 557 |
assert writer.toString() == '<p>test template</p>' |
| 558 |
} |
| 559 |
|
| 560 |
public static class Car { |
| 561 |
String make |
| 562 |
String model |
| 563 |
} |
| 564 |
|
| 565 |
void testCustomSuperClass() { |
| 566 |
// NOTE: please keep the Java style syntax here, it is used in the docs! |
| 567 |
// tag::custombase_config[] |
| 568 |
config.setBaseTemplateClass(MyTemplate.class); |
| 569 |
// end::custombase_config[] |
| 570 |
|
| 571 |
templateContents = ''' |
| 572 |
// tag::custombase_template[] |
| 573 |
if (hasModule('foo')) { |
| 574 |
p 'Found module [foo]' |
| 575 |
} else { |
| 576 |
p 'Module [foo] not found' |
| 577 |
} |
| 578 |
// end::custombase_template[] |
| 579 |
''' |
| 580 |
expectedRendered = stripAsciidocMarkup ''' |
| 581 |
// tag::custombase_expected[] |
| 582 |
<p>Module [foo] not found</p> |
| 583 |
// end::custombase_expected[] |
| 584 |
''' |
| 585 |
assertRendered() |
| 586 |
} |
| 587 |
|
| 588 |
void testTypeCheckedModel() { |
| 589 |
// NOTE: please keep the Java style syntax here, it is used in the docs! |
| 590 |
// tag::typechecked_setup_no_stc[] |
| 591 |
Page p = new Page(); |
| 592 |
p.setTitle("Sample page"); |
| 593 |
p.setBody("Page body"); |
| 594 |
List<Page> pages = new LinkedList<>(); |
| 595 |
pages.add(p); |
| 596 |
model = new HashMap<String,Object>(); |
| 597 |
model.put("pages", pages); |
| 598 |
// end::typechecked_setup_no_stc[] |
| 599 |
|
| 600 |
templateContents = ''' |
| 601 |
// tag::typechecked_template[] |
| 602 |
pages.each { page -> // <1> |
| 603 |
p("Page title: $page.title") // <2> |
| 604 |
p(page.text) // <3> |
| 605 |
} |
| 606 |
// end::typechecked_template[] |
| 607 |
''' |
| 608 |
expectedRendered = stripAsciidocMarkup ''' |
| 609 |
// tag::typechecked_template_expected[] |
| 610 |
<p>Page title: Sample page</p><p>Page body</p> |
| 611 |
// end::typechecked_template_expected[] |
| 612 |
''' |
| 613 |
assertError 'No such property: text' |
| 614 |
|
| 615 |
|
| 616 |
modelTypes = [pages: 'List<MarkupTemplateEngineSpecTest.Page>'] |
| 617 |
/* |
| 618 |
// tag::typechecked_setup_fixed[] |
| 619 |
modelTypes = new HashMap<String,String>(); // <1> |
| 620 |
modelTypes.put("pages", "List<Page>"); // <2> |
| 621 |
Template template = engine.createTypeCheckedModelTemplate("main.tpl", modelTypes) // <3> |
| 622 |
// end::typechecked_setup_fixed[] |
| 623 |
*/ |
| 624 |
|
| 625 |
assertError 'No such property: text for class: MarkupTemplateEngineSpecTest$Page' |
| 626 |
} |
| 627 |
|
| 628 |
void testInlinedTypeCheckedModel() { |
| 629 |
// NOTE: please keep the Java style syntax here, it is used in the docs! |
| 630 |
// tag::typechecked_inlined_setup[] |
| 631 |
Page p = new Page(); |
| 632 |
p.setTitle("Sample page"); |
| 633 |
p.setBody("Page body"); |
| 634 |
List<Page> pages = new LinkedList<>(); |
| 635 |
pages.add(p); |
| 636 |
model = new HashMap<String,Object>(); |
| 637 |
model.put("pages", pages); |
| 638 |
// end::typechecked_inlined_setup[] |
| 639 |
|
| 640 |
templateContents = '''import MarkupTemplateEngineSpecTest.Page as Page |
| 641 |
// tag::typechecked_inlined_template[] |
| 642 |
modelTypes = { // <1> |
| 643 |
List<Page> pages // <2> |
| 644 |
} |
| 645 |
|
| 646 |
pages.each { page -> |
| 647 |
p("Page title: $page.title") |
| 648 |
p(page.text) |
| 649 |
} |
| 650 |
// end::typechecked_inlined_template[] |
| 651 |
''' |
| 652 |
expectedRendered = stripAsciidocMarkup ''' |
| 653 |
// tag::typechecked_inlined_template_expected[] |
| 654 |
<p>Page title: Sample page</p><p>Page body</p> |
| 655 |
// end::typechecked_inlined_template_expected[] |
| 656 |
''' |
| 657 |
|
| 658 |
assertError 'No such property: text for class: MarkupTemplateEngineSpecTest$Page' |
| 659 |
} |
| 660 |
|
| 661 |
void testFragment() { |
| 662 |
model = new HashMap<String,Object>(); |
| 663 |
model.put("pages", Arrays.asList("Page 1", "Page 2")); |
| 664 |
templateContents = ''' |
| 665 |
// tag::fragment_template[] |
| 666 |
ul { |
| 667 |
pages.each { |
| 668 |
fragment "li(line)", line:it |
| 669 |
} |
| 670 |
} |
| 671 |
// end::fragment_template[] |
| 672 |
''' |
| 673 |
expectedRendered = stripAsciidocMarkup ''' |
| 674 |
// tag::fragment_expected[] |
| 675 |
<ul><li>Page 1</li><li>Page 2</li></ul> |
| 676 |
// end::fragment_expected[] |
| 677 |
''' |
| 678 |
assertRendered() |
| 679 |
} |
| 680 |
|
| 681 |
void testLayout() { |
| 682 |
templateContents = ''' |
| 683 |
// tag::layout_template[] |
| 684 |
layout 'layout-main.tpl', // <1> |
| 685 |
title: 'Layout example', // <2> |
| 686 |
bodyContents: contents { p('This is the body') } // <3> |
| 687 |
// end::layout_template[] |
| 688 |
''' |
| 689 |
expectedRendered = stripAsciidocMarkup ''' |
| 690 |
// tag::layout_expected[] |
| 691 |
<html><head><title>Layout example</title></head><body><p>This is the body</p></body></html> |
| 692 |
// end::layout_expected[] |
| 693 |
''' |
| 694 |
assertRendered() |
| 695 |
} |
| 696 |
|
| 697 |
void testLayoutWithInheritedModel() { |
| 698 |
// tag::layout_template_inherit_model[] |
| 699 |
model = new HashMap<String,Object>(); |
| 700 |
model.put('title','Title from main model'); |
| 701 |
// end::layout_template_inherit_model[] |
| 702 |
|
| 703 |
templateContents = ''' |
| 704 |
// tag::layout_template_inherit[] |
| 705 |
layout 'layout-main.tpl', true, // <1> |
| 706 |
bodyContents: contents { p('This is the body') } |
| 707 |
// end::layout_template_inherit[] |
| 708 |
''' |
| 709 |
expectedRendered = stripAsciidocMarkup ''' |
| 710 |
// tag::layout_expected_2[] |
| 711 |
<html><head><title>Title from main model</title></head><body><p>This is the body</p></body></html> |
| 712 |
// end::layout_expected_2[] |
| 713 |
''' |
| 714 |
assertRendered() |
| 715 |
|
| 716 |
templateContents = ''' |
| 717 |
// tag::layout_template_inherit_override[] |
| 718 |
layout 'layout-main.tpl', true, // <1> |
| 719 |
title: 'overridden title', // <2> |
| 720 |
bodyContents: contents { p('This is the body') } |
| 721 |
// end::layout_template_inherit_override[] |
| 722 |
''' |
| 723 |
expectedRendered = stripAsciidocMarkup ''' |
| 724 |
// tag::layout_expected_3[] |
| 725 |
<html><head><title>overridden title</title></head><body><p>This is the body</p></body></html> |
| 726 |
// end::layout_expected_3[] |
| 727 |
''' |
| 728 |
assertRendered() |
| 729 |
} |
| 730 |
|
| 731 |
void testStringMarkupGotcha() { |
| 732 |
templateContents = ''' |
| 733 |
// tag::gotcha_strings_longversion[] |
| 734 |
p { |
| 735 |
yield "This is a " |
| 736 |
a(href:'target.html', "link") |
| 737 |
yield " to another page" |
| 738 |
} |
| 739 |
// end::gotcha_strings_longversion[] |
| 740 |
''' |
| 741 |
expectedRendered = stripAsciidocMarkup ''' |
| 742 |
// tag::gotcha_strings_longversion_expected[] |
| 743 |
<p>This is a <a href='target.html'>link</a> to another page</p> |
| 744 |
// end::gotcha_strings_longversion_expected[] |
| 745 |
''' |
| 746 |
assertRendered() |
| 747 |
|
| 748 |
templateContents = ''' |
| 749 |
// tag::gotcha_strings_naive_fail[] |
| 750 |
p { |
| 751 |
yield "This is a ${a(href:'target.html', "link")} to another page" |
| 752 |
} |
| 753 |
// end::gotcha_strings_naive_fail[] |
| 754 |
''' |
| 755 |
expectedRendered = stripAsciidocMarkup ''' |
| 756 |
// tag::gotcha_strings_naive_fail_expected[] |
| 757 |
<p><a href='target.html'>link</a>This is a to another page</p> |
| 758 |
// end::gotcha_strings_naive_fail_expected[] |
| 759 |
''' |
| 760 |
assertRendered() |
| 761 |
|
| 762 |
templateContents = ''' |
| 763 |
// tag::gotcha_strings_stringof[] |
| 764 |
p("This is a ${stringOf {a(href:'target.html', "link")}} to another page") |
| 765 |
// end::gotcha_strings_stringof[] |
| 766 |
''' |
| 767 |
expectedRendered = stripAsciidocMarkup ''' |
| 768 |
// tag::gotcha_strings_stringof_expected[] |
| 769 |
<p>This is a <a href='target.html'>link</a> to another page</p> |
| 770 |
// end::gotcha_strings_stringof_expected[] |
| 771 |
''' |
| 772 |
assertRendered() |
| 773 |
|
| 774 |
templateContents = ''' |
| 775 |
// tag::gotcha_strings_stringof_dollar[] |
| 776 |
p("This is a ${$a(href:'target.html', "link")} to another page") |
| 777 |
// end::gotcha_strings_stringof_dollar[] |
| 778 |
''' |
| 779 |
expectedRendered = stripAsciidocMarkup ''' |
| 780 |
// tag::gotcha_strings_stringof_dollar_expected[] |
| 781 |
<p>This is a <a href='target.html'>link</a> to another page</p> |
| 782 |
// end::gotcha_strings_stringof_dollar_expected[] |
| 783 |
''' |
| 784 |
assertRendered() |
| 785 |
|
| 786 |
} |
| 787 |
|
| 788 |
// tag::page_class[] |
| 789 |
public class Page { |
| 790 |
|
| 791 |
Long id |
| 792 |
String title |
| 793 |
String body |
| 794 |
} |
| 795 |
// end::page_class[] |
| 796 |
} |