|
Scalactic User Guide Custom equality Default equality Constrained equality Tolerance Normalization The Explicitly DSL Or and Every Requirements Snapshots TimesOnInt |
Custom equality
Scalactic's
For example, say you have a case class that includes a scala> case class Person(name: String, age: Double) defined class Person
Imagine you are calculating the
scala> import org.scalactic._
import org.scalactic._
scala> import TripleEquals._
import TripleEquals._
scala> Person("Joe", 29.0001) === Person("Joe", 29.0)
res0: Boolean = false
Scalactic's
To make the equality check more forgiving, you could define an implicit
scala> import Tolerance._
import Tolerance._
scala> implicit val personEq =
| new Equality[Person] {
| def areEqual(a: Person, b: Any): Boolean =
| b match {
| case p: Person => a.name == p.name && a.age === p.age +- 0.0002
| case _ => false
| }
| }
personEq: org.scalactic.Equality[Person] = $anon$1@2b29f6e7
Now the
scala> Person("Joe", 29.0001) === Person("Joe", 29.0)
res1: Boolean = true
Next, learn more about default equality. |
Scalactic is brought to you by Bill Venners, with
contributions from several other folks. It is sponsored by
Artima, Inc.
ScalaTest is free, open-source software
released under the Apache
2.0 license.
Copyright © 2009-2025 Artima, Inc. All Rights Reserved.