To make it possible, you need to four functions: table, tr, td and text. Some of them need to be added inside builder classes.
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in functional/dsl/Table.kt. You can find there starting code, example usage and unit tests.
Once you are done with the exercise, you can check your solution here.
Playground
import org.junit.Test
import kotlin.test.assertEquals
fun createTable(): TableBuilder = table {
tr {
td { +"A" }
td { +"B" }
}
tr {
td { +"C" }
td { +"D" }
}
}
data class TableBuilder(
var trs: List<TrBuilder> = emptyList()
) {
override fun toString(): String =
"<table>${trs.joinToString(separator = "")}</table>"
}
data class TrBuilder(
var tds: List<TdBuilder> = emptyList()
) {
override fun toString(): String =
"<tr>${tds.joinToString(separator = "")}</tr>"
}
data class TdBuilder(
var text: String = ""
) {
override fun toString(): String = "<td>$text</td>"
}
fun main() {
// println(createTable()) //<table><tr><td>This is row 1</td><td>This is row 2</td></tr></table>
}
class HtmlDslTest {
@Test
fun createTableTest() {
val html = TableBuilder().apply {
trs += TrBuilder().apply {
tds += TdBuilder().apply { text = "A" }
tds += TdBuilder().apply { text = "B" }
}
trs += TrBuilder().apply {
tds += TdBuilder().apply { text = "C" }
tds += TdBuilder().apply { text = "D" }
}
}
assertEquals(html, createTable())
}
@Test
fun `Table can be created and it is empty by default`() {
val expected = TableBuilder()
val actual = table {}
assertEquals(expected, actual)
}
@Test
fun `Tr can be created and it is empty`() {
val expected = TableBuilder().apply {
trs += TrBuilder()
}
val actual = table {
tr {}
}
assertEquals(expected, actual)
}
@Test
fun `Multiple tr can be created`() {
val expected = TableBuilder().apply {
trs += TrBuilder()
trs += TrBuilder()
}
val actual = table {
tr {}
tr {}
}
assertEquals(expected, actual)
}
@Test
fun `Td can be created and it is empty`() {
val expected = TableBuilder().apply {
trs += TrBuilder().apply {
tds += TdBuilder()
}
}
val actual = table {
tr {
td {}
}
}
assertEquals(expected, actual)
}
@Test
fun dslTestStandard() {
val expected = TableBuilder().apply {
trs += TrBuilder().apply {
tds += TdBuilder().apply { text = "A" }
tds += TdBuilder().apply { text = "B" }
}
trs += TrBuilder().apply {
tds += TdBuilder().apply { text = "C" }
tds += TdBuilder().apply { text = "D" }
}
}
val actual = table {
tr {
td { +"A" }
td { +"B" }
}
tr {
td { +"C" }
td { +"D" }
}
}
assertEquals(expected, actual)
}
@Test
fun dslTestMoreColons() {
val expected = TableBuilder().apply {
trs += TrBuilder().apply {
tds += TdBuilder().apply { text = "A" }
tds += TdBuilder().apply { text = "B" }
tds += TdBuilder().apply { text = "C" }
}
trs += TrBuilder().apply {
tds += TdBuilder().apply { text = "C" }
tds += TdBuilder().apply { text = "D" }
}
}
val actual = table {
tr {
td { +"A" }
td { +"B" }
td { +"C" }
}
tr {
td { +"C" }
td { +"D" }
}
}
assertEquals(expected, actual)
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.