Skip to content

Commit a6113b5

Browse files
Tapan AvasthiTapan Avasthi
authored andcommitted
SCALA-626: Add unit test for Trait Parameter and Multiple Inheritance
1 parent dda9cd0 commit a6113b5

File tree

7 files changed

+73
-0
lines changed

7 files changed

+73
-0
lines changed

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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.scala.traits
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should.Matchers
5+
6+
class TraitParametersUnitTest extends AnyFlatSpec with Matchers {
7+
8+
"An Author" should "return a proper introduction" in {
9+
val author = new Author("Mark Twain")
10+
val introduction = author.introduce
11+
12+
introduction shouldEqual "Hello, I'm Mark Twain"
13+
}
14+
15+
"A Poet" should "return a proper introduction" in {
16+
val poet = new Poet("Sylvia Plath")
17+
val introduction = poet.introduce
18+
19+
introduction shouldEqual "Hello, I'm Sylvia Plath"
20+
}
21+
}

0 commit comments

Comments
 (0)