Scalactic User Guide

Custom equality

Default equality

Constrained equality

Tolerance

Normalization

The Explicitly DSL

Or and Every

Requirements

Snapshots

TimesOnInt

Normalization

The Scalactic Normalization trait allows you to define a custom ways normalize objects based on their type.

For example, to normalize Doubles by truncating off any decimal part, you might write:

import org.scalactic._

val truncated =
  new Normalization[Double] {
    def normalized(d: Double) = d.floor
  }

Given this definition you could use it with the explicitly DSL like this:

import org.scalatest._
import Matchers._
import TypeCheckedTripleEquals._

(2.1 should === (2.0)) (after being truncated)

Note that to use a Normalization with the Explicitly DSL, you'll need to be using either TypeCheckedTripleEquals or ConversionCheckedTripleEquals. If you're just using plain-old TripleEquals, you'll need a Uniformity (described below), a Normalization subtrait.

If you make the truncated val implicit and import or mix in the members of NormMethods, you can access the behavior by invoking .norm on Doubles.

implicit val doubleNormalization = truncated
import NormMethods._

val d = 2.1
d.norm // returns 2.0

Uniformity

An important subtype of Normalization is Uniformity, which defines a custom way to normalize instances of a type that can also handle normalization of that type when passed as Any.

For example, to normalize Doubles by truncating off any decimal part, you might write:

import org.scalactic._

val truncated = new Uniformity[Double] { def normalized(d: Double) = d.floor def normalizedCanHandle(o: Any) = o.isInstanceOf[Double] def normalizedOrSame(o: Any): Any = o match { case d: Double => normalized(d) case _ => o } }

Given this definition you could use it with the Explicitly DSL like this:

import org.scalatest._
import Matchers._

2.1 should equal (2.0) (after being truncated)

If you make the truncated val implicit and import or mix in the members of NormMethods, you can access the behavior by invoking .norm on Doubles.

implicit val doubleUniformity = truncated
import NormMethods._

val d = 2.1 d.norm // returns 2.0

As mentioned previously, by creating a Uniformity rather than just an instance of its supertype, Normalization, it can be used more generally. For example, Uniformitys allow you to the Explicitly DSL with TripleEquals, whereas Normalizations require TypeCheckedTripleEquals or ConversionCheckedTripleEquals. Uniformitys also enable you to use the Explicitly DSL with ScalaTest's should ===, equal, and contain matcher syntax, whereas a plain Normalization can only be used with should ===, and only under either TypeCheckedTripleEquals or ConversionCheckedTripleEquals.

Next, learn about The Explicitly DSL.

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-2024 Artima, Inc. All Rights Reserved.

artima