Skip to content

Commit cdcdd3c

Browse files
SCALA-17 Scala Common Collections (#90)
* SCALA-17 Scala Common Collections * Fix assertion and compilation issues in the test case
1 parent 6177922 commit cdcdd3c

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

build.sbt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ lazy val scala_lang = (project in file("scala-lang"))
2424
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
2525
)
2626

27+
lazy val scala_core_collections = (project in file("scala-core-collections"))
28+
.settings(
29+
name := "scala-core-collections",
30+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
31+
)
32+
2733
lazy val scala_test = (project in file("scala-test"))
2834
.settings(
2935
name := "scala-test",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.scala
2+
3+
import scala.collection.mutable
4+
5+
object ScalaCollections {
6+
7+
// Scala List
8+
val numbersList: List[Int] = List(1, 2, 3, 4)
9+
val emptyList: List[Int] = List()
10+
11+
val numbersListWithOperator: List[Int] = 1 :: 2 :: 3 :: 4 :: Nil
12+
val emptyListWithNil: List[Int] = Nil
13+
val x :: xs = numbersList
14+
15+
// Scala Set
16+
val emptySet: Set[Int] = Set()
17+
val numbersSet: Set[Int] = Set(1, 2, 3, 4)
18+
19+
//Scala Map
20+
val immutableMap: Map[Int, String] = Map(1 -> "a", 2 -> "b")
21+
val mutableMap: mutable.Map[Int, String] = collection.mutable.Map(1 -> "a", 2 -> "b")
22+
23+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.baeldung.scala
2+
3+
import org.scalatest.{Matchers, _}
4+
5+
class ScalaCollectionsUnitTest extends FlatSpec with Matchers {
6+
7+
"Scala list" should "concatenate using ::: operator" in {
8+
//given
9+
val list1 = List(1, 2)
10+
val list2 = List(3, 4)
11+
12+
//when
13+
val res = list1 ::: list2
14+
15+
//then
16+
res shouldBe List(1, 2, 3, 4)
17+
}
18+
19+
it should "create a list will all identical elements using fill method" in {
20+
//when and then
21+
List.fill(3)(100) shouldBe List(100, 100, 100)
22+
}
23+
24+
it should "reverse should create a reverse all elements of the list" in {
25+
//given
26+
val list = List(1, 2, 3, 4)
27+
28+
//when and then
29+
list.reverse shouldBe List(4, 3, 2, 1)
30+
}
31+
32+
"Scala set" should "return head of the set when .head method invoked" in {
33+
//given
34+
val set = Set(1, 2, 3, 4)
35+
36+
//when and then
37+
set.head shouldBe 1
38+
}
39+
40+
it should "return all element of set except head when .tail method invoked" in {
41+
//given
42+
val set = Set(1, 2, 3, 4)
43+
44+
//when and then
45+
set.tail shouldBe Set(2, 3, 4)
46+
}
47+
48+
"Maps" should "return all keys from the map when .keys method invoked" in {
49+
//given
50+
val map = Map(1 -> "a", 2 -> "b")
51+
52+
//when and then
53+
map.keys shouldBe Set(1, 2)
54+
}
55+
56+
it should "return all values from the map when .values invoked" in {
57+
//given
58+
val map = Map(1 -> "a", 2 -> "b")
59+
60+
//when and then
61+
map.values.toList shouldBe List("a", "b")
62+
}
63+
64+
it should "return None if the key is not found in map when .get method invoked" in {
65+
//given
66+
val map = Map(1 -> "a", 2 -> "b")
67+
68+
//when
69+
val res = map.get(-1)
70+
71+
//then
72+
res.isEmpty shouldBe true
73+
}
74+
75+
it should "return value if the key is found in map using .get method" in {
76+
//given
77+
val map = Map(1 -> "a", 2 -> "b")
78+
79+
//when
80+
val res = map.get(1)
81+
82+
//then
83+
res shouldBe Some("a")
84+
}
85+
}

scala-lang/src/test/scala/com/baeldung/scala/CaseClasses.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CaseClassesUnitTest {
1111
def givenCaseClass_whenPatternMatched_thenReturnsProperValue() = {
1212
val covidPL = CovidCountryStats("PL", 776, 15366)
1313

14-
val text = covidPL matches {
14+
val text = covidPL match {
1515
case CovidCountryStats("PL", x, y) => "Death rate for Poland is " + x.toFloat / y.toFloat
1616
case _ => "Unknown country"
1717
}
@@ -29,8 +29,8 @@ class CaseClassesUnitTest {
2929
val covidPL = CovidCountryStats("PL", 776, 15366)
3030
val covidUA = covidPL.copy(countryCode = "UA")
3131

32-
assertEquals("UA", covidUA.countryCodeo)
33-
assertEquals(766, covidUA.deaths)
32+
assertEquals("UA", covidUA.countryCode)
33+
assertEquals(776, covidUA.deaths)
3434
assertEquals(15366, covidUA.confirmedCases)
3535
}
3636

0 commit comments

Comments
 (0)