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

org.scalatest.matchers.TypeMatcherHelper.scala Maven / Gradle / Ivy

/*
 * Copyright 2001-2014 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.matchers

import org.scalactic.source
import org.scalatest.{FailureMessages, Resources, Suite, UnquotedString}
import org.scalactic.Prettifier
import org.scalatest.matchers.MatchersHelper._
import org.scalatest.matchers.dsl.{ResultOfATypeInvocation, ResultOfAnTypeInvocation}

import scala.reflect.ClassTag
//import org.scalatest.words.{FactResultOfAnTypeInvocation, FactResultOfATypeInvocation}

/**
 * TypeMatcherHelper is called by TypeMatcherMacro to support a [Type] and an [Type] syntax.
 *
 * 

* This object needs to be public so that the macro-generated code can be compiled. It is expected that ScalaTest * users would ever need to use TypeMatcherHelper directly. *

*/ object TypeMatcherHelper { /** * Create a type matcher for the given ResultOfATypeInvocation. * * @param aType an instance of ResultOfATypeInvocation * @return a type Matcher */ def aTypeMatcher(aType: ResultOfATypeInvocation[_]): Matcher[Any] = new Matcher[Any] { def apply(left: Any): MatchResult = { val clazzTag = aType.clazzTag MatchResult( conform(clazzTag, left), Resources.rawWasNotAnInstanceOf, Resources.rawWasAnInstanceOf, Vector(left, UnquotedString(clazzTag.toString), UnquotedString(left.getClass.getName)), Vector(left, UnquotedString(clazzTag.toString)) ) } override def toString: String = "be (" + Prettifier.default(aType) + ")" } /** * Create a type matcher for the given ResultOfAnTypeInvocation. * * @param anType an instance of ResultOfAnTypeInvocation * @return a type Matcher */ def anTypeMatcher(anType: ResultOfAnTypeInvocation[_]): Matcher[Any] = new Matcher[Any] { def apply(left: Any): MatchResult = { val clazzTag = anType.clazzTag MatchResult( conform(clazzTag, left), Resources.rawWasNotAnInstanceOf, Resources.rawWasAnInstanceOf, Vector(left, UnquotedString(clazzTag.toString), UnquotedString(left.getClass.getName)), Vector(left, UnquotedString(clazzTag.toString)) ) } override def toString: String = "be (" + Prettifier.default(anType) + ")" } /** * Create a negated type matcher for the given ResultOfATypeInvocation. * * @param aType an instance of ResultOfATypeInvocation * @return a negated type Matcher */ def notATypeMatcher(aType: ResultOfATypeInvocation[_]): Matcher[Any] = new Matcher[Any] { def apply(left: Any): MatchResult = { val clazzTag = aType.clazzTag MatchResult( !conform(clazzTag, left), Resources.rawWasAnInstanceOf, Resources.rawWasNotAnInstanceOf, Vector(left, UnquotedString(clazzTag.toString)), Vector(left, UnquotedString(clazzTag.toString), UnquotedString(left.getClass.getName)) ) } override def toString: String = "not be " + Prettifier.default(aType) } /** * Create a negated type matcher for the given ResultOfAnTypeInvocation. * * @param anType an instance of ResultOfAnTypeInvocation * @return a negated type Matcher */ def notAnTypeMatcher(anType: ResultOfAnTypeInvocation[_]): Matcher[Any] = new Matcher[Any] { def apply(left: Any): MatchResult = { val clazzTag = anType.clazzTag MatchResult( !conform(clazzTag, left), Resources.rawWasAnInstanceOf, Resources.rawWasNotAnInstanceOf, Vector(left, UnquotedString(clazzTag.toString)), Vector(left, UnquotedString(clazzTag.toString), UnquotedString(left.getClass.getName)) ) } override def toString: String = "not be " + Prettifier.default(anType) } private def conform[T](classTag: ClassTag[T], x: Any): Boolean = if (classTag == ClassTag.AnyVal) x.isInstanceOf[Byte] || x.isInstanceOf[Short] || x.isInstanceOf[Char] || x.isInstanceOf[Int] || x.isInstanceOf[Long] || x.isInstanceOf[Float] || x.isInstanceOf[Double] || x.isInstanceOf[Boolean] else { val runtimeClass = classTag.runtimeClass // This part of code is inspired from ClassTag's unapply method starting Scala 2.11, // we can't use ClassTag's unapply directly because in Scala 2.10 it wasn't // written this way and it can't meet our purpose. if (null != x && ( (runtimeClass.isInstance(x)) || (x.isInstanceOf[Byte] && runtimeClass.isAssignableFrom(classOf[Byte])) || (x.isInstanceOf[Short] && runtimeClass.isAssignableFrom(classOf[Short])) || (x.isInstanceOf[Char] && runtimeClass.isAssignableFrom(classOf[Char])) || (x.isInstanceOf[Int] && runtimeClass.isAssignableFrom(classOf[Int])) || (x.isInstanceOf[Long] && runtimeClass.isAssignableFrom(classOf[Long])) || (x.isInstanceOf[Float] && runtimeClass.isAssignableFrom(classOf[Float])) || (x.isInstanceOf[Double] && runtimeClass.isAssignableFrom(classOf[Double])) || (x.isInstanceOf[Boolean] && runtimeClass.isAssignableFrom(classOf[Boolean])) || (x.isInstanceOf[Unit] && runtimeClass.isAssignableFrom(classOf[Unit]))) ) true else false } /** * Check if the given left is an instance of the type as described in the given ResultOfATypeInvocation. * A TestFailedException will be thrown if left is not an instance of the type given by ResultOfATypeInvocation. * * @param left the left-hand-side (LHS) to be checked for the type * @param aType an instance of ResultOfATypeInvocation */ def assertAType(left: Any, aType: ResultOfATypeInvocation[_], prettifier: Prettifier, pos: source.Position): org.scalatest.Assertion = { val clazz = aType.clazz if (!conform(aType.clazzTag, left)) { val (leftee, rightee) = Suite.getObjectsForFailureMessage(left, aType.clazzTag.toString) throw newTestFailedException(FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(aType.clazzTag.toString), UnquotedString(left.getClass.getName)), None, pos) } org.scalatest.Succeeded } /** * Check if the given left is an instance of the type as described in the given ResultOfATypeInvocation. * A Fact.No will be returned if left is not an instance of the type given by ResultOfATypeInvocation. * * @param left the left-hand-side (LHS) to be checked for the type * @param aType an instance of ResultOfATypeInvocation */ /*private[scalatest] def expectAType(left: Any, aType: ResultOfATypeInvocation[_], prettifier: Prettifier): org.scalatest.Fact = { val clazz = aType.clazz if (!conform(clazz, left.getClass)) { val (leftee, rightee) = Suite.getObjectsForFailureMessage(left, clazz.getName) org.scalatest.Fact.No(FailureMessages.wasNotAnInstanceOf(aType.prettifier, left, UnquotedString(clazz.getName), UnquotedString(left.getClass.getName)))(aType.prettifier) } else org.scalatest.Fact.Yes(FailureMessages.wasAnInstanceOf(aType.prettifier, left, UnquotedString(clazz.getName)))(aType.prettifier) }*/ /** * Check if the given left is an instance of the type as described in the given ResultOfAnTypeInvocation. * A TestFailedException will be thrown if left is not an instance of the type given by ResultOfAnTypeInvocation. * * @param left the left-hand-side (LHS) to be checked for the type * @param anType an instance of ResultOfAnTypeInvocation */ def assertAnType(left: Any, anType: ResultOfAnTypeInvocation[_], prettifier: Prettifier, pos: source.Position): org.scalatest.Assertion = { val clazz = anType.clazz if (!conform(anType.clazzTag, left)) { val (leftee, rightee) = Suite.getObjectsForFailureMessage(left, anType.clazzTag.toString) throw newTestFailedException(FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(anType.clazzTag.toString), UnquotedString(left.getClass.getName)), None, pos) } org.scalatest.Succeeded } /** * Check if the given left is an instance of the type as described in the given ResultOfAnTypeInvocation. * A Fact.No will be returned if left is not an instance of the type given by ResultOfAnTypeInvocation. * * @param left the left-hand-side (LHS) to be checked for the type * @param anType an instance of ResultOfAnTypeInvocation */ /*private[scalatest] def expectAnType(left: Any, anType: ResultOfAnTypeInvocation[_]): org.scalatest.Fact = { val clazz = anType.clazz if (!conform(clazz, left)) { val (leftee, rightee) = Suite.getObjectsForFailureMessage(left, clazz.getName) org.scalatest.Fact.No(FailureMessages.wasNotAnInstanceOf(anType.prettifier, left, UnquotedString(clazz.getName), UnquotedString(left.getClass.getName)))(anType.prettifier) } else org.scalatest.Fact.Yes(FailureMessages.wasAnInstanceOf(anType.prettifier, left, UnquotedString(clazz.getName)))(anType.prettifier) }*/ /** * Based on shouldBeTrue value, check if the given left is an instance of the type as described in the given ResultOfATypeInvocation. * If shouldBeTrue is true, a TestFailedException will be thrown if left is not an instance of the type given by ResultOfATypeInvocation. * If shouldBeTrue is false, a TestFailedException will be thrown if left is an instance of the type given by ResultOfATypeInvocation. * * @param left the left-hand-side (LHS) to be checked for the type * @param aType an instance of ResultOfATypeInvocation */ def assertATypeShouldBeTrue(left: Any, aType: ResultOfATypeInvocation[_], shouldBeTrue: Boolean, prettifier: Prettifier, pos: source.Position): org.scalatest.Assertion = { val clazz = aType.clazz if (conform(aType.clazzTag, left) != shouldBeTrue) { throw newTestFailedException( if (shouldBeTrue) FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(aType.clazzTag.toString), UnquotedString(left.getClass.getName)) else FailureMessages.wasAnInstanceOf(prettifier, left, UnquotedString(aType.clazzTag.toString)), None, pos ) } else org.scalatest.Succeeded } /** * Based on shouldBeTrue value, check if the given left is an instance of the type as described in the given ResultOfATypeInvocation. * If shouldBeTrue is true, a Fact.No will be returned if left is not an instance of the type given by ResultOfATypeInvocation. * If shouldBeTrue is false, a Fact.No will be returned if left is an instance of the type given by ResultOfATypeInvocation. * * @param left the left-hand-side (LHS) to be checked for the type * @param aType an instance of ResultOfATypeInvocation */ /*def expectATypeWillBeTrue(left: Any, aType: FactResultOfATypeInvocation[_], shouldBeTrue: Boolean): org.scalatest.Fact = { val clazz = aType.clazz if (conform(clazz, left) != shouldBeTrue) { org.scalatest.Fact.No( if (shouldBeTrue) FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(clazz.getName), UnquotedString(left.getClass.getName)) else FailureMessages.wasAnInstanceOf(prettifier, left, UnquotedString(clazz.getName)) ) } else org.scalatest.Fact.Yes( if (shouldBeTrue) FailureMessages.wasAnInstanceOf(prettifier, left, UnquotedString(clazz.getName)) else FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(clazz.getName), UnquotedString(left.getClass.getName)) ) }*/ /** * Based on shouldBeTrue value, check if the given left is an instance of the type as described in the given ResultOfAnTypeInvocation. * If shouldBeTrue is true, a TestFailedException will be thrown if left is not an instance of the type given by ResultOfAnTypeInvocation. * If shouldBeTrue is false, a TestFailedException will be thrown if left is an instance of the type given by ResultOfAnTypeInvocation. * * @param left the left-hand-side (LHS) to be checked for the type * @param anType an instance of ResultOfAnTypeInvocation */ def assertAnTypeShouldBeTrue(left: Any, anType: ResultOfAnTypeInvocation[_], shouldBeTrue: Boolean, prettifier: Prettifier, pos: source.Position): org.scalatest.Assertion = { val clazz = anType.clazz if (conform(anType.clazzTag, left) != shouldBeTrue) { throw newTestFailedException( if (shouldBeTrue) FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(anType.clazzTag.toString), UnquotedString(left.getClass.getName)) else FailureMessages.wasAnInstanceOf(prettifier, left, UnquotedString(anType.clazzTag.toString)), None, pos ) } else org.scalatest.Succeeded } /* * Based on shouldBeTrue value, check if the given left is an instance of the type as described in the given ResultOfAnTypeInvocation. * If shouldBeTrue is true, a Fact.No will be returned if left is not an instance of the type given by ResultOfAnTypeInvocation. * If shouldBeTrue is false, a Fact.No will be returned if left is an instance of the type given by ResultOfAnTypeInvocation. * * @param left the left-hand-side (LHS) to be checked for the type * @param anType an instance of ResultOfAnTypeInvocation */ /*def expectAnTypeWillBeTrue(left: Any, anType: FactResultOfAnTypeInvocation[_], shouldBeTrue: Boolean): org.scalatest.Fact = { val clazz = anType.clazz if (conform(clazz, left) != shouldBeTrue) { org.scalatest.Fact.No( if (shouldBeTrue) FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(clazz.getName), UnquotedString(left.getClass.getName)) else FailureMessages.wasAnInstanceOf(prettifier, left, UnquotedString(clazz.getName)) ) } else org.scalatest.Fact.Yes( if (shouldBeTrue) FailureMessages.wasAnInstanceOf(prettifier, left, UnquotedString(clazz.getName)) else FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(clazz.getName), UnquotedString(left.getClass.getName)) ) }*/ }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy