Skip to content

Commit

Permalink
updated to newer Scala 3 syntax with fewer braces
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Läufer <[email protected]>
  • Loading branch information
klaeufer committed Sep 28, 2023
1 parent a3087b5 commit 53893a7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 40 deletions.
3 changes: 1 addition & 2 deletions src/main/scala/functional/simple/CumAvgFunctional.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ object CumAvgFunctional:
case ((count, sum), value) => (count + 1, sum + value)
}
// convert the count and sum pairs to count and cumulative average pairs
val countsWithAvgs = countsWithSums.map {
val countsWithAvgs = countsWithSums.map:
(count, sum) => (count, sum / count)
}

// print the results except for the first one
countsWithAvgs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,24 @@ class TestCumulativeLengthFunctional extends AnyWordSpec:
/** Refers to the existing immutable SUT instance. */
val sut = CumulativeLengthFunctionalModular

"The functional CumulativeLength calculator" when {
"given an empty iterator" should {
"produce an empty output" in {
"The functional CumulativeLength calculator" when:
"given an empty iterator" should:
"produce an empty output" in:
// exercise SUT
val result: Iterator[(String, Int)] = sut.run(Iterator.empty)
// check resulting iterator
assert(result.isEmpty)
}
}

"given a nonempty iterator" should {
"produce the correct nonempty output" in {
"given a nonempty iterator" should:
"produce the correct nonempty output" in:
// input data for this test case
val data = Seq("hello", "world", "what", "up")
val expected = data.zip(Seq(5, 10, 14, 16))
// exercise SUT
val actual: Iterator[(String, Int)] = sut.run(data.iterator)
// check resulting iterator
assert(actual.toSeq == expected)
}
}

// TODO test incremental correctness
}

end TestCumulativeLengthFunctional
21 changes: 7 additions & 14 deletions src/test/scala/functional/modular/TestLineCountFunctional.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,25 @@ class TestLineCountFunctional extends AnyWordSpec:
/** Refers to the existing immutable SUT instance. */
val sut = LineCountFunctionalModular

"The functional LineCounter" when {
"given an empty iterator" should {
"produce an empty output" in {
"The functional LineCounter" when:
"given an empty iterator" should:
"produce an empty output" in:
// exercise SUT
val result: Iterator[(Int, String)] = sut.run(Iterator.empty)
// check effect on output observer
assert(result.isEmpty)
}
}

"given a nonempty iterator" should {
"produce the correct nonempty output" in {
"given a nonempty iterator" should:
"produce the correct nonempty output" in:
// input data for this test case
val data = Seq("hello", "world", "what", "up")
// exercise SUT
val result: Iterator[(Int, String)] = sut.run(data.iterator)
// check effect on output observer
assert(result.toSeq == (1 to data.length).zip(data))
}
}

"given a nonempty iterator" should {
"exhibit the correct interactive behavior" in {
"given a nonempty iterator" should:
"exhibit the correct interactive behavior" in:
// input data for this test case
val input = Iterator("hello", "world", "what", "up")
// exercise SUT
Expand All @@ -41,8 +37,5 @@ class TestLineCountFunctional extends AnyWordSpec:
i("world"), o((2, "world")),
i("what"), o((3, "what")),
i("up"), o((4, "up"))))
}
}
}

end TestLineCountFunctional
21 changes: 7 additions & 14 deletions src/test/scala/imperative/modular/TestLineCountImperative.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ class TestLineCountImperative extends AnyWordSpec:
def createSUT() = new CountItems with OutputToBuffer:
override type Input = String

"An imperative LineCounter" when {
"given an empty iterator" should {
"produce an empty output" in {
"An imperative LineCounter" when:
"given an empty iterator" should:
"produce an empty output" in:
// create SUT instance for this test case
val sut = createSUT()
// exercise SUT
sut.run(Seq.empty)(Iterator.empty)
// check effect on output observer
assert(sut.buffer.isEmpty)
}
}

"given a nonempty iterator" should {
"produce the correct nonempty output" in {
"given a nonempty iterator" should:
"produce the correct nonempty output" in:
// input data for this test case
val data = Seq("hello", "world", "what", "up")
// create SUT instance for this test case
Expand All @@ -30,12 +28,9 @@ class TestLineCountImperative extends AnyWordSpec:
sut.run(Seq.empty)(data.iterator)
// check effect on output observer
assert(sut.buffer == (1 to data.length).zip(data))
}
}
}

"given a nonempty iterator" should {
"exhibit the correct interactive behavior" in {
"given a nonempty iterator" should:
"exhibit the correct interactive behavior" in:
// input data for this test case
val input = Iterator("hello", "world", "what", "up")
// create SUT instance for this test case
Expand All @@ -50,7 +45,5 @@ class TestLineCountImperative extends AnyWordSpec:
i("world"), o((2, "world")),
i("what"), o((3, "what")),
i("up"), o((4, "up"))))
}
}

end TestLineCountImperative

0 comments on commit 53893a7

Please sign in to comment.