org.scalautils.EqualityConstraints.scala Maven / Gradle / Ivy
Show all versions of scalatest_2.9.1 Show documentation
/* * 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 /** * Trait that defines abstract methods used to enforce compile-time type constraints for equality comparisons, and definesB * @return an===and!==operators * used by matchers. * ** This abstract methods of this trait are selectively implemented as implicit by subclasses to enable a spectrum of type constraints for the *
* *===and!==operators. As an illustration, if in the expression,a === b, the type ofa* isAandbisB, the following three levels of compile-time checking can be obtained from *EqualityConstraintssubtraits: ** Unchecked -
* *AandBcan be any two types. This (weakest) constraint level is available from * subtraitsTripleEqualsandLegacyTripleEquals. ** Conversion checked -
* *Amust be a subtype ofB, or vice versa, or an implicit conversion must be available that converts *AtoB, or vice versa. (BothAandBcan be the same type, because a type is considered a subtype * of itself.) * This (intermediate) constraint level is available from subtraitsConversionCheckedTripleEqualsandConversionCheckedLegacyTripleEquals. ** Type checked -
* *Amust be a subtype ofB, or vice versa. * (BothAandBcan be the same type, because a type is considered a subtype * of itself.) * This (strongest) constraint level is available from subtraitsTypeCheckedTripleEqualsandTypeCheckedLegacyTripleEquals. ** The difference between the regular and “legacy” variants of each pair of traits is that the
* *===and!==operators * provided by the regular variants result inBoolean, whereas those of the legacy variants result inOption[String]. For example, were you * to mix inTripleEquals, the expression1 + 1 === 3would returnfalse. Were you to mix inLegacyTripleEquals, * by contrast, the expression1 + 1 === 3would returnSome("2 did not equal 3"). ** The purpose of the legacy variants is to maintain compatibility with * existing code that uses ScalaTest's original
* *===defined in traitorg.scalatest.Assertions. This *===operator returned an *Option[String]to facilitate better error messages. With the advent of macros in Scala 2.10, it is possible to obtain good error messages by making *asserta macro. Once ScalaTest no longer supports Scala 2.9, the legacy variants (LegacyTripleEquals, *ConversionCheckedLegacyTripleEquals, andTypeCheckedLegacyTripleEquals) will be deprecated and eventually removed,===will * return onlyBoolean, and good error * messages will be obtained via macros. ** This trait defines all methods that need to be defined implicitly by the six subtraits so that if multiple subtraits are used together, the inner-most * subtrait in scope can not only enable the implicits it needs by overriding or hiding those methods (currently-in-scope as regular, non-implicit methods) and making * them implicit, it can also disable any implicits enabled by its sibling subtraits in enclosing scopes. For example, if your test class mixes * in
* *TypeCheckedTripleEquals, inside your test class the following methods will be implicit: **
* *- *
convertToCheckingEqualize- *
typeCheckedEqualityConstraint- *
lowPriorityTypeCheckedEqualityConstraint* If in the body of a test you want to turn off the type checking, you can import the members * of
* *TripleEqualsin the body of that test. This will not only hide * non-implicit methodsconvertToEqualizerunconstrainedEqualityofTypeCheckedTripleEquals, * replacing those with implicit ones defined inTripleEquals, it will also hide the three methods made implicit inTypeCheckedTripleEquals* (and listed above), replacing them by non-implicit ones. ** In short, you should be able to select a primary constraint level via either a mixin or import, then change that in nested scopes * however you want, again either through a mixin or import, without getting any implicit conversion ambiguity. The innermost constraint level in scope * will always be in force. *
* * @author Bill Venners */ trait EqualityConstraints { /** * Return an
Equality[A]for any typeAthat determines equality via the==operator on typeA. * * @return aDefaultEqualityfor typeA*/ def defaultEquality[A]: Equality[A] /** * Convert to anEqualizerthat provides===and!==operators that * result inBooleanand enforce no type constraint. * ** This method is overridden and made implicit by subtrait
* * @param left the object whose type to convert toTripleEqualsand overriden as non-implicit by the other subtraits in this package. *Equalizer. * @throws NullPointerException ifleftisnull. */ def convertToEqualizer[T](left: T): Equalizer[T] /** * Convert to aLegacyEqualizerthat provides===and!==operators that * result inOption[String]and enforce no type constraint. * ** This method is overridden and made implicit by subtrait
* * @param left the object whose type to convert toLegacyTripleEqualsand overriden as non-implicit by the other subtraits in this package. *LegacyEqualizer. * @throws NullPointerException ifleftisnull. */ def convertToLegacyEqualizer[T](left: T): LegacyEqualizer[T] /** * Convert to anCheckingEqualizerthat provides===and!==operators that result inBooleanand enforce a type constraint. * ** This method is overridden and made implicit by subtraits
* * @param left the object whose type to convert toTypeCheckedTripleEqualsandConversionCheckedTripleEquals, and overriden as * non-implicit by the other subtraits in this package. *CheckingEqualizer. * @throws NullPointerException ifleftisnull. */ def convertToCheckingEqualizer[T](left: T): CheckingEqualizer[T] /** * Convert to aLegacyCheckingEqualizerthat provides===and!==operators that result inOption[String]and * enforce a type constraint. * ** This method is overridden and made implicit by subtraits
* * @param left the object whose type to convert toTypeCheckedLegacyTripleEqualsandConversionCheckedLegacyTripleEquals, and * overriden as non-implicit by the other subtraits in this package. *LegacyCheckingEqualizer. * @throws NullPointerException ifleftisnull. */ def convertToLegacyCheckingEqualizer[T](left: T): LegacyCheckingEqualizer[T] /** * Provides anEqualityConstraint[A, B]class for any two typesAandB, with no type constraint enforced, given an * implicitEquality[A]. * ** The implicitly passed
* *Equality[A]must be used to determine equality by the returnedEqualityConstraint's *areEqualmethod. ** This method is overridden and made implicit by subtraits
* * @param equalityOfA anTripleEqualsandLegacyTripleEquals, and * overriden as non-implicit by the other subtraits in this package. *Equality[A]type class to which theEqualityConstraint.areEqualmethod will delegate to determine equality. * @return anEqualityConstraint[A, B]whoseareEqualmethod delegates to theareEqualmethod of * the passedEquality[A]. */ def unconstrainedEquality[A, B](implicit equalityOfA: Equality[A]): EqualityConstraint[A, B] /** * Provides anEqualityConstraint[A, B]class for any two typesAandB, enforcing the type constraint thatAmust be a subtype ofB, given an implicitEquality[A]. * ** The implicitly passed
* *Equality[A]must be used to determine equality by the returnedEqualityConstraint's *areEqualmethod. ** This method is overridden and made implicit by subtraits *
* * @param equalityOfA anLowPriorityTypeCheckedConstraint(extended by *TypeCheckedTripleEquals), and *LowPriorityTypeCheckedLegacyConstraint(extended by *TypeCheckedLegacyTripleEquals), and * overriden as non-implicit by the other subtraits in this package. *Equality[A]type class to which theEqualityConstraint.areEqualmethod * will delegate to determine equality. * @param ev evidence thatAis a subype ofEqualityConstraint[A, B]whoseareEqualmethod delegates to the *areEqualmethod of the passedEquality[A]. */ def lowPriorityTypeCheckedEqualityConstraint[A, B](implicit equalityOfA: Equality[A], ev: A <:< B): EqualityConstraint[A, B] /** * Provides anEqualityConstraint[A, B]class for any two typesAandB, enforcing the type constraint thatBmust be a subtype ofA, given an implicitEquality[A]. * ** The implicitly passed
* *Equality[A]must be used to determine equality by the returnedEqualityConstraint's *areEqualmethod. ** This method is overridden and made implicit by subtraits *
* * @param equalityOfA anTypeCheckedTripleEquals) and *TypeCheckedLegacyTripleEquals, and * overriden as non-implicit by the other subtraits in this package. *Equality[A]type class to which theEqualityConstraint.areEqualmethod will delegate to determine equality. * @param ev evidence thatBis a subype of A * @return anEqualityConstraint[A, B]whoseareEqualmethod delegates to theareEqualmethod of * the passedEquality[A]. */ def typeCheckedEqualityConstraint[A, B](implicit equalityOfA: Equality[A], ev: B <:< A): EqualityConstraint[A, B] /** * Provides anEqualityConstraint[A, B]class for any two typesAandB, enforcing the type constraint thatAis implicitly convertible toB, given an implicitEquality[A]. * ** The implicitly passed
* *Equality[A]must be used to determine equality by the returnedEqualityConstraint's *areEqualmethod. ** This method is overridden and made implicit by subtraits *
* * @param equalityOfA anLowPriorityConversionCheckedConstraint(extended by *ConversionCheckedTripleEquals), and *LowPriorityConversionCheckedLegacyConstraint(extended by *ConversionCheckedLegacyTripleEquals), and * overriden as non-implicit by the other subtraits in this package. *Equality[A]type class to which theEqualityConstraint.areEqualmethod will delegate to determine equality. * @param cnv an implicit conversion fromAto B * @return anEqualityConstraint[A, B]whoseareEqualmethod delegates to theareEqualmethod of * the passedEquality[A]. */ def lowPriorityConversionCheckedEqualityConstraint[A, B](implicit equalityOfB: Equality[B], cnv: A => B): EqualityConstraint[A, B] /** * Provides anEqualityConstraint[A, B]class for any two typesAandB, enforcing the type constraint thatBis implicitly convertible toA, given an implicitEquality[A]. * ** The implicitly passed
* *Equality[A]must be used to determine equality by the returnedEqualityConstraint's *areEqualmethod. ** This method is overridden and made implicit by subtraits *
* * @param equalityOfA anConversionCheckedTripleEquals) and *ConversionCheckedLegacyTripleEquals, and * overriden as non-implicit by the other subtraits in this package. *Equality[A]type class to which theEqualityConstraint.areEqualmethod will delegate to determine equality. * @param cnv an implicit conversion fromBto A * @return anEqualityConstraint[A, B]whoseareEqualmethod delegates to theareEqualmethod of * the passedEquality[A]. */ def conversionCheckedEqualityConstraint[A, B](implicit equalityOfA: Equality[A], cnv: B => A): EqualityConstraint[A, B] /** * Returns aTripleEqualsInvocation[T], given an object of typeT, to facilitate * the “<left> should === <right>” syntax * ofMatchers. * * @param right the right-hand side value for an equality assertion * @return aTripleEqualsInvocationwrapping the passed right value, withexpectingEqual* set totrue. */ def ===[T](right: T): TripleEqualsInvocation[T] = new TripleEqualsInvocation[T](right, true) /** * Returns aTripleEqualsInvocation[T], given an object of typeT, to facilitate * the “<left> should !== <right>” syntax * ofMatchers. * * @param right the right-hand side value for an equality assertion * @return aTripleEqualsInvocationwrapping the passed right value, withexpectingEqual* set tofalse. */ def !==[T](right: T): TripleEqualsInvocation[T] = new TripleEqualsInvocation[T](right, false) /** * Returns aTripleEqualsInvocation[Null], given anullreference, to facilitate * the “<left> should === null” syntax * ofMatchers. * * @param right a null reference * @return aTripleEqualsInvocationwrapping the passednullvalue, withexpectingEqual* set totrue. */ def ===(right: Null): TripleEqualsInvocation[Null] = new TripleEqualsInvocation[Null](right, true) /** * Returns aTripleEqualsInvocation[Null], given anullreference, to facilitate * the “<left> should !== null” syntax * ofMatchers. * * @param right a null reference * @return aTripleEqualsInvocationwrapping the passednullvalue, withexpectingEqual* set tofalse. */ def !==(right: Null): TripleEqualsInvocation[Null] = new TripleEqualsInvocation[Null](right, false) /** * Returns aTripleEqualsInvocationOnInterval[T], given anInterval[T], to facilitate * the “<left> should === (<pivot> +- <tolerance>)” * syntax ofMatchers. * * @param right theInterval[T]against which to compare the left-hand value * @return aTripleEqualsInvocationOnIntervalwrapping the passedInterval[T]value, with *expectingEqualset totrue. */ def ===[T](right: Interval[T]): TripleEqualsInvocationOnInterval[T] = new TripleEqualsInvocationOnInterval[T](right, true) /** * Returns aTripleEqualsInvocationOnInterval[T], given anInterval[T], to facilitate * the “<left> should !== (<pivot> +- <tolerance>)” * syntax ofMatchers. * * @param right theInterval[T]against which to compare the left-hand value * @return aTripleEqualsInvocationOnIntervalwrapping the passedInterval[T]value, with *expectingEqualset tofalse. */ def !==[T](right: Interval[T]): TripleEqualsInvocationOnInterval[T] = new TripleEqualsInvocationOnInterval[T](right, false) }