Skip to content

Commit 24a574e

Browse files
authored
KTLN-835: HTML builder in Kotlin (#895)
* unit test code added * unit test code added * code fixes
1 parent 7b110ba commit 24a574e

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

core-kotlin-modules/core-kotlin-advanced-3/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
</parent>
1515

1616
<dependencies>
17+
<dependency>
18+
<groupId>org.jetbrains.kotlinx</groupId>
19+
<artifactId>kotlinx-html-jvm</artifactId>
20+
<version>0.7.2</version>
21+
</dependency>
1722

1823
</dependencies>
1924
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.baeldung.hmtlBuilder
2+
3+
import kotlinx.html.*
4+
import kotlinx.html.stream.appendHTML
5+
import kotlinx.html.stream.createHTML
6+
import org.junit.jupiter.api.Assertions.assertEquals
7+
import org.junit.jupiter.api.Test
8+
9+
class HtmlBuilderUnitTest {
10+
11+
@Test
12+
fun testBuildHtml() {
13+
val expectedHtml = """
14+
<html>
15+
<head>
16+
<title>My Kotlin HTML Page</title>
17+
</head>
18+
<body>
19+
<h1>Welcome to Kotlin HTML Builder</h1>
20+
<p>This is a demonstration of kotlinx.html.</p>
21+
<ul>
22+
<li>Item 0</li>
23+
<li>Item 1</li>
24+
<li>Item 2</li>
25+
<li>Item 3</li>
26+
<li>Item 4</li>
27+
</ul>
28+
</body>
29+
</html>
30+
""".trimIndent()
31+
32+
val actualHtml = buildHTML()
33+
34+
assertEquals(expectedHtml, actualHtml)
35+
}
36+
37+
@Test
38+
fun testParagraphAttributes() {
39+
val expectedHtml = """
40+
<p id="intro-paragraph" class="intro" style="color: red; font-size: 16px;">This is a demonstration of kotlinx.html.</p>
41+
""".trimIndent()
42+
43+
val actualHtml = buildParagraphWithAttributes()
44+
45+
assertEquals(expectedHtml, actualHtml)
46+
}
47+
}
48+
fun buildHTML(): String {
49+
return createHTML().html {
50+
head {
51+
title { +"My Kotlin HTML Page" }
52+
}
53+
body {
54+
h1 { +"Welcome to Kotlin HTML Builder" }
55+
p { +"This is a demonstration of kotlinx.html." }
56+
ul {
57+
repeat(5) {
58+
li { +"Item $it" }
59+
}
60+
}
61+
}
62+
}.toString().trim()
63+
}
64+
65+
fun buildParagraphWithAttributes(): String {
66+
return createHTML().p {
67+
id = "intro-paragraph"
68+
classes = setOf("intro")
69+
style = "color: red; font-size: 16px;"
70+
+"This is a demonstration of kotlinx.html."
71+
}.toString().trim()
72+
}

0 commit comments

Comments
 (0)