Skip to content

Commit 0ab51d2

Browse files
committed
Create a final exercise in ClassesVsCaseClasses and a test
1 parent 7a66f0e commit 0ab51d2

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/main/scala/scalatutorial/sections/ClassesVsCaseClasses.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ object ClassesVsCaseClasses extends ScalaTutorialSection {
155155
* }
156156
*
157157
* // toString redefinition to return the value of an instance instead of its memory addres
158-
* override def toString = s"Note($name, $duration, $octave)"
158+
* override def toString = s"Note($name,$duration,$octave)"
159159
*
160160
* // Create a copy of a case class, with potentially modified field values
161161
* def copy(name: String = name, duration: String = duration, octave: Int = octave): Note =
@@ -170,12 +170,19 @@ object ClassesVsCaseClasses extends ScalaTutorialSection {
170170
* new Note(name, duration, octave)
171171
*
172172
* // Extractor for pattern matching
173-
* def unapply(note: Note): Option[(String, String, Int)) =
173+
* def unapply(note: Note): Option[(String, String, Int)] =
174174
* if (note eq null) None
175175
* else Some((note.name, note.duration, note.octave))
176176
*
177177
* }
178178
* }}}
179179
*/
180-
def nothing(): Unit = ()
180+
def encoding(res0: String, res1: Boolean, res2: String): Unit = {
181+
val c3 = Note("C", "Quarter", 3)
182+
c3.toString shouldBe res0
183+
val d = Note("D", "Quarter", 3)
184+
c3.equals(d) shouldBe res1
185+
val c4 = c3.copy(octave = 4)
186+
c4.toString shouldBe res2
187+
}
181188
}

src/test/scala/scalatutorial/sections/ClassesVsCaseClassesSpec.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ class ClassesVsCaseClassesSpec extends Spec with Checkers {
1919
def `check equality`: Unit =
2020
check(Test.testSuccess(ClassesVsCaseClasses.equality _, false :: true :: HNil))
2121

22+
def `check encoding`: Unit =
23+
check(
24+
Test.testSuccess(
25+
ClassesVsCaseClasses.encoding _,
26+
"Note(C,Quarter,3)" :: false :: "Note(C,Quarter,4)" :: HNil))
27+
2228
}

0 commit comments

Comments
 (0)