/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import groovy.text.Template import groovy.text.markup.MarkupTemplateEngine import groovy.text.markup.TemplateConfiguration import java.nio.charset.Charset class MarkupTemplateEngineSpecTest extends GroovyTestCase { private MarkupTemplateEngine engine private TemplateConfiguration config private Map model private Map modelTypes private String templateContents private String expectedRendered @Override void setUp() { super.setUp(); config = new TemplateConfiguration() model = null modelTypes = null } @Override void tearDown() { super.tearDown(); config = null engine = null } private void assertRendered() { engine = new MarkupTemplateEngine(this.class.classLoader, config) def template = modelTypes ? engine.createTypeCheckedModelTemplate(templateContents, modelTypes) : engine.createTemplate(templateContents); def ready = template.make(model) def sw = new StringWriter() ready.writeTo(sw) assert sw.toString() == expectedRendered } private void assertError(String error) { def msg = shouldFail { assertRendered() } assert msg.contains(error) } private String stripAsciidocMarkup(String string) { boolean inside = false StringBuilder sb = new StringBuilder() new StringReader(string).eachLine { line -> if (line =~ 'tag::') { inside = true } else if (line =~ 'end::') { inside = false } else if (inside) { if (sb.length() > 0) { sb.append(config.newLineString) } sb.append(line) } } sb } void testSimpleTemplate() { templateContents = ''' // tag::example1_template[] xmlDeclaration() cars { cars.each { car(make: it.make, model: it.model) } } // end::example1_template[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::example1_expected[] // end::example1_expected[] ''' // tag::example1_model[] model = [cars: [new Car(make: 'Peugeot', model: '508'), new Car(make: 'Toyota', model: 'Prius')]] // end::example1_model[] assertRendered() /* // tag::example1_template_with_bullets[] xmlDeclaration() <1> cars { <2> cars.each { <3> car(make: it.make, model: it.model) <4> } <5> } // end::example1_template_with_bullets[] */ } void testSimpleHtmlTemplate() { templateContents = ''' // tag::example2_template[] yieldUnescaped '' // <1> html(lang:'en') { // <2> head { // <3> meta('http-equiv':'"Content-Type" content="text/html; charset=utf-8"') // <4> title('My page') // <5> } // <6> body { // <7> p('This is an example of HTML contents') // <8> } // <9> } // <10> // end::example2_template[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::example2_expected[] My page

This is an example of HTML contents

// end::example2_expected[] ''' assertRendered() } void testYield() { templateContents = ''' // tag::yield[] yield 'Some text with ' // end::yield[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::yield_expected[] Some text with <angle brackets> // end::yield_expected[] ''' assertRendered() } void testYieldUnescaped() { templateContents = ''' // tag::yieldUnescaped[] yieldUnescaped 'Some text with ' // end::yieldUnescaped[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::yieldUnescaped_expected[] Some text with // end::yieldUnescaped_expected[] ''' assertRendered() } void testXMLDeclaration() { config.newLineString = '' templateContents = ''' // tag::xmlDeclaration[] xmlDeclaration() // end::xmlDeclaration[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::xmlDeclaration_expected[] // end::xmlDeclaration_expected[] ''' assertRendered() } void testXMLDeclarationWithEncoding() { config.newLineString = '' config.declarationEncoding = 'UTF-8' templateContents = ''' // tag::xmlDeclaration_encoding[] xmlDeclaration() // end::xmlDeclaration_encoding[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::xmlDeclaration_encoding_expected[] // end::xmlDeclaration_encoding_expected[] ''' assertRendered() } void testComment() { templateContents = ''' // tag::comment[] comment 'This is commented out' // end::comment[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::comment_expected[] // end::comment_expected[] ''' assertRendered() } void testNewLine() { config.newLineString = '\n' templateContents = ''' // tag::newline[] p('text') newLine() p('text on new line') // end::newline[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::newline_expected[]

text

text on new line

// end::newline_expected[] ''' assertRendered() } void testPI() { config.newLineString = '' templateContents = ''' // tag::pi[] pi("xml-stylesheet":[href:"mystyle.css", type:"text/css"]) // end::pi[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::pi_expected[] // end::pi_expected[] ''' assertRendered() } void testTryEscape() { templateContents = ''' // tag::tryEscape[] yieldUnescaped tryEscape('Some text with ') // end::tryEscape[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::tryEscape_expected[] Some text with <angle brackets> // end::tryEscape_expected[] ''' assertRendered() } void testIncludeTemplate() { templateContents = ''' // tag::include_template[] include template: 'other_template.tpl' // end::include_template[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::include_template_expected[] included as a template // end::include_template_expected[] ''' assertRendered() } void testIncludeRaw() { templateContents = ''' // tag::include_raw[] include unescaped: 'raw.txt' // end::include_raw[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::include_raw_expected[] This is raw contents that may include . // end::include_raw_expected[] ''' assertRendered() } void testIncludeEscaped() { templateContents = ''' // tag::include_escaped[] include escaped: 'to_be_escaped.txt' // end::include_escaped[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::include_escaped_expected[] This is contents that may include <tags>. // end::iinclude_escaped_expected[] ''' assertRendered() } void testExpandEmptyElements() { templateContents = ''' // tag::expandEmptyElements[] p() // end::expandEmptyElements[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::expandEmptyElements_false[]

// end::expandEmptyElements_false[] ''' assertRendered() config.expandEmptyElements = true expectedRendered = stripAsciidocMarkup ''' // tag::expandEmptyElements_true[]

// end::expandEmptyElements_true[] ''' assertRendered() } void testUseDoubleQuotes() { templateContents = ''' // tag::useDoubleQuotes[] tag(attr:'value') // end::useDoubleQuotes[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::useDoubleQuotes_false[] // end::useDoubleQuotes_false[] ''' assertRendered() config.useDoubleQuotes = true expectedRendered = stripAsciidocMarkup ''' // tag::useDoubleQuotes_true[] // end::useDoubleQuotes_true[] ''' assertRendered() } void testNewLineString() { config.newLineString = 'BAR' templateContents = ''' // tag::newLineString[] p('foo') newLine() p('baz') // end::newLineString[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::newLineString_expected[]

foo

BAR

baz

// end::newLineString_expected[] ''' assertRendered() } void testAutoformat() { config.newLineString = '\n' // do NOT change the Java-style of the code below! Used in documentation! // tag::autoformat_setup[] config.setAutoNewLine(true); config.setAutoIndent(true); // end::autoformat_setup[] templateContents = ''' // tag::autoformat_template[] html { head { title('Title') } } // end::autoformat_template[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::autoformat_template_expected[] Title // end::autoformat_template_expected[] ''' assertRendered() templateContents = ''' // tag::autoformat_template2[] html { head { title('Title') } } // end::autoformat_template2[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::autoformat_template2_expected[] Title // end::autoformat_template2_expected[] ''' assertRendered() templateContents = ''' // tag::autoformat_template3[] html { head { meta(attr:'value') // <1> title('Title') // <2> newLine() // <3> meta(attr:'value2') // <4> } } // end::autoformat_template3[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::autoformat_template3_expected[] Title // end::autoformat_template3_expected[] ''' assertRendered() } void testAutoEscape() { config.newLineString = '\n' // do NOT change the Java-style of the code below! Used in documentation! // tag::autoescape_setup[] config.setAutoEscape(false); model = new HashMap(); model.put("unsafeContents", "I am an hacker."); // end::autoescape_setup[] templateContents = ''' // tag::autoescape_template[] html { body { div(unsafeContents) } } // end::autoescape_template[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::autoescape_template_expected[]
I am an hacker.
// end::autoescape_template_expected[] ''' assertRendered() // tag::autoescape_setup_fixed[] config.setAutoEscape(true); // end::autoescape_setup_fixed[] expectedRendered = stripAsciidocMarkup ''' // tag::autoescape_template_fixed_expected[]
I am an <html> hacker.
// end::autoescape_template_fixed_expected[] ''' assertRendered() templateContents = ''' // tag::autoescape_template_unescaped[] html { body { div(unescaped.unsafeContents) } } // end::autoescape_template_unescaped[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::autoescape_template_expected2[]
I am an hacker.
// end::autoescape_template_expected2[] ''' assertRendered() } void testLocale() { // TODO shouldn't this (or a better) test pass in all environments?? if (!System.getProperty("os.name").toLowerCase().contains("windows")) { config.locale = Locale.ENGLISH templateContents = ''' // tag::locale_explicit_import[] include template: 'locale_include_fr_FR.tpl' // end::locale_explicit_import[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::locale_explicit_import_expected[] Texte en français // end::locale_explicit_import_expected[] ''' assertRendered() templateContents = ''' // tag::locale_implicit_import[] include template: 'locale_include.tpl' // end::locale_implicit_import[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::locale_implicit_import_expected[] Default text // end::locale_implicit_import_expected[] ''' assertRendered() config.locale = Locale.FRANCE expectedRendered = stripAsciidocMarkup ''' // tag::locale_implicit_import_expected2[] Texte en français // end::locale_implicit_import_expected2[] ''' assertRendered() } } void testRenderingSetup() { // NOTE: please keep the Java style syntax here, it is used in the docs! def writer = new StringWriter() // tag::rendering_setup[] TemplateConfiguration config = new TemplateConfiguration(); // <1> MarkupTemplateEngine engine = new MarkupTemplateEngine(config); // <2> Template template = engine.createTemplate("p('test template')"); // <3> Map model = new HashMap<>(); // <4> Writable output = template.make(model); // <5> output.writeTo(writer); // <6> // end::rendering_setup[] assert writer.toString() == '

test template

' } void testRenderingByTemplateName() { // NOTE: please keep the Java style syntax here, it is used in the docs! def writer = new StringWriter() TemplateConfiguration config = new TemplateConfiguration(); MarkupTemplateEngine engine = new MarkupTemplateEngine(config); Map model = new HashMap<>(); // tag::rendering_by_name[] Template template = engine.createTemplateByPath("main.tpl"); Writable output = template.make(model); output.writeTo(writer); // end::rendering_by_name[] assert writer.toString() == '

test template

' } public static class Car { String make String model } void testCustomSuperClass() { // NOTE: please keep the Java style syntax here, it is used in the docs! // tag::custombase_config[] config.setBaseTemplateClass(MyTemplate.class); // end::custombase_config[] templateContents = ''' // tag::custombase_template[] if (hasModule('foo')) { p 'Found module [foo]' } else { p 'Module [foo] not found' } // end::custombase_template[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::custombase_expected[]

Module [foo] not found

// end::custombase_expected[] ''' assertRendered() } void testTypeCheckedModel() { // NOTE: please keep the Java style syntax here, it is used in the docs! // tag::typechecked_setup_no_stc[] Page p = new Page(); p.setTitle("Sample page"); p.setBody("Page body"); List pages = new LinkedList<>(); pages.add(p); model = new HashMap(); model.put("pages", pages); // end::typechecked_setup_no_stc[] templateContents = ''' // tag::typechecked_template[] pages.each { page -> // <1> p("Page title: $page.title") // <2> p(page.text) // <3> } // end::typechecked_template[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::typechecked_template_expected[]

Page title: Sample page

Page body

// end::typechecked_template_expected[] ''' assertError 'No such property: text' modelTypes = [pages: 'List'] /* // tag::typechecked_setup_fixed[] modelTypes = new HashMap(); // <1> modelTypes.put("pages", "List"); // <2> Template template = engine.createTypeCheckedModelTemplate("main.tpl", modelTypes) // <3> // end::typechecked_setup_fixed[] */ assertError 'No such property: text for class: MarkupTemplateEngineSpecTest$Page' } void testInlinedTypeCheckedModel() { // NOTE: please keep the Java style syntax here, it is used in the docs! // tag::typechecked_inlined_setup[] Page p = new Page(); p.setTitle("Sample page"); p.setBody("Page body"); List pages = new LinkedList<>(); pages.add(p); model = new HashMap(); model.put("pages", pages); // end::typechecked_inlined_setup[] templateContents = '''import MarkupTemplateEngineSpecTest.Page as Page // tag::typechecked_inlined_template[] modelTypes = { // <1> List pages // <2> } pages.each { page -> p("Page title: $page.title") p(page.text) } // end::typechecked_inlined_template[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::typechecked_inlined_template_expected[]

Page title: Sample page

Page body

// end::typechecked_inlined_template_expected[] ''' assertError 'No such property: text for class: MarkupTemplateEngineSpecTest$Page' } void testFragment() { model = new HashMap(); model.put("pages", Arrays.asList("Page 1", "Page 2")); templateContents = ''' // tag::fragment_template[] ul { pages.each { fragment "li(line)", line:it } } // end::fragment_template[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::fragment_expected[]
  • Page 1
  • Page 2
// end::fragment_expected[] ''' assertRendered() } void testLayout() { templateContents = ''' // tag::layout_template[] layout 'layout-main.tpl', // <1> title: 'Layout example', // <2> bodyContents: contents { p('This is the body') } // <3> // end::layout_template[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::layout_expected[] Layout example

This is the body

// end::layout_expected[] ''' assertRendered() } void testLayoutWithInheritedModel() { // tag::layout_template_inherit_model[] model = new HashMap(); model.put('title','Title from main model'); // end::layout_template_inherit_model[] templateContents = ''' // tag::layout_template_inherit[] layout 'layout-main.tpl', true, // <1> bodyContents: contents { p('This is the body') } // end::layout_template_inherit[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::layout_expected_2[] Title from main model

This is the body

// end::layout_expected_2[] ''' assertRendered() templateContents = ''' // tag::layout_template_inherit_override[] layout 'layout-main.tpl', true, // <1> title: 'overridden title', // <2> bodyContents: contents { p('This is the body') } // end::layout_template_inherit_override[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::layout_expected_3[] overridden title

This is the body

// end::layout_expected_3[] ''' assertRendered() } void testStringMarkupGotcha() { templateContents = ''' // tag::gotcha_strings_longversion[] p { yield "This is a " a(href:'target.html', "link") yield " to another page" } // end::gotcha_strings_longversion[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::gotcha_strings_longversion_expected[]

This is a link to another page

// end::gotcha_strings_longversion_expected[] ''' assertRendered() templateContents = ''' // tag::gotcha_strings_naive_fail[] p { yield "This is a ${a(href:'target.html', "link")} to another page" } // end::gotcha_strings_naive_fail[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::gotcha_strings_naive_fail_expected[]

linkThis is a to another page

// end::gotcha_strings_naive_fail_expected[] ''' assertRendered() templateContents = ''' // tag::gotcha_strings_stringof[] p("This is a ${stringOf {a(href:'target.html', "link")}} to another page") // end::gotcha_strings_stringof[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::gotcha_strings_stringof_expected[]

This is a link to another page

// end::gotcha_strings_stringof_expected[] ''' assertRendered() templateContents = ''' // tag::gotcha_strings_stringof_dollar[] p("This is a ${$a(href:'target.html', "link")} to another page") // end::gotcha_strings_stringof_dollar[] ''' expectedRendered = stripAsciidocMarkup ''' // tag::gotcha_strings_stringof_dollar_expected[]

This is a link to another page

// end::gotcha_strings_stringof_dollar_expected[] ''' assertRendered() } // tag::page_class[] public class Page { Long id String title String body } // end::page_class[] }