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