File tree Expand file tree Collapse file tree 7 files changed +73
-0
lines changed
main/scala-3/com/baeldung/scala/traits
test/scala-3/com/baeldung/scala/traits Expand file tree Collapse file tree 7 files changed +73
-0
lines changed Original file line number Diff line number Diff line change
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"
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments