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

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

Go to download

ScalaUtils is an open-source library for Scala projects that contains portions of the ScalaTest project that may make sense to use in production.

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 two objects to be compared with
 * === and !== with a Boolean result and an 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 CheckingEqualizer[L](left: L) { /** * Compare two objects for equality, returning a Boolean, using the EqualityConstraint instance passed as constraint. * * @param right the object to compare for equality with left, passed to the constructor * @param constraint an implicit EqualityConstraint instance that enforces a relationship between types L and R and * 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 EqualityConstraint instance. */ def ===[R](right: R)(implicit constraint: EqualityConstraint[L, R]): Boolean = constraint.areEqual(left, right) /** * Compare two objects for inequality, returning a Boolean, using the EqualityConstraint instance passed as constraint. * * @param right the object to compare for inequality with left, passed to the constructor * @param constraint an implicit EqualityConstraint instance that enforces a relationship between types L and R and * 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 EqualityConstraint instance. */ def !==[R](right: R)(implicit constraint: EqualityConstraint[L, R]): Boolean = !constraint.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 not 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 }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy