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

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 Suite to disable the implicit conversions provided by default in trait
 * Assertions, which trait Suite 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, and assertTypeError: *

* *
 *   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 the super implementation of convertToEqualizer, 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 to Equalizer. * @throws NullArgumentException if left is null. */ 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) /** * The lowPriorityConversionCheckedConstraint method has been deprecated and will be removed in a future version of Scalactic. It * is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced. * *

* Provides an A CanEqual B instance for any two types A and B, enforcing the type constraint that A is * implicitly convertible to B, given an implicit Equivalence[B]. *

* *

* The returned Constraint's areEqual method uses the implicitly passed Equivalence[B]'s * areEquivalent method to determine equality. *

* *

* This method is overridden and made implicit by subtraits * LowPriorityConversionCheckedConstraint (extended by * ConversionCheckedTripleEquals), and * overriden as non-implicit by the other subtraits in this package. *

* * @param equalityOfB an Equivalence[B] type class to which the Constraint.areEqual method will delegate to determine equality. * @param cnv an implicit conversion from A to
B * @return an A CanEqual B instance whose areEqual method delegates to the areEquivalent method of * the passed Equivalence[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) /** * The convertEquivalenceToAToBConversionConstraint method has been deprecated and will be removed in a future version of Scalactic. * It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced. * *

* Provides an A CanEqual B instance for any two types A and B, enforcing the type constraint that A is * implicitly convertible to B, given an explicit Equivalence[B]. *

* *

* This method is used to enable the Explicitly DSL for * ConversionCheckedTripleEquals by requiring an explicit Equivalance[B], but * taking an implicit function that converts from A to B. *

* *

* The returned Constraint's areEqual method uses the implicitly passed Equivalence[B]'s * areEquivalent method to determine equality. *

* *

* This method is overridden and made implicit by subtraits * LowPriorityConversionCheckedConstraint (extended by * ConversionCheckedTripleEquals), and * overriden as non-implicit by the other subtraits in this package. *

* * @param equalityOfB an Equivalence[B] type class to which the Constraint.areEqual method will delegate to determine equality. * @param cnv an implicit conversion from A to B * @return an A CanEqual B instance whose areEqual method delegates to the areEquivalent method of * the passed Equivalence[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) /** * The conversionCheckedConstraint method has been deprecated and will be removed in a future version of Scalactic. It * is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced. * *

* Provides an A CanEqual B instance for any two types A and B, enforcing the type constraint that B is * implicitly convertible to A, given an implicit Equivalence[A]. *

* *

* The returned Constraint's areEqual method uses the implicitly passed Equivalence[A]'s * areEquivalent method to determine equality. *

* *

* This method is overridden and made implicit by subtraits * ConversionCheckedTripleEquals) and * overriden as non-implicit by the other subtraits in this package. *

* * @param equivalenceOfA an Equivalence[A] type class to which the Constraint.areEqual method will delegate to determine equality. * @param cnv an implicit conversion from B to A * @return an A CanEqual B instance whose areEqual method delegates to the areEquivalent method of * the passed Equivalence[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) /** * The convertEquivalenceToBToAConversionConstraint method has been deprecated and will be removed in a future version of Scalactic. * It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced. * *

* Provides an A CanEqual B instance for any two types A and B, enforcing the type constraint that B is * implicitly convertible to A, given an explicit Equivalence[A]. *

* *

* This method is used to enable the Explicitly DSL for * ConversionCheckedTripleEquals by requiring an explicit Equivalance[A], but * taking an implicit function that converts from B to A. For example, under ConversionCheckedTripleEquals, * 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's areEqual method uses the implicitly passed Equivalence[A]'s * areEquivalent method to determine equality. *

* *

* This method is overridden and made implicit by subtraits * ConversionCheckedTripleEquals) and * overriden as non-implicit by the other subtraits in this package. *

* * @param equivalenceOfA an Equivalence[A] type class to which the Constraint.areEqual method will delegate to determine equality. * @param cnv an implicit conversion from B to A * @return an A CanEqual B instance whose areEqual method delegates to the areEquivalent method of * the passed Equivalence[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 trait Assertions without importing the implicit conversions * it provides by default. One use case for this object is to import the non-implicit Assertions members so you can use * them in the Scala interpreter along with another library whose implicits conflict with those provided by Assertions: * *
 * $ 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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy