All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scalautils.Equalizer.scala Maven / Gradle / Ivy

Go to download

ScalaTest is a free, open-source testing toolkit for Scala and Java programmers.

The newest version!
/*
 * Copyright 2001-2013 Artima, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.scalautils

/**
 * Class used via an implicit conversion to enable any two objects to be compared with
 * === and !== with a Boolean result and no enforced type constraint between
 * two object types. For example:
 *
 * 
 * assert(a === b)
 * assert(c !== d)
 * 
* *

* You can also check numeric values against another with a tolerance. Here are some examples: *

* *
 * assert(a === (2.0 +- 0.1))
 * assert(c !== (2.0 +- 0.1))
 * 
* * @param left An object to convert to Equalizer, which represents the value * on the left side of a === or !== invocation. * * @author Bill Venners */ class Equalizer[L](left: L) { /** * Compare two objects for equality, returning a Boolean, using the Equality type class passed as equality. * * @param right the object to compare for equality with left, passed to the constructor * @param equality an implicit Equality type class that defines a way of calculating equality for objects of type L * @return true if the left and right objects are equal according to the passed Equality type class. */ def ===(right: Any)(implicit equality: Equality[L]): Boolean = equality.areEqual(left, right) /** * Compare two objects for inequality, returning a Boolean, using the Equality type class passed as equality. * * @param right the object to compare for inequality with left, passed to the constructor * @param equality an implicit Equality type class that defines a way of calculating equality for objects of type L * @return true if the left and right objects are not equal according to the passed Equality type class. */ def !==(right: Any)(implicit equality: Equality[L]): Boolean = !equality.areEqual(left, right) /** * Determine whether a numeric object is within the passed Interval, returning a Boolean. * * @param interval the Interval against which to compare the value passed to the constructor as left * @return true if the value passed to the constructor as left is within the Interval passed to this method. */ def ===(interval: Interval[L]): Boolean = if (interval != null) interval.isWithin(left) else left == interval /** * Determine whether a numeric object is outside the passed Interval, returning a Boolean. * * @param interval the Interval against which to compare the value passed to the constructor as left * @return true if the value passed to the constructor as left is not within the Interval passed to this method. */ def !==(interval: Interval[L]): Boolean = if (interval != null) !interval.isWithin(left) else left != interval /** * Determine whether an object reference is null. * * @param literalNull a null value against which to compare the value passed to the constructor as left for equality * @return true if the value passed to the constructor as left is null. */ def ===(literalNull: Null): Boolean = left == null /** * Determines whether an object reference is non-null. * * @param literalNull a null value against which to compare the value passed to the constructor as left for inequality * @return true if the value passed to the constructor as left is non-null. */ def !==(literalNull: Null): Boolean = left != null }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy