Skip to content

Commit 7e43719

Browse files
author
Matteo Di Pirro
committed
Merge branch 'master' into scala-355
# Conflicts: # build.sbt
2 parents 746cf4d + b0ab80d commit 7e43719

File tree

12 files changed

+178
-6
lines changed

12 files changed

+178
-6
lines changed

build.sbt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ lazy val scala_akka = (project in file("scala-akka"))
205205

206206
lazy val scala_akka_2 = (project in file("scala-akka-2"))
207207
.enablePlugins(AkkaGrpcPlugin)
208+
.configs(IntegrationTest)
208209
.settings(
209210
name := "scala-akka-2",
211+
Defaults.itSettings,
210212
libraryDependencies ++= Seq(
211213
"com.typesafe.akka" %% "akka-actor-typed" % AkkaVersion,
212214
"com.typesafe.akka" %% "akka-stream" % AkkaVersion,
@@ -216,10 +218,10 @@ lazy val scala_akka_2 = (project in file("scala-akka-2"))
216218
"com.typesafe.akka" %% "akka-http-testkit" % AkkaHttpVersion,
217219
"com.lightbend.akka" %% "akka-stream-alpakka-sse" % "5.0.0",
218220
"com.typesafe.akka" %% "akka-persistence-typed" % AkkaVersion,
219-
"com.typesafe.akka" %% "akka-actor-testkit-typed" % AkkaVersion % Test,
220-
"com.typesafe.akka" %% "akka-http-testkit" % AkkaHttpVersion % Test,
221-
"com.typesafe.akka" %% "akka-stream-testkit" % AkkaVersion % Test
222-
) ++ scalaTestDeps
221+
"com.typesafe.akka" %% "akka-actor-testkit-typed" % AkkaVersion % "it,test",
222+
"com.typesafe.akka" %% "akka-http-testkit" % AkkaHttpVersion % "it,test",
223+
"com.typesafe.akka" %% "akka-stream-testkit" % AkkaVersion % Test
224+
) ++ scalaTestDeps.map(_.withConfigurations(Some("it,test")))
223225
)
224226
val monocleVersion = "2.1.0"
225227
val slickVersion = "3.4.1"

play-scala/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
- [Caching in Play Framework for Scala](https://www.baeldung.com/scala/play-caching)
77
- [Error Handling in the Play Framework Using Scala](https://www.baeldung.com/scala/play-error-handling)
88
- [Dependency Injection with Play](https://www.baeldung.com/scala/play-di)
9+
- [Testing a Play Application](https://www.baeldung.com/scala/scalatest-play-app)

scala-akka-2/src/test/scala-2/com/baeldung/scala/akka_2/alpakka_sse/WikimediaSSEConsumerTest.scala renamed to scala-akka-2/src/it/scala-2/com/baeldung/scala/akka_2/alpakka_sse/WikimediaSSEConsumerLiveTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import akka.testkit.TestKit
55
import org.scalatest.BeforeAndAfterAll
66
import org.scalatest.wordspec.AsyncWordSpecLike
77

8-
class WikimediaSSEConsumerTest
8+
class WikimediaSSEConsumerLiveTest
99
extends TestKit(ActorSystem("wikimediaSSESpec"))
1010
with AsyncWordSpecLike
1111
with BeforeAndAfterAll {
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.baeldung.scala.highorderfunctions
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should.Matchers
5+
6+
class HighOrderFunctionsUnitTest extends AnyFlatSpec with Matchers {
7+
8+
"reduceLeft" should "calculate the sum correctly" in {
9+
val numbers = List(1, 2, 3, 4, 5)
10+
val actualSum = numbers.reduceLeft(_ + _)
11+
// op: 1 + 2 = 3
12+
// op: 3 + 3 = 6
13+
// op: 6 + 4 = 10
14+
// op: 10 + 5 = 15
15+
// res: Int = 15
16+
assert(15 == actualSum)
17+
}
18+
19+
"reduceLeft" should "concatenate strings correctly" in {
20+
val alphabets = List("A", "B", "C", "D", "E")
21+
val actualResult = alphabets.reduceLeft(_ + _)
22+
assert("ABCDE" == actualResult)
23+
}
24+
25+
"reduceLeft" should "find the largest number correctly" in {
26+
val numbers = List(10, 22, 5, 71, 43)
27+
val actualResult = numbers.reduceLeft(_ max _)
28+
assert(71 == actualResult)
29+
}
30+
31+
"reduceRight" should "calculate the sum correctly" in {
32+
val numbers = List(1, 2, 3, 4, 5)
33+
val actualSum = numbers.reduceRight(_ + _)
34+
// op: 5 + 4 = 9
35+
// op: 9 + 3 = 12
36+
// op: 12 + 2 = 14
37+
// op: 14 + 1 = 15
38+
// res: Int = 15
39+
assert(15 == actualSum)
40+
}
41+
42+
"foldLeft" should "calculate the sum correctly" in {
43+
val numbers = List(1, 2, 3, 4, 5)
44+
val actualSum = numbers.foldLeft(5)(_ + _)
45+
// op: 5 + 1 = 6
46+
// op: 6 + 2 = 8
47+
// op: 8 + 3 = 11
48+
// op: 11 + 4 = 15
49+
// op: 15 + 5 = 20
50+
// res: Int = 20
51+
assert(20 == actualSum)
52+
}
53+
54+
"foldRight" should "concatenate the strings correctly" in {
55+
val alphabets = List("A", "B", "C", "D", "E")
56+
val actualResult = alphabets.foldRight("$")(_ + _)
57+
// op: E + $ = E$
58+
// op: D + E$ = DE$
59+
// op: C + DE$ = CDE$
60+
// op: B + CDE$ = BCDE$
61+
// op: A + BCDE$ = ABCDE$
62+
// res: String = ABCDE$
63+
assert("ABCDE$" == actualResult)
64+
}
65+
66+
"scanLeft" should "have correct intermediate states" in {
67+
val numbers = List(1, 2, 3, 4, 5)
68+
val actualResult = numbers.scanLeft(1)(_ + _)
69+
assert(List(1, 2, 4, 7, 11, 16) == actualResult)
70+
}
71+
72+
"scanRight" should "have correct intermediate states" in {
73+
val numbers = List(1, 2, 3, 4, 5)
74+
val actualResult = numbers.scanRight(1)(_ + _)
75+
assert(List(16, 15, 13, 10, 6, 1) == actualResult)
76+
}
77+
78+
"reduceLeft" should "throw an exception" in {
79+
val numbers = List.empty[Int]
80+
assertThrows[UnsupportedOperationException] {
81+
numbers.reduceLeft(_ max _)
82+
}
83+
}
84+
85+
"foldRight" should "return the initial element i.e $" in {
86+
val alphabets = List.empty[String]
87+
val actualResult = alphabets.foldRight("$")(_ + _)
88+
assert("$" == actualResult)
89+
}
90+
91+
"scanRight" should "return the initial element i.e 5" in {
92+
val numbers = List.empty[Int]
93+
val actualResult = numbers.scanRight(5)(_ + _)
94+
assert(List(5) == actualResult)
95+
}
96+
}

scala-core/build.sbt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Scala 3 configuration
2+
lazy val Scala3Test = config("scala3Test").extend(Test)
3+
// Source directories
4+
scalaSource in Scala3Test := baseDirectory.value / "src" / "main" / "scala-3"
5+
// Test directories
6+
scalaSource in Scala3Test := baseDirectory.value / "src" / "test" / "scala-3"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.scala.traits
2+
3+
class Author(name: String) extends Writer(name) {
4+
def write(): String = s"$name is writing a book"
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.scala.traits
2+
3+
class MultipleInheritance extends Trait1 with Trait2 {
4+
override def method1(): String = "Trait1 method"
5+
6+
override def method2(): String = "Trait2 method"
7+
}
8+
9+
trait Trait1 {
10+
def method1(): String
11+
}
12+
13+
trait Trait2 {
14+
def method2(): String
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.scala.traits
2+
3+
class Poet(name: String) extends Writer(name) {
4+
def write(): String = s"$name is composing poetry"
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.scala.traits
2+
3+
trait Writer(val name: String) {
4+
def introduce = s"Hello, I'm $name"
5+
6+
def write(): String
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.scala.traits
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should.Matchers
5+
6+
class MultipleInheritanceUnitTest extends AnyFlatSpec with Matchers {
7+
8+
"A class extended from multiple traits" should "have access to all the methods" in {
9+
val instance = new MultipleInheritance()
10+
11+
instance.method1() shouldEqual "Trait1 method"
12+
instance.method2() shouldEqual "Trait2 method"
13+
}
14+
}

0 commit comments

Comments
 (0)