| 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 gls.CompilableTestSupport |
| 20 |
|
| 21 |
class TemplateEnginesTest extends CompilableTestSupport { |
| 22 |
|
| 23 |
void testSimpleTemplateEngine1() { |
| 24 |
// tag::simple_template_engine1[] |
| 25 |
def text = 'Dear "$firstname $lastname",\nSo nice to meet you in <% print city %>.\nSee you in ${month},\n${signed}' |
| 26 |
|
| 27 |
def binding = ["firstname":"Sam", "lastname":"Pullara", "city":"San Francisco", "month":"December", "signed":"Groovy-Dev"] |
| 28 |
|
| 29 |
def engine = new groovy.text.SimpleTemplateEngine() |
| 30 |
def template = engine.createTemplate(text).make(binding) |
| 31 |
|
| 32 |
def result = 'Dear "Sam Pullara",\nSo nice to meet you in San Francisco.\nSee you in December,\nGroovy-Dev' |
| 33 |
|
| 34 |
assert result == template.toString() |
| 35 |
// end::simple_template_engine1[] |
| 36 |
} |
| 37 |
|
| 38 |
void testSimpleTemplateEngineOther() { |
| 39 |
def binding = [firstname: 'andrey'] |
| 40 |
def engine = new groovy.text.SimpleTemplateEngine() |
| 41 |
def text = '''\ |
| 42 |
// tag::simple_template_engine2[] |
| 43 |
$firstname |
| 44 |
// end::simple_template_engine2[] |
| 45 |
''' |
| 46 |
def template = engine.createTemplate(text).make(binding) |
| 47 |
def result = template.toString() |
| 48 |
assert result.readLines()[1].trim() == 'andrey' |
| 49 |
|
| 50 |
text = '''\ |
| 51 |
// tag::simple_template_engine3[] |
| 52 |
${firstname.capitalize()} |
| 53 |
// end::simple_template_engine3[] |
| 54 |
''' |
| 55 |
template = engine.createTemplate(text).make(binding) |
| 56 |
result = template.toString() |
| 57 |
assert result.readLines()[1].trim() == 'Andrey' |
| 58 |
|
| 59 |
binding = [city: 'Moscow'] |
| 60 |
text = '''\ |
| 61 |
// tag::simple_template_engine4[] |
| 62 |
<% print city %> |
| 63 |
// end::simple_template_engine4[] |
| 64 |
''' |
| 65 |
template = engine.createTemplate(text).make(binding) |
| 66 |
result = template.toString() |
| 67 |
assert result.readLines()[1].trim() == 'Moscow' |
| 68 |
|
| 69 |
binding = [city: 'New York'] |
| 70 |
text = '''\ |
| 71 |
// tag::simple_template_engine5[] |
| 72 |
<% print city == "New York" ? "The Big Apple" : city %> |
| 73 |
// end::simple_template_engine5[] |
| 74 |
''' |
| 75 |
template = engine.createTemplate(text).make(binding) |
| 76 |
result = template.toString() |
| 77 |
assert result.readLines()[1].trim() == 'The Big Apple' |
| 78 |
|
| 79 |
text = '''\ |
| 80 |
// tag::simple_template_engine6[] |
| 81 |
<% print city == "New York" ? "\\"The Big Apple\\"" : city %> |
| 82 |
// end::simple_template_engine6[] |
| 83 |
''' |
| 84 |
template = engine.createTemplate(text).make(binding) |
| 85 |
result = template.toString() |
| 86 |
assert result.readLines()[1].trim() == '"The Big Apple"' |
| 87 |
|
| 88 |
text = '''\ |
| 89 |
// tag::simple_template_engine7[] |
| 90 |
\\n |
| 91 |
// end::simple_template_engine7[] |
| 92 |
''' |
| 93 |
template = engine.createTemplate(text).make() |
| 94 |
result = template.toString() |
| 95 |
assert result.readLines().size() == 5 |
| 96 |
|
| 97 |
text = '''\ |
| 98 |
// tag::simple_template_engine8[] |
| 99 |
\\\\ |
| 100 |
// end::simple_template_engine8[] |
| 101 |
''' |
| 102 |
template = engine.createTemplate(text).make() |
| 103 |
result = template.toString() |
| 104 |
assert result.readLines()[1].trim() == '\\' |
| 105 |
} |
| 106 |
|
| 107 |
void testStreamingTemplateEngine() { |
| 108 |
// tag::streaming_template_engine[] |
| 109 |
def text = '''\ |
| 110 |
Dear <% out.print firstname %> ${lastname}, |
| 111 |
|
| 112 |
We <% if (accepted) out.print 'are pleased' else out.print 'regret' %> \ |
| 113 |
to inform you that your paper entitled |
| 114 |
'$title' was ${ accepted ? 'accepted' : 'rejected' }. |
| 115 |
|
| 116 |
The conference committee.''' |
| 117 |
|
| 118 |
def template = new groovy.text.StreamingTemplateEngine().createTemplate(text) |
| 119 |
|
| 120 |
def binding = [ |
| 121 |
firstname : "Grace", |
| 122 |
lastname : "Hopper", |
| 123 |
accepted : true, |
| 124 |
title : 'Groovy for COBOL programmers' |
| 125 |
] |
| 126 |
|
| 127 |
String response = template.make(binding) |
| 128 |
|
| 129 |
assert response == '''Dear Grace Hopper, |
| 130 |
|
| 131 |
We are pleased to inform you that your paper entitled |
| 132 |
'Groovy for COBOL programmers' was accepted. |
| 133 |
|
| 134 |
The conference committee.''' |
| 135 |
// end::streaming_template_engine[] |
| 136 |
} |
| 137 |
|
| 138 |
void testGStringTemplateEngine() { |
| 139 |
def binding = [firstname: 'Sam', lastname: 'Pullara', city: 'New York', month: 'December', signed: 'Groovy-Dev'] |
| 140 |
def engine = new groovy.text.GStringTemplateEngine() |
| 141 |
def text = '''\ |
| 142 |
// tag::gstring_template_engine1[] |
| 143 |
Dear "$firstname $lastname", |
| 144 |
So nice to meet you in <% out << (city == "New York" ? "\\"The Big Apple\\"" : city) %>. |
| 145 |
See you in ${month}, |
| 146 |
${signed} |
| 147 |
// end::gstring_template_engine1[] |
| 148 |
''' |
| 149 |
def template = engine.createTemplate(text).make(binding) |
| 150 |
List result = template.toString().readLines() |
| 151 |
result.remove(0); result.remove(result.size() - 2) |
| 152 |
assert result.join('\n') == '''\ |
| 153 |
Dear "Sam Pullara", |
| 154 |
So nice to meet you in "The Big Apple". |
| 155 |
See you in December, |
| 156 |
Groovy-Dev |
| 157 |
''' |
| 158 |
|
| 159 |
shouldCompile ''' |
| 160 |
// tag::gstring_template_engine2[] |
| 161 |
def f = new File('test.template') |
| 162 |
def engine = new groovy.text.GStringTemplateEngine() |
| 163 |
def template = engine.createTemplate(f).make(binding) |
| 164 |
println template.toString() |
| 165 |
// end::gstring_template_engine2[] |
| 166 |
''' |
| 167 |
} |
| 168 |
|
| 169 |
void testXmlTemplateEngine() { |
| 170 |
// tag::xml_template_engine[] |
| 171 |
def binding = [firstname: 'Jochen', lastname: 'Theodorou', nickname: 'blackdrag', salutation: 'Dear'] |
| 172 |
def engine = new groovy.text.XmlTemplateEngine() |
| 173 |
def text = '''\ |
| 174 |
<document xmlns:gsp='http://groovy.codehaus.org/2005/gsp' xmlns:foo='baz' type='letter'> |
| 175 |
<gsp:scriptlet>def greeting = "${salutation}est"</gsp:scriptlet> |
| 176 |
<gsp:expression>greeting</gsp:expression> |
| 177 |
<foo:to>$firstname "$nickname" $lastname</foo:to> |
| 178 |
How are you today? |
| 179 |
</document> |
| 180 |
''' |
| 181 |
def template = engine.createTemplate(text).make(binding) |
| 182 |
println template.toString() |
| 183 |
// end::xml_template_engine[] |
| 184 |
|
| 185 |
assert template.toString() == '''\ |
| 186 |
<document type='letter'> |
| 187 |
Dearest |
| 188 |
<foo:to xmlns:foo='baz'> |
| 189 |
Jochen "blackdrag" Theodorou |
| 190 |
</foo:to> |
| 191 |
How are you today? |
| 192 |
</document> |
| 193 |
''' |
| 194 |
} |
| 195 |
} |