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)
override def lowPriorityConversionCheckedConstraint[A, B](implicit equivalenceOfB: Equivalence[B], cnv: A => B): A CanEqual B = new AToBEquivalenceConstraint[A, B](equivalenceOfB, cnv)
override def convertEquivalenceToAToBConversionConstraint[A, B](equivalenceOfB: Equivalence[B])(implicit ev: A => B): A CanEqual B = new AToBEquivalenceConstraint[A, B](equivalenceOfB, ev)
override def conversionCheckedConstraint[A, B](implicit equivalenceOfA: Equivalence[A], cnv: B => A): A CanEqual B = new BToAEquivalenceConstraint[A, B](equivalenceOfA, cnv)
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