From 8c7703b7f9931ecfed30715a0dac9d853b2918f7 Mon Sep 17 00:00:00 2001 From: Marcos Pereira Date: Sun, 12 May 2024 06:55:37 -0400 Subject: [PATCH] Use Java modern features/APIs (#357) * refactor: Run openrewrite to use Java 17 features This mainly uses: - Text blocks: https://openjdk.org/jeps/378 - Pattern matching: https://openjdk.org/jeps/394 * refactor: a few usages of newer Collections APIs --- .../src/main/java/example/JteContext.java | 14 +- .../convert/jsp/JspToJteConverterTest.java | 6 +- .../compiler/kotlin/KotlinClassCompiler.java | 2 +- .../gg/jte/kotlin/TemplateEngineTest.java | 844 +++++++++++------- .../TemplateEngine_HtmlInterceptorTest.java | 368 ++++---- ...TemplateEngine_HtmlOutputEscapingTest.java | 428 +++++---- .../gg/jte/models/generator/ModelConfig.java | 2 +- .../java/gg/jte/models/generator/Util.java | 2 +- .../gg/jte/models/runtime/StaticJteModel.java | 4 +- .../models/generator/TestModelExtension.java | 28 +- .../nativeimage/NativeResourcesExtension.java | 2 +- .../src/main/java/gg/jte/TemplateEngine.java | 9 +- .../gg/jte/support/LocalizationSupport.java | 48 +- .../main/java/gg/jte/compiler/ClassUtils.java | 4 +- .../main/java/gg/jte/compiler/LineInfo.java | 6 +- .../gg/jte/compiler/TemplateCompiler.java | 12 +- .../java/gg/jte/compiler/TemplateParser.java | 4 +- .../ExtensionTemplateDescription.java | 2 +- .../test/java/gg/jte/DummyCodeResolver.java | 2 +- .../java/gg/jte/PreCompileTemplatesTest.java | 2 +- .../test/java/gg/jte/TemplateEngineTest.java | 298 ++++--- .../TemplateEngine_HtmlInterceptorTest.java | 30 +- ...TemplateEngine_HtmlOutputEscapingTest.java | 138 ++- .../gg/jte/TemplateEngine_MapParamsTest.java | 6 +- .../gg/jte/output/Utf8ByteOutputTest.java | 6 +- 25 files changed, 1340 insertions(+), 927 deletions(-) diff --git a/jte-jsp-converter/src/main/java/example/JteContext.java b/jte-jsp-converter/src/main/java/example/JteContext.java index f9db2e5b..2f9df712 100644 --- a/jte-jsp-converter/src/main/java/example/JteContext.java +++ b/jte-jsp-converter/src/main/java/example/JteContext.java @@ -17,14 +17,14 @@ public class JteContext { public static boolean isEmpty(Object obj) { if (obj == null) { return true; - } else if (obj instanceof String) { - return ((String) obj).length() == 0; - } else if (obj instanceof Object[]) { - return ((Object[]) obj).length == 0; - } else if (obj instanceof Collection) { - return ((Collection) obj).isEmpty(); + } else if (obj instanceof String string) { + return string.isEmpty(); + } else if (obj instanceof Object[] objects) { + return objects.length == 0; + } else if (obj instanceof Collection collection) { + return collection.isEmpty(); } else { - return obj instanceof Map && ((Map) obj).isEmpty(); + return obj instanceof Map m && m.isEmpty(); } } diff --git a/jte-jsp-converter/src/test/java/gg/jte/convert/jsp/JspToJteConverterTest.java b/jte-jsp-converter/src/test/java/gg/jte/convert/jsp/JspToJteConverterTest.java index 3489d654..3e7dd1dd 100644 --- a/jte-jsp-converter/src/test/java/gg/jte/convert/jsp/JspToJteConverterTest.java +++ b/jte-jsp-converter/src/test/java/gg/jte/convert/jsp/JspToJteConverterTest.java @@ -155,8 +155,10 @@ void simpleTagWithNotYetConvertedTags() { givenUsecase("simpleTagWithNotYetConvertedTags"); Throwable throwable = catchThrowable(() -> whenJspTagIsConverted("my/simple.tag", "tag/my/simple.jte")); assertThat(throwable).isInstanceOf(UnsupportedOperationException.class).hasMessage( - "The tag is used by this tag and not converted to jte yet. You should convert first. If this is a tag that should be always converted by hand, implement getNotConvertedTags() and add it there.\n" + - "The tag is used by this tag and not converted to jte yet. You should convert first. If this is a tag that should be always converted by hand, implement getNotConvertedTags() and add it there." + """ + The tag is used by this tag and not converted to jte yet. You should convert first. If this is a tag that should be always converted by hand, implement getNotConvertedTags() and add it there. + The tag is used by this tag and not converted to jte yet. You should convert first. If this is a tag that should be always converted by hand, implement getNotConvertedTags() and add it there.\ + """ ); } diff --git a/jte-kotlin/src/main/java/gg/jte/compiler/kotlin/KotlinClassCompiler.java b/jte-kotlin/src/main/java/gg/jte/compiler/kotlin/KotlinClassCompiler.java index 932c8492..58f4d3a7 100644 --- a/jte-kotlin/src/main/java/gg/jte/compiler/kotlin/KotlinClassCompiler.java +++ b/jte-kotlin/src/main/java/gg/jte/compiler/kotlin/KotlinClassCompiler.java @@ -91,7 +91,7 @@ public void report(CompilerMessageSeverity severity, @SuppressWarnings("Nullable line = location.getLine(); } - errors.add(String.format("%s%n%s:%d:%d%nReason: %s", location.getLineContent(), location.getPath(), + errors.add("%s%n%s:%d:%d%nReason: %s".formatted(location.getLineContent(), location.getPath(), location.getLine(), location.getColumn(), s)); } else { diff --git a/jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngineTest.java b/jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngineTest.java index 97491128..f2a85214 100644 --- a/jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngineTest.java +++ b/jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngineTest.java @@ -149,11 +149,13 @@ void condition() { @Test void condition_else() { - givenTemplate("@if (model.x == 42)" + - "Bingo" + - "@else" + - "Bongo" + - "@endif"); + givenTemplate(""" + @if (model.x == 42)\ + Bingo\ + @else\ + Bongo\ + @endif\ + """); model.x = 42; thenOutputIs("Bingo"); @@ -163,11 +165,13 @@ void condition_else() { @Test void condition_elseif() { - givenTemplate("@if (model.x == 42)" + - "Bingo" + - "@elseif (model.x == 43)" + - "Bongo" + - "@endif!"); + givenTemplate(""" + @if (model.x == 42)\ + Bingo\ + @elseif (model.x == 43)\ + Bongo\ + @endif!\ + """); model.x = 42; thenOutputIs("Bingo!"); @@ -196,64 +200,76 @@ void conditionNested() { @Test void conditionInContentBlock() { model.x = 0; - givenTemplate("x is ${@`@if (model.x > 0)" + - "positive!" + - "@elseif(model.x < 0)" + - "negative!" + - "@else" + - "zero!" + - "@endif`}"); + givenTemplate(""" + x is ${@`@if (model.x > 0)\ + positive!\ + @elseif(model.x < 0)\ + negative!\ + @else\ + zero!\ + @endif`}\ + """); thenOutputIs("x is zero!"); } @Test void loop() { model.array = new int[]{1, 2, 3}; - givenTemplate("@for (i in model.array)" + - "${i}" + - "@endfor"); + givenTemplate(""" + @for (i in model.array)\ + ${i}\ + @endfor\ + """); thenOutputIs("123"); } @Test void loopWithCondition() { model.array = new int[]{1, 2, 3}; - givenTemplate("@for (i in model.array)" + - "@if (i > 1)" + - "${i}" + - "@endif" + - "@endfor"); + givenTemplate(""" + @for (i in model.array)\ + @if (i > 1)\ + ${i}\ + @endif\ + @endfor\ + """); thenOutputIs("23"); } @Test void classicLoop() { model.array = new int[]{10, 20, 30}; - givenTemplate("@for (i in model.array.indices)" + - "Index ${i} is ${model.array[i]}" + - "@if (i < model.array.size - 1)" + - "
" + - "@endif" + - "@endfor"); + givenTemplate(""" + @for (i in model.array.indices)\ + Index ${i} is ${model.array[i]}\ + @if (i < model.array.size - 1)\ +
\ + @endif\ + @endfor\ + """); thenOutputIs("Index 0 is 10
Index 1 is 20
Index 2 is 30"); } @Test void loopWithVariable() { model.array = new int[]{1, 2, 3}; - givenTemplate("@for (i in model.array)" + - "!{var y = i + 1}" + - "${y}" + - "@endfor"); + givenTemplate(""" + @for (i in model.array)\ + !{var y = i + 1}\ + ${y}\ + @endfor\ + """); thenOutputIs("234"); } @Test void loopInContentBlock() { model.array = new int[]{1, 2, 3}; - givenTemplate("${@`@for (i in model.array)" + - "${i}" + - "@endfor`}"); + givenTemplate(""" + ${@`@for (i in model.array)\ + ${i}\ + @endfor`}\ + """); thenOutputIs("123"); } @@ -261,22 +277,26 @@ void loopInContentBlock() { @Test void loopElse_empty() { model.array = new int[]{}; - givenTemplate("@for (i in model.array)" + - "${i}" + - "@else" + - "Empty" + - "@endfor"); + givenTemplate(""" + @for (i in model.array)\ + ${i}\ + @else\ + Empty\ + @endfor\ + """); thenOutputIs("Empty"); } @Test void loopElse_notEmpty() { model.array = new int[]{1}; - givenTemplate("@for (i in model.array)" + - "${i}" + - "@else" + - "Empty" + - "@endfor"); + givenTemplate(""" + @for (i in model.array)\ + ${i}\ + @else\ + Empty\ + @endfor\ + """); thenOutputIs("1"); } @@ -320,11 +340,13 @@ void loopElseNestedContentBlock_innerEmpty() { @Test void loopElse_if() { model.array = new int[]{}; - givenTemplate("@for (i in model.array)" + - "@if(i > 0)${i}@else@endif" + - "@else" + - "Empty" + - "@endfor"); + givenTemplate(""" + @for (i in model.array)\ + @if(i > 0)${i}@else@endif\ + @else\ + Empty\ + @endfor\ + """); thenOutputIs("Empty"); } @@ -353,16 +375,18 @@ void loopElseNested_innerEmptyThrowsException() { @Test void loopElse_multiple() { model.array = new int[]{1}; - givenTemplate("@for (i in model.array)" + - "${i}" + - "@else" + - "Empty" + - "@endfor\n" + - "@for (i in model.array)" + - "${i}" + - "@else" + - "Empty" + - "@endfor"); + givenTemplate(""" + @for (i in model.array)\ + ${i}\ + @else\ + Empty\ + @endfor + @for (i in model.array)\ + ${i}\ + @else\ + Empty\ + @endfor\ + """); thenOutputIs("1\n1"); } @@ -393,24 +417,29 @@ void variableInContentBlock() { @Test void jsStringInterpolationInContentBlock() { - givenTemplate("!{var content = @`\n" - + "

Hello World!

\n" - + " @raw\n" - + " \n" - + " @endraw\n" - + "`}\n" - + "${content}"); - thenOutputIs("\n\n" - + "

Hello World!

\n" - + " \n" - + " \n" - + " \n"); + givenTemplate(""" + !{var content = @` +

Hello World!

+ @raw + + @endraw + `} + ${content}\ + """); + thenOutputIs(""" + + +

Hello World!

+ \s + + \s + """); } @Test @@ -447,36 +476,46 @@ void braceInJavaStringWithEscapedQuote() { @Test void tag() { - givenTag("card", "@param firstParam:String\n" + - "@param secondParam:Int\n" + - "One: ${firstParam}, two: ${secondParam}"); + givenTag("card", """ + @param firstParam:String + @param secondParam:Int + One: ${firstParam}, two: ${secondParam}\ + """); givenTemplate("@template.tag.card(model.hello, model.x), That was a tag!"); thenOutputIs("One: Hello, two: 42, That was a tag!"); } @Test void tag_content() { - givenTag("card", "@param content:gg.jte.Content\n" + - "${content}"); + givenTag("card", """ + @param content:gg.jte.Content + ${content}\ + """); givenTemplate("@template.tag.card(@`${model.hello}`), That was a tag!"); thenOutputIs("Hello, That was a tag!"); } @Test void tag_content_comma() { - givenTag("card", "@param content:gg.jte.Content\n" + - "${content}"); + givenTag("card", """ + @param content:gg.jte.Content + ${content}\ + """); givenTemplate("@template.tag.card(@`Hello, ${model.hello}`), That was a tag!"); thenOutputIs("Hello, Hello, That was a tag!"); } @Test void tagWithGenericParam() { - givenTag("entry", "@param list:List?\n" + - "${list.toString()}"); - givenRawTemplate("@param map:Map>\n" + - "@template.tag.entry(map[\"one\"])\n" + - "@template.tag.entry(map[\"two\"])\n"); + givenTag("entry", """ + @param list:List? + ${list.toString()}\ + """); + givenRawTemplate(""" + @param map:Map> + @template.tag.entry(map["one"]) + @template.tag.entry(map["two"]) + """); Map> model = new TreeMap<>(); model.put("one", Arrays.asList("1", "2")); @@ -490,39 +529,49 @@ void tagWithGenericParam() { @Test void tagWithMethodCallForParam() { - givenTag("card", "@param firstParam:String\n" + - "@param secondParam:Int\n" + - "One: ${firstParam}, two: ${secondParam}"); + givenTag("card", """ + @param firstParam:String + @param secondParam:Int + One: ${firstParam}, two: ${secondParam}\ + """); givenTemplate("@template.tag.card(model.anotherWorld, model.x), That was a tag!"); thenOutputIs("One: Another World, two: 42, That was a tag!"); } @Test void tagInTag() { - givenTag("divTwo", "@param amount:Int\n" + - "Divided by two is ${amount / 2}!"); - givenTag("card", "@param firstParam:String\n" + - "@param secondParam:Int\n" + - "${firstParam}, @template.tag.divTwo(secondParam)"); + givenTag("divTwo", """ + @param amount:Int + Divided by two is ${amount / 2}!\ + """); + givenTag("card", """ + @param firstParam:String + @param secondParam:Int + ${firstParam}, @template.tag.divTwo(secondParam)\ + """); givenTemplate("@template.tag.card (model.hello, model.x) That was a tag in a tag!"); thenOutputIs("Hello, Divided by two is 21! That was a tag in a tag!"); } @Test void sameTagReused() { - givenTag("divTwo", "@param amount:Int\n" + - "${amount / 2}!"); + givenTag("divTwo", """ + @param amount:Int + ${amount / 2}!\ + """); givenTemplate("@template.tag.divTwo(model.x),@template.tag.divTwo(2 * model.x)"); thenOutputIs("21!,42!"); } @Test void tagRecursion() { - givenTag("recursion", "@param amount:Int\n" + - "${amount}" + - "@if (amount > 0)" + - "@template.tag.recursion(amount - 1)" + - "@endif" + givenTag("recursion", """ + @param amount:Int + ${amount}\ + @if (amount > 0)\ + @template.tag.recursion(amount - 1)\ + @endif\ + """ ); givenTemplate("@template.tag.recursion(5)"); thenOutputIs("543210"); @@ -551,40 +600,50 @@ void tagWithPackage() { @Test void tagWithNamedParam() { - givenTag("named", "@param one:Int\n" + - "@param two:Int\n" + - "${one}, ${two}"); + givenTag("named", """ + @param one:Int + @param two:Int + ${one}, ${two}\ + """); givenTemplate("@template.tag.named(two = 2, one = 1)"); thenOutputIs("1, 2"); } @Test void tagWithNamedParamString() { - givenTag("named", "@param one:Int\n" + - "@param two:Int\n" + - "@param three:String\n" + - "${one}, ${two}, ${three}"); - givenTemplate("@template.tag.named(\n" + - "two = 2,\n" + - "three = \"Hello, there ;-)\",\n" + - "one = 1)"); + givenTag("named", """ + @param one:Int + @param two:Int + @param three:String + ${one}, ${two}, ${three}\ + """); + givenTemplate(""" + @template.tag.named( + two = 2, + three = "Hello, there ;-)", + one = 1)\ + """); thenOutputIs("1, 2, Hello, there ;-)"); } @Test void tagWithNamedParam_ternary() { - givenTag("named", "@param one:Int\n" + - "@param two:Int\n" + - "${one}, ${two}"); + givenTag("named", """ + @param one:Int + @param two:Int + ${one}, ${two}\ + """); givenTemplate("@template.tag.named(two = if(1 == 2) 1 else 0, one = 1)"); thenOutputIs("1, 0"); } @Test void tagWithDefaultParam() { - givenTag("named", "@param one:Int = 1\n" + - "@param two:Int = 2\n" + - "${one}, ${two}"); + givenTag("named", """ + @param one:Int = 1 + @param two:Int = 2 + ${one}, ${two}\ + """); givenTemplate("@template.tag.named()"); thenOutputIs("1, 2"); @@ -592,9 +651,11 @@ void tagWithDefaultParam() { @Test void tagWithDefaultContentParam() { - givenTag("named", "@param one:Int = 1\n" + - "@param content:gg.jte.Content = @`Some Content`\n" + - "First param = ${one}, Content param = ${content}"); + givenTag("named", """ + @param one:Int = 1 + @param content:gg.jte.Content = @`Some Content` + First param = ${one}, Content param = ${content}\ + """); givenTemplate("@template.tag.named()"); thenOutputIs("First param = 1, Content param = Some Content"); @@ -602,8 +663,10 @@ void tagWithDefaultContentParam() { @Test void tagWithDefaultParam_generic() { - givenTag("named", "@param files: Map = emptyMap()\n" + - "${files.size}"); + givenTag("named", """ + @param files: Map = emptyMap() + ${files.size}\ + """); givenTemplate("@template.tag.named()"); thenOutputIs("0"); @@ -611,8 +674,10 @@ void tagWithDefaultParam_generic() { @Test void tagWithDefaultParam_generic_nullDefault() { - givenTag("named", "@param files: Map? = null\n" + - "${files?.size}"); + givenTag("named", """ + @param files: Map? = null + ${files?.size}\ + """); givenTemplate("@template.tag.named()"); thenOutputIs(""); @@ -620,8 +685,10 @@ void tagWithDefaultParam_generic_nullDefault() { @Test void tagWithDefaultParam_generic_nullDefault_withValue() { - givenTag("named", "@param files: Map? = null\n" + - "${files?.size}"); + givenTag("named", """ + @param files: Map? = null + ${files?.size}\ + """); givenTemplate("@template.tag.named(mapOf(\"foo\" to ByteArray(1)))"); thenOutputIs("1"); @@ -629,9 +696,11 @@ void tagWithDefaultParam_generic_nullDefault_withValue() { @Test void tagWithDefaultParam_firstSet() { - givenTag("named", "@param one:Int = 1\n" + - "@param two:Int = 2\n" + - "${one}, ${two}"); + givenTag("named", """ + @param one:Int = 1 + @param two:Int = 2 + ${one}, ${two}\ + """); givenTemplate("@template.tag.named(one = 6)"); thenOutputIs("6, 2"); @@ -639,9 +708,11 @@ void tagWithDefaultParam_firstSet() { @Test void tagWithDefaultParam_secondSet() { - givenTag("named", "@param one:Int = 1\n" + - "@param two:Int = 2\n" + - "${one}, ${two}"); + givenTag("named", """ + @param one:Int = 1 + @param two:Int = 2 + ${one}, ${two}\ + """); givenTemplate("@template.tag.named(two= 5)"); thenOutputIs("1, 5"); @@ -649,9 +720,11 @@ void tagWithDefaultParam_secondSet() { @Test void templateWithDefaultParam_content() { - givenTag("named", "@param one:gg.jte.Content = @`This is`\n" + - "@param two:Int = 2\n" + - "${one}: ${two}"); + givenTag("named", """ + @param one:gg.jte.Content = @`This is` + @param two:Int = 2 + ${one}: ${two}\ + """); givenTemplate("@template.tag.named()"); thenOutputIs("This is: 2"); @@ -660,8 +733,10 @@ void templateWithDefaultParam_content() { @Test void tagWithVarArgs1() { givenTag("varargs", - "@param vararg values:String\n" + - "@for(value in values)${value} @endfor"); + """ + @param vararg values:String + @for(value in values)${value} @endfor\ + """); givenTemplate("@template.tag.varargs(\"Hello\")"); thenOutputIs("Hello "); } @@ -669,8 +744,10 @@ void tagWithVarArgs1() { @Test void tagWithVarArgs2() { givenTag("varargs", - "@param vararg values:String\n" + - "@for(value in values)${value} @endfor"); + """ + @param vararg values:String + @for(value in values)${value} @endfor\ + """); givenTemplate("@template.tag.varargs(\"Hello\", \"World\")"); thenOutputIs("Hello World "); } @@ -678,24 +755,31 @@ void tagWithVarArgs2() { @Test void tagWithVarArgs3() { givenTag("localize", - "@param key:String\n" + - "@param vararg values:String\n" + - "${key} with @for(value in values)${value} @endfor"); + """ + @param key:String + @param vararg values:String + ${key} with @for(value in values)${value} @endfor\ + """); givenTemplate("@template.tag.localize(key = \"test.key\", \"Hello\", \"World\")"); thenOutputIs("test.key with Hello World "); } @Test void comment() { - givenTemplate("<%--This is a comment" + - " ${model.hello} everything in here is omitted--%>" + - "This is visible..."); + givenTemplate(""" + <%--This is a comment\ + ${model.hello} everything in here is omitted--%>\ + This is visible...\ + """); thenOutputIs("This is visible..."); } @Test void commentBeforeImports() { - givenRawTemplate("<%--This is a comment--%>@import gg.jte.kotlin.TemplateEngineTest.Model\n@param model:Model\n" + "!{model.setX(12)}${model.x}"); + givenRawTemplate(""" + <%--This is a comment--%>@import gg.jte.kotlin.TemplateEngineTest.Model + @param model:Model + !{model.setX(12)}${model.x}"""); thenOutputIs("12"); } @@ -731,19 +815,28 @@ void htmlCommentInPlainTemplate() { @Test void importInCss() { - givenTemplate(""); - thenOutputIs(""); + givenTemplate(""" + \ + """); + thenOutputIs(""" + \ + """); } @Test @@ -815,94 +908,112 @@ void contentParamWithDefaultValue() { @Test void layout() { - givenLayout("main", "@param model:gg.jte.kotlin.TemplateEngineTest.Model\n" + - "@param content:gg.jte.Content\n" + - "@param footer:gg.jte.Content\n" + - "\n" + - "Welcome to my site - you are on page ${model.x}\n" + - "\n" + - "
\n" + - " ${content}\n" + - "
\n" + - "\n" + - "
\n" + - " ${footer}\n" + - "
\n" + - ""); - - givenTemplate("@template.layout.main(model, content = @`\n" + - " ${model.hello}, enjoy this great content\n" + - " `,\n" + - " footer = @`\n" + - " Come again!\n" + - " `)"); + givenLayout("main", """ + @param model:gg.jte.kotlin.TemplateEngineTest.Model + @param content:gg.jte.Content + @param footer:gg.jte.Content + + Welcome to my site - you are on page ${model.x} + +
+ ${content} +
+ + + \ + """); + + givenTemplate(""" + @template.layout.main(model, content = @` + ${model.hello}, enjoy this great content + `, + footer = @` + Come again! + `)\ + """); thenOutputIs( - "\n" + - "Welcome to my site - you are on page 42\n" + - "\n" + - "
\n" + - " \n" + - " Hello, enjoy this great content\n" + - " \n" + - "
\n" + - "\n" + - "
\n" + - " \n" + - " Come again!\n" + - " \n" + - "
\n" + - ""); + """ + + Welcome to my site - you are on page 42 + +
+ \s + Hello, enjoy this great content + \s +
+ + + \ + """); } @Test void nestedLayouts() { givenLayout("main", - "@param header:gg.jte.Content? = null\n" + - "@param content:gg.jte.Content\n" + - "@param footer:gg.jte.Content? = null\n" + - "@if(header != null)
${header}
@endif" + - "${content}" + - "@if(footer != null)
${footer}
@endif"); + """ + @param header:gg.jte.Content? = null + @param content:gg.jte.Content + @param footer:gg.jte.Content? = null + @if(header != null)
${header}
@endif\ + ${content}\ + @if(footer != null)
${footer}
@endif\ + """); givenLayout("mainExtended", - "@param header:gg.jte.Content? = null\n" + - "@param contentPrefix:gg.jte.Content? = null\n" + - "@param content:gg.jte.Content\n" + - "@param contentSuffix:gg.jte.Content? = null\n" + - "@param footer:gg.jte.Content? = null\n" + - "@template.layout.main(header = header, content = @`" + - "@if(contentPrefix != null)${contentPrefix}@endif" + - "${content}" + - "@if(contentSuffix != null)${contentSuffix}@endif" + - "`, footer = footer)"); - givenTemplate("@template.layout.mainExtended(" + - "header = @`" + - "this is the header" + - "`," + - "contentPrefix = @`" + - "" + - "`," + - "content = @`" + - "this is the content" + - "`, " + - "contentSuffix=@`" + - "" + - "`)"); - - thenOutputIs("
this is the header
" + - "this is the content"); + """ + @param header:gg.jte.Content? = null + @param contentPrefix:gg.jte.Content? = null + @param content:gg.jte.Content + @param contentSuffix:gg.jte.Content? = null + @param footer:gg.jte.Content? = null + @template.layout.main(header = header, content = @`\ + @if(contentPrefix != null)${contentPrefix}@endif\ + ${content}\ + @if(contentSuffix != null)${contentSuffix}@endif\ + `, footer = footer)\ + """); + givenTemplate(""" + @template.layout.mainExtended(\ + header = @`\ + this is the header\ + `,\ + contentPrefix = @`\ + \ + `,\ + content = @`\ + this is the content\ + `, \ + contentSuffix=@`\ + \ + `)\ + """); + + thenOutputIs(""" +
this is the header
\ + this is the content\ + """); } @Test void layoutWithNamedParams() { givenLayout("main", - "@param status:Int = 5\n" + - "@param duration:Int = -1\n" + - "@param content:gg.jte.Content\n" + - "Hello, ${content} your status is ${status}, the duration is ${duration}"); + """ + @param status:Int = 5 + @param duration:Int = -1 + @param content:gg.jte.Content + Hello, ${content} your status is ${status}, the duration is ${duration}\ + """); - givenTemplate("@template.layout.main(content = @`" + - "Sir`)"); + givenTemplate(""" + @template.layout.main(content = @`\ + Sir`)\ + """); thenOutputIs("Hello, Sir your status is 5, the duration is -1"); } @@ -910,10 +1021,12 @@ void layoutWithNamedParams() { @Test void layoutWithNamedParams_noNames() { givenLayout("main", - "@param status:Int = 5\n" + - "@param duration:Int = -1\n" + - "@param content:gg.jte.Content\n" + - "Hello, ${content} your status is ${status}, the duration is ${duration}"); + """ + @param status:Int = 5 + @param duration:Int = -1 + @param content:gg.jte.Content + Hello, ${content} your status is ${status}, the duration is ${duration}\ + """); givenTemplate("@template.layout.main(42, 10, @`Sir`)"); @@ -923,8 +1036,10 @@ void layoutWithNamedParams_noNames() { @Test void layoutWithVarArgs() { givenLayout("varargs", - "@param vararg values:String\n" + - "@for(value in values)${value} @endfor"); + """ + @param vararg values:String + @for(value in values)${value} @endfor\ + """); givenTemplate("@template.layout.varargs(\"Hello\", \"World\")"); thenOutputIs("Hello World "); } @@ -932,14 +1047,16 @@ void layoutWithVarArgs() { @Test void enumCheck() { givenRawTemplate( - "@import gg.jte.kotlin.TemplateEngineTest.Model\n" + - "@import gg.jte.kotlin.TemplateEngineTest.ModelType\n" + - "@param model:Model\n" + - "@if (model.type == ModelType.One)" + - "one" + - "@else" + - "not one" + - "@endif"); + """ + @import gg.jte.kotlin.TemplateEngineTest.Model + @import gg.jte.kotlin.TemplateEngineTest.ModelType + @param model:Model + @if (model.type == ModelType.One)\ + one\ + @else\ + not one\ + @endif\ + """); model.type = ModelType.One; thenOutputIs("one"); @@ -957,27 +1074,31 @@ void enumOutput() { @Test void nestedJavascript() { - givenTemplate("@if (model.isCaseA() && model.isCaseB())\n" + - " \n" + - " @elseif (model.isCaseB())\n" + - " \n" + - " @elseif (model.isCaseA())\n" + - " \n" + - " @endif" + - "@if (model.x > 0)\n" + - " \n" + - " @endif\n" + - "\n" + - " \n" + - "@endraw"); - thenOutputIs("\n" + - "\n"); + givenTemplate(""" + @raw + + @endraw\ + """); + thenOutputIs(""" + + + """); } @Test diff --git a/jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngine_HtmlInterceptorTest.java b/jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngine_HtmlInterceptorTest.java index c86965d3..e2c6ee62 100644 --- a/jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngine_HtmlInterceptorTest.java +++ b/jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngine_HtmlInterceptorTest.java @@ -32,9 +32,11 @@ void setUp() { @Test void noFields_additionalFieldWritten() { - dummyCodeResolver.givenCode("page.kte", "@param url:String\n" + - "
\n" + - "
"); + dummyCodeResolver.givenCode("page.kte", """ + @param url:String +
+
\ + """); templateEngine.render("page.kte", "hello.htm", output); @@ -43,77 +45,95 @@ void noFields_additionalFieldWritten() { @Test void formInIf() { - dummyCodeResolver.givenCode("page.kte", "@param url:String\n" + - "@if(true)\n" + - "
\n" + - " \n" + - "
" + - "@endif"); + dummyCodeResolver.givenCode("page.kte", """ + @param url:String + @if(true) +
+ +
\ + @endif\ + """); templateEngine.render("page.kte", "hello.htm", output); - assertThat(output.toString()).isEqualTo("
\n" + - " \n" + - "\n" + - "
"); + assertThat(output.toString()).isEqualTo(""" +
+ + +
\ + """); } @Test void input() { - dummyCodeResolver.givenCode("page.kte", "@param url:String\n" + - "
\n" + - "\n" + - "\n" + - "
"); + dummyCodeResolver.givenCode("page.kte", """ + @param url:String +
+ + +
\ + """); templateEngine.render("page.kte", "hello.htm", output); - assertThat(output.toString()).isEqualTo("
\n" + - "\n" + - "\n" + - "\n" + - "
"); + assertThat(output.toString()).isEqualTo(""" +
+ + + +
\ + """); } @Test void input_int() { - dummyCodeResolver.givenCode("page.kte", "@param url:String\n" + - "
\n" + - "\n" + - "
"); + dummyCodeResolver.givenCode("page.kte", """ + @param url:String +
+ +
\ + """); templateEngine.render("page.kte", "hello.htm", output); - assertThat(output.toString()).isEqualTo("
\n" + - "\n" + - "\n" + - "
"); + assertThat(output.toString()).isEqualTo(""" +
+ + +
\ + """); } @Test void input_closed1() { - dummyCodeResolver.givenCode("page.kte", "@param url:String\n" + - "
\n" + - "\n" + - "\n" + - "
"); + dummyCodeResolver.givenCode("page.kte", """ + @param url:String +
+ + +
\ + """); templateEngine.render("page.kte", "hello.htm", output); - assertThat(output.toString()).isEqualTo("
\n" + - "\n" + - "\n" + - "\n" + - "
"); + assertThat(output.toString()).isEqualTo(""" +
+ + + +
\ + """); } @Test void input_closedWrongly() { - dummyCodeResolver.givenCode("page.kte", "@param url:String\n" + - "
\n" + - "\n" + - "\n" + - "
"); + dummyCodeResolver.givenCode("page.kte", """ + @param url:String +
+ + +
\ + """); Throwable throwable = catchThrowable(() -> templateEngine.render("page.kte", "hello.htm", output)); @@ -122,8 +142,10 @@ void input_closedWrongly() { @Test void input_noAttributes() { - dummyCodeResolver.givenCode("page.kte", "@param url:String\n" + - ""); + dummyCodeResolver.givenCode("page.kte", """ + @param url:String + \ + """); templateEngine.render("page.kte", "hello.htm", output); @@ -132,164 +154,196 @@ void input_noAttributes() { @Test void input_disabled() { - dummyCodeResolver.givenCode("page.kte", "@param url:String\n" + - "
\n" + - "\n" + - "\n" + - "
"); + dummyCodeResolver.givenCode("page.kte", """ + @param url:String +
+ + +
\ + """); templateEngine.render("page.kte", "hello.htm", output); - assertThat(output.toString()).isEqualTo("
\n" + - "\n" + - "\n" + - "\n" + // No param1 here - "
"); + assertThat(output.toString()).isEqualTo(""" +
+ + + +
\ + """); } @Test void select() { - dummyCodeResolver.givenCode("page.kte", "@param controller:gg.jte.kotlin.TemplateEngine_HtmlInterceptorTest.Controller\n" + - "
\n" + - "\n" + - "
"); + dummyCodeResolver.givenCode("page.kte", """ + @param controller:gg.jte.kotlin.TemplateEngine_HtmlInterceptorTest.Controller +
+ +
\ + """); controller.setFoodOption("Onion"); templateEngine.render("page.kte", controller, output); - assertThat(output.toString()).isEqualTo("
\n" + - "\n" + - "\n" + - "
"); + assertThat(output.toString()).isEqualTo(""" +
+ + +
\ + """); } @Test void tag() { dummyCodeResolver.givenCode("tag/formContent.kte", - "\n" + - "\n"); + """ + + + """); - dummyCodeResolver.givenCode("page.kte", "@param url:String\n" + - "
\n" + - "@template.tag.formContent()" + - "
"); + dummyCodeResolver.givenCode("page.kte", """ + @param url:String +
+ @template.tag.formContent()\ +
\ + """); templateEngine.render("page.kte", "hello.htm", output); - assertThat(output.toString()).isEqualTo("
\n" + - "\n" + - "\n" + - "\n" + - "
"); + assertThat(output.toString()).isEqualTo(""" +
+ + + +
\ + """); } @Test void layout() { dummyCodeResolver.givenCode("layout/formContent.kte", - "@param url:String\n" + - "@param content:gg.jte.Content\n" + - "
\n" + - "${content}" + - "
"); - - dummyCodeResolver.givenCode("page.kte", "@param url:String\n" + - "@template.layout.formContent(url, content = @`" + - "\n" + - "\n" + - "`)"); + """ + @param url:String + @param content:gg.jte.Content +
+ ${content}\ +
\ + """); + + dummyCodeResolver.givenCode("page.kte", """ + @param url:String + @template.layout.formContent(url, content = @`\ + + + `)\ + """); templateEngine.render("page.kte", "hello.htm", output); - assertThat(output.toString()).isEqualTo("
\n" + - "\n" + - "\n" + - "\n" + - "
"); + assertThat(output.toString()).isEqualTo(""" +
+ + + +
\ + """); } @Test void form() { - dummyCodeResolver.givenCode("page.kte", "@param controller:gg.jte.kotlin.TemplateEngine_HtmlInterceptorTest.Controller\n" - + "\n" - + "

Hello

\n" - + "\n" - + "
\n" - + "\n" - + " \n" - + "\n" - + " \n" - + "
\n" - + ""); + dummyCodeResolver.givenCode("page.kte", """ + @param controller:gg.jte.kotlin.TemplateEngine_HtmlInterceptorTest.Controller + +

Hello

+ +
+ + + + +
+ \ + """); templateEngine.render("page.kte", controller, output); - assertThat(output.toString()).isEqualTo("\n" - + "

Hello

\n" - + "\n" - + "
\n" - + "\n" - + " \n" - + "\n" - + " \n" - + " \n" - + "
\n" - + ""); + assertThat(output.toString()).isEqualTo(""" + +

Hello

+ +
+ + + + + +
+ \ + """); } @Test void errorClass() { - dummyCodeResolver.givenCode("page.kte", "@param url:String\n" + - "
\n" + - "\n" + - "
"); + dummyCodeResolver.givenCode("page.kte", """ + @param url:String +
+ +
\ + """); templateEngine.render("page.kte", "hello.htm", output); - assertThat(output.toString()).isEqualTo("
\n" + - "\n" + - "\n" + - "
"); + assertThat(output.toString()).isEqualTo(""" +
+ + +
\ + """); } @Test void errorClass_indentation() { - dummyCodeResolver.givenCode("page.kte", "@param url:String\n" + - "
\n" + - "@if(true)\n" + - " \n" + - "@endif\n" + - "
"); + dummyCodeResolver.givenCode("page.kte", """ + @param url:String +
+ @if(true) + + @endif +
\ + """); templateEngine.render("page.kte", "hello.htm", output); - assertThat(output.toString()).isEqualTo("
\n" + - "\n" + - "\n" + - "
"); + assertThat(output.toString()).isEqualTo(""" +
+ + +
\ + """); } @Test diff --git a/jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngine_HtmlOutputEscapingTest.java b/jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngine_HtmlOutputEscapingTest.java index fdfb3eb2..ea897652 100644 --- a/jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngine_HtmlOutputEscapingTest.java +++ b/jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngine_HtmlOutputEscapingTest.java @@ -32,9 +32,11 @@ public class TemplateEngine_HtmlOutputEscapingTest { @Test void outputEscaping() { codeResolver.givenCode("template.kte", - "@param url:String\n" + - "@param title:String\n" + - "Look at ${title}"); + """ + @param url:String + @param title:String + Look at ${title}\ + """); templateEngine.render("template.kte", TemplateUtils.toMap("url", "https://www.test.com?param1=1¶m2=2", "title", ""), output); @@ -398,8 +400,10 @@ void booleanAttributes_noParams2() { templateEngine.render("template.kte", "dummy", output); - assertThat(output.toString()).isEqualTo("\n" + - ""); + assertThat(output.toString()).isEqualTo(""" + + \ + """); } @Test @@ -777,47 +781,51 @@ void script3() { @Test void script4() { - codeResolver.givenCode("template.kte", "
\n" + - " \n" + - "
"); + codeResolver.givenCode("template.kte", """ +
+ +
\ + """); templateEngine.render("template.kte", (Object)null, output); - assertThat(output.toString()).isEqualTo("
\n" + - " \n" + - "
"); + assertThat(output.toString()).isEqualTo(""" +
+ +
\ + """); } @Test @@ -843,35 +851,39 @@ void onMethods_policy() { @Test void css() { - codeResolver.givenCode("template.kte", ""); + codeResolver.givenCode("template.kte", """ + \ + """); templateEngine.render("template.kte", (Object)null, output); - assertThat(output.toString()).isEqualTo(""); + assertThat(output.toString()).isEqualTo(""" + \ + """); } @Test @@ -1021,9 +1033,11 @@ void contentBlockInAttribute2() { @Test void contentBlockInAttribute3() { codeResolver.givenCode("template.kte", - "@param url:String\n" + - "!{val content = @`Hello`}\n" + - "${content}"); + """ + @param url:String + !{val content = @`Hello`} + ${content}\ + """); templateEngine.render("template.kte", TemplateUtils.toMap("url", "https://jte.gg"), output); @@ -1033,14 +1047,18 @@ void contentBlockInAttribute3() { @Test void contentBlockInAttribute4() { codeResolver.givenCode("template.kte", - "@param url:String\n" + - "!{val content = @`Hello`;}\n" + - "${content}"); + """ + @param url:String + !{val content = @`Hello`;} + ${content}\ + """); templateEngine.render("template.kte", TemplateUtils.toMap("url", "https://jte.gg"), output); - assertThat(output.toString()).isEqualTo("\n" + - "Hello</a>\">Hello"); + assertThat(output.toString()).isEqualTo(""" + + Hello\ + """); } @Test @@ -1101,8 +1119,10 @@ void unsafe_null() { @Test void localization_notFound_noParams() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "${localizer.localize(\"unknown\")}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + ${localizer.localize("unknown")}\ + """); templateEngine.render("template.kte", localizer, output); @@ -1111,8 +1131,10 @@ void localization_notFound_noParams() { @Test void localization_notFound_withParams() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "${localizer.localize(\"unknown\", 1)}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + ${localizer.localize("unknown", 1)}\ + """); templateEngine.render("template.kte", localizer, output); @@ -1121,8 +1143,10 @@ void localization_notFound_withParams() { @Test void localization_noParams() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "${localizer.localize(\"no-params\")}${localizer.localize(\"no-params\")}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + ${localizer.localize("no-params")}${localizer.localize("no-params")}\ + """); templateEngine.render("template.kte", localizer, output); @@ -1131,8 +1155,10 @@ void localization_noParams() { @Test void localization_html() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "${localizer.localize(\"no-params-html\")}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + ${localizer.localize("no-params-html")}\ + """); templateEngine.render("template.kte", localizer, output); @@ -1141,9 +1167,11 @@ void localization_html() { @Test void localization_oneParam() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "@param param:String\n" + - "${localizer.localize(\"one-param\", param)}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + @param param:String + ${localizer.localize("one-param", param)}\ + """); templateEngine.render("template.kte", TemplateUtils.toMap("localizer", localizer, "param", ""), output); @@ -1152,9 +1180,11 @@ void localization_oneParam() { @Test void localization_html_oneParam() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "@param param:String\n" + - "${localizer.localize(\"one-param-html\", param)}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + @param param:String + ${localizer.localize("one-param-html", param)}\ + """); templateEngine.render("template.kte", TemplateUtils.toMap("localizer", localizer, "param", ""), output); @@ -1163,9 +1193,11 @@ void localization_html_oneParam() { @Test void localization_inception() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "@param param:String\n" + - "${localizer.localize(\"one-param-html\", localizer.localize(\"one-param-html\", param))}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + @param param:String + ${localizer.localize("one-param-html", localizer.localize("one-param-html", param))}\ + """); templateEngine.render("template.kte", TemplateUtils.toMap("localizer", localizer, "param", ""), output); @@ -1174,8 +1206,10 @@ void localization_inception() { @Test void localization_quotesInAttribute() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - ""); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + \ + """); templateEngine.render("template.kte", TemplateUtils.toMap("localizer", localizer), output); @@ -1184,11 +1218,13 @@ void localization_quotesInAttribute() { @Test void localization_quotesInAttributeWithParams() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "@param p1:String\n" + - "@param p2:String\n" + - "@param p3:String\n" + - ""); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + @param p1:String + @param p2:String + @param p3:String + \ + """); templateEngine.render("template.kte", TemplateUtils.toMap("localizer", localizer, "p1", "", "p2", "p2", "p3", "p3"), output); @@ -1197,9 +1233,11 @@ void localization_quotesInAttributeWithParams() { @Test void localization_manyParams_noneSet() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "@param param:String\n" + - "${localizer.localize(\"many-params-html\")}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + @param param:String + ${localizer.localize("many-params-html")}\ + """); templateEngine.render("template.kte", TemplateUtils.toMap("localizer", localizer, "param", ""), output); @@ -1208,9 +1246,11 @@ void localization_manyParams_noneSet() { @Test void localization_manyParams_primitives() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "@param param:String?\n" + - "${localizer.localize(\"many-params-html\")}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + @param param:String? + ${localizer.localize("many-params-html")}\ + """); templateEngine.render("template.kte", TemplateUtils.toMap("localizer", localizer, "p1", true, "p2", 1, "p3", 2), output); @@ -1219,9 +1259,11 @@ void localization_manyParams_primitives() { @Test void localization_manyParams_oneSet() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "@param param:String\n" + - "${localizer.localize(\"many-params-html\", param)}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + @param param:String + ${localizer.localize("many-params-html", param)}\ + """); templateEngine.render("template.kte", TemplateUtils.toMap("localizer", localizer, "param", ""), output); @@ -1230,9 +1272,11 @@ void localization_manyParams_oneSet() { @Test void localization_manyParams_allSame() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "@param param:String\n" + - "${localizer.localize(\"many-params-html\", param, param, param)}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + @param param:String + ${localizer.localize("many-params-html", param, param, param)}\ + """); templateEngine.render("template.kte", TemplateUtils.toMap("localizer", localizer, "param", ""), output); @@ -1241,9 +1285,11 @@ void localization_manyParams_allSame() { @Test void localization_badPattern() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "@param param:String\n" + - "${localizer.localize(\"bad-pattern\", param)}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + @param param:String + ${localizer.localize("bad-pattern", param)}\ + """); templateEngine.render("template.kte", TemplateUtils.toMap("localizer", localizer, "param", ""), output); @@ -1252,8 +1298,10 @@ void localization_badPattern() { @Test void localization_primitives() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "${localizer.localize(\"all-primitives\", false, 1.toByte(), 2.toShort(), 3, 4L, 5.0f, 6.0, 'c')}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + ${localizer.localize("all-primitives", false, 1.toByte(), 2.toShort(), 3, 4L, 5.0f, 6.0, 'c')}\ + """); templateEngine.render("template.kte", localizer, output); @@ -1262,8 +1310,10 @@ void localization_primitives() { @Test void localization_primitives_inAttribute() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - ""); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + \ + """); templateEngine.render("template.kte", localizer, output); @@ -1272,9 +1322,11 @@ void localization_primitives_inAttribute() { @Test void localization_enum() { - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "@param contentType:gg.jte.ContentType\n" + - "${localizer.localize(\"enum\", contentType)}"); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + @param contentType:gg.jte.ContentType + ${localizer.localize("enum", contentType)}\ + """); templateEngine.render("template.kte", TemplateUtils.toMap("localizer", localizer, "contentType", ContentType.Html), output); @@ -1283,11 +1335,15 @@ void localization_enum() { @Test void localization_tag() { - codeResolver.givenCode("tag/card.kte", "@param content:gg.jte.Content\n" + - "${content}"); - codeResolver.givenCode("template.kte", "@param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer\n" + - "@param name:String\n" + - "@template.tag.card(content = @`${localizer.localize(\"one-param\", name)}`)"); + codeResolver.givenCode("tag/card.kte", """ + @param content:gg.jte.Content + ${content}\ + """); + codeResolver.givenCode("template.kte", """ + @param localizer:gg.jte.kotlin.TemplateEngine_HtmlOutputEscapingTest.MyLocalizer + @param name:String + @template.tag.card(content = @`${localizer.localize("one-param", name)}`)\ + """); templateEngine.render("template.kte", TemplateUtils.toMap("localizer", localizer, "name", "