removed the old experiments for templating; so we now just create a StringTemplate object that can be converted to html or used with JDBC queries/statements etc

This commit is contained in:
James Strachan
2012-03-14 16:00:11 +00:00
parent 34c4808c76
commit e2d85427ce
15 changed files with 100 additions and 600 deletions
@@ -1,6 +1,6 @@
package test.template.experiment
package test.template
import kotlin.template.experiment2.*
import kotlin.template.*
import kotlin.test.assertEquals
import junit.framework.TestCase
@@ -13,7 +13,10 @@ class HtmlTemplateTest : TestCase() {
// 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()
// TODO will use a tuple soon
//val actual = StringTemplate(Tuple3<String,String,String>("<h1>", foo, "</h1> <p>hey ", bar, "</p>")).toHtml()
val actual = StringTemplate(array("<h1>", foo, "</h1> <p>hey ", bar, "</p>")).toHtml()
assertEquals("<h1>James</h1> <p>hey x &gt; 1</p>", actual)
}
@@ -1,6 +1,6 @@
package test.template.experiment
package test.template
import kotlin.template.experiment2.*
import kotlin.template.*
import kotlin.test.assertEquals
import junit.framework.TestCase
@@ -12,7 +12,10 @@ class StringTemplateTest : TestCase() {
// Code generated by the following template expression:
//
// val actual = "hello $name!".toString()
val actual = StringTemplate(array("hello ", "!"), array(name)).toString()
// TODO will use a tuple soon
//val actual = StringTemplate(Tuple3<String,String,String>("hello ", name, "!"))).toString()
val actual = StringTemplate(array("hello ", name, "!")).toString()
assertEquals("hello James!", actual)
}
@@ -1,32 +0,0 @@
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"
val format = I18nFormatter()
// Code generated by the following template expression:
//
// val actual = format.toHtml("<h1>$foo</h1> <p>hey $bar</p>")
val actual = format.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")
}
}
@@ -1,46 +0,0 @@
package template.experiment1
import kotlin.template.experiment1.*
import kotlin.test.assertEquals
import junit.framework.TestCase
import java.util.Date
import java.util.Locale
class I18nTemplateTest : TestCase() {
fun testDefaultLocale() : Unit {
format(I18nFormatter())
}
fun testFrance() : Unit {
format(I18nFormatter(Locale.FRANCE.sure()))
}
fun testGermany() : Unit {
format(I18nFormatter(Locale.GERMANY.sure()))
}
fun format(format: I18nFormatter): Unit {
val name = "James"
// TODO currently numbers cause: java.lang.ClassNotFoundException: jet.Number
//val price: Double = 1.99
val now = Date()
// Code generated by the following template expression:
//
// val actual = format.toI18n("hello $name!")
val actual = format.toString{
it.text("hello ")
it.expression(name)
/*
it.text(" price ")
it.expression(price)
*/
it.text(" date ")
it.expression(now)
}
println("Got text: $actual")
}
}
@@ -1,24 +0,0 @@
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)
}
}
@@ -1,23 +0,0 @@
package test.template
import kotlin.template.experiment3.*
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 = [Html]"<h1>$foo</h1> <p>hey $bar</p>"
val builder = HtmlTemplate(array("<h1>", "</h1> <p>hey ", "</p>")).builder()
builder.expression(foo)
builder.expression(bar)
val actual = builder.build()
assertEquals("<h1>James</h1> <p>hey x &gt; 1</p>", actual)
}
}
@@ -1,21 +0,0 @@
package test.template
import kotlin.template.experiment3.*
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!"
val builder = StringTemplate(array("hello ", "!")).builder()
builder.expression(name)
val actual = builder.build()
assertEquals("hello James!", actual)
}
}
@@ -1,52 +0,0 @@
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)
}
}
}