-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathChapter09Spec.scala
193 lines (161 loc) · 5.66 KB
/
Chapter09Spec.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
187
188
189
190
191
192
193
import Chapter09._
import TestUtils.{printToTmpFile, runApp}
import java.io.File
import org.scalatest.{FlatSpec, Matchers}
import scala.io.Source.fromFile
class Chapter09Spec extends FlatSpec with Matchers {
"reverseLines" should "reverse lines in file" in {
//given
val file = printToTmpFile("reverseLines", """line 1
|line 2
|line 3
|""".stripMargin)
//when
reverseLines(file)
//then
fromFile(file).mkString shouldBe """line 3
|line 2
|line 1
|""".stripMargin
}
"replaceTabs" should "replace tabs with spaces using column boundaries" in {
//given
val file = printToTmpFile("replaceTabs", """text text2 text3
| text text2 text3
| text text2 text3
|""".stripMargin)
//when
replaceTabs(file, 2)
//then
fromFile(file).mkString shouldBe """text text2 text3
| text text2 text3
| text text2 text3
|""".stripMargin
}
"printLongWords" should "read a file and print all words longer than 12 chars" in {
//given
val file = printToTmpFile("printLongWords", "text toooooooolong text2 text3texttext2text3")
//when
val (exit, out, err) = runApp("Chapter09PrintLongWordsApp", file.getAbsolutePath)
//then
exit shouldBe 0
err shouldBe ""
out shouldBe """toooooooolong
|text3texttext2text3
|""".stripMargin
}
"printNumbersStat" should "read numbers from file and print sum, average, min, max" in {
//given
val file = printToTmpFile("printNumbersStat", "1 1.2 2.34 -5 25.5 0.0 1.234")
//when
val (exit, out, err) = runApp("Chapter09PrintNumbersStatApp", file.getAbsolutePath)
//then
exit shouldBe 0
err shouldBe ""
out shouldBe """sum: 26.274
|average: 3.753
|minimum: -5.000
|maximum: 25.500
|""".stripMargin
}
"printPowersOf2" should "print powers of 2 and their reciprocals to a file" in {
//given
val file = File.createTempFile("printPowersOf2", "txt")
file.deleteOnExit()
//when
printPowersOf2(file)
//then
val res = new StringBuilder
for (i <- 0 to 20) {
res.append("%8.0f %f\n".format(math.pow(2.0, i), math.pow(2.0, -i)))
}
fromFile(file).mkString shouldBe res.toString
}
"printQuotedStrings" should "read source file and print all double quoted strings" in {
//given
val file = "src/main/scala/Chapter09.scala"
//when
val (exit, out, err) = runApp("Chapter09PrintQuotedStringsApp", file)
//then
exit shouldBe 0
err shouldBe ""
out shouldBe """\\s+
|\\s+
|sum: %.3f\n
|average: %.3f\n
|minimum: %.3f\n
|maximum: %.3f\n
|%8.0f %f
|like this, maybe with \" or \\
|\"(([^\\\\\"]+|\\\\([btnfr\"'\\\\]|[0-3]?[0-7]{1,2}|u[0-9a-fA-F]{4}))*)\"
|(?![\\d]+(\\.[\\d]+)?)\\w+
|(?i)<img\\s+(.*?\\s+)?src\\s*=\\s*\"([^\"]+)\"
|.class
|""".stripMargin
}
"printNonNumberTokens" should "print tokens from file that are not floating-point numbers" in {
//given
val file = printToTmpFile("printNonNumberTokens", "1 first 2.34 second -5 0.0 1.234 third")
//when
val (exit, out, err) = runApp("Chapter09PrintNonNumberTokensApp", file.getAbsolutePath)
//then
exit shouldBe 0
err shouldBe ""
out shouldBe """first
|second
|third
|""".stripMargin
}
"printSrcOfImageTags" should "replace tabs with spaces using column boundaries" in {
//given
val file = printToTmpFile("printSrcOfImageTags",
"""<!DOCTYPE html>
|<html>
|<body>
| <IMG alt="scala logo" src="http://scala-lang.org/resources/img/scala-logo-white.png"/>
|</body>
|</html>
|""".stripMargin)
//when
val (exit, out, err) = runApp("Chapter09PrintSrcOfImageTagsApp", file.getAbsolutePath)
//then
exit shouldBe 0
err shouldBe ""
out shouldBe """http://scala-lang.org/resources/img/scala-logo-white.png
|""".stripMargin
}
"countClassFiles" should "count .class files in directory and all sub-directories" in {
//given
val dir = "target/scala-2.11/classes/com"
//when
val count = countClassFiles(dir)
//then
count shouldBe 16
}
"Person" should "be serializable" in {
//given
val file = File.createTempFile("personSerialization", "bin")
file.deleteOnExit()
val fred = new Person("Fred")
val bob = new Person("Bob")
val john = new Person("John")
fred.addFriend(bob)
fred.addFriend(john)
bob.addFriend(fred)
bob.addFriend(john)
john.addFriend(fred)
john.addFriend(bob)
val friends = Array[Person](fred, bob, john, new Person("Dummy"))
//when
savePersons(file, friends)
val result: Array[Person] = readPersons(file)
//then
result.length shouldBe friends.length
for ((person, friend) <- result.zip(friends)) {
person.size shouldBe friend.size
for ((p, f) <- person.zip(friend)) {
p.name shouldBe f.name
}
}
}
}