added a few experimental alternatives implementations of string templates for string/html/jdbc etc for KT-1565 seems there's a few ways we could do it...

This commit is contained in:
James Strachan
2012-03-12 20:46:44 +00:00
parent 3748507ac1
commit f927b38a14
17 changed files with 563 additions and 25 deletions
@@ -0,0 +1,31 @@
package template.experiment1
import kotlin.template.experiment1.*
import kotlin.test.assertEquals
import kotlin.dom.toXmlString
import junit.framework.TestCase
import org.w3c.dom.Node
class HtmlTemplateTest : TestCase() {
fun testTemplate(): Unit {
val foo = "James"
val bar = "x > 1"
// Code generated by the following template expression:
//
// val actual = toHtml("<h1>$foo</h1> <p>hey $bar</p>")
val actual = toHtml{
it.text("<h1>")
it.expression(foo)
it.text("</h1> <p>hey ")
it.expression(bar)
it.text("</p>")
}
assertEquals("<h1>James</h1> <p>hey x &gt; 1</p>", actual)
println("Generated HTML: $actual")
}
}
@@ -0,0 +1,24 @@
package template.experiment1
import kotlin.template.experiment1.*
import kotlin.test.assertEquals
import junit.framework.TestCase
class StringTemplateTest : TestCase() {
fun testTemplate() : Unit {
val name = "James"
// Code generated by the following template expression:
//
// val actual = format("hello $name!")
val actual = format{
it.text("hello ")
it.expression(name)
it.text("!")
}
println("Got text: $actual")
assertEquals("hello James!", actual)
}
}
@@ -0,0 +1,20 @@
package test.template.experiment
import kotlin.template.experiment2.*
import kotlin.test.assertEquals
import junit.framework.TestCase
class HtmlTemplateTest : TestCase() {
fun testTemplate(): Unit {
val foo = "James"
val bar = "x > 1"
// Code generated by the following template expression:
//
// val actual = "<h1>$foo</h1> <p>hey $bar</p>".toHtml()
val actual = StringTemplate(array("<h1>", "</h1> <p>hey ", "</p>"), array(foo, bar)).toHtml()
assertEquals("<h1>James</h1> <p>hey x &gt; 1</p>", actual)
}
}
@@ -0,0 +1,19 @@
package test.template.experiment
import kotlin.template.experiment2.*
import kotlin.test.assertEquals
import junit.framework.TestCase
class StringTemplateTest : TestCase() {
fun testTemplate(): Unit {
val name = "James"
// Code generated by the following template expression:
//
// val actual = "hello $name!".toString()
val actual = StringTemplate(array("hello ", "!"), array(name)).toString()
assertEquals("hello James!", actual)
}
}
@@ -1,9 +1,10 @@
package test.template
import junit.framework.TestCase
import kotlin.template.*
import kotlin.template.experiment3.*
import kotlin.test.assertEquals
import junit.framework.TestCase
class HtmlTemplateTest : TestCase() {
fun testTemplate(): Unit {
val foo = "James"
@@ -1,9 +1,10 @@
package test.template
import junit.framework.TestCase
import kotlin.template.StringTemplate
import kotlin.template.experiment3.*
import kotlin.test.assertEquals
import junit.framework.TestCase
class StringTemplateTest : TestCase() {
fun testTemplate(): Unit {
val name = "James"
@@ -0,0 +1,52 @@
package template.experiment4
import junit.framework.TestCase
import kotlin.test.assertEquals
import kotlin.test.todo
/**
* Creates a string template from some constant string expressions and some dynamic expressions.
*/
class StringTemplate<T>(val constantText : Array<String>, val fn : (T) -> Unit) {
}
fun StringBuilder.text(value : String) : Unit {
this.append(value)
}
fun StringBuilder.expression(value : Any?) : Unit {
this.append(value)
}
/**
* Converts a [[StringTemplate]] to a String
*/
fun toString(template : StringTemplate<StringBuilder>) : String {
val builder = StringBuilder()
template.fn(builder)
return builder.toString() ?: ""
}
class StringTemplateTest : TestCase() {
fun testTemplate() : Unit {
val name = "James"
// Code generated by the following template expression:
//
// val actual = "hello $name!"
val template = StringTemplate<StringBuilder>(array("hello ", "!"), { (it : StringBuilder) ->
println("About to apply function!")
it.text("hello ")
it.expression(name)
it.text("!")
})
println("Template has constants ${template.constantText.toList()} and function ${template.fn}")
todo {
val actual = toString(template)
assertEquals("hello James!", actual)
}
}
}