Skip to content

Commit

Permalink
fixup! Reformat the templates for Kotlin model code
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusVolkhart authored and casid committed Jan 16, 2024
1 parent eec51dc commit 4848042
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 25 deletions.
1 change: 0 additions & 1 deletion jte-models/src/main/jte/dynamictemplates/kmain.jte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
@param Set<TemplateDescription> templates
@param Iterable<String> imports
@param ModelConfig modelConfig

@file:Suppress("ktlint")
package ${config.packageName()}

Expand Down
1 change: 0 additions & 1 deletion jte-models/src/main/jte/interfacetemplates/kmain.jte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
@param Set<TemplateDescription> templates
@param Iterable<String> imports
@param ModelConfig modelConfig

@file:Suppress("ktlint")
package ${config.packageName()}

Expand Down
1 change: 0 additions & 1 deletion jte-models/src/main/jte/statictemplates/kmain.jte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
@param Set<TemplateDescription> templates
@param Iterable<String> imports
@param ModelConfig modelConfig

@file:Suppress("ktlint")
package ${config.packageName()}

Expand Down
145 changes: 123 additions & 22 deletions jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,70 @@ public void generatesKotlinWithoutDefaultValueForContentParams() {
);

// Then
generatedPaths.forEach(path -> {
try {
assertThat(Files.readString(path)).contains("fun hello(content: gg.jte.Content): JteModel");
} catch (IOException ex) {
fail("Could not read file " + path, ex);
var actual = generatedPaths.stream().collect(Collectors.toMap(
path -> path.getFileName().toString(),
path -> {
try {
return Files.readString(path);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
));
var expected = Map.of(
"Templates.kt", """
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.models.runtime.*
interface Templates {
\s
@JteView("hello.kte")
fun hello(content: gg.jte.Content): JteModel
}""",
"StaticTemplates.kt", """
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.models.runtime.*
import gg.jte.ContentType
import gg.jte.html.HtmlTemplateOutput
class StaticTemplates : Templates {
\s
override fun hello(content: gg.jte.Content): JteModel = StaticJteModel<TemplateOutput>(
ContentType.Plain,
{ output, interceptor -> JtehelloGenerated.render(output, interceptor, content) },
"hello.kte",
"test.myktemplates",
JtehelloGenerated.JTE_LINE_INFO
)
}""",
"DynamicTemplates.kt", """
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.TemplateEngine
import gg.jte.models.runtime.*
class DynamicTemplates(private val engine: TemplateEngine) : Templates {
\s
override fun hello(content: gg.jte.Content): JteModel {
\s
val paramMap = buildMap<String, Any?> {
\s
put("content", content)
}
\s
return DynamicJteModel(engine, "hello.kte", paramMap)
}
}"""
);
assertThat(actual).containsExactlyInAnyOrderEntriesOf(expected);
}

@Test
Expand All @@ -244,23 +301,67 @@ public void generatesKotlinFacadesWithNoParameters() {
);

// Then
generatedPaths.forEach(path -> {
try {
assertThat(Files.readString(path)).contains("fun hello(): JteModel");
} catch (IOException ex) {
fail("Could not read file " + path, ex);
var actual = generatedPaths.stream().collect(Collectors.toMap(
path -> path.getFileName().toString(),
path -> {
try {
return Files.readString(path);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});

// Dynamic has a map with explicit generics types declared.
// This is relevant for empty maps (templates with no params).
assertThat(generatedPaths).anySatisfy(path -> {
try {
assertThat(Files.readString(path)).contains("val paramMap = mapOf<String, Any?>");
} catch (IOException ex) {
fail("Could not read file " + path, ex);
}
});
));
var expected = Map.of(
"Templates.kt", """
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.models.runtime.*
interface Templates {
\s
@JteView("hello.kte")
fun hello(): JteModel
}""",
"StaticTemplates.kt", """
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.models.runtime.*
import gg.jte.ContentType
import gg.jte.html.HtmlTemplateOutput
class StaticTemplates : Templates {
\s
override fun hello(): JteModel = StaticJteModel<TemplateOutput>(
ContentType.Plain,
{ output, interceptor -> JtehelloGenerated.render(output, interceptor) },
"hello.kte",
"test.myktemplates",
JtehelloGenerated.JTE_LINE_INFO
)
}""",
"DynamicTemplates.kt", """
@file:Suppress("ktlint")
package test.myktemplates
import gg.jte.TemplateEngine
import gg.jte.models.runtime.*
class DynamicTemplates(private val engine: TemplateEngine) : Templates {
\s
override fun hello(): JteModel {
\s
val paramMap = emptyMap<String, Any?>()
\s
return DynamicJteModel(engine, "hello.kte", paramMap)
}
}"""
);
assertThat(actual).containsExactlyInAnyOrderEntriesOf(expected);
}
}
}

0 comments on commit 4848042

Please sign in to comment.