org.scalatest.NonImplicitAssertions.scala Maven / Gradle / Ivy
/* * 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.scalatest import org.scalactic._ import TripleEqualsSupport._ /** * Trait that can be mixed into a
B * @return anSuite
to disable the implicit conversions provided by default in trait *Assertions
, which traitSuite
extends. * ** This trait can be used to quickly solve a problem in which ScalaTest's default implicit conversion is clashing with those of some other library * you need to use in your tests. After mixing in this trait, like this: *
* ** class MySuite extends FunSuite with NonImplicitAssertions { * // ... * } ** ** You can write tests using
* *assert
(without triple equals),assertResult
,assertThrows
, *intercept
,assertCompiles
,assertDoesNotCompile
, andassertTypeError
: ** assert(a < 7) * * assertResult(2) { 1 + 1 } * * assertThrows[IndexOutOfBoundsException] { * "hi".charAt(-1) * } * * val caught = * intercept[IndexOutOfBoundsException] { * "hi".charAt(-1) * } * * assertDoesNotCompile("val a: String = 1") * * assertTypeError("val a: String = 1") * * assertCompiles("val a: Int = 1") ** * @author Chua Chee Seng * @author Bill Venners */ trait NonImplicitAssertions extends Assertions { /** * Overrides thesuper
implementation ofconvertToEqualizer
, turning off the implicit * modifier (if present) to remove the method from the space of implicit conversions. * * @param left the object whose type to convert toEqualizer
. * @throws NullArgumentException ifleft
isnull
. */ override def convertToEqualizer[T](left: T): Equalizer[T] = new Equalizer(left) override def convertToCheckingEqualizer[T](left: T): CheckingEqualizer[T] = new CheckingEqualizer(left) override def lowPriorityTypeCheckedConstraint[A, B](implicit equivalenceOfB: Equivalence[B], ev: A <:< B): A CanEqual B = new AToBEquivalenceConstraint[A, B](equivalenceOfB, ev) override def convertEquivalenceToAToBConstraint[A, B](equivalenceOfB: Equivalence[B])(implicit ev: A <:< B): A CanEqual B = new AToBEquivalenceConstraint[A, B](equivalenceOfB, ev) override def typeCheckedConstraint[A, B](implicit equivalenceOfA: Equivalence[A], ev: B <:< A): A CanEqual B = new BToAEquivalenceConstraint[A, B](equivalenceOfA, ev) override def convertEquivalenceToBToAConstraint[A, B](equivalenceOfA: Equivalence[A])(implicit ev: B <:< A): A CanEqual B = new BToAEquivalenceConstraint[A, B](equivalenceOfA, ev) /** * ThelowPriorityConversionCheckedConstraint
method has been deprecated and will be removed in a future version of Scalactic. It * is no longer needed now that the deprecation period ofConversionCheckedTripleEquals
has expired. It will not be replaced. * ** Provides an
* *A CanEqual B
instance for any two typesA
andB
, enforcing the type constraint thatA
is * implicitly convertible toB
, given an implicitEquivalence[B]
. ** The returned
* *Constraint
'sareEqual
method uses the implicitly passedEquivalence[B]
's *areEquivalent
method to determine equality. ** This method is overridden and made implicit by subtraits *
* * @param equalityOfB anLowPriorityConversionCheckedConstraint
(extended by *ConversionCheckedTripleEquals
), and * overriden as non-implicit by the other subtraits in this package. *Equivalence[B]
type class to which theConstraint.areEqual
method will delegate to determine equality. * @param cnv an implicit conversion fromA
toA CanEqual B
instance whoseareEqual
method delegates to theareEquivalent
method of * the passedEquivalence[B]
. */ @deprecated("The lowPriorityConversionCheckedConstraint method has been deprecated and will be removed in a future version of ScalaTest. It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced.", "3.1.0") override def lowPriorityConversionCheckedConstraint[A, B](implicit equivalenceOfB: Equivalence[B], cnv: A => B): A CanEqual B = new AToBEquivalenceConstraint[A, B](equivalenceOfB, cnv) /** * TheconvertEquivalenceToAToBConversionConstraint
method has been deprecated and will be removed in a future version of Scalactic. * It is no longer needed now that the deprecation period ofConversionCheckedTripleEquals
has expired. It will not be replaced. * ** Provides an
* *A CanEqual B
instance for any two typesA
andB
, enforcing the type constraint thatA
is * implicitly convertible toB
, given an explicitEquivalence[B]
. ** This method is used to enable the
* *Explicitly
DSL for *ConversionCheckedTripleEquals
by requiring an explicitEquivalance[B]
, but * taking an implicit function that converts fromA
to B. ** The returned
* *Constraint
'sareEqual
method uses the implicitly passedEquivalence[B]
's *areEquivalent
method to determine equality. ** This method is overridden and made implicit by subtraits *
* * @param equalityOfB anLowPriorityConversionCheckedConstraint
(extended by *ConversionCheckedTripleEquals
), and * overriden as non-implicit by the other subtraits in this package. *Equivalence[B]
type class to which theConstraint.areEqual
method will delegate to determine equality. * @param cnv an implicit conversion fromA
to B * @return anA CanEqual B
instance whoseareEqual
method delegates to theareEquivalent
method of * the passedEquivalence[B]
. */ @deprecated("The convertEquivalenceToAToBConversionConstraint method has been deprecated and will be removed in a future version of ScalaTest. It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced.", "3.1.0") override def convertEquivalenceToAToBConversionConstraint[A, B](equivalenceOfB: Equivalence[B])(implicit ev: A => B): A CanEqual B = new AToBEquivalenceConstraint[A, B](equivalenceOfB, ev) /** * TheconversionCheckedConstraint
method has been deprecated and will be removed in a future version of Scalactic. It * is no longer needed now that the deprecation period ofConversionCheckedTripleEquals
has expired. It will not be replaced. * ** Provides an
* *A CanEqual B
instance for any two typesA
andB
, enforcing the type constraint thatB
is * implicitly convertible toA
, given an implicitEquivalence[A]
. ** The returned
* *Constraint
'sareEqual
method uses the implicitly passedEquivalence[A]
's *areEquivalent
method to determine equality. ** This method is overridden and made implicit by subtraits *
* * @param equivalenceOfA anConversionCheckedTripleEquals
) and * overriden as non-implicit by the other subtraits in this package. *Equivalence[A]
type class to which theConstraint.areEqual
method will delegate to determine equality. * @param cnv an implicit conversion fromB
to A * @return anA CanEqual B
instance whoseareEqual
method delegates to theareEquivalent
method of * the passedEquivalence[A]
. */ @deprecated("The conversionCheckedConstraint method has been deprecated and will be removed in a future version of ScalaTest. It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced.", "3.1.0") override def conversionCheckedConstraint[A, B](implicit equivalenceOfA: Equivalence[A], cnv: B => A): A CanEqual B = new BToAEquivalenceConstraint[A, B](equivalenceOfA, cnv) /** * TheconvertEquivalenceToBToAConversionConstraint
method has been deprecated and will be removed in a future version of Scalactic. * It is no longer needed now that the deprecation period ofConversionCheckedTripleEquals
has expired. It will not be replaced. * ** Provides an
* *A CanEqual B
instance for any two typesA
andB
, enforcing the type constraint thatB
is * implicitly convertible toA
, given an explicitEquivalence[A]
. ** This method is used to enable the
* *Explicitly
DSL for *ConversionCheckedTripleEquals
by requiring an explicitEquivalance[A]
, but * taking an implicit function that converts fromB
to A. For example, underConversionCheckedTripleEquals
, * this method (as an implicit method), would be used to compile this statement: ** def closeEnoughTo1(num: Double): Boolean = * (num === 1.0)(decided by forgivingEquality) ** ** The returned
* *Constraint
'sareEqual
method uses the implicitly passedEquivalence[A]
's *areEquivalent
method to determine equality. ** This method is overridden and made implicit by subtraits *
* * @param equivalenceOfA anConversionCheckedTripleEquals
) and * overriden as non-implicit by the other subtraits in this package. *Equivalence[A]
type class to which theConstraint.areEqual
method will delegate to determine equality. * @param cnv an implicit conversion fromB
to A * @return anA CanEqual B
instance whoseareEqual
method delegates to theareEquivalent
method of * the passedEquivalence[A]
. */ @deprecated("The convertEquivalenceToBToAConversionConstraint method has been deprecated and will be removed in a future version of ScalaTest. It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced.", "3.1.0") override def convertEquivalenceToBToAConversionConstraint[A, B](equivalenceOfA: Equivalence[A])(implicit ev: B => A): A CanEqual B = new BToAEquivalenceConstraint[A, B](equivalenceOfA, ev) } /** * Companion object that facilitates the importing of the members of traitAssertions
without importing the implicit conversions * it provides by default. One use case for this object is to import the non-implicitAssertions
members so you can use * them in the Scala interpreter along with another library whose implicits conflict with those provided byAssertions
: * ** $ scala -cp scalatest-1.7.jar * Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29). * Type in expressions to have them evaluated. * Type :help for more information. * * scala> import org.scalatest._ * import org.scalatest._ * * scala> import NonImplicitAssertions._ * import NonImplicitAssertions._ * * scala> assert(1 + 1 === 2) * <console>:14: error: value === is not a member of Int * assert(1 + 1 === 2) * ^ * * scala> assert(1 + 1 == 2) * * scala> expect(2) { 1 + 1 } * * scala> expect(2) { 1 + 1 + 1 } * org.scalatest.TestFailedException: Expected 2, but got 3 * at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:318) * at org.scalatest.NonImplicitAssertions$.newAssertionFailedException(NonImplicitAssertions.scala:73) * ... * * scala> intercept[IndexOutOfBoundsException] { "hi".charAt(-1) } * res3: IndexOutOfBoundsException = java.lang.StringIndexOutOfBoundsException: String index out of range: -1 **/ object NonImplicitAssertions extends NonImplicitAssertions