Skip to content

SCALA-55 #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions scala-lang/src/test/scala/com/baeldung/scala/CaseClasses.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.baeldung.scala


import org.junit.Assert.assertEquals
import org.junit.Test

case class CovidCountryStats(countryCode: String, deaths: Int, confirmedCases: Int)

class CaseClassesUnitTest {
@Test
def givenCaseClass_whenPatternMatched_thenReturnsProperValue() = {
val covidPL = CovidCountryStats("PL", 776, 15366)

val text = covidPL matches {
case CovidCountryStats("PL", x, y) => "Death rate for Poland is " + x.toFloat / y.toFloat
case _ => "Unknown country"
}

assertEquals("Death rate for Poland is 0.05050111", text)
}

@Test
def givenTwoEqualsCaseClasses_whenCheckingEquality_thenReturnsTrue(): Unit = {
assert(CovidCountryStats("PL", 776, 15366) == CovidCountryStats("PL", 776, 15366))
}

@Test
def givenCaseClass_whenCallingCopy_thenParametersAreCopied(): Unit = {
val covidPL = CovidCountryStats("PL", 776, 15366)
val covidUA = covidPL.copy(countryCode = "UA")

assertEquals("UA", covidUA.countryCodeo)
assertEquals(766, covidUA.deaths)
assertEquals(15366, covidUA.confirmedCases)
}

@Test
def givenTuple_whenCallingApply_thenCreatesNewInstance() = {
val tuple = ("PL", 776, 15366)
val covidPL = (CovidCountryStats.apply _).tupled(tuple)

assertEquals(CovidCountryStats("PL", 776, 15366), covidPL)
}
}