org.scalautils.AsAny.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scalatest_2.9.1 Show documentation
Show all versions of scalatest_2.9.1 Show documentation
ScalaTest is a free, open-source testing toolkit for Scala and Java
programmers.
The newest version!
/*
* 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 containing an implicit conversion that adds an asAny method to
* anything, which returns the same object as type Any.
*
*
* The purpose of this method is to appease the type checker when necessary. For example,
* in ScalaTest's matchers DSL the type passed to contain must be consistent
* with the element type of the collection on which should is invoked. So
* this type checks:
*
*
*
* Set(1, 2) should contain (2)
*
*
*
* But this does not type check:
*
*
*
* Set(1, 2) should contain ("2")
*
*
*
* That is all well and good, but it turns out that this does also not type check, because the element type of
* the collection (Any) is a supertype of the type passed to contain (String):
*
*
*
* Set(1, "2") should contain ("2") // Does not compile
*
*
*
* You can appease the type checker by casting the type of "2" to Any, a cast that
* will always succeed. Using asAny makes this prettier:
*
*
*
* Set(1, "2") should contain ("2".asAny)
*
*
*/
trait AsAny {
/**
* Wrapper class with an asAny method that returns the passed object
* as type Any.
*
* @param o the object to return from asAny
*
* @author Bill Venners
*/
class AsAnyWrapper(o: Any) {
/**
* Returns the object, o, passed to the constructor.
*
* @return the object passed to the constructor
*/
def asAny: Any = o
}
/**
* Implicit conversion that adds an asAny method to an object, which returns
* the exact same object but as type Any.
*/
implicit def convertToAsAnyWrapper(o: Any): AsAnyWrapper = new AsAnyWrapper(o)
}
/**
* Companion object to trait AsAny that facilitates the importing of AsAny members as
* an alternative to mixing it in. One use case is to import AsAny members so you can use
* them in the Scala interpreter:
*
*
* $ scala -classpath scalatest.jar
* Welcome to Scala version 2.10.0
* Type in expressions to have them evaluated.
* Type :help for more information.
*
* scala> import org.scalatest._
* import org.scalatest._
*
* scala> import Matchers._
* import Matchers._
*
* scala> Set(1, "2") should contain (1)
* :14: error: overloaded method value should with alternatives:
* [R](inv: org.scalautils.TripleEqualsInvocation[R])(implicit constraint: org.scalautils.EqualityConstraint[scala.collection.immutable.Set[Any],R])Unit
* (notWord: org.scalatest.Matchers.NotWord)org.scalatest.Matchers.ResultOfNotWordForTraversable[Any,scala.collection.immutable.Set]
* (beWord: org.scalatest.Matchers.BeWord)org.scalatest.Matchers.ResultOfBeWordForAnyRef[scala.collection.GenTraversable[Any]]
* (containMatcher: org.scalatest.ContainMatcher[Any])Unit
* (containWord: org.scalatest.Matchers.ContainWord)org.scalatest.Matchers.ResultOfContainWordForTraversable[Any]
* (haveWord: org.scalatest.Matchers.HaveWord)org.scalatest.Matchers.ResultOfHaveWordForTraversable[Any]
* (rightMatcherGen1: org.scalatest.Matchers.MatcherGen1[scala.collection.immutable.Set[Any],org.scalautils.Equality])(implicit equality: org.scalautils.Equality[scala.collection.immutable.Set[Any]])Unit
* (rightMatcherX6: org.scalatest.matchers.Matcher[scala.collection.GenTraversable[Any]])Unit
*cannot be applied to (org.scalatest.matchers.Matcher[scala.collection.GenTraversable[Int]])
* Set(1, "2") should contain (1)
* ^
*
* scala> Set(1, "2") should contain (1.asAny)
*
* scala>
*
*/
object AsAny extends AsAny
© 2015 - 2025 Weber Informatics LLC | Privacy Policy