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

org.scalautils.LegacyEqualizer.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 an Option[String] 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))
 * 
* *

* The benefit of using assert(a === b) rather than assert(a == b) in ScalaTest code is * that a TestFailedException produced by the former will include the values of a and b * in its detail message. *

* *

* * Note: This class has "Legacy" in its name because its approach to error messages will eventually be replaced by macros. Once ScalaTest no longer supports Scala 2.9, * this class will be deprecated in favor of class Equalizer. Instead of obtaining nice error messages via the Option[String] * returned by the methods of this class, the error messages will be obtained by a macro. The "legacy" approach to good error messages will continue to be * used, however, until ScalaTest no longer supports Scala 2.9, since macros were introduced to Scala (in experimental form) in 2.10. * *

* *

* The primary constructor takes one object, left, whose type is being converted to Equalizer. The left * value may be a null reference, because this is allowed by Scala's == operator. *

* * @param left An object to convert to Equalizer, which represents the left value * of a === or !== equality check. * * @author Bill Venners */ class LegacyEqualizer[L](left: L) { private def diffStrings(s: String, t: String): Tuple2[String, String] = { def findCommonPrefixLength(s: String, t: String): Int = { val max = s.length.min(t.length) // the maximum potential size of the prefix var i = 0 var found = false while (i < max & !found) { found = (s.charAt(i) != t.charAt(i)) if (!found) i = i + 1 } i } def findCommonSuffixLength(s: String, t: String): Int = { val max = s.length.min(t.length) // the maximum potential size of the suffix var i = 0 var found = false while (i < max & !found) { found = (s.charAt(s.length - 1 - i) != t.charAt(t.length - 1 - i)) if (!found) i = i + 1 } i } val commonPrefixLength = findCommonPrefixLength(s, t) val commonSuffixLength = findCommonSuffixLength(s.substring(commonPrefixLength), t.substring(commonPrefixLength)) val prefix = s.substring(0, commonPrefixLength) val suffix = if (s.length - commonSuffixLength < 0) "" else s.substring(s.length - commonSuffixLength) val sMiddleEnd = s.length - commonSuffixLength val tMiddleEnd = t.length - commonSuffixLength val sMiddle = s.substring(commonPrefixLength, sMiddleEnd) val tMiddle = t.substring(commonPrefixLength, tMiddleEnd) val MaxContext = 20 val shortPrefix = if (commonPrefixLength > MaxContext) "..." + prefix.substring(prefix.length - MaxContext) else prefix val shortSuffix = if (commonSuffixLength > MaxContext) suffix.substring(0, MaxContext) + "..." else suffix (shortPrefix + "[" + sMiddle + "]" + shortSuffix, shortPrefix + "[" + tMiddle + "]" + shortSuffix) } // If the objects are two strings, replace them with whatever is returned by diffStrings. // Otherwise, use the same objects. private def getObjectsForFailureMessage(a: Any, b: Any) = a match { case aStr: String => { b match { case bStr: String => { diffStrings(aStr, bStr) } case _ => (a, b) } } case _ => (a, b) } /** * Compare two objects for equality, returning an Option[String], 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 None if the left and right objects are equal according to the passed Equality type class. * else returns an error message string wrapped in a Some. */ def ===(right: Any)(implicit equality: Equality[L]): Option[String] = if (equality.areEqual(left, right)) None else { val (leftee, rightee) = getObjectsForFailureMessage(left, right) Some(FailureMessages("didNotEqual", leftee, rightee)) } /** * Compare two objects for inequality, returning an Option[String], 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 None if the left and right objects are not equal according to the passed Equality type class. * else returns an error message string wrapped in a Some. */ def !==(right: Any)(implicit equality: Equality[L]): Option[String] = if (!equality.areEqual(left, right)) None else { val (leftee, rightee) = getObjectsForFailureMessage(left, right) Some(FailureMessages("equaled", leftee, rightee)) } /** * Determine whether a numeric object is within the passed Interval, returning an Option[String]. * * @param interval the Interval against which to compare the value passed to the constructor as left * @return None if the value passed to the constructor as left is not within the Interval passed to this method, * else returns an error message string wrapped in a Some. */ def ===(interval: Interval[L]): Option[String] = if (interval == null) { if (left == null) None else { val (leftee, rightee) = getObjectsForFailureMessage(left, interval) Some(FailureMessages("equaled", leftee, rightee)) } } else { if (interval.isWithin(left)) None else Some(FailureMessages("wasNotPlusOrMinus", left, interval.pivot, interval.tolerance)) } /** * Determine whether a numeric object is outside the passed Interval, returning an Option[String]. * * @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. * else returns an error message string wrapped in a Some. */ def !==(interval: Interval[L]): Option[String] = if (interval == null) { if (left != null) None else { val (leftee, rightee) = getObjectsForFailureMessage(left, interval) Some(FailureMessages("equaled", leftee, rightee)) } } else { if (if (interval != null) !interval.isWithin(left) else left != interval) None else Some(FailureMessages("wasPlusOrMinus", left, interval.pivot, interval.tolerance)) } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy