-
Notifications
You must be signed in to change notification settings - Fork 25
/
Chapter16Spec.scala
186 lines (158 loc) · 5.98 KB
/
Chapter16Spec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import Chapter16._
import TestUtils.runApp
import java.io.File
import org.scalatest.{FlatSpec, Matchers}
import scala.io.Source
import scala.xml.{Elem, Node, Text, XML}
class Chapter16Spec extends FlatSpec with Matchers {
"task1" should "check results" in {
//when & then
<fred/>(0).toString() shouldBe "<fred/>"
<fred/>(0)(0).toString() shouldBe "<fred/>"
<fred/>(0)(0)(0).toString() shouldBe "<fred/>"
<fred/>(0)(0)(0)(0).toString() shouldBe "<fred/>"
}
"task2" should "escape special characters" in {
//when & then
val result = <ul>
<li>Opening bracket: [</li>
<li>Closing bracket: ]</li>
<li>Opening brace: {{</li>
<li>Closing brace: }}</li>
</ul>
result.toString() shouldBe """<ul>
| <li>Opening bracket: [</li>
| <li>Closing bracket: ]</li>
| <li>Opening brace: {</li>
| <li>Closing brace: }</li>
| </ul>""".stripMargin
}
"task3" should "check expressions" in {
//when & then
val res1 = <li>Fred</li> match { case <li>{Text(t)}</li> => t }
res1.toString shouldBe "Fred"
a [scala.MatchError] should be thrownBy {
<li>{"Fred"}</li> match { case <li>{Text(t)}</li> => t }
}
val res2 = <li>{Text("Fred")}</li> match { case <li>{Text(t)}</li> => t }
res2.toString shouldBe "Fred"
}
"printImgWithoutAlt" should "print all img elements without alt attribute" in {
//given
val file = "/Chapter16Task04.html"
//when
val (exit, out, err) = runApp("Chapter16PrintImgWithoutAltApp", file)
//then
exit shouldBe 0
err shouldBe ""
out shouldBe """<img src="http://test.com/img1.png"/>
|<img src="http://test.com/img2.png"/>
|""".stripMargin
}
"printAllImg" should "print all img src attributes" in {
//given
val file = "/Chapter16Task04.html"
//when
val (exit, out, err) = runApp("Chapter16PrintAllImgApp", file)
//then
exit shouldBe 0
err shouldBe ""
out shouldBe """http://test.com/img1.png
|http://test.com/img2.png
|http://test.com/img3.png
|""".stripMargin
}
"printAllHyperlinks" should "print a table of all hyperlinks together with their URLs" in {
//given
val file = "/Chapter16Task04.html"
//when
val (exit, out, err) = runApp("Chapter16PrintAllHyperlinksApp", file)
//then
exit shouldBe 0
err shouldBe ""
out shouldBe """+---------------------------------------+----------------------+
|| <img src="http://test.com/img1.png"/> | http://test.com/ref1 |
|| Ref 2 | http://test.com/ref2 |
|| Ref 3 | http://test.com/ref3 |
|+---------------------------------------+----------------------+
|""".stripMargin
}
"mapToXml" should "produce XML from the given map" in {
//given
val map = Map("A" -> "1", "B" -> "2")
//when
val result: Elem = mapToXml(map)
//then
result shouldBe <dl><dt>A</dt><dd>1</dd><dt>B</dt><dd>2</dd></dl>
}
"xmlToMap" should "produce map from the given Xml" in {
//given
val xml = <dl><dt>A</dt><dd>1</dd><dt>B</dt><dd>2</dd></dl>
//when
val result: Map[String, String] = xmlToMap(xml)
//then
result shouldBe Map("A" -> "1", "B" -> "2")
}
"transformXhtml" should "transform an XHTML document" in {
//given
val root = XML.load(getClass.getResourceAsStream("/Chapter16Task04.html"))
//when
val result: Node = transformXhtml(root)
//then
result.toString() shouldBe """<html>
| <body>
| <p>
| <a href="http://test.com/ref1">
| <img alt="TODO" src="http://test.com/img1.png"/>
| </a>
| </p>
| <a href="http://test.com/ref2">Ref 2</a>
| <div>
| <img alt="TODO" src="http://test.com/img2.png"/>
|
| <a href="http://test.com/ref3">Ref 3</a>
| </div>
| <img src="http://test.com/img3.png" alt="Some text"/>
| </body>
|</html>""".stripMargin
}
"transformXhtmlFile" should "transform XHTML file and saves the result to another file" in {
//given
val inFile = "/Chapter16Task10.xhtml"
val tmpFile = File.createTempFile("Chapter16Task10Out", "xhtml")
tmpFile.deleteOnExit()
//when
transformXhtmlFile(inFile, tmpFile.getAbsolutePath)
//then
Source.fromFile(tmpFile).mkString shouldBe
"""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|<html>
|<head>
| <title>
| Sample XHTML document
| </title>
|</head>
|<body>
| <div>
| [CDATA[should be preserved]]
| </div>
| <p>
| <a href="http://test.com/ref1">
| <img alt="TODO" src="http://test.com/img1.png"/>
| </a>
| </p>
| <p>
| <a href="http://test.com/ref2">Ref 2</a>
| </p>
| <div>
| <img alt="TODO" src="http://test.com/img2.png"/>
|
| <a href="http://test.com/ref3">Ref 3</a>
| </div>
| <p>
| <img src="http://test.com/img3.png" alt="Some text"/>
| </p>
|</body>
|</html>""".stripMargin
}
}