-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuite.scala
504 lines (388 loc) · 12.2 KB
/
Suite.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import scala.concurrent.duration.Duration
import scala.collection.mutable.ArrayBuffer
import javax.lang.model.`type`.UnknownTypeException
import scala.collection.mutable.ListBuffer
import scala.annotation.switch
abstract class BaseSuite extends munit.FunSuite {
override val munitTimeout = Duration(10, "sec")
val on_base: String = "on base"
}
class BaseFixture extends BaseSuite {
var on_setup: String = ""
var ints = ArrayBuffer.empty[Int]
val f = FunFixture[String](
setup = { _ => {
ints = ArrayBuffer(1, 2, 3, 4, 5)
on_setup = "on setup"
on_setup
}
},
teardown = { _ => on_setup = "this fixture was torn down" },
)
f.test("base suite is extended") { _ =>
assertEquals(on_base, "on base")
}
f.test("fixture setup context is set") { _ =>
assertEquals(on_base, "on base")
assertEquals(on_setup, "on setup")
}
f.test(".fail test fails".fail) { _ =>
assert(false)
}
}
class ControlStructures extends BaseFixture {
f.test("control structures are expressions") { _ =>
val is = if 1 > 0 then "greater" else "less"
assertEquals(is, "greater")
}
f.test("scala 3 ifs don't need braces") { _ =>
val x =
if 0 == 1 then
val a = 2
a + 3
else if 2 == 2 then
val b = 3
b - 1
else
val c = 4
c / 2
end if // optional
assertEquals(x, 2)
}
f.test("for loop generator") { _ =>
var ints2 = ArrayBuffer.empty[Int]
var ints3 = ArrayBuffer.empty[Int]
// one-liner
for i <- ints do ints2 += i
assertEquals(ints2, ArrayBuffer(1, 2, 3, 4, 5))
// multi-line
for i <- ints
do
val j = i * -1
ints3 += j
assertEquals(ints3, ArrayBuffer(-1, -2, -3, -4, -5))
}
f.test("for loops can have multiple generators") { _ =>
var list = ListBuffer.empty[Char]
for
i <- 'a' to 'b'
j <- 'c' to 'd'
k <- 'e' to 'f'
do
list += i
list += j
list += k
assertEquals(list, ListBuffer(
'a', 'c', 'e',
'a', 'c', 'f',
'a', 'd', 'e',
'a', 'd', 'f',
'b', 'c', 'e',
'b', 'c', 'f',
'b', 'd', 'e',
'b', 'd', 'f',
))
}
f.test("for loops can iterate maps") { _ =>
var list = ListBuffer.empty[String]
val states = Map(
"AP" -> "Amapá",
"MT" -> "Mato Grosso",
"CE" -> "Ceará",
)
for (abbrev, full_name) <- states do list += s"$abbrev: $full_name"
assertEquals(list, ListBuffer(
"AP: Amapá",
"MT: Mato Grosso",
"CE: Ceará",
))
}
f.test("for loop guards") { _ =>
var greater = ArrayBuffer.empty[Int]
for i <- ints
if i > 3
do
greater += i
assertEquals(greater, ArrayBuffer(4, 5))
}
f.test("for with multiple guards and generators") { _ =>
var last_i = -1
var last_j = 'Z'
for
i <- 1 to 3
j <- 'a' to 'c'
if i == 2
if j == 'b'
do
last_i = i
last_j = j
assertEquals(last_i, 2)
assertEquals(last_j, 'b')
}
f.test("for yield returns the same data structure with results") { _ =>
val doubles = for i <- ints yield i * 2
assertEquals(doubles, ArrayBuffer(2, 4, 6, 8, 10))
}
f.test("yield expressions can have a block body") { _ =>
val names = List("_olivia", "_walter", "_peter")
val cap_names = for name <- names yield
val name_without_underscore = name.drop(1)
name_without_underscore.capitalize
assertEquals(cap_names, List("Olivia", "Walter", "Peter"))
}
f.test("for yield is equivalent to a map expression") { _ =>
val map_list = (10 to 22).map(_ * 2)
val for_list = for i <- 10 to 22 yield i * 2
assertEquals(map_list, for_list)
}
f.test("for can yield multiple values") { _ =>
val results = ArrayBuffer.empty[Int]
val result = for
int <- ints if int % 2 == 0
yield
results += int
val square = int * int
results += square
results += square * 2
assertEquals(results, ArrayBuffer(2, 4, 8, 4, 16, 32))
}
f.test("while loop syntax") { _ =>
var i = 0
while i < 9 do
i -= 1
i += 2
assertEquals(i, 9)
}
f.test("while loop with block return") { _ =>
val result = {
var x = 0
while x < 13 do x += 3
x
}
assertEquals(result, 15)
}
f.test("basic match expression") { _ =>
@switch val i = 3
var number = ""
i match
case 1 => number = "one"
case 2 => number = "two"
case 3 => number = "three"
case _ => number = "other"
assertEquals(number, "three")
}
f.test("match structures are expressions") { _ =>
@switch val i = 2
val number = i match
case 1 => "one"
case 2 => "two"
case 3 => "three"
case _ => "other"
assertEquals(number, "two")
}
f.test("match expressions can match types") { _ =>
def matchType(x: Matchable): String = x match
case s: String => s"String containing: $s"
case i: Int => "Int"
case d: Double => "Double"
case l: List[?] => "List"
case _ => "Unexpected type"
assertEquals(matchType("a string"), "String containing: a string")
assertEquals(matchType(4.92), "Double")
assertEquals(matchType(List(3, 2, 1)), "List")
}
f.test("a lowercase variable on match left captures a default") { _ =>
val i = 3
val r = i match
case 0 => '0'
case 1 => '1'
case 2 => '2'
case x => x
assertEquals(r, 3)
}
f.test("an uppercase variable on match left uses scope") { _ =>
val i, Y = 3
val r = i match
case 0 => '0'
case 1 => '1'
case 2 => '2'
case Y => '3'
case x => 'x' // unreachable
assertEquals(r, '3')
}
f.test("match can handle multiple values in a single line") { _ =>
def even_or_odd(n: Int): String =
n match
case 1 | 3 | 5 | 7 | 9 => "odd"
case 2 | 4 | 6 | 8 | 10 => "even"
case n => s"$n is out of bounds"
assertEquals(even_or_odd(4), "even")
assertEquals(even_or_odd(7), "odd")
assertEquals(even_or_odd(31), "31 is out of bounds")
}
f.test("match cases can have guards") { _ =>
def assert(f: Int => String) =
val pairs = Map(
1 -> "one",
5 -> "between 2 and 5",
10 -> "10 or greater",
31 -> "10 or greater",
-8 -> "0 or less",
)
for (number, range) <- pairs do assertEquals(f(number), range)
def get_range(i: Int) =
i match
case 1 => "one"
case a if a > 1 && a < 6 => "between 2 and 5"
case b if b > 5 && b < 10 => "between 6 and 9"
case c if c >= 10 => "10 or greater"
case _ => "0 or less"
assert(get_range)
// same example using a more idiomatic, readable syntax:
def get_range_2(i: Int) =
i match
case 1 => "one"
case a if 1 to 5 contains a => "between 2 and 5"
case b if 6 to 9 contains b => "between 6 and 9"
case c if c >= 10 => "10 or greater"
case _ => "0 or less"
assert(get_range_2)
}
f.test("fields from classes can be extracted") { _ =>
case class Person(name: String)
def speak(p: Person) = p match
case Person(name) if name == "Fred" => s"$name says, Yubba dubba doo"
case Person(name) if name == "Bam Bam" => s"$name says, Bam bam!"
assertEquals(speak(Person("Fred")), "Fred says, Yubba dubba doo")
}
f.test("extractor methods can initialize values") { _ =>
import scala.util.Random
object CustomerID:
def apply(name: String) = s"$name--${Random.nextLong().abs}"
def unapply(customerID: String): Option[String] =
val stringArray: Array[String] = customerID.split("--").nn.map(_.nn)
if stringArray.tail.nonEmpty then Option(stringArray.head) else None
val customer = CustomerID("Sukyoung")
// these three assignments are all equivalent:
val name1 = customer match
case CustomerID(name) => name
case _ => "Couldn't extract name"
// @unchecked quiets a "pattern binding uses refutable extractor" warning
val CustomerID(name2) = customer: @unchecked
@SuppressWarnings(Array("org.wartremover.warts.OptionPartial"))
val name3 = CustomerID.unapply(customer).get
assertEquals(name1, "Sukyoung")
assertEquals(name1, name2)
assertEquals(name1, name3)
// the corresponding instance doesn't really have to exist
// it will regardless return the unapply result for the given string:
val CustomerID(name4) = s"Jane--0": @unchecked
assertEquals(name4, "Jane")
// dows throw since the body of unapply fails to run for lack of a `-` char
intercept[scala.MatchError]{
val CustomerID(name5) = s"Peter-000000000000": @unchecked
println(name5)
}
}
f.test("try structures are expressions") { _ =>
var always: String = ""
val exception = try
2/0
catch
case e: NumberFormatException => "Got a NumberFormatException"
case e: ArithmeticException => "Got an ArithmeticException"
finally
always = "This always executes"
assertEquals(exception, "Got an ArithmeticException")
assertEquals(always, "This always executes")
}
}
class DomainModelling extends BaseFixture {
// Object-oriented
f.test("traits are similar to interfaces") { _ =>
trait Speaker:
def speak(): String
trait TailWagger:
def startTail(): Unit = {}
def stopTail(): Unit = {}
trait Runner:
def startRunning(): Unit = {}
def stopRunning(): Unit = {}
class Dog(val name: String) extends Speaker, TailWagger, Runner:
def speak(): String = "Woof!"
class Cat(val name: String) extends Speaker, TailWagger, Runner:
def speak(): String = "Meow!"
override def startRunning(): Unit = {}
override def stopRunning(): Unit = {}
val d = Dog("Rover")
assertEquals(d.name, "Rover")
val c = Cat("Morris")
assertEquals(d.speak(), "Woof!")
assertNotEquals(d.speak(), "Meow!")
}
f.test("class declarations create constructors") { _ =>
class Person(var firstName: String, var lastName: String):
def getFullName() = s"$firstName $lastName"
val p = Person("John", "Stephens")
assertEquals(p.firstName, "John")
p.lastName = "Legend"
assertEquals(p.getFullName(), "John Legend")
}
f.test("parameters not assigned as val or var are not accessible") { _ =>
class Person(firstName: String, val lastName: String):
def getFullName() = s"$firstName $lastName"
val p = Person("Jane", "Doe")
// p.firstName // won't compile: Reference Error
assertEquals(p.getFullName(), "Jane Doe")
}
// Functional
f.test("algebraic sum types can be modeled with enums") { _ =>
enum CrustSize:
case Small, Medium, Large
enum CrustType:
case Thin, Thick, Regular
enum Topping:
case Cheese, Pepperoni, BlackOlives, GreenOlives, Onions
import CrustSize.*
val currentCrustSize = Small
val size = currentCrustSize match
case Small => "Small crust size"
case Medium => "Medium crust size"
case Large => "Large crust size"
assertEquals(size, "Small crust size")
}
f.test("sum types can be defined using enum cases with parameters") { _ =>
enum Nat:
case Zero
case Succ(pred: Nat)
val number: Nat = Nat.Succ(Nat.Succ(Nat.Zero)) // represents 2
def increment(n: Nat): Nat = n match
case Nat.Zero => Nat.Succ(Nat.Zero)
case Nat.Succ(pred) => Nat.Succ(increment(pred))
// Nat.Succ(Nat.Succ(Nat.Succ(Nat.Zero))), representing 3
val incrementedNum: Nat = increment(number)
assertEquals(
incrementedNum,
Nat.Succ(Nat.Succ(Nat.Succ(Nat.Zero))),
)
}
f.test("product types can be defined using case classes") { _ =>
case class Musician(
name: String,
vocation: String,
)
val m = Musician("Reginald", "Singer")
assertEquals(m.name, "Reginald")
assertEquals(m.vocation, "Singer")
// m.name = "Joe" // won't compile: Reassignment to val
val m2 = m.copy(name = "나윤선")
assertEquals(m2.name, "나윤선")
}
f.test("methods can be chained for less assignments and brevity") { _ =>
val nums = (1 to 10).toList
val result = nums.filter(_ > 3)
.filter(_ < 7)
.map(_ * 10)
assertEquals(result, List(40, 50, 60))
}
}