Skip to content

[SCALA-26] Equality in scala #80

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 4 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.baeldung.scala

package object equality {
class PersonSimpleClass(val name: String, val age: Int)

class PersonClassWithOverrides(val name: String, val age: Int) {
override def equals(other: Any): Boolean = other match {
case person: PersonClassWithOverrides =>
this.name == person.name && this.age == person.age
case _ => false
}

override def hashCode(): Int = if (name eq null) age else name.hashCode + 31 * age
}

case class PersonCaseClass(name: String, age: Int)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.baeldung.scala.equality

import org.scalatest.FlatSpec

class EqualityTest extends FlatSpec {
"Equality operator for AnyVal" should "work as in Java" in {
val intAnyVal = 4
assert(intAnyVal == 2 * 2)
}

"Equality operator for referential types" should "work like null-safe equals()" in {
val firstString = new String("AnyRef")
val secondString = new String("AnyRef")
val thirdString = null
val fourthString = null
assert(firstString == secondString)
// Unlike in java, the following lines of code do not cause NullPointerExceptions
assert(thirdString != secondString)
assert(fourthString == thirdString)
}

"Equals()" should "work as in Java" in {
val firstString = new String("AnyRef")
val secondString = new String("AnyRef")
assert(firstString.equals(secondString))
}

"Eq and ne" should "check referential equality" in {
val firstString = new String("AnyRef")
val secondString = new String("AnyRef")
val thirdString = secondString
assert(firstString ne secondString)
assert(thirdString eq secondString)
// Both operators are null-safe
assert(null eq null)
assert(null ne firstString)
}

"Equality operator" should "not work out of the box for PersonSimpleClass" in {
val firstSimpleClassInstance = new PersonSimpleClass("Donald", 66)
val secondSimpleClassInstance = new PersonSimpleClass("Donald", 66)
assert(firstSimpleClassInstance != secondSimpleClassInstance)
}

"Equality operator" should "work for PersonClassWithOverrides" in {
val firstClassWithOverridesInstance = new PersonClassWithOverrides("Donald", 66)
val secondClassWithOverridesInstance = new PersonClassWithOverrides("Donald", 66)
assert(firstClassWithOverridesInstance == secondClassWithOverridesInstance)
}

"Equality operator" should "work for PersonCaseClass" in {
val firstCaseClassInstance = PersonCaseClass("Donald", 66)
val secondCaseClassInstance = PersonCaseClass("Donald", 66)
assert(firstCaseClassInstance == secondCaseClassInstance)
}
}