Files
kotlin-fork/libraries/testlib/test/template/experiment4/Experiment4.kt
T

52 lines
1.3 KiB
Kotlin

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)
}
}
}