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

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

Go to download

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.scalatest.matchers

import org.scalatest._

import org.scalatest.matchers._
import org.scalatest.enablers._
import java.lang.reflect.Method
import java.lang.reflect.Modifier
import scala.util.matching.Regex
import java.lang.reflect.Field
import scala.reflect.Manifest
import MatchersHelper.transformOperatorChars
import scala.collection.Traversable
import Assertions.areEqualComparingArraysStructurally
import scala.collection.GenTraversable
import scala.collection.GenSeq
import scala.collection.GenMap
import org.scalautils.Tolerance
import org.scalautils.Explicitly
import org.scalautils.Interval
import org.scalautils.TripleEqualsInvocation
import org.scalautils.Equality
import org.scalautils.TripleEqualsInvocationOnInterval
import org.scalautils.EqualityConstraint
import MatchersHelper.andMatchersAndApply
import MatchersHelper.orMatchersAndApply
import org.scalatest.words._
import MatchersHelper.matchSymbolToPredicateMethod
import MatchersHelper.accessProperty
import MatchersHelper.newTestFailedException
import MatchersHelper.fullyMatchRegexWithGroups
import MatchersHelper.startWithRegexWithGroups
import MatchersHelper.endWithRegexWithGroups
import MatchersHelper.includeRegexWithGroups
import org.scalautils.NormalizingEquality

// TODO: drop generic support for be as an equality comparison, in favor of specific ones.
// TODO: mention on JUnit and TestNG docs that you can now mix in MustMatchers or MustMatchers
// TODO: Put links from MustMatchers to wherever I reveal the matrix and algo of how properties are checked dynamically.
// TODO: double check that I wrote tests for (length (7)) and (size (8)) in parens
// TODO: document how to turn off the === implicit conversion
// TODO: Document you can use JMock, EasyMock, etc.

/**
 * Trait that provides a domain specific language (DSL) for expressing assertions in tests
 * using the word must.
 *
 * 

* For example, if you mix Matchers into * a suite class, you can write an equality assertion in that suite like this: *

* *
 * result must equal (3)
 * 
* *

* Here result is a variable, and can be of any type. If the object is an * Int with the value 3, execution will continue (i.e., the expression will result * in the unit value, ()). Otherwise, a TestFailedException * will be thrown with a detail message that explains the problem, such as "7 did not equal 3". * This TestFailedException will cause the test to fail. *

* * *

Matchers migration in ScalaTest 2.0

* *

* In ScalaTest 2.0, traits org.scalatest.matchers.MustMatchers and org.scalatest.matchers.MustMatchers are deprecated, replaced * by trait org.scalatest.Matchers. * MustMatchers and MustMatchers will continue to work during a lengthy deprecation cycle, but will eventually be removed in * a future version of ScalaTest. You can migrate existing uses of MustMatchers * by simply importing or mixing in org.scalatest.Matchers instead of org.scalatest.matchers.MustMatchers. You can migrate existing * uses of org.scalatest.matchers.MustMatchers in the same manner, by importing or mixing in org.scalatest.Matchers instead of * org.scalatest.matchers.MustMatchers, but with one extra step: replacing "should" with "must". org.scalatest.Matchers * only supports the verb "must"; We apologize for imposing such a large search-and-replace job on users, but we want to * make the verb "must" available to be used for a different purpose in ScalaTest after the deprecation cycle for MustMatchers * is completed. *

* *

* All previously documented syntax for matchers must continue to work exactly the same in the current ScalaTest 2.0.M6-SNAP release, with two potential breakages, both of * which must be quite rare, and one deprecation. First, support for "have length" and "have size" based solely on * structural types has been removed. Any use of this syntax on types other than Scala or Java collections, arrays, or strings will no longer compile. * To migrate such code, you will need to implicitly provide either a Length[T] or Size[T] for your type T, as * described below. * The other, even rarer (and likely temporary), potential breakage is that if length or size were used along with other custom have-property matchers, * length and size must now come first in the list, as described below. * The deprecation is be === <value> syntax. This will continue to work as before, but will generate a deprecation * warning and eventually be removed in a later version of ScalaTest. Please replace uses of this syntax with one of the other * ways to check equality described in the next section. *

* * *

Checking equality with matchers

* *

* ScalaTest matchers provides five different ways to check equality, each designed to address a different need. They are: *

* *
 * result must equal (3) // can customize equality
 * result must === (3)   // can customize equality and enforce type constraints
 * result must be (3)    // cannot customize equality
 * result mustEqual 3    // can customize equality, no parentheses required
 * result mustBe 3       // cannot customize equality, no parentheses required
 * 
* *

* The "left must equal (right)" syntax requires an org.scalautils.Equality[L] to be provided (either implicitly or explicitly), where * L is the left-hand type on which must is invoked. In the "left must equal (right)" case, * for example, L is the type of left. Thus if left is type Int, the "left must * equal (right)" * statement would require an Equality[Int]. *

* *

* By default, an implicit Equality[T] instance is available for any type T, in which equality is implemented * by simply invoking == on the left * value, passing in the right value, with special treatment for arrays. If either left or right is an array, deep * will be invoked on it before comparing with ==. Thus, even though the following expression * will yield false, because Array's equals method compares object identity: *

* *
 * Array(1, 2) == Array(1, 2) // yields false
 * 
* *

* The next expression will by default not result in a TestFailedException, because default Equality[Array[Int]] compares * the two arrays structurally, taking into consideration the equality of the array's contents: *

* *
 * Array(1, 2) must equal (Array(1, 2)) // succeeds (i.e., does not throw TestFailedException)
 * 
* *

* If you ever do want to verify that two arrays are actually the same object (have the same identity), you can use the * be theSameInstanceAs syntax, described below. *

* *

* You can customize the meaning of equality for a type when using "must equal," "must ===," * or mustEqual syntax by defining implicit Equality instances that will be used instead of default Equality. * You might do this to normalize types before comparing them with ==, for instance, or to avoid calling the == method entirely, * such as if you want to compare Doubles with a tolerance. * For an example, see the main documentation of trait Equality. *

* *

* You can always supply implicit parameters explicitly, but in the case of implicit parameters of type Equality[T], ScalaTest provides a * simple "explictly" DSL. For example, here's how you could explicitly supply an Equality[String] instance that normalizes both left and right * sides (which must be strings), by transforming them to lowercase: *

* *
 * scala> import org.scalatest.Matchers._
 * import org.scalatest.Matchers._
 *
 * scala> import org.scalautils.Explicitly._
 * import org.scalautils.Explicitly._
 *
 * scala> import org.scalautils.StringNormalizations._
 * import org.scalautils.StringNormalizations._
 *
 * scala> "Hi" must equal ("hi") (after being lowerCased)
 * 
* *

* The after being lowerCased expression results in an Equality[String], which is then passed * explicitly as the second curried parameter to equal. For more information on the explictly DSL, see the main documentation * for trait Explicitly. *

* *

* The "must be" and mustBe syntax do not take an Equality[T] and can therefore not be customized. * They always use the default approach to equality described above. As a result, "must be" and mustBe will * likely be the fastest-compiling matcher syntax for equality comparisons, since the compiler need not search for * an implicit Equality[T] each time. *

* *

* The must === syntax (and its complement, must !==) can be used to enforce type * constraints at compile-time between the left and right sides of the equality comparison. Here's an example: *

* *
 * scala> import org.scalatest.Matchers._
 * import org.scalatest.Matchers._
 *
 * scala> import org.scalautils.TypeCheckedTripleEquals._
 * import org.scalautils.TypeCheckedTripleEquals._
 *
 * scala> Some(2) must === (2)
 * <console>:17: error: types Some[Int] and Int do not adhere to the equality constraint
 * selected for the === and !== operators; the missing implicit parameter is of
 * type org.scalautils.EqualityConstraint[Some[Int],Int]
 *               Some(2) must === (2)
 *                       ^
 * 
* *

* By default, the "Some(2) must === (2)" statement would fail at runtime. By mixing in * the equality constraints provided by TypeCheckedTripleEquals, however, the statement fails to compile. For more information * and examples, see the main documentation for trait TypeCheckedTripleEquals. *

* * *

Checking size and length

* *

* You can check the size or length of any type of object for which it * makes sense. Here's how checking for length looks: *

*
 * result must have length (3)
 * 
* *

* Size is similar: *

* *
 * result must have size (10)
 * 
* *

* The length syntax can be used with String, Array, any scala.collection.GenSeq, * any java.util.List, and any type T for which an implicit Length[T] type class is * available in scope. * Similarly, the size syntax can be used with Array, any scala.collection.GenTraversable, * any java.util.Collection, any java.util.Map, and any type T for which an implicit Size[T] type class is * available in scope. You can enable the length or size syntax for your own arbitrary types, therefore, * by defining Length or Size type * classes for those types. *

* *

Checking strings

* *

* You can check for whether a string starts with, ends with, or includes a substring like this: *

* *
 * string must startWith ("Hello")
 * string must endWith ("world")
 * string must include ("seven")
 * 
* *

* You can check for whether a string starts with, ends with, or includes a regular expression, like this: *

* *
 * string must startWith regex ("Hel*o")
 * string must endWith regex ("wo.ld")
 * string must include regex ("wo.ld")
 * 
* *

* And you can check whether a string fully matches a regular expression, like this: *

* *
 * string must fullyMatch regex ("""(-)?(\d+)(\.\d*)?""")
 * 
* *

* The regular expression passed following the regex token can be either a String * or a scala.util.matching.Regex. *

* *

* With the startWith, endWith, include, and fullyMatch * tokens can also be used with an optional specification of required groups, like this: *

* *
 * "abbccxxx" must startWith regex ("a(b*)(c*)" withGroups ("bb", "cc"))
 * "xxxabbcc" must endWith regex ("a(b*)(c*)" withGroups ("bb", "cc"))
 * "xxxabbccxxx" must include regex ("a(b*)(c*)" withGroups ("bb", "cc"))
 * "abbcc" must fullyMatch regex ("a(b*)(c*)" withGroups ("bb", "cc"))
 * 
* *

Greater and less than

*

* You can check whether any type that is, or can be implicitly converted to, * an Ordered[T] is greater than, less than, greater than or equal, or less * than or equal to a value of type T. The syntax is: *

*
 * one must be < (7)
 * one must be > (0)
 * one must be <= (7)
 * one must be >= (0)
 * 
* *

Checking Boolean properties with be

* *

* If an object has a method that takes no parameters and returns boolean, you can check * it by placing a Symbol (after be) that specifies the name * of the method (excluding an optional prefix of "is"). A symbol literal * in Scala begins with a tick mark and ends at the first non-identifier character. Thus, * 'empty results in a Symbol object at runtime, as does * 'defined and 'file. Here's an example: *

* *
 * emptySet must be ('empty)
 * 
* * Given this code, ScalaTest will use reflection to look on the object referenced from * emptySet for a method that takes no parameters and results in Boolean, * with either the name empty or isEmpty. If found, it will invoke * that method. If the method returns true, execution will continue. But if it returns * false, a TestFailedException will be thrown that will contain a detail message, such as: * *
 * Set(1, 2, 3) was not empty
 * 
* *

* This be syntax can be used with any type. If the object does * not have an appropriately named predicate method, you'll get a TestFailedException * at runtime with a detail message that explains the problem. * (For the details on how a field or method is selected during this * process, see the documentation for BeWord.) *

* *

* If you think it reads better, you can optionally put a or an after * be. For example, java.io.File has two predicate methods, * isFile and isDirectory. Thus with a File object * named temp, you could write: *

* *
 * temp must be a ('file)
 * 
* *

* Or, given java.awt.event.KeyEvent has a method isActionKey that takes * no arguments and returns Boolean, you could assert that a KeyEvent is * an action key with: *

* *
 * keyEvent must be an ('actionKey)
 * 
* *

* If you prefer to check Boolean properties in a type-safe manner, you can use a BePropertyMatcher. * This would allow you to write expressions such as: *

* *
 * emptySet must be (empty)
 * temp must be a (file)
 * keyEvent must be an (actionKey)
 * 
* *

* These expressions would fail to compile if must is used on an inappropriate type, as determined * by the type parameter of the BePropertyMatcher being used. (For example, file in this example * would likely be of type BePropertyMatcher[java.io.File]. If used with an appropriate type, such an expression will compile * and at run time the Boolean property method or field will be accessed directly; i.e., no reflection will be used. * See the documentation for BePropertyMatcher for more information. *

* *

Using custom BeMatchers

* * If you want to create a new way of using be, which doesn't map to an actual property on the * type you care about, you can create a BeMatcher. You could use this, for example, to create BeMatcher[Int] * called odd, which would match any odd Int, and even, which would match * any even Int. * Given this pair of BeMatchers, you could check whether an Int was odd or even with expressions like: *

* *
 * num must be (odd)
 * num must not be (even)
 * 
* * For more information, see the documentation for BeMatcher. * *

Checking object identity

* *

* If you need to check that two references refer to the exact same object, you can write: *

* *
 * ref1 must be theSameInstanceAs (ref2)
 * 
* *

Checking numbers against a range

* *

* To check whether a floating point number has a value that exactly matches another, you * can use must equal: *

* *
 * sevenDotOh must equal (7.0)
 * 
* *

* Often, however, you may want to check whether a floating point number is within a * range. You can do that using be and +-, like this: *

* *
 * sevenDotOh must be (6.9 +- 0.2)
 * 
* *

* This expression will cause a TestFailedException to be thrown if the floating point * value, sevenDotOh is outside the range 6.7 to 7.1. * You can also use +- with integral types, for example: *

* *
 * seven must be (6 +- 2)
 * 
* *

Working with collections

* *

* You can use some of the syntax shown previously with Scala collections, i.e., GenTraversable and its * subtypes. For example, you can check whether a GenTraversable is empty, * like this: *

* *
 * traversable must be ('empty)
 * 
* *

* You can check the length of a GenSeq (or an Array) * like this: *

* *
 * array must have length 3
 * list must have length 9
 * 
* *

* You can check the size of any GenTraversable, like this: *

* *
 * map must have size 20
 * set must have size 90
 * 
* *

* In addition, you can check whether an GenTraversable contains a particular * element, like this: *

* *
 * traversable must contain ("five")
 * 
* *

* You can also check whether a GenMap contains a particular key, or value, like this: *

* *
 * map must contain key 1
 * map must contain value "Howdy"
 * 
* *

Java collections and maps

* *

* You can use similar syntax on Java collections (java.util.Collection) and maps (java.util.Map). * For example, you can check whether a Java Collection or Map is empty, * like this: *

* *
 * javaCollection must be ('empty)
 * javaMap must be ('empty)
 * 
* *

* Even though Java's List type doesn't actually have a length or getLength method, * you can nevertheless check the length of a Java List (java.util.List) like this: *

* *
 * javaList must have length 9
 * 
* *

* You can check the size of any Java Collection or Map, like this: *

* *
 * javaMap must have size 20
 * javaSet must have size 90
 * 
* *

* In addition, you can check whether a Java Collection contains a particular * element, like this: *

* *
 * javaCollection must contain ("five")
 * 
* *

* One difference to note between the syntax supported on Java collections and that of Scala * GenTraversables is that you can't use contain (...) syntax with a Java Map. * Java differs from Scala in that its Map is not a subtype of its Collection type. * If you want to check that a Java Map contains a specific key/value pair, the best approach is * to invoke entrySet on the Java Map and check that entry set for the appropriate * element (a java.util.Map.Entry) using contain (...). *

* *

* Despite this difference, the other (more commonly used) map matcher syntax works just fine on Java Maps. * You can, for example, check whether a Java Map contains a particular key, or value, like this: *

* *
 * javaMap must contain key 1
 * javaMap must contain value "Howdy"
 * 
* * *

Working with "containers"

* *

* The contain syntax shown above can be used with any type C that has a "containing" nature, as evidenced by * an org.scalatest.enablers.Containing[L] instance that must be supplied as a curried parameter to contain, either implicitly or * explicitly, with L being the left-hand type on which must is invoked. In the Containing * companion object, implicits are provided for types GenTraversable[E], java.util.Collection[E], * java.util.Map[K, V], String, Array[E], and Option[E]. (Note: in 2.0.M6, Scala and Java Iterators * will be added to this list.) Here are some examples: *

* *
 * scala> import org.scalatest.Matchers._
 * import org.scalatest.Matchers._
 *
 * scala> List(1, 2, 3) must contain (2)
 *
 * scala> Map('a' -> 1, 'b' -> 2, 'c' -> 3) must contain ('b' -> 2)
 *
 * scala> Set(1, 2, 3) must contain (2)
 *
 * scala> Array(1, 2, 3) must contain (2)
 *
 * scala> "123" must contain ('2')
 *
 * scala> Some(2) must contain (2)
 * 
* *

* ScalaTest's implicit methods that provide the Containing[L] typeclasses require an Equality[E], where * E is an element type. For example, to obtain a Containing[Array[Int]] you must supply an Equality[Int], * either implicitly or explicitly. The contain syntax uses this Equality[E] to determine containership. * Thus if you want to change how containership is determined for an element type E, place an implicit Equality[E] * in scope or use the explicitly DSL. Although the implicit parameter required for the contain syntax is of type Containing[L], * implicit conversions are provided in the Containing companion object from Equality[E] to the various * types of containers of E. Here's an example: *

* *
 * scala> import org.scalatest.Matchers._
 * import org.scalatest.Matchers._
 *
 * scala> List("Hi", "Di", "Ho") must contain ("ho")
 * org.scalatest.exceptions.TestFailedException: List(Hi, Di, Ho) did not contain element "ho"
 *         at ...
 *
 * scala> import org.scalautils.Explicitly._
 * import org.scalautils.Explicitly._
 *
 * scala> import org.scalautils.StringNormalizations._
 * import org.scalautils.StringNormalizations._
 *
 * scala> (List("Hi", "Di", "Ho") must contain ("ho")) (after being lowerCased)
 * 
* *

* Note that when you use the explicitly DSL with contain you need to wrap the entire * contain expression in parentheses, as shown here. *

* *
 * (List("Hi", "Di", "Ho") must contain ("ho")) (after being lowerCased)
 * ^                                            ^
 * 
* *

* In addition to determining whether an object contains another object, you can use contain to * make other determinations. * For exmple, the contain oneOf syntax ensures that one and only one of the specified elements are * contained in the containing object: *

* *
 * List(1, 2, 3, 4, 5) must contain oneOf (5, 7, 9)
 * Some(7) must contain oneOf (5, 7, 9)
 * "howdy" must contain oneOf ('a', 'b', 'c', 'd')
 * 
* *

* Note that if multiple specified elements appear in the containing object, oneOf will fail: *

* *
 * scala> List(1, 2, 3) must contain oneOf (2, 3, 4)
 * org.scalatest.exceptions.TestFailedException: List(1, 2, 3) did not contain one of (2, 3, 4)
 *         at ...
 * 
* *

* If you really want to ensure one or more of the specified elements are contained in the containing object, * use atLeastOneOf, described below, instead of oneOf. In other words, oneOf * means "exactly one of." *

* *

* Note also that with any contain syntax, you can place custom implicit Equality[E] instances in scope * to customize how containership is determined, or use the explicitly DSL. Here's an example: *

* *
 * (Array("Doe", "Ray", "Me") must contain oneOf ("X", "RAY", "BEAM")) (after being lowerCased)
 * 
* *

* The contain noneOf syntax does the opposite of oneOf: it ensures none of the specified elements * are contained in the containing object: *

* *
 * List(1, 2, 3, 4, 5) must contain noneOf (7, 8, 9)
 * Some(0) must contain noneOf (7, 8, 9)
 * "12345" must contain noneOf ('7', '8', '9')
 * 
* * *

Working with "aggregations"

* *

* As mentioned, the "contain," "contain oneOf," and "contain noneOf" syntax requires a * Containing[L] be provided, where L is the left-hand type. By contrast, the rest of the contain syntax, which * will be described in this section, requires an Aggregating[L] be provided, where again L is the left-hand type. * (An Aggregating[L] instance defines the "aggregating nature" of a type L.) * The reason, essentially, is that contain syntax that makes sense for Option is enabled by * Containing[L], whereas syntax that does not make sense for Option is enabled * by Aggregating[L]. For example, it doesn't make sense to assert that an Option[Int] contains all of a set of integers, as it * could only ever contain one of them. But this does make sense for a type such as List[Int] that can aggregate zero to many integers. *

* *

* The Aggregating companion object provides implicit instances of Aggregating[L] * for types GenTraversable[E], java.util.Collection[E], * java.util.Map[K, V], String, Array[E]. Note that these are the same types as are supported with * Containing, but with Option[E] missing. * (And as with Containing, in 2.0.M6, Scala and Java Iterators * will be added to this list.) Here are some examples: *

* *

* The contain atLeastOneOf syntax, for example, works for any type L for which an Aggregating[L] exists. It ensures * that at least one of (i.e., one or more of) the specified objects are contained in the containing object: *

* *
 * List(1, 2, 3) must contain atLeastOneOf (2, 3, 4)
 * Array(1, 2, 3) must contain atLeastOneOf (3, 4, 5)
 * "abc" must contain atLeastOneOf ('c', 'a', 't')
 * 
* *

* Note: The contain atMostOneOf syntax is currently unimplemented, but will be added for 2.0.M6. *

* *

* Similar to Containing[L], the implicit methods that provide the Aggregating[L] instances require an Equality[E], where * E is an element type. For example, to obtain a Aggregating[Vector[String]] you must supply an Equality[String], * either implicitly or explicitly. The contain syntax uses this Equality[E] to determine containership. * Thus if you want to change how containership is determined for an element type E, place an implicit Equality[E] * in scope or use the explicitly DSL. Although the implicit parameter required for the contain syntax is of type Aggregating[L], * implicit conversions are provided in the Aggregating companion object from Equality[E] to the various * types of aggregations of E. Here's an example: *

* *
 * (Vector(" A", "B ") must contain atLeastOneOf ("a ", "b", "c")) (after being lowerCased and trimmed)
 * 
* *

* The "contain allOf" syntax lets you specify a set of objects that must all be contained in the containing object: *

* *
 * List(1, 2, 3, 4, 5) must contain allOf (2, 3, 5)
 * 
* *

* The "contain only" syntax lets you assert that the containing object contains only the specified objects, though it may * contain more than one of each: *

* *
 * List(1, 2, 3, 2, 1) must contain only (1, 2, 3)
 * 
* *

* Note: In the current SNAP release, contain only currently allows a specified right-hand element to not appear in the left-hand aggregation. * This will be disallowed in 2.0.M6. *

* *

* The "contain inOrderOnly" syntax lets you assert that the containing object contains only the specified objects, in order. * The specified objects may appear multiple times, but must appear in the order they appear in the right-hand list. Here's an example: *

* *
 * List(1, 2, 2, 3, 3, 3) must contain inOrderOnly (1, 2, 3)
 * 
* *

* The "contain inOrder" syntax lets you assert that the containing object contains only the specified objects in order, like * inOrderOnly, but allows other objects to appear in the left-hand aggregation as well: * contain more than one of each: *

* *
 * List(0, 1, 2, 2, 99, 3, 3, 3, 5) must contain inOrder (1, 2, 3)
 * 
* *

* Note that "order" in inOrder, inOrderOnly, and theSameElementsInOrderAs (described below) * in the Aggregation[L] instances built-in to ScalaTest is defined as "iteration order". *

* *

* The "contain theSameElementsAs" and "contain theSameElementsInOrderAs syntax differ from the others * in that the right hand side is a GenTraversable[_] rather than a varargs of Any. (Note: in a future 2.0 milestone release, possibly * 2.0.M6, these will likely be widened to accept any type R for which an Aggregating[R] exists.) *

* *

* The "contain theSameElementsAs" syntax lets you assert that two aggregations contain the same objects: *

* *
 * List(1, 2, 2, 3, 3, 3) must contain theSameElementsAs Vector(3, 2, 3, 1, 2, 3)
 * 
* *

* The number of times any family of equal objects appears must also be the same in both the left and right aggregations. * The specified objects may appear multiple times, but must appear in the order they appear in the right-hand list. For example, if * the last 3 element is left out of the right-hand list in the previous example, the expression would fail because the left side * has three 3's and the right hand side has only two: *

* *
 * List(1, 2, 2, 3, 3, 3) must contain theSameElementsAs Vector(3, 2, 3, 1, 2)
 * org.scalatest.exceptions.TestFailedException: List(1, 2, 2, 3, 3, 3) did not contain the same elements as Vector(3, 2, 3, 1, 2)
 *         at ...
 * 
* *

* Lastly, the "contain theSameElementsInOrderAs" syntax lets you assert that two aggregations contain * the same exact elements in the same (iteration) order: *

* *
 * List(1, 2, 3) must contain theSameElementsInOrderAs collection.mutable.TreeSet(3, 2, 1)
 * 
* *

* The previous assertion succeeds because the iteration order of aTreeSet is the natural * ordering of its elements, which in this case is 1, 2, 3. An iterator obtained from the left-hand List will produce the same elements * in the same order. *

* * *

Inspector shorthands

* *

* You can use the Inspectors syntax with matchers as well as assertions. If you have a multi-dimensional collection, such as an * list of lists, using Inspectors is your best option: *

* *
 * val yss =
 *   List(
 *     List(1, 2, 3),
 *     List(1, 2, 3),
 *     List(1, 2, 3)
 *   )
 *
 * forAll (yss) { ys =>
 *   forAll (ys) { y => y must be > 0 }
 * }
 * 
* *

* For assertions on one-dimensional collections, however, matchers provides "inspector shorthands." Instead of writing: *

* *
 * val xs = List(1, 2, 3)
 * forAll (xs) { x => x must be < 10 }
 * 
* *

* You can write: *

* *
 * all (xs) must be < 10
 * 
* *

* The previous statement asserts that all elements of the xs list must be less than 10. * All of the inspectors have shorthands in matchers. Here is the full list: *

* *
    *
  • all - succeeds if the assertion holds true for every element
  • *
  • atLeast - succeeds if the assertion holds true for at least the specified number of elements
  • *
  • atMost - succeeds if the assertion holds true for at most the specified number of elements
  • *
  • between - succeeds if the assertion holds true for between the specified minimum and maximum number of elements, inclusive
  • *
  • every - same as all, but lists all failing elements if it fails (whereas all just reports the first failing element)
  • *
  • exactly - succeeds if the assertion holds true for exactly the specified number of elements
  • *
* *

* Here are some examples: *

* *
 * scala> import org.scalatest.Matchers._
 * import org.scalatest.Matchers._
 *
 * scala> val xs = List(1, 2, 3, 4, 5)
 * xs: List[Int] = List(1, 2, 3, 4, 5)
 *
 * scala> all (xs) must be > 0
 *
 * scala> atMost(2, xs) must be >= 4
 *
 * scala> atLeast(3, xs) must be < 5
 *
 * scala> between(2, 3, xs) must (be > 1 and be < 5)
 *
 * scala> exactly (2, xs) must be <= 2
 *
 * scala> every (xs) must be < 10
 *
 * scala> // And one that fails...
 *
 * scala> exactly (2, xs) mustEqual 2
 * org.scalatest.exceptions.TestFailedException: 'exactly(2)' inspection failed, because only 1 element
 *     satisfied the assertion block at index 1: 
 *   at index 0, 1 did not equal 2, 
 *   at index 2, 3 did not equal 2, 
 *   at index 3, 4 did not equal 2, 
 *   at index 4, 5 did not equal 2 
 * in List(1, 2, 3, 4, 5)
 *         at ...
 * 
* *

* Note: in the current 2.0.M6-SNAP release, the type of object used with inspector shorthands must be GenTraversable, but this will likely be widened to * include Java collections, arrays, iterators, etc., for 2.0.M6. *

* * *

Single-element collections

* *

* To assert both that a collection contains just one "lone" element as well as something else about that element, you can use * the loneElement syntax. For example, if a Set[Int] must contain just one element, an Int * less than or equal to 10, you could write: *

* *
 * set.loneElement must be <= 10
 * 
* *

* Note: in the current 2.0.M6-SNAP release, the type of object on which you can invoke loneElement must be GenTraversable, but this * will likely be widened to include Java collections, arrays, iterators, etc., for 2.0.M6. *

* *

Be as an equality comparison

* *

* All uses of be other than those shown previously perform an equality comparison. In other words, they work * the same as equal when it is used with default equality. This redundancy between be and equals exists in part * because it enables syntax that sometimes sounds more natural. For example, instead of writing: *

* *
 * result must equal (null)
 * 
* *

* You can write: *

* *
 * result must be (null)
 * 
* *

* (Hopefully you won't write that too much given null is error prone, and Option * is usually a better, well, option.) * Here are some other examples of be used for equality comparison: *

* *
 * sum must be (7.0)
 * boring must be (false)
 * fun must be (true)
 * list must be (Nil)
 * option must be (None)
 * option must be (Some(1))
 * 
* *

* As with equal used with default equality, using be on arrays results in deep being called on both arrays prior to * calling equal. As a result, * the following expression would not throw a TestFailedException: *

* *
 * Array(1, 2) must be (Array(1, 2)) // succeeds (i.e., does not throw TestFailedException)
 * 
* *

* Because be is used in several ways in ScalaTest matcher syntax, just as it is used in many ways in English, one * potential point of confusion in the event of a failure is determining whether be was being used as an equality comparison or * in some other way, such as a property assertion. To make it more obvious when be is being used for equality, the failure * messages generated for those equality checks will include the word equal in them. For example, if this expression fails with a * TestFailedException: *

* *
 * option must be (Some(1))
 * 
* *

* The detail message in that TestFailedException will include the words "equal to" to signify be * was in this case being used for equality comparison: *

* *
 * Some(2) was not equal to Some(1)
 * 
* *

Being negative

* *

* If you wish to check the opposite of some condition, you can simply insert not in the expression. * Here are a few examples: *

* *
 * result must not be (null)
 * sum must not be <= (10)
 * mylist must not equal (yourList)
 * string must not startWith ("Hello")
 * 
* *

Logical expressions with and and or

* *

* You can also combine matcher expressions with and and/or or, however, * you must place parentheses or curly braces around the and or or expression. For example, * this and-expression would not compile, because the parentheses are missing: *

* *
 * map must contain key ("two") and not contain value (7) // ERROR, parentheses missing!
 * 
* *

* Instead, you need to write: *

* *
 * map must (contain key ("two") and not contain value (7))
 * 
* *

* Here are some more examples: *

* *
 * number must (be > (0) and be <= (10))
 * option must (equal (Some(List(1, 2, 3))) or be (None))
 * string must (
 *   equal ("fee") or
 *   equal ("fie") or
 *   equal ("foe") or
 *   equal ("fum")
 * )
 * 
* *

* Two differences exist between expressions composed of these and and or operators and the expressions you can write * on regular Booleans using its && and || operators. First, expressions with and * and or do not short-circuit. The following contrived expression, for example, would print "hello, world!": *

* *
 * "yellow" must (equal ("blue") and equal { println("hello, world!"); "green" })
 * 
* *

* In other words, the entire and or or expression is always evaluated, so you'll see any side effects * of the right-hand side even if evaluating * only the left-hand side is enough to determine the ultimate result of the larger expression. Failure messages produced by these * expressions will "short-circuit," however, * mentioning only the left-hand side if that's enough to determine the result of the entire expression. This "short-circuiting" behavior * of failure messages is intended * to make it easier and quicker for you to ascertain which part of the expression caused the failure. The failure message for the previous * expression, for example, would be: *

* *
 * "yellow" did not equal "blue"
 * 
* *

* Most likely this lack of short-circuiting would rarely be noticeable, because evaluating the right hand side will usually not * involve a side effect. One situation where it might show up, however, is if you attempt to and a null check on a variable with an expression * that uses the variable, like this: *

* *
 * map must (not be (null) and contain key ("ouch"))
 * 
* *

* If map is null, the test will indeed fail, but with a NullPointerException, not a * TestFailedException. Here, the NullPointerException is the visible right-hand side effect. To get a * TestFailedException, you would need to check each assertion separately: *

* *
 * map must not be (null)
 * map must contain key ("ouch")
 * 
* *

* If map is null in this case, the null check in the first expression will fail with * a TestFailedException, and the second expression will never be executed. *

* *

* The other difference with Boolean operators is that although && has a higher precedence than ||, * and and or * have the same precedence. Thus although the Boolean expression (a || b && c) will evaluate the && expression * before the || expression, like (a || (b && c)), the following expression: *

* *
 * traversable must (contain (7) or contain (8) and have size (9))
 * 
* *

* Will evaluate left to right, as: *

* *
 * traversable must ((contain (7) or contain (8)) and have size (9))
 * 
* *

* If you really want the and part to be evaluated first, you'll need to put in parentheses, like this: *

* *
 * traversable must (contain (7) or (contain (8) and have size (9)))
 * 
* *

Working with Options

* *

* ScalaTest matchers has no special support for Options, but you can * work with them quite easily using syntax shown previously. For example, if you wish to check * whether an option is None, you can write any of: *

* *
 * option must equal (None)
 * option must be (None)
 * option must not be ('defined)
 * option must be ('empty)
 * 
* *

* If you wish to check an option is defined, and holds a specific value, you can write either of: *

* *
 * option must equal (Some("hi"))
 * option must be (Some("hi"))
 * 
* *

* If you only wish to check that an option is defined, but don't care what it's value is, you can write: *

* *
 * option must be ('defined)
 * 
* *

* If you mix in (or import the members of) OptionValues, * you can write one statement that indicates you believe an option must be defined and then say something else about its value. Here's an example: *

* *
 * import org.scalatest.OptionValues._
 * option.value must be < (7)
 * 
* *

Checking arbitrary properties with have

* *

* Using have, you can check properties of any type, where a property is an attribute of any * object that can be retrieved either by a public field, method, or JavaBean-style get * or is method, like this: *

* *
 * book must have (
 *   'title ("Programming in Scala"),
 *   'author (List("Odersky", "Spoon", "Venners")),
 *   'pubYear (2008)
 * )
 * 
* *

* This expression will use reflection to ensure the title, author, and pubYear properties of object book * are equal to the specified values. For example, it will ensure that book has either a public Java field or method * named title, or a public method named getTitle, that when invoked (or accessed in the field case) results * in a the string "Programming in Scala". If all specified properties exist and have their expected values, respectively, * execution will continue. If one or more of the properties either does not exist, or exists but results in an unexpected value, * a TestFailedException will be thrown that explains the problem. (For the details on how a field or method is selected during this * process, see the documentation for HavePropertyMatcherGenerator.) *

* *

* When you use this syntax, you must place one or more property values in parentheses after have, seperated by commas, where a property * value is a symbol indicating the name of the property followed by the expected value in parentheses. The only exceptions to this rule is the syntax * for checking size and length shown previously, which does not require parentheses. If you forget and put parentheses in, however, everything will * still work as you'd expect. Thus instead of writing: *

* *
 * array must have length (3)
 * set must have size (90)
 * 
* *

* You can alternatively, write: *

* *
 * array must have (length (3))
 * set must have (size (90))
 * 
* *

* If a property has a value different from the specified expected value, a TestFailedError will be thrown * with a detail message that explains the problem. For example, if you assert the following on * a book whose title is Moby Dick: *

* *
 * book must have ('title ("A Tale of Two Cities"))
 * 
* *

* You'll get a TestFailedException with this detail message: *

* *
 * The title property had value "Moby Dick", instead of its expected value "A Tale of Two Cities",
 * on object Book("Moby Dick", "Melville", 1851)
 * 
* *

* If you prefer to check properties in a type-safe manner, you can use a HavePropertyMatcher. * This would allow you to write expressions such as: *

* *
 * book must have (
 *   title ("Programming in Scala"),
 *   author (List("Odersky", "Spoon", "Venners")),
 *   pubYear (2008)
 * )
 * 
* *

* These expressions would fail to compile if must is used on an inappropriate type, as determined * by the type parameter of the HavePropertyMatcher being used. (For example, title in this example * might be of type HavePropertyMatcher[org.publiclibrary.Book]. If used with an appropriate type, such an expression will compile * and at run time the property method or field will be accessed directly; i.e., no reflection will be used. * See the documentation for HavePropertyMatcher for more information. *

* * *

Using length and size with HavePropertyMatchers

* *

* In the currenty 2.0.M6-SNAP release, if you want to use length or size syntax with your own custom HavePropertyMatchers, you * can do so, but you must put the length or size statement first in the list. For example, you could write: *

* *
 * book must have (
 *   length (220),
 *   title ("A Tale of Two Cities"),
 *   author ("Dickens")
 * )
 * 
* *

* By contrast, the following code would not compile, because length does not come first in the list: *

* *
 * book must have (
 *   title ("A Tale of Two Cities"),
 *   length (220),
 *   author ("Dickens")
 * )
 * 
* *

* Prior to ScalaTest 2.0, length (22) yielded a HavePropertyMatcher[Any, Int] that used reflection to dynamically look * for a length field or getLength method. In ScalaTest 2.0, length (22) yields a * MatcherFactory1[Any, Length], so it is no longer a HavePropertyMatcher. In 2.0.M6 this restriction will likely be lifted via * an implicit conversions in the HavePropertyMatcher companion object, but for the current SNAP release the restriction exists. *

* *

Using custom matchers

* *

* If none of the built-in matcher syntax (or options shown so far for extending the syntax) satisfy a particular need you have, you can create * custom Matchers that allow * you to place your own syntax directly after must. For example, class java.io.File has a method exists, which * indicates whether a file of a certain path and name exists. Because the exists method takes no parameters and returns Boolean, * you can call it using be with a symbol or BePropertyMatcher, yielding assertions like: *

* *
 * file must be ('exists)  // using a symbol
 * file must be (inExistance)   // using a BePropertyMatcher
 * 
* *

* Although these expressions will achieve your goal of throwing a TestFailedException if the file does not exist, they don't produce * the most readable code because the English is either incorrect or awkward. In this case, you might want to create a * custom Matcher[java.io.File] * named exist, which you could then use to write expressions like: *

* *
 * // using a plain-old Matcher
 * file must exist
 * file must not (exist)
 * file must (exist and have ('name ("temp.txt")))
 * 
* *

* Note that when you use custom Matchers, you will need to put parentheses around the custom matcher in more cases than with * the built-in syntax. For example you will often need the parentheses after not, as shown above. (There's no penalty for * always surrounding custom matchers with parentheses, and if you ever leave them off when they are needed, you'll get a compiler error.) * For more information about how to create custom Matchers, please see the documentation for the Matcher trait. *

* *

Checking for expected exceptions

* *

* Sometimes you need to test whether a method throws an expected exception under certain circumstances, such * as when invalid arguments are passed to the method. With Matchers mixed in, you can * check for an expected exception like this: *

* *
 * evaluating { s.charAt(-1) } must produce [IndexOutOfBoundsException]
 * 
* *

* If charAt throws an instance of StringIndexOutOfBoundsException, * this expression will result in that exception. But if charAt completes normally, or throws a different * exception, this expression will complete abruptly with a TestFailedException. * This expression returns the caught exception so that you can inspect it further if you wish, for * example, to ensure that data contained inside the exception has the expected values. Here's an * example: *

* *
 * val thrown = evaluating { s.charAt(-1) } must produce [IndexOutOfBoundsException]
 * thrown.getMessage must equal ("String index out of range: -1")
 * 
* *

Those pesky parens

* *

* Perhaps the most tricky part of writing assertions using ScalaTest matchers is remembering * when you need or don't need parentheses, but bearing in mind a few simple rules should help. * It is also reassuring to know that if you ever leave off a set of parentheses when they are * required, your code will not compile. Thus the compiler will help you remember when you need the parens. * That said, the rules are: *

* *

* 1. Although you don't always need them, it is recommended style to always put parentheses * around right-hand values, such as the 7 in num must equal (7): *

* *
 * result must equal (4)
 * array must have length (3)
 * book must have (
 *   'title ("Programming in Scala"),
 *   'author (List("Odersky", "Spoon", "Venners")),
 *   'pubYear (2008)
 * )
 * option must be ('defined)
 * catMap must (contain key (9) and contain value ("lives"))
 * keyEvent must be an ('actionKey)
 * javaSet must have size (90)
 * 
* *

* 2. Except for length and size, you must always put parentheses around * the list of one or more property values following a have: *

* *
 * file must (exist and have ('name ("temp.txt")))
 * book must have (
 *   title ("Programming in Scala"),
 *   author (List("Odersky", "Spoon", "Venners")),
 *   pubYear (2008)
 * )
 * javaList must have length (9) // parens optional for length and size
 * 
* *

* 3. You must always put parentheses around and and or expressions, as in: *

* *
 * catMap must (contain key (9) and contain value ("lives"))
 * number must (equal (2) or equal (4) or equal (8))
 * 
* *

* 4. Although you don't always need them, it is recommended style to always put parentheses * around custom Matchers when they appear directly after not: *

* *
 * file must exist
 * file must not (exist)
 * file must (exist and have ('name ("temp.txt")))
 * file must (not (exist) and have ('name ("temp.txt"))
 * file must (have ('name ("temp.txt") or exist)
 * file must (have ('name ("temp.txt") or not (exist))
 * 
* *

* That's it. With a bit of practice it should become natural to you, and the compiler will always be there to tell you if you * forget a set of needed parentheses. *

*/ @deprecated("Please use org.scalatest.Matchers instead.") trait MustMatchers extends Assertions with Tolerance with MustVerb with LoneElement with MatcherWords with Explicitly { matchers => // // This class is used as the return type of the overloaded must method (in MapMustWrapper) // that takes a HaveWord. It's key method will be called in situations like this: // // map must have key 1 // // This gets changed to : // // convertToMapMustWrapper(map).must(have).key(1) // // Thus, the map is wrapped in a convertToMapMustWrapper call via an implicit conversion, which results in // a MapMustWrapper. This has a must method that takes a HaveWord. That method returns a // ResultOfHaveWordPassedToMust that remembers the map to the left of must. Then this class // ha a key method that takes a K type, they key type of the map. It does the assertion thing. // /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class ResultOfContainWordForMap[K, V, L[_, _] <: scala.collection.GenMap[_, _]](val left: scala.collection.GenMap[K, V], val mustBeTrue: Boolean) extends ResultOfContainWord[L[K, V]](left.asInstanceOf[L[K, V]]) with ContainMethods[(K, V)] { /** * This method enables the following syntax: * *
     * map must contain key ("one")
     *                    ^
     * 
*/ def key(expectedKey: K) { if (left.exists(_._1 == expectedKey) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainKey" else "containedKey", left, expectedKey) ) } /** * This method enables the following syntax: * *
     * map must contain value (1)
     *                    ^
     * 
*/ def value(expectedValue: V) { // if (left.values.contains(expectedValue) != mustBeTrue) CHANGING FOR 2.8.0 RC1 if (left.exists(expectedValue == _._2) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainValue" else "containedValue", left, expectedValue) ) } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class ResultOfContainWordForJavaMap[K, V, L[_, _] <: java.util.Map[_, _]](left: L[K, V], mustBeTrue: Boolean) extends ResultOfContainWord[L[K, V]](left) { /** * This method enables the following syntax (javaMap is a java.util.Map): * *
     * javaMap must contain key ("two")
     *                        ^
     * 
*/ def key(expectedKey: K) { if (left.containsKey(expectedKey) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainKey" else "containedKey", left, expectedKey) ) } /** * This method enables the following syntax (javaMap is a java.util.Map): * *
     * javaMap must contain value ("2")
     *                        ^
     * 
*/ def value(expectedValue: V) { if (left.containsValue(expectedValue) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainValue" else "containedValue", left, expectedValue) ) } /** * This method enables the following syntax (positiveNumberKey is a AMatcher): * *
     * javaMap must contain a positiveNumberKey
     *                        ^
     * 
*/ def a(aMatcher: AMatcher[(K, V)]) { val leftWrapper = new JavaMapWrapper(left.asInstanceOf[java.util.Map[K, V]]) leftWrapper.find(e => aMatcher(e).matches) match { case Some(e) => if (!mustBeTrue) { val result = aMatcher(e) throw newTestFailedException(FailureMessages("containedA", leftWrapper, UnquotedString(aMatcher.nounName), UnquotedString(result.negatedFailureMessage))) } case None => if (mustBeTrue) throw newTestFailedException(FailureMessages("didNotContainA", leftWrapper, UnquotedString(aMatcher.nounName))) } } /** * This method enables the following syntax (oddNumberKey is a AnMatcher): * *
     * javaMap must contain an oddNumberKey
     *                        ^
     * 
*/ def an(anMatcher: AnMatcher[(K, V)]) { val leftWrapper = new JavaMapWrapper(left.asInstanceOf[java.util.Map[K, V]]) leftWrapper.find(e => anMatcher(e).matches) match { case Some(e) => if (!mustBeTrue) { val result = anMatcher(e) throw newTestFailedException(FailureMessages("containedAn", leftWrapper, UnquotedString(anMatcher.nounName), UnquotedString(result.negatedFailureMessage))) } case None => if (mustBeTrue) throw newTestFailedException(FailureMessages("didNotContainAn", leftWrapper, UnquotedString(anMatcher.nounName))) } } } // TODO: I think I'll be able to drop the next three implicit conversions after the enablers for contain are done. /** * This implicit conversion method enables the following syntax (javaColl is a java.util.Collection): * *
   * javaColl must contain ("two")
   * 
* * The (contain ("two")) expression will result in a Matcher[GenTraversable[String]]. This * implicit conversion method will convert that matcher to a Matcher[java.util.Collection[String]]. */ implicit def convertTraversableMatcherToJavaCollectionMatcher[T](traversableMatcher: Matcher[scala.collection.GenTraversable[T]]): Matcher[java.util.Collection[T]] = new Matcher[java.util.Collection[T]] { def apply(left: java.util.Collection[T]): MatchResult = traversableMatcher.apply(new JavaCollectionWrapper(left)) } /** * This implicit conversion method enables the following syntax: * *
   * Array(1, 2) must (not contain (3) and not contain (2))
   * 
* * The (not contain ("two")) expression will result in a Matcher[GenTraversable[String]]. This * implicit conversion method will convert that matcher to a Matcher[Array[String]]. */ implicit def convertTraversableMatcherToArrayMatcher[T](traversableMatcher: Matcher[scala.collection.GenTraversable[T]]): Matcher[Array[T]] = new Matcher[Array[T]] { def apply(left: Array[T]): MatchResult = traversableMatcher.apply(new ArrayWrapper(left)) } /** * This implicit conversion method enables the following syntax (javaMap is a java.util.Map): * *
   * javaMap must (contain key ("two"))
   * 
* * The (contain key ("two")) expression will result in a Matcher[scala.collection.GenMap[String, Any]]. This * implicit conversion method will convert that matcher to a Matcher[java.util.Map[String, Any]]. */ implicit def convertMapMatcherToJavaMapMatcher[K, V](mapMatcher: Matcher[scala.collection.GenMap[K, V]]): Matcher[java.util.Map[K, V]] = new Matcher[java.util.Map[K, V]] { def apply(left: java.util.Map[K, V]): MatchResult = mapMatcher.apply(new JavaMapWrapper(left)) } // Ack. The above conversion doesn't apply to java.util.Maps, because java.util.Map is not a subinterface // of java.util.Collection. But right now Matcher[Traversable] supports only "contain" and "have size" // syntax, and thus that must work on Java maps too, why not. Well I'll tell you why not. It is too complicated. // Since java Map is not a java Collection, I'll say the contain syntax doesn't work on it. But you can say // have key. // The getLength and getSize field conversions seem inconsistent with // what I do in symbol HavePropertyMatchers. It isn't, though because the difference is here // it's a Scala field and there a Java field: a val getLength is a // perfectly valid Scala way to get a JavaBean property Java method in the bytecodes. // This guy is generally done through an implicit conversion from a symbol. It takes that symbol, and // then represents an object with an apply method. So it gives an apply method to symbols. // book must have ('author ("Gibson")) // ^ // Basically this 'author symbol gets converted into this class, and its apply method takes "Gibson" // TODO, put the documentation of the details of the algo for selecting a method or field to use here. /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * *

* This class is used as the result of an implicit conversion from class Symbol, to enable symbols to be * used in have ('author ("Dickens")) syntax. The name of the implicit conversion method is * convertSymbolToHavePropertyMatcherGenerator. *

* *

* Class HavePropertyMatcherGenerator's primary constructor takes a Symbol. The * apply method uses reflection to find and access a property that has the name specified by the * Symbol passed to the constructor, so it can determine if the property has the expected value * passed to apply. * If the symbol passed is 'title, for example, the apply method * will use reflection to look for a public Java field named * "title", a public method named "title", or a public method named "getTitle". * If a method, it must take no parameters. If multiple candidates are found, * the apply method will select based on the following algorithm: *

* * * * * * * * * * * *
FieldMethod"get" MethodResult
   Throws TestFailedException, because no candidates found
  getTitle()Invokes getTitle()
 title() Invokes title()
 title()getTitle()Invokes title() (this can occur when BeanProperty annotation is used)
title  Accesses field title
title getTitle()Invokes getTitle()
titletitle() Invokes title()
titletitle()getTitle()Invokes title() (this can occur when BeanProperty annotation is used)
* * * @author Bill Venners */ final class HavePropertyMatcherGenerator(symbol: Symbol) { /** * This method enables the following syntax: * *
     * book must have ('title ("A Tale of Two Cities"))
     *                          ^
     * 
* *

* This class has an apply method that will produce a HavePropertyMatcher[AnyRef, Any]. * The implicit conversion method, convertSymbolToHavePropertyMatcherGenerator, will cause the * above line of code to be eventually transformed into: *

* *
     * book must have (convertSymbolToHavePropertyMatcherGenerator('title).apply("A Tale of Two Cities"))
     * 
*/ def apply(expectedValue: Any): HavePropertyMatcher[AnyRef, Any] = new HavePropertyMatcher[AnyRef, Any] { /** * This method enables the following syntax: * *
         * book must have ('title ("A Tale of Two Cities"))
         * 
* *

* This method uses reflection to discover a field or method with a name that indicates it represents * the value of the property with the name contained in the Symbol passed to the * HavePropertyMatcherGenerator's constructor. The field or method must be public. To be a * candidate, a field must have the name symbol.name, so if symbol is 'title, * the field name sought will be "title". To be a candidate, a method must either have the name * symbol.name, or have a JavaBean-style get or is. If the type of the * passed expectedValue is Boolean, "is" is prepended, else "get" * is prepended. Thus if 'title is passed as symbol, and the type of the expectedValue is * String, a method named getTitle will be considered a candidate (the return type * of getTitle will not be checked, so it need not be String. By contrast, if 'defined * is passed as symbol, and the type of the expectedValue is Boolean, a method * named isTitle will be considered a candidate so long as its return type is Boolean. *

* TODO continue the story */ def apply(objectWithProperty: AnyRef): HavePropertyMatchResult[Any] = { // If 'empty passed, propertyName would be "empty" val propertyName = symbol.name val isBooleanProperty = expectedValue match { case o: Boolean => true case _ => false } accessProperty(objectWithProperty, symbol, isBooleanProperty) match { case None => // if propertyName is '>, mangledPropertyName would be "$greater" val mangledPropertyName = transformOperatorChars(propertyName) // methodNameToInvoke would also be "title" val methodNameToInvoke = mangledPropertyName // methodNameToInvokeWithGet would be "getTitle" val methodNameToInvokeWithGet = "get"+ mangledPropertyName(0).toUpper + mangledPropertyName.substring(1) throw newTestFailedException(Resources("propertyNotFound", methodNameToInvoke, expectedValue.toString, methodNameToInvokeWithGet)) case Some(result) => new HavePropertyMatchResult[Any]( result == expectedValue, propertyName, expectedValue, result ) } } } } /** * This implicit conversion method converts a Symbol to a * HavePropertyMatcherGenerator, to enable the symbol to be used with the have ('author ("Dickens")) syntax. */ implicit def convertSymbolToHavePropertyMatcherGenerator(symbol: Symbol): HavePropertyMatcherGenerator = new HavePropertyMatcherGenerator(symbol) /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ class ResultOfBeWordForAny[T](left: T, mustBeTrue: Boolean) { /** * This method enables the following syntax (positiveNumber is a AMatcher): * *
     * 1 must be a positiveNumber
     *               ^
     * 
*/ def a(aMatcher: AMatcher[T]) { val matcherResult = aMatcher(left) if (matcherResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) matcherResult.failureMessage else matcherResult.negatedFailureMessage ) } } /** * This method enables the following syntax (positiveNumber is a AMatcher): * *
     * 1 must be an oddNumber
     *                ^
     * 
*/ def an(anMatcher: AnMatcher[T]) { val matcherResult = anMatcher(left) if (matcherResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) matcherResult.failureMessage else matcherResult.negatedFailureMessage ) } } /** * This method enables the following syntax: * *
     * result must be theSameInstanceAs anotherObject
     *                  ^
     * 
*/ def theSameInstanceAs(right: AnyRef)(implicit ev: T <:< AnyRef) { if ((left eq right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "wasNotSameInstanceAs" else "wasSameInstanceAs", left, right ) ) } /* * * This method enables the following syntax: * *
     * result must be a [String]
     *                  ^
     * 
def a[EXPECTED : ClassManifest] { val clazz = implicitly[ClassManifest[EXPECTED]].erasure.asInstanceOf[Class[EXPECTED]] if (clazz.isAssignableFrom(left.getClass)) { throw newTestFailedException( if (mustBeTrue) FailureMessages("wasNotAnInstanceOf", left, UnquotedString(clazz.getName)) else FailureMessages("wasAnInstanceOf") ) } } */ /** * This method enables the following syntax: * *
     * fileMock must be a ('file)
     *                    ^
     * 
*/ def a(symbol: Symbol)(implicit ev: T <:< AnyRef) { val matcherResult = matchSymbolToPredicateMethod(left, symbol, true, true) if (matcherResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) matcherResult.failureMessage else matcherResult.negatedFailureMessage ) } } // TODO: Check the mustBeTrues, are they sometimes always false or true? /** * This method enables the following syntax, where badBook is, for example, of type Book and * goodRead refers to a BePropertyMatcher[Book]: * *
     * badBook must be a (goodRead)
     *                   ^
     * 
*/ def a(bePropertyMatcher: BePropertyMatcher[T])(implicit ev: T <:< AnyRef) { // TODO: Try expanding this to 2.10 AnyVals val result = bePropertyMatcher(left) if (result.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) FailureMessages("wasNotA", left, UnquotedString(result.propertyName)) else FailureMessages("wasA", left, UnquotedString(result.propertyName)) ) } } // TODO, in both of these, the failure message doesn't have a/an /** * This method enables the following syntax: * *
     * fruit must be an ('orange)
     *                 ^
     * 
*/ def an(symbol: Symbol)(implicit ev: T <:< AnyRef) { val matcherResult = matchSymbolToPredicateMethod(left, symbol, true, false) if (matcherResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) matcherResult.failureMessage else matcherResult.negatedFailureMessage ) } } /** * This method enables the following syntax, where badBook is, for example, of type Book and * excellentRead refers to a BePropertyMatcher[Book]: * *
     * book must be an (excellentRead)
     *                ^
     * 
*/ def an(beTrueMatcher: BePropertyMatcher[T])(implicit ev: T <:< AnyRef) { // TODO: Try expanding this to 2.10 AnyVals val beTrueMatchResult = beTrueMatcher(left) if (beTrueMatchResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) FailureMessages("wasNotAn", left, UnquotedString(beTrueMatchResult.propertyName)) else FailureMessages("wasAn", left, UnquotedString(beTrueMatchResult.propertyName)) ) } } /** * This method enables the following syntax, where fraction is, for example, of type PartialFunction: * *
     * fraction must be definedAt (6)
     *                    ^
     * 
*/ def definedAt[U](right: U)(implicit ev: T <:< PartialFunction[U, _]) { if (left.isDefinedAt(right) != mustBeTrue) throw newTestFailedException( if (mustBeTrue) FailureMessages("wasNotDefinedAt", left, right) else FailureMessages("wasDefinedAt", left, right) ) } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class RegexWord { /** * This method enables the following syntax: * *
     * "eight" must not fullyMatch regex ("""(-)?(\d+)(\.\d*)?""".r)
     *                                     ^
     * 
*/ def apply(regexString: String): ResultOfRegexWordApplication = new ResultOfRegexWordApplication(regexString, IndexedSeq.empty) /** * This method enables the following syntax: * *
     * "eight" must not fullyMatch regex ("""(-)?(\d+)(\.\d*)?""")
     *                                     ^
     * 
*/ def apply(regex: Regex): ResultOfRegexWordApplication = new ResultOfRegexWordApplication(regex, IndexedSeq.empty) /** * This method enables the following syntax: * *
     * string must not fullyMatch regex ("a(b*)c" withGroup "bb") 
     *                                    ^
     * 
*/ def apply(regexWithGroups: RegexWithGroups) = new ResultOfRegexWordApplication(regexWithGroups.regex, regexWithGroups.groups) } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class ResultOfIncludeWordForString(left: String, mustBeTrue: Boolean) { /** * This method enables the following syntax: * *
     * string must include regex ("world")
     *                       ^
     * 
*/ def regex(rightRegexString: String) { regex(rightRegexString.r) } /** * This method enables the following syntax: * *
     * string must include regex ("a(b*)c" withGroup "bb")
     *                       ^
     * 
*/ def regex(regexWithGroups: RegexWithGroups) { val result = includeRegexWithGroups(left, regexWithGroups.regex, regexWithGroups.groups) if (result.matches != mustBeTrue) throw newTestFailedException( if (mustBeTrue) result.failureMessage else result.negatedFailureMessage ) } /** * This method enables the following syntax: * *
     * string must include regex ("wo.ld".r)
     *                       ^
     * 
*/ def regex(rightRegex: Regex) { if (rightRegex.findFirstIn(left).isDefined != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotIncludeRegex" else "includedRegex", left, rightRegex ) ) } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class ResultOfStartWithWordForString(left: String, mustBeTrue: Boolean) { /** * This method enables the following syntax: * *
     * string must startWith regex ("Hel*o")
     *                         ^
     * 
*/ def regex(rightRegexString: String) { regex(rightRegexString.r) } /** * This method enables the following syntax: * *
     * string must startWith regex ("a(b*)c" withGroup "bb")
     *                         ^
     * 
*/ def regex(regexWithGroups: RegexWithGroups) { val result = startWithRegexWithGroups(left, regexWithGroups.regex, regexWithGroups.groups) if (result.matches != mustBeTrue) throw newTestFailedException( if (mustBeTrue) result.failureMessage else result.negatedFailureMessage ) } /** * This method enables the following syntax: * *
     * string must startWith regex ("Hel*o".r)
     *                         ^
     * 
*/ def regex(rightRegex: Regex) { if (rightRegex.pattern.matcher(left).lookingAt != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotStartWithRegex" else "startedWithRegex", left, rightRegex ) ) } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class ResultOfEndWithWordForString(left: String, mustBeTrue: Boolean) { /** * This method enables the following syntax: * *
     * string must endWith regex ("wor.d")
     *                       ^
     * 
*/ def regex(rightRegexString: String) { regex(rightRegexString.r) } /** * This method enables the following syntax: * *
     * string must endWith regex ("a(b*)c" withGroup "bb")
     *                       ^
     * 
*/ def regex(regexWithGroups: RegexWithGroups) { val result = endWithRegexWithGroups(left, regexWithGroups.regex, regexWithGroups.groups) if (result.matches != mustBeTrue) throw newTestFailedException( if (mustBeTrue) result.failureMessage else result.negatedFailureMessage ) } /** * This method enables the following syntax: * *
     * string must endWith regex ("wor.d".r)
     *                       ^
     * 
*/ def regex(rightRegex: Regex) { val allMatches = rightRegex.findAllIn(left) if ((allMatches.hasNext && (allMatches.end == left.length)) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotEndWithRegex" else "endedWithRegex", left, rightRegex ) ) } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class ResultOfFullyMatchWordForString(left: String, mustBeTrue: Boolean) { /** * This method enables the following syntax: * *
     * string must fullMatch regex ("Hel*o world")
     *                         ^
     * 
*/ def regex(rightRegexString: String) { regex(rightRegexString.r) } /** * This method enables the following syntax: * *
     * string must fullMatch regex ("a(b*)c" withGroup "bb") 
     *                         ^
     * 
*/ def regex(regexWithGroups: RegexWithGroups) { val result = fullyMatchRegexWithGroups(left, regexWithGroups.regex, regexWithGroups.groups) if (result.matches != mustBeTrue) throw newTestFailedException( if (mustBeTrue) result.failureMessage else result.negatedFailureMessage ) } /** * This method enables the following syntax: * *
     * string must fullymatch regex ("Hel*o world".r)
     *                          ^
     * 
*/ def regex(rightRegex: Regex) { if (rightRegex.pattern.matcher(left).matches != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotFullyMatchRegex" else "fullyMatchedRegex", left, rightRegex ) ) } } // Going back to original, legacy one to get to a good place to check in. /* def equal(right: Any): Matcher[Any] = new Matcher[Any] { def apply(left: Any): MatchResult = { val (leftee, rightee) = Suite.getObjectsForFailureMessage(left, right) MatchResult( areEqualComparingArraysStructurally(left, right), FailureMessages("didNotEqual", leftee, rightee), FailureMessages("equaled", left, right) ) } } */ /** * This method enables syntax such as the following: * *
   * result must equal (100 +- 1)
   *               ^
   * 
*/ def equal[T](interval: Interval[T]): Matcher[T] = { new Matcher[T] { def apply(left: T): MatchResult = { MatchResult( interval.isWithin(left), FailureMessages("didNotEqualPlusOrMinus", left, interval.pivot, interval.tolerance), FailureMessages("equaledPlusOrMinus", left, interval.pivot, interval.tolerance) ) } } } /** * This method enables syntax such as the following: * *
   * result must equal (null)
   *        ^
   * 
*/ def equal(o: Null): Matcher[AnyRef] = new Matcher[AnyRef] { def apply(left: AnyRef): MatchResult = { MatchResult( left == null, FailureMessages("didNotEqualNull", left), FailureMessages("equaledNull"), FailureMessages("didNotEqualNull", left), FailureMessages("midSentenceEqualedNull") ) } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class ResultOfElementWordApplication[T](val expectedElement: T) /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class KeyWord { /** * This method enables the following syntax: * *
     * map must not contain key (10)
     *                            ^
     * 
*/ def apply[T](expectedKey: T): ResultOfKeyWordApplication[T] = new ResultOfKeyWordApplication(expectedKey) } /** * This field enables the following syntax: * *
   * map must not contain key (10)
   *                        ^
   * 
*/ val key = new KeyWord /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class ValueWord { /** * This method enables the following syntax: * *
     * map must not contain value (10)
     *                              ^
     * 
*/ def apply[T](expectedValue: T): ResultOfValueWordApplication[T] = new ResultOfValueWordApplication(expectedValue) } /** * This field enables the following syntax: * *
   * map must not contain value (10)
   *                        ^
   * 
*/ val value = new ValueWord /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class AWord { /** * This method enables the following syntax: * *
     * badBook must not be a ('goodRead)
     *                         ^
     * 
*/ def apply(symbol: Symbol): ResultOfAWordToSymbolApplication = new ResultOfAWordToSymbolApplication(symbol) /** * This method enables the following syntax, where, for example, badBook is of type Book and goodRead * is a BePropertyMatcher[Book]: * *
     * badBook must not be a (goodRead)
     *                         ^
     * 
*/ def apply[T](beTrueMatcher: BePropertyMatcher[T]): ResultOfAWordToBePropertyMatcherApplication[T] = new ResultOfAWordToBePropertyMatcherApplication(beTrueMatcher) /** * This method enables the following syntax, where, positiveNumber is an AMatcher[Book]: * *
     * result must not be a (positiveNumber)
     *                        ^
     * 
*/ def apply[T](aMatcher: AMatcher[T]): ResultOfAWordToAMatcherApplication[T] = new ResultOfAWordToAMatcherApplication(aMatcher) } /** * This field enables the following syntax: * *
   * badBook must not be a ('goodRead)
   *                       ^
   * 
*/ val a = new AWord /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class AnWord { /** * This method enables the following syntax: * *
     * badBook must not be an ('excellentRead)
     *                          ^
     * 
*/ def apply(symbol: Symbol): ResultOfAnWordToSymbolApplication = new ResultOfAnWordToSymbolApplication(symbol) /** * This method enables the following syntax, where, for example, badBook is of type Book and excellentRead * is a BePropertyMatcher[Book]: * *
     * badBook must not be an (excellentRead)
     *                          ^
     * 
*/ def apply[T](beTrueMatcher: BePropertyMatcher[T]): ResultOfAnWordToBePropertyMatcherApplication[T] = new ResultOfAnWordToBePropertyMatcherApplication(beTrueMatcher) /** * This method enables the following syntax, where, positiveNumber is an AnMatcher[Book]: * *
     * result must not be an (positiveNumber)
     *                         ^
     * 
*/ def apply[T](anMatcher: AnMatcher[T]): ResultOfAnWordToAnMatcherApplication[T] = new ResultOfAnWordToAnMatcherApplication(anMatcher) } /** * This field enables the following syntax: * *
   * badBook must not be an (excellentRead)
   *                       ^
   * 
*/ val an = new AnWord /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class TheSameInstanceAsPhrase { /** * This method enables the following syntax: * *
     * oneString must not be theSameInstanceAs (anotherString)
     *                                           ^
     * 
*/ def apply(anyRef: AnyRef): ResultOfTheSameInstanceAsApplication = new ResultOfTheSameInstanceAsApplication(anyRef) } /** * This field enables the following syntax: * *
   * oneString must not be theSameInstanceAs (anotherString)
   *                         ^
   * 
*/ val theSameInstanceAs: TheSameInstanceAsPhrase = new TheSameInstanceAsPhrase /** * This field enables the following syntax: * *
   * "eight" must not fullyMatch regex ("""(-)?(\d+)(\.\d*)?""".r)
   *                               ^
   * 
*/ val regex = new RegexWord /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class ResultOfHaveWordForExtent[A](left: A, mustBeTrue: Boolean) { /** * This method enables the following syntax: * *
     * obj must have length (2L)
     *                      ^
     * 
* *

* This method is ultimately invoked for objects that have a length property structure * of type Long, * but is of a type that is not handled by implicit conversions from nominal types such as * scala.Seq, java.lang.String, and java.util.List. *

*/ def length(expectedLength: Long)(implicit len: Length[A]) { val leftLength = len.lengthOf(left) if ((leftLength == expectedLength) != mustBeTrue) throw newTestFailedException( if (mustBeTrue) FailureMessages("hadLengthInsteadOfExpectedLength", left, leftLength, expectedLength) else FailureMessages("hadExpectedLength", left, expectedLength) ) } /** * This method enables the following syntax: * *
     * obj must have size (2L)
     *                 ^
     * 
* *

* This method is ultimately invoked for objects that have a size property structure * of type Long, * but is of a type that is not handled by implicit conversions from nominal types such as * Traversable and java.util.Collection. *

*/ def size(expectedSize: Long)(implicit sz: Size[A]) { val leftSize = sz.sizeOf(left) if ((leftSize == expectedSize) != mustBeTrue) throw newTestFailedException( if (mustBeTrue) FailureMessages("hadSizeInsteadOfExpectedSize", left, leftSize, expectedSize) else FailureMessages("hadExpectedSize", left, expectedSize) ) } } /** * This method enables the following syntax: * *
   * num must (not be < (10) and not be > (17))
   *                    ^
   * 
*/ def <[T <% Ordered[T]] (right: T): ResultOfLessThanComparison[T] = new ResultOfLessThanComparison(right) /** * This method enables the following syntax: * *
   * num must (not be > (10) and not be < (7))
   *                    ^
   * 
*/ def >[T <% Ordered[T]] (right: T): ResultOfGreaterThanComparison[T] = new ResultOfGreaterThanComparison(right) /** * This method enables the following syntax: * *
   * num must (not be <= (10) and not be > (17))
   *                    ^
   * 
*/ def <=[T <% Ordered[T]] (right: T): ResultOfLessThanOrEqualToComparison[T] = new ResultOfLessThanOrEqualToComparison(right) /** * This method enables the following syntax: * *
   * num must (not be >= (10) and not be < (7))
   *                    ^
   * 
*/ def >=[T <% Ordered[T]] (right: T): ResultOfGreaterThanOrEqualToComparison[T] = new ResultOfGreaterThanOrEqualToComparison(right) /** * This method enables the following syntax: * *
   * list must (not be definedAt (7) and not be definedAt (9))
   *                     ^
   * 
*/ def definedAt[T](right: T): ResultOfDefinedAt[T] = new ResultOfDefinedAt(right) /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class ResultOfEvaluatingApplication(val fun: () => Any) { /** * This method enables syntax such as the following: * *
     * evaluating { "hi".charAt(-1) } must produce [StringIndexOutOfBoundsException]
     *                                ^
     * 
*/ def must[T](resultOfProduceApplication: ResultOfProduceInvocation[T]): T = { val clazz = resultOfProduceApplication.clazz val caught = try { fun() None } catch { case u: Throwable => { if (!clazz.isAssignableFrom(u.getClass)) { val s = Resources("wrongException", clazz.getName, u.getClass.getName) throw newTestFailedException(s, Some(u)) // throw new TestFailedException(s, u, 3) } else { Some(u) } } } caught match { case None => val message = Resources("exceptionExpected", clazz.getName) throw newTestFailedException(message) // throw new TestFailedException(message, 3) case Some(e) => e.asInstanceOf[T] // I know this cast will succeed, becuase isAssignableFrom succeeded above } } } /** * This method enables syntax such as the following: * *
   * evaluating { "hi".charAt(-1) } must produce [StringIndexOutOfBoundsException]
   * ^
   * 
*/ def evaluating(fun: => Any): ResultOfEvaluatingApplication = new ResultOfEvaluatingApplication(fun _) /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class ResultOfProduceInvocation[T](val clazz: Class[T]) /** * This method enables the following syntax: * *
   * evaluating { "hi".charAt(-1) } must produce [StringIndexOutOfBoundsException]
   * ^
   * 
*/ def produce[T](implicit manifest: Manifest[T]): ResultOfProduceInvocation[T] = new ResultOfProduceInvocation(manifest.erasure.asInstanceOf[Class[T]]) trait ContainMethods[T] { val left: scala.collection.GenTraversable[T] val mustBeTrue: Boolean /** * This method enables the following syntax (positiveNumber is a AMatcher): * *
     * traversable must contain a positiveNumber
     *                            ^
     * 
*/ def a(aMatcher: AMatcher[T]) { left.find(aMatcher(_).matches) match { case Some(e) => if (!mustBeTrue) { val result = aMatcher(e) throw newTestFailedException(FailureMessages("containedA", left, UnquotedString(aMatcher.nounName), UnquotedString(result.negatedFailureMessage))) } case None => if (mustBeTrue) throw newTestFailedException(FailureMessages("didNotContainA", left, UnquotedString(aMatcher.nounName))) } } /** * This method enables the following syntax (oddNumber is a AMatcher): * *
     * traversable must contain an oddNumber
     *                            ^
     * 
*/ def an(anMatcher: AnMatcher[T]) { left.find(anMatcher(_).matches) match { case Some(e) => if (!mustBeTrue) { val result = anMatcher(e) throw newTestFailedException(FailureMessages("containedAn", left, UnquotedString(anMatcher.nounName), UnquotedString(result.negatedFailureMessage))) } case None => if (mustBeTrue) throw newTestFailedException(FailureMessages("didNotContainAn", left, UnquotedString(anMatcher.nounName))) } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ class ResultOfContainWordForTraversable[E, L[_] <: scala.collection.GenTraversable[_]](val left: scala.collection.GenTraversable[E], val mustBeTrue: Boolean = true) extends ResultOfContainWord[L[E]](left.asInstanceOf[L[E]]) with ContainMethods[E] class ResultOfContainWordForArray[E](val left: Array[E], val mustBeTrue: Boolean = true) extends ResultOfContainWord[Array[E]](left) { /** * This method enables the following syntax (positiveNumber is a AMatcher): * *
     * traversable must contain a positiveNumber
     *                            ^
     * 
*/ def a(aMatcher: AMatcher[E]) { left.find(aMatcher(_).matches) match { case Some(e) => if (!mustBeTrue) { val result = aMatcher(e) throw newTestFailedException(FailureMessages("containedA", left, UnquotedString(aMatcher.nounName), UnquotedString(result.negatedFailureMessage))) } case None => if (mustBeTrue) throw newTestFailedException(FailureMessages("didNotContainA", left, UnquotedString(aMatcher.nounName))) } } /** * This method enables the following syntax (oddNumber is a AMatcher): * *
     * traversable must contain an oddNumber
     *                            ^
     * 
*/ def an(anMatcher: AnMatcher[E]) { left.find(anMatcher(_).matches) match { case Some(e) => if (!mustBeTrue) { val result = anMatcher(e) throw newTestFailedException(FailureMessages("containedAn", left, UnquotedString(anMatcher.nounName), UnquotedString(result.negatedFailureMessage))) } case None => if (mustBeTrue) throw newTestFailedException(FailureMessages("didNotContainAn", left, UnquotedString(anMatcher.nounName))) } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class ResultOfContainWordForJavaCollection[E, L[_] <: java.util.Collection[_]](left: L[E], mustBeTrue: Boolean) extends ResultOfContainWord[L[E]](left) { /** * This method enables the following syntax (positiveNumber is a AMatcher): * *
     * javaCol must contain a positiveNumber
     *                        ^
     * 
*/ def a(aMatcher: AMatcher[E]) { val leftWrapper = new JavaCollectionWrapper(left.asInstanceOf[java.util.Collection[E]]) leftWrapper.find(e => aMatcher(e).matches) match { case Some(e) => if (!mustBeTrue) { val result = aMatcher(e) throw newTestFailedException(FailureMessages("containedA", leftWrapper, UnquotedString(aMatcher.nounName), UnquotedString(result.negatedFailureMessage))) } case None => if (mustBeTrue) throw newTestFailedException(FailureMessages("didNotContainA", leftWrapper, UnquotedString(aMatcher.nounName))) } } /** * This method enables the following syntax (oddNumber is a AnMatcher): * *
     * javaCol must contain an oddNumber
     *                        ^
     * 
*/ def an(anMatcher: AnMatcher[E]) { val leftWrapper = new JavaCollectionWrapper(left.asInstanceOf[java.util.Collection[E]]) leftWrapper.find(e => anMatcher(e).matches) match { case Some(e) => if (!mustBeTrue) { val result = anMatcher(e) throw newTestFailedException(FailureMessages("containedAn", leftWrapper, UnquotedString(anMatcher.nounName), UnquotedString(result.negatedFailureMessage))) } case None => if (mustBeTrue) throw newTestFailedException(FailureMessages("didNotContainAn", leftWrapper, UnquotedString(anMatcher.nounName))) } } } /** * This method enables the following syntax: * *
   * List(1, 2, 3) must contain (oneOf(1, 2))
   *                               ^
   * 
*/ def oneOf(xs: Any*) = new ResultOfOneOfApplication(xs) /** * This method enables the following syntax: * *
   * List(1, 2, 3) must contain (atLeastOneOf(1, 2))
   *                               ^
   * 
*/ def atLeastOneOf(xs: Any*) = new ResultOfAtLeastOneOfApplication(xs) /** * This method enables the following syntax: * *
   * List(1, 2, 3) must contain (noneOf(1, 2))
   *                               ^
   * 
*/ def noneOf(xs: Any*) = new ResultOfNoneOfApplication(xs) /** * This method enables the following syntax: * *
   * List(1, 2, 3) must contain (theSameElementsAs(1, 2))
   *                               ^
   * 
*/ def theSameElementsAs(xs: GenTraversable[_]) = new ResultOfTheSameElementsAsApplication(xs) /** * This method enables the following syntax: * *
   * List(1, 2, 3) must contain (theSameElementsInOrderAs(1, 2))
   *                               ^
   * 
*/ def theSameElementsInOrderAs(xs: GenTraversable[_]) = new ResultOfTheSameElementsInOrderAsApplication(xs) /** * This method enables the following syntax: * *
   * List(1, 2, 3) must contain (only(1, 2))
   *                               ^
   * 
*/ def only(xs: Any*) = new ResultOfOnlyApplication(xs) /** * This method enables the following syntax: * *
   * List(1, 2, 3) must contain (inOrderOnly(1, 2))
   *                               ^
   * 
*/ def inOrderOnly[T](xs: Any*) = new ResultOfInOrderOnlyApplication(xs) /** * This method enables the following syntax: * *
   * List(1, 2, 3) must contain (allOf(1, 2))
   *                               ^
   * 
*/ def allOf(xs: Any*) = new ResultOfAllOfApplication(xs) /** * This method enables the following syntax: * *
   * List(1, 2, 3) must contain (inOrder(1, 2))
   *                               ^
   * 
*/ def inOrder(xs: Any*) = new ResultOfInOrderApplication(xs) // For safe keeping private implicit def nodeToCanonical(node: scala.xml.Node) = new Canonicalizer(node) private class Canonicalizer(node: scala.xml.Node) { def toCanonical: scala.xml.Node = { node match { case elem: scala.xml.Elem => val canonicalizedChildren = for (child <- node.child if !child.toString.trim.isEmpty) yield { child match { case elem: scala.xml.Elem => elem.toCanonical case other => other } } new scala.xml.Elem(elem.prefix, elem.label, elem.attributes, elem.scope, canonicalizedChildren: _*) case other => other } } } /* class AType[T : ClassManifest] { private val clazz = implicitly[ClassManifest[T]].erasure.asInstanceOf[Class[T]] def isAssignableFromClassOf(o: Any): Boolean = clazz.isAssignableFrom(o.getClass) def className: String = clazz.getName } def a[T : ClassManifest]: AType[T] = new AType[T] */ // This is where InspectorShorthands started private sealed trait Collected private case object AllCollected extends Collected private case object EveryCollected extends Collected private case class BetweenCollected(from: Int, to: Int) extends Collected private case class AtLeastCollected(num: Int) extends Collected private case class AtMostCollected(num: Int) extends Collected private case object NoCollected extends Collected private case class ExactlyCollected(num: Int) extends Collected import InspectorsHelper._ def doCollected[T](collected: Collected, xs: scala.collection.GenTraversable[T], methodName: String, stackDepth: Int)(fun: T => Unit) { collected match { case AllCollected => doForAll(xs, "allShorthandFailed", "Matchers.scala", methodName, stackDepth) { e => fun(e) } case AtLeastCollected(num) => doForAtLeast(num, xs, "atLeastShorthandFailed", "Matchers.scala", methodName, stackDepth) { e => fun(e) } case EveryCollected => doForEvery(xs, "everyShorthandFailed", "Matchers.scala", methodName, stackDepth) { e => fun(e) } case ExactlyCollected(num) => doForExactly(num, xs, "exactlyShorthandFailed", "Matchers.scala", methodName, stackDepth) { e => fun(e) } case NoCollected => doForNo(xs, "noShorthandFailed", "Matchers.scala", methodName, stackDepth) { e => fun(e) } case BetweenCollected(from, to) => doForBetween(from, to, xs, "betweenShorthandFailed", "Matchers.scala", methodName, stackDepth) { e => fun(e) } case AtMostCollected(num) => doForAtMost(num, xs, "atMostShorthandFailed", "Matchers.scala", methodName, stackDepth) { e => fun(e) } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ sealed class ResultOfNotWordForCollectedAny[T](collected: Collected, xs: scala.collection.GenTraversable[T], mustBeTrue: Boolean) { import org.scalatest.InspectorsHelper._ /** * This method enables the following syntax: * *
     * all(xs) must not equal (7)
     *                    ^
     * 
*/ def equal(right: Any)(implicit equality: Equality[T]) { doCollected(collected, xs, "equal", 1) { e => if ((equality.areEqual(e, right)) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotEqual" else "equaled", e, right ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all(xs) must not be (7)
     *                    ^
     * 
*/ def be(right: Any) { doCollected(collected, xs, "be", 1) { e => if ((e == right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "wasNotEqualTo" else "wasEqualTo", e, right ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all(xs) must not be <= (7)
     *                    ^
     * 
*/ def be(comparison: ResultOfLessThanOrEqualToComparison[T]) { doCollected(collected, xs, "be", 1) { e => if (comparison(e) != mustBeTrue) { throw newTestFailedException( FailureMessages( if (mustBeTrue) "wasNotLessThanOrEqualTo" else "wasLessThanOrEqualTo", e, comparison.right ), None, 6 ) } } } /** * This method enables the following syntax: * *
     * all(xs) must not be >= (7)
     *                    ^
     * 
*/ def be(comparison: ResultOfGreaterThanOrEqualToComparison[T]) { doCollected(collected, xs, "be", 1) { e => if (comparison(e) != mustBeTrue) { throw newTestFailedException( FailureMessages( if (mustBeTrue) "wasNotGreaterThanOrEqualTo" else "wasGreaterThanOrEqualTo", e, comparison.right ), None, 6 ) } } } /** * This method enables the following syntax: * *
     * all(xs) must not be < (7)
     *                    ^
     * 
*/ def be(comparison: ResultOfLessThanComparison[T]) { doCollected(collected, xs, "be", 1) { e => if (comparison(e) != mustBeTrue) { throw newTestFailedException( FailureMessages( if (mustBeTrue) "wasNotLessThan" else "wasLessThan", e, comparison.right ), None, 6 ) } } } /** * This method enables the following syntax: * *
     * all(xs) must not be > (7)
     *                    ^
     * 
*/ def be(comparison: ResultOfGreaterThanComparison[T]) { doCollected(collected, xs, "be", 1) { e => if (comparison(e) != mustBeTrue) { throw newTestFailedException( FailureMessages( if (mustBeTrue) "wasNotGreaterThan" else "wasGreaterThan", e, comparison.right ), None, 6 ) } } } /** * * The must be === syntax has been deprecated and will be removed in a future version of ScalaTest. Please use must equal, must ===, mustEqual, * must be, or mustBe instead. Note, the reason this was deprecated was so that === would mean only one thing in ScalaTest: a customizable, type- * checkable equality comparison. * * * This method enables the following syntax: * *
     * all(xs) must not be === (7)
     *                    ^
     * 
*/ @deprecated("The must be === syntax has been deprecated. Please use must equal, must ===, mustEqual, must be, or mustBe instead.") def be(comparison: TripleEqualsInvocation[_]) { doCollected(collected, xs, "be", 1) { e => if ((e == comparison.right) != mustBeTrue) { throw newTestFailedException( FailureMessages( if (mustBeTrue) "wasNotEqualTo" else "wasEqualTo", e, comparison.right ), None, 6 ) } } } /** * This method enables the following syntax, where odd refers to * a BeMatcher[Int]: * *
     * all(xs) must not be (odd)
     *                    ^
     * 
*/ def be(beMatcher: BeMatcher[T]) { doCollected(collected, xs, "be", 1) { e => val result = beMatcher(e) if (result.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) result.failureMessage else result.negatedFailureMessage, None, 10 ) } } } /** * This method enables the following syntax, where stack is, for example, of type Stack and * empty refers to a BePropertyMatcher[Stack]: * *
     * all(xs) must not be (empty)
     *                    ^
     * 
*/ def be(bePropertyMatcher: BePropertyMatcher[T]) { doCollected(collected, xs, "be", 1) { e => val result = bePropertyMatcher(e) if (result.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) FailureMessages("wasNot", e, UnquotedString(result.propertyName)) else FailureMessages("was", e, UnquotedString(result.propertyName)), None, 6 ) } } } /** * This method enables the following syntax, where notFileMock is, for example, of type File and * file refers to a BePropertyMatcher[File]: * *
     * all(xs) must not be a (file)
     *                    ^
     * 
*/ def be[U >: T](resultOfAWordApplication: ResultOfAWordToBePropertyMatcherApplication[U]) { doCollected(collected, xs, "be", 1) { e => val result = resultOfAWordApplication.bePropertyMatcher(e) if (result.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) FailureMessages("wasNotA", e, UnquotedString(result.propertyName)) else FailureMessages("wasA", e, UnquotedString(result.propertyName)), None, 6 ) } } } /** * This method enables the following syntax, where keyEvent is, for example, of type KeyEvent and * actionKey refers to a BePropertyMatcher[KeyEvent]: * *
     * all(keyEvents) must not be an (actionKey)
     *                           ^
     * 
*/ def be[U >: T](resultOfAnWordApplication: ResultOfAnWordToBePropertyMatcherApplication[U]) { doCollected(collected, xs, "be", 1) { e => val result = resultOfAnWordApplication.bePropertyMatcher(e) if (result.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) FailureMessages("wasNotAn", e, UnquotedString(result.propertyName)) else FailureMessages("wasAn", e, UnquotedString(result.propertyName)), None, 6 ) } } } /** * This method enables the following syntax: * *
     * all(xs) must not be theSameInstanceAs (string)
     *                    ^
     * 
*/ def be(resultOfSameInstanceAsApplication: ResultOfTheSameInstanceAsApplication) { doCollected(collected, xs, "be", 1) { e => e match { case ref: AnyRef => if ((resultOfSameInstanceAsApplication.right eq ref) != mustBeTrue) { throw newTestFailedException( FailureMessages( if (mustBeTrue) "wasNotSameInstanceAs" else "wasSameInstanceAs", e, resultOfSameInstanceAsApplication.right ), None, 6 ) } case _ => throw new IllegalArgumentException("theSameInstanceAs must only be used for AnyRef") } } } /** * This method enables the following syntax: * *
     * all(xs) must not be definedAt ("apple")
     *                    ^
     * 
*/ def be[U](resultOfDefinedAt: ResultOfDefinedAt[U])(implicit ev: T <:< PartialFunction[U, _]) { doCollected(collected, xs, "be", 1) { e => if (e.isDefinedAt(resultOfDefinedAt.right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "wasNotDefinedAt" else "wasDefinedAt", e, resultOfDefinedAt.right ), None, 6 ) } } // Any for not TODO: Scaladoc // TODO: Write tests and implement cases for: // have(length (9), title ("hi")) (this one we'll use this have method but add a HavePropertyMatcher* arg) // have(size (9), title ("hi")) (this one we'll use the next have method but add a HavePropertyMatcher* arg) // have(length(9), size (9), title ("hi")) (for this one we'll need a new overloaded have(ROLWA, ROSWA, HPM*)) // have(size(9), length (9), title ("hi")) (for this one we'll need a new overloaded have(ROSWA, ROLWA, HPM*)) def have(resultOfLengthWordApplication: ResultOfLengthWordApplication)(implicit len: Length[T]) { doCollected(collected, xs, "have", 1) { e => val right = resultOfLengthWordApplication.expectedLength val leftLength = len.lengthOf(e) if ((leftLength == right) != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) FailureMessages("hadLengthInsteadOfExpectedLength", e, leftLength, right) else FailureMessages("hadExpectedLength", e, right), None, 6 ) } } } // Any for not TODO: Scaladoc def have(resultOfSizeWordApplication: ResultOfSizeWordApplication)(implicit sz: Size[T]) { doCollected(collected, xs, "have", 1) { e => val right = resultOfSizeWordApplication.expectedSize val leftSize = sz.sizeOf(e) if ((leftSize == right) != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) FailureMessages("hadSizeInsteadOfExpectedSize", e, leftSize, right) else FailureMessages("hadExpectedSize", e, right), None, 6 ) } } } /** * This method enables the following syntax, where badBook is, for example, of type Book and * title ("One Hundred Years of Solitude") results in a HavePropertyMatcher[Book]: * *
     * all(books) must not have (title ("One Hundred Years of Solitude"))
     *                       ^
     * 
*/ def have[U >: T](firstPropertyMatcher: HavePropertyMatcher[U, _], propertyMatchers: HavePropertyMatcher[U, _]*) { doCollected(collected, xs, "have", 1) { e => val results = for (propertyVerifier <- firstPropertyMatcher :: propertyMatchers.toList) yield propertyVerifier(e) val firstFailureOption = results.find(pv => !pv.matches) val justOneProperty = propertyMatchers.length == 0 // if mustBeTrue is false, then it is like "not have ()", and must throw TFE if firstFailureOption.isDefined is false // if mustBeTrue is true, then it is like "not (not have ()), which must behave like have ()", and must throw TFE if firstFailureOption.isDefined is true if (firstFailureOption.isDefined == mustBeTrue) { firstFailureOption match { case Some(firstFailure) => // This is one of these cases, thus will only get here if mustBeTrue is true // 0 0 | 0 | 1 // 0 1 | 0 | 1 // 1 0 | 0 | 1 throw newTestFailedException( FailureMessages( "propertyDidNotHaveExpectedValue", UnquotedString(firstFailure.propertyName), firstFailure.expectedValue, firstFailure.actualValue, e ), None, 6 ) case None => // This is this cases, thus will only get here if mustBeTrue is false // 1 1 | 1 | 0 val failureMessage = if (justOneProperty) { val firstPropertyResult = results.head // know this will succeed, because firstPropertyMatcher was required FailureMessages( "propertyHadExpectedValue", UnquotedString(firstPropertyResult.propertyName), firstPropertyResult.expectedValue, e ) } else FailureMessages("allPropertiesHadExpectedValues", e) throw newTestFailedException(failureMessage, None, 6) } } } } /** * This method enables the following syntax: * *
     * all(xs) must not be (null)
     *                    ^
     * 
*/ def be(o: Null)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "be", 1) { e => if ((e == null) != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) FailureMessages("wasNotNull", e) else FailureMessages("wasNull"), None, 6 ) } } } /** * This method enables the following syntax: * *
     * all(xs) must not be ('empty)
     *                    ^
     * 
*/ def be(symbol: Symbol)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "be", 1) { e => val matcherResult = matchSymbolToPredicateMethod(e, symbol, false, false) if (matcherResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) matcherResult.failureMessage else matcherResult.negatedFailureMessage, None, 6 ) } } } /** * This method enables the following syntax: * *
     * all(xs) must not be a ('file)
     *                    ^
     * 
*/ def be(resultOfAWordApplication: ResultOfAWordToSymbolApplication)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "be", 1) { e => val matcherResult = matchSymbolToPredicateMethod(e, resultOfAWordApplication.symbol, true, true) if (matcherResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) matcherResult.failureMessage else matcherResult.negatedFailureMessage, None, 6 ) } } } /** * This method enables the following syntax: * *
     * all(xs) must not be an ('actionKey)
     *                    ^
     * 
*/ def be(resultOfAnWordApplication: ResultOfAnWordToSymbolApplication)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "be", 1) { e => val matcherResult = matchSymbolToPredicateMethod(e, resultOfAnWordApplication.symbol, true, false) if (matcherResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) matcherResult.failureMessage else matcherResult.negatedFailureMessage, None, 6 ) } } } /** * This method enables the following syntax: * *
     * all(xs) must not be sorted
     *                    ^
     * 
*/ def be(sortedWord: SortedWord)(implicit sortable: Sortable[T]) { doCollected(collected, xs, "be", 1) { e => if (sortable.isSorted(e) != mustBeTrue) { throw newTestFailedException( FailureMessages(if (mustBeTrue) "wasNotSorted" else "wasSorted", e), None, 6 ) } } } /** * This method enables the following syntax: * *
     * all (xs) must not contain ("one")
     *                     ^
     * 
*/ def contain(expectedElement: Any)(implicit containing: Containing[T]) { doCollected(collected, xs, "contain", 1) { e => val right = expectedElement if ((containing.contains(e, right)) != mustBeTrue) { throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainExpectedElement" else "containedExpectedElement", e, right ), None, 6 ) } } } /** * This method enables the following syntax: * *
     * all (xs) must not contain oneOf ("one")
     *                     ^
     * 
*/ def contain(newOneOf: ResultOfOneOfApplication)(implicit containing: Containing[T]) { val right = newOneOf.right doCollected(collected, xs, "contain", 1) { e => if (containing.containsOneOf(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainOneOfElements" else "containedOneOfElements", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all (xs) must not contain atLeastOneOf ("one")
     *                     ^
     * 
*/ def contain(atLeastOneOf: ResultOfAtLeastOneOfApplication)(implicit aggregating: Aggregating[T]) { val right = atLeastOneOf.right doCollected(collected, xs, "contain", 1) { e => if (aggregating.containsAtLeastOneOf(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainAtLeastOneOf" else "containedAtLeastOneOf", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all (xs) must not contain noneOf ("one")
     *                     ^
     * 
*/ def contain(newNoneOf: ResultOfNoneOfApplication)(implicit containing: Containing[T]) { val right = newNoneOf.right doCollected(collected, xs, "contain", 1) { e => if (containing.containsNoneOf(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "containedOneOfElements" else "didNotContainOneOfElements", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all (xs) must not contain theSameElementsAs ("one")
     *                     ^
     * 
*/ def contain(theSameElementsAs: ResultOfTheSameElementsAsApplication)(implicit aggregating: Aggregating[T]) { val right = theSameElementsAs.right doCollected(collected, xs, "contain", 1) { e => if (aggregating.containsTheSameElementsAs(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainSameElements" else "containedSameElements", e, right ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all (xs) must not contain theSameElementsInOrderAs ("one")
     *                     ^
     * 
*/ def contain(theSameElementsInOrderAs: ResultOfTheSameElementsInOrderAsApplication)(implicit aggregating: Aggregating[T]) { val right = theSameElementsInOrderAs.right doCollected(collected, xs, "contain", 1) { e => if (aggregating.containsTheSameElementsInOrderAs(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainSameElementsInOrder" else "containedSameElementsInOrder", e, right ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all (xs) must not contain only ("one")
     *                     ^
     * 
*/ def contain(only: ResultOfOnlyApplication)(implicit aggregating: Aggregating[T]) { val right = only.right doCollected(collected, xs, "contain", 1) { e => if (aggregating.containsOnly(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainOnlyElements" else "containedOnlyElements", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all (xs) must not contain inOrderOnly ("one", "two")
     *                     ^
     * 
*/ def contain(only: ResultOfInOrderOnlyApplication)(implicit aggregating: Aggregating[T]) { val right = only.right doCollected(collected, xs, "contain", 1) { e => if (aggregating.containsInOrderOnly(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainInOrderOnlyElements" else "containedInOrderOnlyElements", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all (xs) must not contain allOf ("one")
     *                     ^
     * 
*/ def contain(only: ResultOfAllOfApplication)(implicit aggregating: Aggregating[T]) { val right = only.right doCollected(collected, xs, "contain", 1) { e => if (aggregating.containsAllOf(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainAllOfElements" else "containedAllOfElements", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all (xs) must not contain inOrder ("one")
     *                     ^
     * 
*/ def contain(only: ResultOfInOrderApplication)(implicit aggregating: Aggregating[T]) { val right = only.right doCollected(collected, xs, "contain", 1) { e => if (aggregating.containsInOrder(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainAllOfElementsInOrder" else "containedAllOfElementsInOrder", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfNotWordForCollectedString(collected: Collected, xs: scala.collection.GenTraversable[String], mustBeTrue: Boolean) extends ResultOfNotWordForCollectedAny[String](collected, xs, mustBeTrue) { /** * This method enables the following syntax: * *
     * all(string) must not startWith ("1.7")
     *                        ^
     * 
*/ def startWith(right: String) { doCollected(collected, xs, "startWith", 1) { e => if ((e.indexOf(right) == 0) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotStartWith" else "startedWith", e, right ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all(string) must not startWith regex ("Hel*o")
     *                        ^
     * 
* *

* The regular expression passed following the regex token can be either a String * or a scala.util.matching.Regex. *

*/ def startWith(resultOfRegexWordApplication: ResultOfRegexWordApplication) { doCollected(collected, xs, "startWith", 1) { e => val result = startWithRegexWithGroups(e, resultOfRegexWordApplication.regex, resultOfRegexWordApplication.groups) if (result.matches != mustBeTrue) throw newTestFailedException( if (mustBeTrue) result.failureMessage else result.negatedFailureMessage, None, 6 ) } } /** * This method enables the following syntax: * *
     * all(string) must not endWith ("1.7")
     *                        ^
     * 
*/ def endWith(expectedSubstring: String) { doCollected(collected, xs, "endWith", 1) { e => if ((e endsWith expectedSubstring) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotEndWith" else "endedWith", e, expectedSubstring ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all(string) must not endWith regex ("wor.d")
     *                        ^
     * 
*/ def endWith(resultOfRegexWordApplication: ResultOfRegexWordApplication) { doCollected(collected, xs, "endWith", 1) { e => val result = endWithRegexWithGroups(e, resultOfRegexWordApplication.regex, resultOfRegexWordApplication.groups) if (result.matches != mustBeTrue) throw newTestFailedException( if (mustBeTrue) result.failureMessage else result.negatedFailureMessage, None, 6 ) } } /** * This method enables the following syntax: * *
     * all(string) must not include regex ("wo.ld")
     *                        ^
     * 
* *

* The regular expression passed following the regex token can be either a String * or a scala.util.matching.Regex. *

*/ def include(resultOfRegexWordApplication: ResultOfRegexWordApplication) { doCollected(collected, xs, "include", 1) { e => val result = includeRegexWithGroups(e, resultOfRegexWordApplication.regex, resultOfRegexWordApplication.groups) if (result.matches != mustBeTrue) throw newTestFailedException( if (mustBeTrue) result.failureMessage else result.negatedFailureMessage, None, 6 ) } } /** * This method enables the following syntax: * *
     * all(string) must not include ("world")
     *                        ^
     * 
*/ def include(expectedSubstring: String) { doCollected(collected, xs, "include", 1) { e => if ((e.indexOf(expectedSubstring) >= 0) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotIncludeSubstring" else "includedSubstring", e, expectedSubstring ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all(string) must not fullyMatch regex ("""(-)?(\d+)(\.\d*)?""")
     *                        ^
     * 
* *

* The regular expression passed following the regex token can be either a String * or a scala.util.matching.Regex. *

*/ def fullyMatch(resultOfRegexWordApplication: ResultOfRegexWordApplication) { doCollected(collected, xs, "fullyMatch", 1) { e => val result = fullyMatchRegexWithGroups(e, resultOfRegexWordApplication.regex, resultOfRegexWordApplication.groups) if (result.matches != mustBeTrue) throw newTestFailedException( if (mustBeTrue) result.failureMessage else result.negatedFailureMessage, None, 6 ) } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ sealed class ResultOfNotWordForCollectedGenTraversable[E, C[_] <: scala.collection.GenTraversable[_]](collected: Collected, xs: scala.collection.GenTraversable[C[E]], mustBeTrue: Boolean) extends ResultOfNotWordForCollectedAny[C[E]](collected, xs, mustBeTrue) /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ sealed class ResultOfNotWordForCollectedArray[E, T <: Array[E]](collected: Collected, xs: scala.collection.GenTraversable[T], mustBeTrue: Boolean) extends ResultOfNotWordForCollectedAny[T](collected, xs, mustBeTrue) { // TODO: I think we could merge the next three overrides up, and just do a pattern match looking for arrays. /** * This method enables the following syntax: * *
     * all(colOfArray) must not be ('empty)
     *                            ^
     * 
*/ override def be(symbol: Symbol)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "be", 1) { e => val matcherResult = matchSymbolToPredicateMethod(e.deep, symbol, false, false) if (matcherResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) matcherResult.failureMessage else matcherResult.negatedFailureMessage, None, 6 ) } } } /** * This method enables the following syntax: * *
     * all(colOfArray) must not be a ('file)
     *                            ^
     * 
*/ override def be(resultOfAWordApplication: ResultOfAWordToSymbolApplication)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "be", 1) { e => val matcherResult = matchSymbolToPredicateMethod(e.deep, resultOfAWordApplication.symbol, true, true) if (matcherResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) matcherResult.failureMessage else matcherResult.negatedFailureMessage, None, 10 ) } } } /** * This method enables the following syntax: * *
     * all(colOfArray) must not be an ('actionKey)
     *                            ^
     * 
*/ override def be(resultOfAnWordApplication: ResultOfAnWordToSymbolApplication)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "be", 1) { e => val matcherResult = matchSymbolToPredicateMethod(e.deep, resultOfAnWordApplication.symbol, true, false) if (matcherResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) matcherResult.failureMessage else matcherResult.negatedFailureMessage, None, 10 ) } } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfNotWordForCollectedGenMap[K, V, T <: scala.collection.GenMap[K, V]](collected: Collected, xs: scala.collection.GenTraversable[T], mustBeTrue: Boolean) extends ResultOfNotWordForCollectedAny[T](collected, xs, mustBeTrue) { /** * This method enables the following syntax: * *
     * all(colOfMap) must not contain key ("three")
     *                          ^
     * 
*/ def contain(resultOfKeyWordApplication: ResultOfKeyWordApplication[K]) { doCollected(collected, xs, "contain", 1) { e => val right = resultOfKeyWordApplication.expectedKey if ((e.exists(_._1 == right)) != mustBeTrue) { throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainKey" else "containedKey", e, right ), None, 6 ) } } } /** * This method enables the following syntax: * *
     * all(colOfMap) must not contain value (3)
     *                          ^
     * 
*/ def contain(resultOfValueWordApplication: ResultOfValueWordApplication[V]) { doCollected(collected, xs, "contain", 1) { e => val right = resultOfValueWordApplication.expectedValue if ((e.exists(_._2 == right)) != mustBeTrue) { throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainValue" else "containedValue", e, right ), None, 6 ) } } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ sealed class ResultOfContainWordForCollectedAny[T](collected: Collected, xs: scala.collection.GenTraversable[T], mustBeTrue: Boolean) { /** * This method enables the following syntax: * *
     * option must contain oneOf (1, 2)
     *                       ^
     * 
*/ def oneOf(right: Any*)(implicit containing: Containing[T]) { doCollected(collected, xs, "oneOf", 1) { e => if (containing.containsOneOf(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainOneOfElements" else "containedOneOfElements", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } /** * This method enables the following syntax: * *
     * option must contain atLeastOneOf (1, 2)
     *                       ^
     * 
*/ def atLeastOneOf(right: Any*)(implicit aggregating: Aggregating[T]) { doCollected(collected, xs, "atLeastOneOf", 1) { e => if (aggregating.containsAtLeastOneOf(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainAtLeastOneOf" else "containedAtLeastOneOf", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } /** * This method enables the following syntax: * *
     * option must contain noneOf (1, 2)
     *                       ^
     * 
*/ def noneOf(right: Any*)(implicit containing: Containing[T]) { doCollected(collected, xs, "noneOf", 1) { e => if (containing.containsNoneOf(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "containedOneOfElements" else "didNotContainOneOfElements", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } /** * This method enables the following syntax: * *
     * option must contain theSameElementsAs (1, 2)
     *                       ^
     * 
*/ def theSameElementsAs(right: GenTraversable[_])(implicit aggregating: Aggregating[T]) { doCollected(collected, xs, "theSameElementsAs", 1) { e => if (aggregating.containsTheSameElementsAs(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainSameElements" else "containedSameElements", e, right ), None, 6 ) } } /** * This method enables the following syntax: * *
     * option must contain theSameElementsInOrderAs (1, 2)
     *                       ^
     * 
*/ def theSameElementsInOrderAs(right: GenTraversable[_])(implicit aggregating: Aggregating[T]) { doCollected(collected, xs, "theSameElementsInOrderAs", 1) { e => if (aggregating.containsTheSameElementsInOrderAs(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainSameElementsInOrder" else "containedSameElementsInOrder", e, right ), None, 6 ) } } /** * This method enables the following syntax: * *
     * option must contain only (1, 2)
     *                       ^
     * 
*/ def only(right: Any*)(implicit aggregating: Aggregating[T]) { doCollected(collected, xs, "only", 1) { e => if (aggregating.containsOnly(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainOnlyElements" else "containedOnlyElements", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } /** * This method enables the following syntax: * *
     * option must contain inOrderOnly (1, 2)
     *                       ^
     * 
*/ def inOrderOnly(right: Any*)(implicit aggregating: Aggregating[T]) { doCollected(collected, xs, "inOrderOnly", 1) { e => if (aggregating.containsInOrderOnly(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainInOrderOnlyElements" else "containedInOrderOnlyElements", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } /** * This method enables the following syntax: * *
     * option must contain allOf (1, 2)
     *                       ^
     * 
*/ def allOf(right: Any*)(implicit aggregating: Aggregating[T]) { doCollected(collected, xs, "allOf", 1) { e => if (aggregating.containsAllOf(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainAllOfElements" else "containedAllOfElements", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } /** * This method enables the following syntax: * *
     * option must contain inOrder (1, 2)
     *                       ^
     * 
*/ def inOrder(right: Any*)(implicit aggregating: Aggregating[T]) { doCollected(collected, xs, "inOrder", 1) { e => if (aggregating.containsInOrder(e, right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainAllOfElementsInOrder" else "containedAllOfElementsInOrder", e, UnquotedString(right.map(FailureMessages.decorateToStringValue).mkString(", ")) ), None, 6 ) } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ sealed class ResultOfBeWordForCollectedAny[T](collected: Collected, xs: scala.collection.GenTraversable[T], mustBeTrue: Boolean) { // TODO: Missing must(AMatcher) and must(AnMatcher) /** * This method enables the following syntax: * *
     * all(xs) must be theSameInstanceAs anotherObject
     *                   ^
     * 
*/ def theSameInstanceAs(right: AnyRef)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "theSameInstanceAs", 1) { e => if ((e eq right) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "wasNotSameInstanceAs" else "wasSameInstanceAs", e, right ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all(xs) must be a ('file)
     *                   ^
     * 
*/ def a(symbol: Symbol)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "a", 1) { e => val matcherResult = matchSymbolToPredicateMethod(e, symbol, true, true) if (matcherResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) matcherResult.failureMessage else matcherResult.negatedFailureMessage, None, 6 ) } } } /** * This method enables the following syntax: * *
     * all(xs) must be an ('orange)
     *                   ^
     * 
*/ def an(symbol: Symbol)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "an", 1) { e => val matcherResult = matchSymbolToPredicateMethod(e, symbol, true, false) if (matcherResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) matcherResult.failureMessage else matcherResult.negatedFailureMessage, None, 6 ) } } } /** * This method enables the following syntax, where badBook is, for example, of type Book and * goodRead refers to a BePropertyMatcher[Book]: * *
     * all(books) must be a (goodRead)
     *                      ^
     * 
*/ def a[U <: T](bePropertyMatcher: BePropertyMatcher[U])(implicit ev: T <:< AnyRef) { // TODO: Try supporting 2.10 AnyVals doCollected(collected, xs, "a", 1) { e => val result = bePropertyMatcher(e.asInstanceOf[U]) if (result.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) FailureMessages("wasNotA", e, UnquotedString(result.propertyName)) else FailureMessages("wasA", e, UnquotedString(result.propertyName)), None, 6 ) } } } /** * This method enables the following syntax, where badBook is, for example, of type Book and * excellentRead refers to a BePropertyMatcher[Book]: * *
     * all(books) must be an (excellentRead)
     *                      ^
     * 
*/ def an[U <: T](beTrueMatcher: BePropertyMatcher[U])(implicit ev: T <:< AnyRef) { // TODO: Try supporting 2.10 AnyVals doCollected(collected, xs, "an", 1) { e => val beTrueMatchResult = beTrueMatcher(e.asInstanceOf[U]) if (beTrueMatchResult.matches != mustBeTrue) { throw newTestFailedException( if (mustBeTrue) FailureMessages("wasNotAn", e, UnquotedString(beTrueMatchResult.propertyName)) else FailureMessages("wasAn", e, UnquotedString(beTrueMatchResult.propertyName)), None, 6 ) } } } /** * This method enables the following syntax, where fraction is, for example, of type PartialFunction: * *
     * all(xs) must be definedAt (6)
     *                   ^
     * 
*/ def definedAt[U](right: U)(implicit ev: T <:< PartialFunction[U, _]) { doCollected(collected, xs, "definedAt", 1) { e => if (e.isDefinedAt(right) != mustBeTrue) throw newTestFailedException( if (mustBeTrue) FailureMessages("wasNotDefinedAt", e, right) else FailureMessages("wasDefinedAt", e, right), None, 6 ) } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfBeWordForCollectedArray[T](collected: Collected, xs: scala.collection.GenTraversable[Array[T]], mustBeTrue: Boolean) extends ResultOfBeWordForCollectedAny(collected, xs, mustBeTrue) { /** * This method enables the following syntax: * *
     * all(colOfArray) must be ('empty)
     *                           ^
     * 
*/ def apply(right: Symbol): Matcher[Array[T]] = new Matcher[Array[T]] { def apply(left: Array[T]): MatchResult = matchSymbolToPredicateMethod(left.deep, right, false, false) } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfContainWordForCollectedArray[T](collected: Collected, xs: scala.collection.GenTraversable[Array[T]], mustBeTrue: Boolean) extends ResultOfContainWordForCollectedAny[Array[T]](collected, xs, mustBeTrue) { // TODO: This apply method looks very wrong /** * This method enables the following syntax: * *
     * all(colOfArray) must contain (element)
     *                        ^
     * 
*/ def apply(expectedElement: T): Matcher[Array[T]] = new Matcher[Array[T]] { def apply(left: Array[T]): MatchResult = MatchResult( left.exists(_ == expectedElement), FailureMessages("didNotContainExpectedElement", left, expectedElement), FailureMessages("containedExpectedElement", left, expectedElement) ) } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ sealed class ResultOfCollectedAny[T](collected: Collected, xs: scala.collection.GenTraversable[T]) { // TODO: mustBe null works, b ut must be (null) does not when type is Any: /* scala> val ys = List(null, null, 1) ys: List[Any] = List(null, null, 1) scala> all (ys) mustBe null :15: error: ambiguous reference to overloaded definition, both method mustBe in class ResultOfCollectedAny of type (interval: org.scalautils.Interval[Any])Unit and method mustBe in class ResultOfCollectedAny of type (beMatcher: org.scalatest.matchers.BeMatcher[Any])Unit match argument types (Null) all (ys) mustBe null ^ scala> all (ys) must be (null) org.scalatest.exceptions.TestFailedException: org.scalatest.Matchers$ResultOfCollectedAny@18515783 was not null at org.scalatest.MatchersHelper$.newTestFailedException(MatchersHelper.scala:163) at org.scalatest.Matchers$MustMethodHelper$.mustMatcher(Matchers.scala:5529) at org.scalatest.Matchers$AnyMustWrapper.must(Matchers.scala:5563) at .(:15) at .() */ /** * This method enables syntax such as the following: * *
     * all(xs) must be (3)
     *         ^
     * 
*/ def must(rightMatcher: Matcher[T]) { doCollected(collected, xs, "must", 1) { e => rightMatcher(e) match { case MatchResult(false, failureMessage, _, _, _) => throw newTestFailedException(failureMessage, None, 6) case _ => () } } } /** * This method enables syntax such as the following: * *
     * all (xs) mustEqual 7
     *          ^
     * 
*/ def mustEqual(right: Any)(implicit equality: Equality[T]) { doCollected(collected, xs, "mustEqual", 1) { e => if (!equality.areEqual(e, right)) { val (eee, rightee) = Suite.getObjectsForFailureMessage(e, right) throw newTestFailedException(FailureMessages("didNotEqual", eee, rightee), None, 6) } } } /** * This method enables syntax such as the following: * *
     * result mustEqual 7.1 +- 0.2
     *        ^
     * 
*/ def mustEqual(interval: Interval[T]) { doCollected(collected, xs, "mustEqual", 1) { e => if (!interval.isWithin(e)) { throw newTestFailedException(FailureMessages("didNotEqualPlusOrMinus", e, interval.pivot, interval.tolerance), None, 6) } } } /** * This method enables the following syntax: * *
     * all(xs) mustBe sorted
     *         ^
     * 
*/ def mustBe(sortedWord: SortedWord)(implicit sortable: Sortable[T]) { doCollected(collected, xs, "mustBe", 1) { e => if (!sortable.isSorted(e)) throw newTestFailedException(FailureMessages("wasNotSorted", e), None, 6) } } /** * This method enables syntax such as the following: * *
     * result mustEqual null
     *        ^
     * 
*/ def mustEqual(right: Null)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "mustEqual", 1) { e => if (e != null) { throw newTestFailedException(FailureMessages("didNotEqualNull", e), None, 6) } } } /** * This method enables syntax such as the following: * *
     * all(xs) must equal (3)
     *         ^
     * 
*/ def must[TYPECLASS1[_]](rightMatcherFactory1: MatcherFactory1[T, TYPECLASS1])(implicit typeClass1: TYPECLASS1[T]) { val rightMatcher = rightMatcherFactory1.matcher doCollected(collected, xs, "must", 1) { e => rightMatcher(e) match { case MatchResult(false, failureMessage, _, _, _) => throw newTestFailedException(failureMessage, None, 6) case _ => () } } } /** * This method enables syntax such as the following: * *
     * all(xs) must (equal (expected) and have length 12)
     *         ^
     * 
*/ def must[TYPECLASS1[_], TYPECLASS2[_]](rightMatcherFactory2: MatcherFactory2[T, TYPECLASS1, TYPECLASS2])(implicit typeClass1: TYPECLASS1[T], typeClass2: TYPECLASS2[T]) { val rightMatcher = rightMatcherFactory2.matcher doCollected(collected, xs, "must", 1) { e => rightMatcher(e) match { case MatchResult(false, failureMessage, _, _, _) => throw newTestFailedException(failureMessage, None, 6) case _ => () } } } /** * This method enables syntax such as the following: * *
     * all(xs) must be theSameInstanceAs anotherObject
     *         ^
     * 
*/ def must(beWord: BeWord) = new ResultOfBeWordForCollectedAny[T](collected, xs, true) /** * This method enables syntax such as the following: * *
     * all(xs) must not equal (3)
     *         ^
     * 
*/ def must(notWord: NotWord): ResultOfNotWordForCollectedAny[T] = new ResultOfNotWordForCollectedAny(collected, xs, false) /** * This method enables syntax such as the following: * *
     * all (results) must have length (3)
     *        ^
     * all (results) must have size (3)
     *        ^
     * 
*/ def must(haveWord: HaveWord): ResultOfHaveWordForCollectedExtent[T] = new ResultOfHaveWordForCollectedExtent(collected, xs, true) /** * This method enables syntax such as the following: * *
     * all (xs) mustBe 7
     *          ^
     * 
*/ def mustBe(right: Any) { doCollected(collected, xs, "mustBe", 1) { e => if (e != right) { val (eee, rightee) = Suite.getObjectsForFailureMessage(e, right) throw newTestFailedException(FailureMessages("wasNot", eee, rightee), None, 6) } } } /** * This method enables syntax such as the following: * *
     * all(4, 5, 6) mustBe < (7) 
     *              ^
     * 
*/ def mustBe(comparison: ResultOfLessThanComparison[T]) { doCollected(collected, xs, "mustBe", 1) { e => if (!comparison(e)) { throw newTestFailedException( FailureMessages( "wasNotLessThan", e, comparison.right ), None, 6 ) } } } /** * This method enables syntax such as the following: * *
     * all(4, 5, 6) mustBe <= (7) 
     *              ^
     * 
*/ def mustBe(comparison: ResultOfLessThanOrEqualToComparison[T]) { doCollected(collected, xs, "mustBe", 1) { e => if (!comparison(e)) { throw newTestFailedException( FailureMessages( "wasNotLessThanOrEqualTo", e, comparison.right ), None, 6 ) } } } /** * This method enables syntax such as the following: * *
     * all(8, 9, 10) mustBe > (7) 
     *               ^
     * 
*/ def mustBe(comparison: ResultOfGreaterThanComparison[T]) { doCollected(collected, xs, "mustBe", 1) { e => if (!comparison(e)) { throw newTestFailedException( FailureMessages( "wasNotGreaterThan", e, comparison.right ), None, 6 ) } } } /** * This method enables syntax such as the following: * *
     * all(8, 9, 10) mustBe >= (7) 
     *               ^
     * 
*/ def mustBe(comparison: ResultOfGreaterThanOrEqualToComparison[T]) { doCollected(collected, xs, "mustBe", 1) { e => if (!comparison(e)) { throw newTestFailedException( FailureMessages( "wasNotGreaterThanOrEqualTo", e, comparison.right ), None, 6 ) } } } /** * This method enables the following syntax, where odd refers to a BeMatcher[Int]: * *
testing
     * all(xs) mustBe odd
     *         ^
     * 
*/ def mustBe(beMatcher: BeMatcher[T]) { doCollected(collected, xs, "mustBe", 1) { e => val result = beMatcher.apply(e) if (!result.matches) throw newTestFailedException(result.failureMessage, None, 6) } } /** * This method enables syntax such as the following: * *
     * all(xs) mustBe 7.1 +- 0.2
     *         ^
     * 
*/ def mustBe(interval: Interval[T]) { doCollected(collected, xs, "mustBe", 1) { e => if (!interval.isWithin(e)) throw newTestFailedException(FailureMessages("wasNotPlusOrMinus", e, interval.pivot, interval.tolerance), None, 6) } } /** * This method enables syntax such as the following: * *
     * all(xs) mustBe theSameInstanceAs (anotherObject)
     *         ^
     * 
*/ def mustBe(resultOfSameInstanceAsApplication: ResultOfTheSameInstanceAsApplication)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "mustBe", 1) { e => if (e ne resultOfSameInstanceAsApplication.right) throw newTestFailedException( FailureMessages( "wasNotSameInstanceAs", e, resultOfSameInstanceAsApplication.right ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all(xs) mustBe 'empty
     *         ^
     * 
*/ def mustBe(symbol: Symbol)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "mustBe", 1) { e => val matcherResult = matchSymbolToPredicateMethod(e, symbol, false, true, 6) if (!matcherResult.matches) throw newTestFailedException(matcherResult.failureMessage, None, 6) } } /** * This method enables the following syntax: * *
     * all(xs) mustBe a ('empty)
     *      ^
     * 
*/ def mustBe(resultOfAWordApplication: ResultOfAWordToSymbolApplication)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "mustBe", 1) { e => val matcherResult = matchSymbolToPredicateMethod(e, resultOfAWordApplication.symbol, true, true, 6) if (!matcherResult.matches) { throw newTestFailedException(matcherResult.failureMessage, None, 6) } } } /** * This method enables the following syntax: * *
     * all(xs) mustBe an ('empty)
     *      ^
     * 
*/ def mustBe(resultOfAnWordApplication: ResultOfAnWordToSymbolApplication)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "mustBe", 1) { e => val matcherResult = matchSymbolToPredicateMethod(e, resultOfAnWordApplication.symbol, true, false, 6) if (!matcherResult.matches) { throw newTestFailedException(matcherResult.failureMessage, None, 6) } } } /** * This method enables the following syntax: * *
     * all(xs) mustBe null
     *         ^
     * 
*/ def mustBe(o: Null)(implicit ev: T <:< AnyRef) { doCollected(collected, xs, "mustBe", 1) { e => if (e != null) throw newTestFailedException(FailureMessages("wasNotNull", e), None, 6) } } /** * This method enables the following syntax, where excellentRead refers to a BePropertyMatcher[Book]: * *
     * all(xs) mustBe excellentRead
     *         ^
     * 
*/ def mustBe[U <: T](bePropertyMatcher: BePropertyMatcher[U])(implicit ev: T <:< AnyRef) { // TODO: Try supporting this with 2.10 AnyVals doCollected(collected, xs, "mustBe", 1) { e => val result = bePropertyMatcher(e.asInstanceOf[U]) if (!result.matches) throw newTestFailedException(FailureMessages("wasNot", e, UnquotedString(result.propertyName)), None, 6) } } /** * This method enables the following syntax, where goodRead refers to a BePropertyMatcher[Book]: * *
     * all(xs) mustBe a (goodRead)
     *         ^
     * 
*/ def mustBe[U <: T](resultOfAWordApplication: ResultOfAWordToBePropertyMatcherApplication[U])(implicit ev: T <:< AnyRef) {// TODO: Try supporting this with 2.10 AnyVals doCollected(collected, xs, "mustBe", 1) { e => val result = resultOfAWordApplication.bePropertyMatcher(e.asInstanceOf[U]) if (!result.matches) throw newTestFailedException(FailureMessages("wasNotA", e, UnquotedString(result.propertyName)), None, 6) } } /** * This method enables the following syntax, where excellentRead refers to a BePropertyMatcher[Book]: * *
     * all(xs) mustBe an (excellentRead)
     *         ^
     * 
*/ def mustBe[U <: T](resultOfAnWordApplication: ResultOfAnWordToBePropertyMatcherApplication[U])(implicit ev: T <:< AnyRef) {// TODO: Try supporting this with 2.10 AnyVals doCollected(collected, xs, "mustBe", 1) { e => val result = resultOfAnWordApplication.bePropertyMatcher(e.asInstanceOf[U]) if (!result.matches) throw newTestFailedException(FailureMessages("wasNotAn", e, UnquotedString(result.propertyName)), None, 6) } } /** * This method enables syntax such as the following: * *
     * all(xs) mustNot (be (3))
     *         ^
     * 
*/ def mustNot[U <: T](rightMatcherX1: Matcher[U]) { doCollected(collected, xs, "mustNot", 1) { e => val result = try rightMatcherX1.apply(e.asInstanceOf[U]) catch { case tfe: TestFailedException => throw newTestFailedException(tfe.getMessage, tfe.cause, 6) } if (result.matches) throw newTestFailedException(result.negatedFailureMessage, None, 6) } } def mustNot[TYPECLASS1[_]](rightMatcherFactory1: MatcherFactory1[T, TYPECLASS1])(implicit typeClass1: TYPECLASS1[T]) { val rightMatcher = rightMatcherFactory1.matcher doCollected(collected, xs, "mustNot", 1) { e => rightMatcher(e) match { case MatchResult(true, _, negatedFailureMessage, _, _) => throw newTestFailedException(negatedFailureMessage, None, 6) case _ => () } } } /** * This method enables syntax such as the following: * *
     * all (xs) must === (b)
     *          ^
     * 
*/ def must[U](inv: TripleEqualsInvocation[U])(implicit constraint: EqualityConstraint[T, U]) { doCollected(collected, xs, "must", 1) { e => if ((constraint.areEqual(e, inv.right)) != inv.expectingEqual) throw newTestFailedException( FailureMessages( if (inv.expectingEqual) "didNotEqual" else "equaled", e, inv.right ), None, 6 ) } } /** * This method enables syntax such as the following: * *
     * all (xs) must === (100 +- 1)
     *          ^
     * 
*/ def must(inv: TripleEqualsInvocationOnInterval[T])(implicit ev: Numeric[T]) { doCollected(collected, xs, "must", 1) { e => if ((inv.interval.isWithin(e)) != inv.expectingEqual) throw newTestFailedException( FailureMessages( if (inv.expectingEqual) "didNotEqualPlusOrMinus" else "equaledPlusOrMinus", e, inv.interval.pivot, inv.interval.tolerance ), None, 6 ) } } /** * This method enables the following syntax: * *
     * all(xs) mustNot be theSameInstanceAs anotherInstance
     *         ^
     * 
*/ def mustNot(beWord: BeWord): ResultOfBeWordForCollectedAny[T] = new ResultOfBeWordForCollectedAny[T](collected, xs, false) /** * This method enables syntax such as the following: * *
     * all (xs) must contain oneOf (1, 2, 3)
     *    ^
     * 
*/ def must(containWord: ContainWord): ResultOfContainWordForCollectedAny[T] = { new ResultOfContainWordForCollectedAny(collected, xs, true) } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * * @author Bill Venners */ final class ResultOfHaveWordForCollectedExtent[A](collected: Collected, xs: scala.collection.GenTraversable[A], mustBeTrue: Boolean) { /** * This method enables the following syntax: * *
     * all (xs) must have length (12)
     *                      ^
     * 
*/ def length(expectedLength: Long)(implicit len: Length[A]) { doCollected(collected, xs, "length", 1) { e => val eLength = len.lengthOf(e) if ((eLength == expectedLength) != mustBeTrue) throw newTestFailedException( if (mustBeTrue) FailureMessages("hadLengthInsteadOfExpectedLength", e, eLength, expectedLength) else FailureMessages("hadExpectedLength", e, expectedLength), None, 6 ) } } /** * This method enables the following syntax: * *
     * all (xs) must have size (12)
     *                      ^
     * 
*/ def size(expectedSize: Long)(implicit sz: Size[A]) { doCollected(collected, xs, "size", 1) { e => val eSize = sz.sizeOf(e) if ((eSize == expectedSize) != mustBeTrue) throw newTestFailedException( if (mustBeTrue) FailureMessages("hadSizeInsteadOfExpectedSize", e, eSize, expectedSize) else FailureMessages("hadExpectedSize", e, expectedSize), None, 6 ) } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfCollectedString(collected: Collected, xs: scala.collection.GenTraversable[String]) extends ResultOfCollectedAny(collected, xs) { /** * This method enables syntax such as the following: * *
     * all(string) must not have length (3)
     *             ^
     * 
*/ override def must(notWord: NotWord): ResultOfNotWordForCollectedString = new ResultOfNotWordForCollectedString(collected, xs, false) /** * This method enables syntax such as the following: * *
     * all(string) must startWith regex ("Hel*o")
     *             ^
     * 
*/ def must(startWithWord: StartWithWord): ResultOfStartWithWordForCollectedString = new ResultOfStartWithWordForCollectedString(collected, xs, true) /** * This method enables syntax such as the following: * *
     * all(string) must endWith regex ("wo.ld")
     *             ^
     * 
*/ def must(endWithWord: EndWithWord): ResultOfEndWithWordForCollectedString = new ResultOfEndWithWordForCollectedString(collected, xs, true) /** * This method enables syntax such as the following: * *
     * all(string) must include regex ("wo.ld")
     *             ^
     * 
*/ def must(includeWord: IncludeWord): ResultOfIncludeWordForCollectedString = new ResultOfIncludeWordForCollectedString(collected, xs, true) /** * This method enables syntax such as the following: * *
     * all(string) must fullyMatch regex ("""(-)?(\d+)(\.\d*)?""")
     *             ^
     * 
*/ def must(fullyMatchWord: FullyMatchWord): ResultOfFullyMatchWordForCollectedString = new ResultOfFullyMatchWordForCollectedString(collected, xs, true) /** * This method enables syntax such as the following: * *
     * all(string) mustNot fullyMatch regex ("""(-)?(\d+)(\.\d*)?""")
     *             ^
     * 
*/ def mustNot(fullyMatchWord: FullyMatchWord): ResultOfFullyMatchWordForCollectedString = new ResultOfFullyMatchWordForCollectedString(collected, xs, false) /** * This method enables syntax such as the following: * *
     * all(string) mustNot startWith regex ("Hel*o")
     *             ^
     * 
*/ def mustNot(startWithWord: StartWithWord): ResultOfStartWithWordForCollectedString = new ResultOfStartWithWordForCollectedString(collected, xs, false) /** * This method enables syntax such as the following: * *
     * all(string) mustNot endWith regex ("wo.ld")
     *             ^
     * 
*/ def mustNot(endWithWord: EndWithWord): ResultOfEndWithWordForCollectedString = new ResultOfEndWithWordForCollectedString(collected, xs, false) /** * This method enables syntax such as the following: * *
     * all(string) mustNot include regex ("wo.ld")
     *             ^
     * 
*/ def mustNot(includeWord: IncludeWord): ResultOfIncludeWordForCollectedString = new ResultOfIncludeWordForCollectedString(collected, xs, false) } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfStartWithWordForCollectedString(collected: Collected, xs: scala.collection.GenTraversable[String], mustBeTrue: Boolean) { /** * This method enables the following syntax: * *
     * all(string) must startWith regex ("Hel*o")
     *                              ^
     * 
*/ def regex(rightRegexString: String) { checkRegex(rightRegexString.r) } /** * This method enables the following syntax: * *
     * all(string) must fullMatch regex ("a(b*)c" withGroup "bb") 
     *                              ^
     * 
*/ def regex(regexWithGroups: RegexWithGroups) { checkRegex(regexWithGroups.regex, regexWithGroups.groups) } /** * This method enables the following syntax: * *
     * all(string) must startWith regex ("Hel*o".r)
     *                              ^
     * 
*/ def regex(rightRegex: Regex) { checkRegex(rightRegex) } def checkRegex(rightRegex: Regex, groups: IndexedSeq[String] = IndexedSeq.empty) { doCollected(collected, xs, "regex", 2) { e => val result = startWithRegexWithGroups(e, rightRegex, groups) if (result.matches != mustBeTrue) throw newTestFailedException( if (mustBeTrue) result.failureMessage else result.negatedFailureMessage, None, 7 ) } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfIncludeWordForCollectedString(collected: Collected, xs: scala.collection.GenTraversable[String], mustBeTrue: Boolean) { /** * This method enables the following syntax: * *
     * all(string) must include regex ("world")
     *                            ^
     * 
*/ def regex(rightRegexString: String) { checkRegex(rightRegexString.r) } /** * This method enables the following syntax: * *
     * all(string) must include regex ("a(b*)c" withGroup "bb") 
     *                            ^
     * 
*/ def regex(regexWithGroups: RegexWithGroups) { checkRegex(regexWithGroups.regex, regexWithGroups.groups) } /** * This method enables the following syntax: * *
     * all(string) must include regex ("wo.ld".r)
     *                            ^
     * 
*/ def regex(rightRegex: Regex) { checkRegex(rightRegex) } private def checkRegex(rightRegex: Regex, groups: IndexedSeq[String] = IndexedSeq.empty) { doCollected(collected, xs, "regex", 2) { e => val result = includeRegexWithGroups(e, rightRegex, groups) if (result.matches != mustBeTrue) throw newTestFailedException( if (mustBeTrue) result.failureMessage else result.negatedFailureMessage, None, 7 ) } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfEndWithWordForCollectedString(collected: Collected, xs: scala.collection.GenTraversable[String], mustBeTrue: Boolean) { /** * This method enables the following syntax: * *
     * all(string) must endWith regex ("wor.d")
     *                            ^
     * 
*/ def regex(rightRegexString: String) { checkRegex(rightRegexString.r) } /** * This method enables the following syntax: * *
     * all(string) must endWith regex ("a(b*)c" withGroup "bb") 
     *                            ^
     * 
*/ def regex(regexWithGroups: RegexWithGroups) { checkRegex(regexWithGroups.regex, regexWithGroups.groups) } /** * This method enables the following syntax: * *
     * all(string) must endWith regex ("wor.d".r)
     *                            ^
     * 
*/ def regex(rightRegex: Regex) { checkRegex(rightRegex) } private def checkRegex(rightRegex: Regex, groups: IndexedSeq[String] = IndexedSeq.empty) { doCollected(collected, xs, "regex", 2) { e => val result = endWithRegexWithGroups(e, rightRegex, groups) if (result.matches != mustBeTrue) throw newTestFailedException( if (mustBeTrue) result.failureMessage else result.negatedFailureMessage, None, 7 ) } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfFullyMatchWordForCollectedString(collected: Collected, xs: scala.collection.GenTraversable[String], mustBeTrue: Boolean) { /** * This method enables the following syntax: * *
     * all(string) must fullMatch regex ("Hel*o world")
     *                              ^
     * 
*/ def regex(rightRegexString: String) { checkRegex(rightRegexString.r) } /** * This method enables the following syntax: * *
     * all(string) must fullMatch regex ("a(b*)c" withGroup "bb") 
     *                              ^
     * 
*/ def regex(regexWithGroups: RegexWithGroups) { checkRegex(regexWithGroups.regex, regexWithGroups.groups) } /** * This method enables the following syntax: * *
     * all(string) must fullymatch regex ("Hel*o world".r)
     *                               ^
     * 
*/ def regex(rightRegex: Regex) { checkRegex(rightRegex) } private def checkRegex(rightRegex: Regex, groups: IndexedSeq[String] = IndexedSeq.empty) { doCollected(collected, xs, "regex", 2) { e => val result = fullyMatchRegexWithGroups(e, rightRegex, groups) if (result.matches != mustBeTrue) throw newTestFailedException( if (mustBeTrue) result.failureMessage else result.negatedFailureMessage, None, 7 ) } } } // TODO add loneElement /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfCollectedGenTraversable[E, C[_] <: scala.collection.GenTraversable[_]](collected: Collected, xs: scala.collection.GenTraversable[C[E]]) extends ResultOfCollectedAny(collected, xs) { /** * This method enables syntax such as the following: * *
     * all(colOfTraversable) must not have size (3)
     *                       ^
     * 
*/ override def must(notWord: NotWord): ResultOfNotWordForCollectedGenTraversable[E, C] = new ResultOfNotWordForCollectedGenTraversable(collected, xs, false) /** * This method enables syntax such as the following: * *
     * all(colOfTraversable) must contain (containMatcher)
     *                       ^
     * 
*/ override def must(containWord: ContainWord): ResultOfContainWordForCollectedGenTraversable[E, C] = new ResultOfContainWordForCollectedGenTraversable(collected, xs, true) } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfContainWordForCollectedGenTraversable[E, C[_] <: scala.collection.GenTraversable[_]](collected: Collected, xs: scala.collection.GenTraversable[C[E]], mustBeTrue: Boolean) extends ResultOfContainWordForCollectedAny[C[E]](collected, xs, mustBeTrue) /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfCollectedArray[T](collected: Collected, xs: scala.collection.GenTraversable[Array[T]]) extends ResultOfCollectedAny(collected, xs) { /** * This method enables syntax such as the following: * *
     * all(colOfArray) must be theSameInstanceAs anotherObject
     *                 ^
     * 
*/ override def must(beWord: BeWord) = new ResultOfBeWordForCollectedArray(collected, xs, true) /** * This method enables syntax such as the following: * *
     * all(colOfArray) must not have size (3)
     *                 ^
     * 
*/ override def must(notWord: NotWord): ResultOfNotWordForCollectedArray[T, Array[T]] = new ResultOfNotWordForCollectedArray(collected, xs, false) /** * This method enables syntax such as the following: * *
     * all(colOfArray) must contain (containMatcher)
     *                 ^
     * 
*/ override def must(containWord: ContainWord): ResultOfContainWordForCollectedArray[T] = new ResultOfContainWordForCollectedArray(collected, xs, true) } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfCollectedGenMap[K, V](collected: Collected, xs: scala.collection.GenTraversable[scala.collection.GenMap[K, V]]) extends ResultOfCollectedAny(collected, xs) { /** * This method enables syntax such as the following: * *
     * all(colOfMap) must contain key (10)
     *               ^
     * 
*/ override def must(containWord: ContainWord): ResultOfContainWordForCollectedGenMap[K, V] = new ResultOfContainWordForCollectedGenMap(collected, xs, true) /** * This method enables syntax such as the following: * *
     * all(colOfMap) must not have size (3)
     *               ^
     * 
*/ override def must(notWord: NotWord): ResultOfNotWordForCollectedGenMap[K, V, scala.collection.GenMap[K, V]] = new ResultOfNotWordForCollectedGenMap(collected, xs, false) } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for InspectorsMatchers for an overview of * the matchers DSL. * * @author Bill Venners * @author Chee Seng */ final class ResultOfContainWordForCollectedGenMap[K, V](collected: Collected, xs: scala.collection.GenTraversable[scala.collection.GenMap[K, V]], mustBeTrue: Boolean) extends ResultOfContainWordForCollectedAny[scala.collection.GenMap[K, V]](collected, xs, mustBeTrue) { /** * This method enables the following syntax: * *
     * all(colOfMap) must contain key ("one")
     *                              ^
     * 
*/ def key(expectedKey: K) { doCollected(collected, xs, "key", 1) { e => if (e.exists(_._1 == expectedKey) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainKey" else "containedKey", e, expectedKey), None, 6 ) } } /** * This method enables the following syntax: * *
     * all(colOfMap) must contain value (1)
     *                              ^
     * 
*/ def value(expectedValue: V) { doCollected(collected, xs, "value", 1) { e => if (e.exists(expectedValue == _._2) != mustBeTrue) throw newTestFailedException( FailureMessages( if (mustBeTrue) "didNotContainValue" else "containedValue", e, expectedValue), None, 6 ) } } } def all[T](xs: scala.collection.GenTraversable[T]): ResultOfCollectedAny[T] = new ResultOfCollectedAny(AllCollected, xs) def all(xs: scala.collection.GenTraversable[String]): ResultOfCollectedString = new ResultOfCollectedString(AllCollected, xs) def all[E, C[_] <: scala.collection.GenTraversable[_]](xs: scala.collection.GenTraversable[C[E]]) = new ResultOfCollectedGenTraversable(AllCollected, xs) def all[T](xs: scala.collection.GenTraversable[Array[T]]) = new ResultOfCollectedArray(AllCollected, xs) def all[K, V](xs: scala.collection.GenTraversable[scala.collection.GenMap[K, V]]) = new ResultOfCollectedGenMap(AllCollected, xs) def atLeast[T](num: Int, xs: scala.collection.GenTraversable[T]): ResultOfCollectedAny[T] = new ResultOfCollectedAny(AtLeastCollected(num), xs) def atLeast(num: Int, xs: scala.collection.GenTraversable[String]): ResultOfCollectedString = new ResultOfCollectedString(AtLeastCollected(num), xs) def atLeast[E, C[_] <: scala.collection.GenTraversable[_]](num: Int, xs: scala.collection.GenTraversable[C[E]]) = new ResultOfCollectedGenTraversable(AtLeastCollected(num), xs) def atLeast[T](num: Int, xs: scala.collection.GenTraversable[Array[T]]) = new ResultOfCollectedArray(AtLeastCollected(num), xs) def atLeast[K, V](num: Int, xs: scala.collection.GenTraversable[scala.collection.GenMap[K, V]]) = new ResultOfCollectedGenMap(AtLeastCollected(num), xs) def every[T](xs: scala.collection.GenTraversable[T]): ResultOfCollectedAny[T] = new ResultOfCollectedAny(EveryCollected, xs) def every(xs: scala.collection.GenTraversable[String]): ResultOfCollectedString = new ResultOfCollectedString(EveryCollected, xs) def every[E, C[_] <: scala.collection.GenTraversable[_]](xs: scala.collection.GenTraversable[C[E]]) = new ResultOfCollectedGenTraversable(EveryCollected, xs) def every[T](xs: scala.collection.GenTraversable[Array[T]]) = new ResultOfCollectedArray(EveryCollected, xs) def every[K, V](xs: scala.collection.GenTraversable[scala.collection.GenMap[K, V]]) = new ResultOfCollectedGenMap(EveryCollected, xs) def exactly[T](num: Int, xs: scala.collection.GenTraversable[T]): ResultOfCollectedAny[T] = new ResultOfCollectedAny(ExactlyCollected(num), xs) def exactly(num: Int, xs: scala.collection.GenTraversable[String]): ResultOfCollectedString = new ResultOfCollectedString(ExactlyCollected(num), xs) def exactly[E, C[_] <: scala.collection.GenTraversable[_]](num: Int, xs: scala.collection.GenTraversable[C[E]]) = new ResultOfCollectedGenTraversable(ExactlyCollected(num), xs) def exactly[T](num: Int, xs: scala.collection.GenTraversable[Array[T]]) = new ResultOfCollectedArray(ExactlyCollected(num), xs) def exactly[K, V](num: Int, xs: scala.collection.GenTraversable[scala.collection.GenMap[K, V]]) = new ResultOfCollectedGenMap(ExactlyCollected(num), xs) def no[T](xs: scala.collection.GenTraversable[T]): ResultOfCollectedAny[T] = new ResultOfCollectedAny(NoCollected, xs) def no(xs: scala.collection.GenTraversable[String]): ResultOfCollectedString = new ResultOfCollectedString(NoCollected, xs) def no[E, C[_] <: scala.collection.GenTraversable[_]](xs: scala.collection.GenTraversable[C[E]]) = new ResultOfCollectedGenTraversable(NoCollected, xs) def no[T](xs: scala.collection.GenTraversable[Array[T]]) = new ResultOfCollectedArray(NoCollected, xs) def no[K, V](xs: scala.collection.GenTraversable[scala.collection.GenMap[K, V]]) = new ResultOfCollectedGenMap(NoCollected, xs) def between[T](from: Int, upTo:Int, xs: scala.collection.GenTraversable[T]): ResultOfCollectedAny[T] = new ResultOfCollectedAny(BetweenCollected(from, upTo), xs) def between(from: Int, upTo:Int, xs: scala.collection.GenTraversable[String]): ResultOfCollectedString = new ResultOfCollectedString(BetweenCollected(from, upTo), xs) def between[E, C[_] <: scala.collection.GenTraversable[_]](from: Int, upTo:Int, xs: scala.collection.GenTraversable[C[E]]) = new ResultOfCollectedGenTraversable(BetweenCollected(from, upTo), xs) def between[T](from: Int, upTo:Int, xs: scala.collection.GenTraversable[Array[T]]) = new ResultOfCollectedArray(BetweenCollected(from, upTo), xs) def between[K, V](from: Int, upTo:Int, xs: scala.collection.GenTraversable[scala.collection.GenMap[K, V]]) = new ResultOfCollectedGenMap(BetweenCollected(from, upTo), xs) def atMost[T](num: Int, xs: scala.collection.GenTraversable[T]): ResultOfCollectedAny[T] = new ResultOfCollectedAny(AtMostCollected(num), xs) def atMost(num: Int, xs: scala.collection.GenTraversable[String]): ResultOfCollectedString = new ResultOfCollectedString(AtMostCollected(num), xs) def atMost[E, C[_] <: scala.collection.GenTraversable[_]](num: Int, xs: scala.collection.GenTraversable[C[E]]) = new ResultOfCollectedGenTraversable(AtMostCollected(num), xs) def atMost[T](num: Int, xs: scala.collection.GenTraversable[Array[T]]) = new ResultOfCollectedArray(AtMostCollected(num), xs) def atMost[K, V](num: Int, xs: scala.collection.GenTraversable[scala.collection.GenMap[K, V]]) = new ResultOfCollectedGenMap(AtMostCollected(num), xs) // This is where MustMatchers.scala started private object MustMethodHelper { def mustMatcher[T](left: T, rightMatcher: Matcher[T], stackDepthAdjustment: Int = 0) { rightMatcher(left) match { case MatchResult(false, failureMessage, _, _, _) => throw newTestFailedException(failureMessage, None, stackDepthAdjustment) case _ => () } } def mustNotMatcher[T](left: T, rightMatcher: Matcher[T], stackDepthAdjustment: Int = 0) { rightMatcher(left) match { case MatchResult(true, _, negatedFailureMessage, _, _) => throw newTestFailedException(negatedFailureMessage, None, stackDepthAdjustment) case _ => () } } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * *

* This class is used in conjunction with an implicit conversion to enable must methods to * be invoked on objects of type Any. *

* * @author Bill Venners */ class AnyMustWrapper[T](left: T) { /** * This method enables syntax such as the following: * *
     * result must be (3)
     *        ^
     * 
*/ def must(rightMatcherX1: Matcher[T]) { MustMethodHelper.mustMatcher(left, rightMatcherX1) } /** * This method enables syntax such as the following: * *
     * result must equal (3)
     *        ^
     * 
*/ def must[TYPECLASS1[_]](rightMatcherFactory1: MatcherFactory1[T, TYPECLASS1])(implicit typeClass1: TYPECLASS1[T]) { MustMethodHelper.mustMatcher(left, rightMatcherFactory1.matcher) } /** * This method enables syntax such as the following: * *
     * result must (equal (expected) and have length 3)
     *        ^
     * 
*/ def must[TYPECLASS1[_], TYPECLASS2[_]](rightMatcherFactory2: MatcherFactory2[T, TYPECLASS1, TYPECLASS2])(implicit typeClass1: TYPECLASS1[T], typeClass2: TYPECLASS2[T]) { MustMethodHelper.mustMatcher(left, rightMatcherFactory2.matcher) } /** * This method enables syntax such as the following: * *
     * a mustEqual b
     *   ^
     * 
*/ def mustEqual(right: Any)(implicit equality: Equality[T]) { if (!equality.areEqual(left, right)) { val (leftee, rightee) = Suite.getObjectsForFailureMessage(left, right) throw newTestFailedException(FailureMessages("didNotEqual", leftee, rightee)) } } /** * This method enables syntax such as the following: * *
     * result mustEqual 7.1 +- 0.2
     *        ^
     * 
*/ def mustEqual(interval: Interval[T]) { if (!interval.isWithin(left)) { throw newTestFailedException(FailureMessages("didNotEqualPlusOrMinus", left, interval.pivot, interval.tolerance)) } } /** * This method enables syntax such as the following: * *
     * result mustEqual null
     *        ^
     * 
*/ def mustEqual(right: Null)(implicit ev: T <:< AnyRef) { if (left != null) { throw newTestFailedException(FailureMessages("didNotEqualNull", left)) } } /** * This method enables syntax such as the following: * *
     * result must not equal (3)
     *        ^
     * 
*/ def must(notWord: NotWord): ResultOfNotWordForAny[T] = new ResultOfNotWordForAny[T](left, false) // In 2.10, will work with AnyVals. TODO: Also, Need to ensure Char works /** * This method enables syntax such as the following: * *
     * a must === (b)
     *        ^
     * 
*/ def must[U](inv: TripleEqualsInvocation[U])(implicit constraint: EqualityConstraint[T, U]) { if ((constraint.areEqual(left, inv.right)) != inv.expectingEqual) throw newTestFailedException( FailureMessages( if (inv.expectingEqual) "didNotEqual" else "equaled", left, inv.right ) ) } /** * This method enables syntax such as the following: * *
     * result must === (100 +- 1)
     *        ^
     * 
*/ def must(inv: TripleEqualsInvocationOnInterval[T])(implicit ev: Numeric[T]) { if ((inv.interval.isWithin(left)) != inv.expectingEqual) throw newTestFailedException( FailureMessages( if (inv.expectingEqual) "didNotEqualPlusOrMinus" else "equaledPlusOrMinus", left, inv.interval.pivot, inv.interval.tolerance ) ) } // TODO: Need to make sure this works in inspector shorthands. I moved this // up here from NumericMustWrapper. /** * This method enables syntax such as the following: * *
     * result must be a aMatcher
     *        ^
     * 
*/ def must(beWord: BeWord): ResultOfBeWordForAny[T] = new ResultOfBeWordForAny(left, true) /** * This method enables syntax such as the following: * *
     * aDouble mustBe 8.8
     *         ^
     * 
*/ def mustBe(right: T) { if (!areEqualComparingArraysStructurally(left, right)) { val (leftee, rightee) = Suite.getObjectsForFailureMessage(left, right) throw newTestFailedException(FailureMessages("wasNotEqualTo", leftee, rightee)) } } /** * This method enables syntax such as the following: * *
     * 5 mustBe < (7) 
     *   ^
     * 
*/ def mustBe(comparison: ResultOfLessThanComparison[T]) { if (!comparison(left)) { throw newTestFailedException( FailureMessages( "wasNotLessThan", left, comparison.right ) ) } } /** * This method enables syntax such as the following: * *
     * 8 mustBe > (7) 
     *   ^
     * 
*/ def mustBe(comparison: ResultOfGreaterThanComparison[T]) { if (!comparison(left)) { throw newTestFailedException( FailureMessages( "wasNotGreaterThan", left, comparison.right ) ) } } /** * This method enables syntax such as the following: * *
     * 5 mustBe <= (7) 
     *   ^
     * 
*/ def mustBe(comparison: ResultOfLessThanOrEqualToComparison[T]) { if (!comparison(left)) { throw newTestFailedException( FailureMessages( "wasNotLessThanOrEqualTo", left, comparison.right ) ) } } /** * This method enables syntax such as the following: * *
     * 8 mustBe >= (7) 
     *   ^
     * 
*/ def mustBe(comparison: ResultOfGreaterThanOrEqualToComparison[T]) { if (!comparison(left)) { throw newTestFailedException( FailureMessages( "wasNotGreaterThanOrEqualTo", left, comparison.right ) ) } } /** * This method enables the following syntax, where odd refers to a BeMatcher[Int]: * *
testing
     * 1 mustBe odd
     *   ^
     * 
*/ def mustBe(beMatcher: BeMatcher[T]) { val result = beMatcher.apply(left) if (!result.matches) throw newTestFailedException(result.failureMessage) } /** * This method enables syntax such as the following: * *
     * result mustBe 7.1 +- 0.2
     *        ^
     * 
*/ def mustBe(interval: Interval[T]) { if (!interval.isWithin(left)) { throw newTestFailedException(FailureMessages("wasNotPlusOrMinus", left, interval.pivot, interval.tolerance)) } } /** * This method enables syntax such as the following: * *
     * result mustBe sorted
     *        ^
     * 
*/ def mustBe(right: SortedWord)(implicit sortable: Sortable[T]) { if (!sortable.isSorted(left)) throw newTestFailedException(FailureMessages("wasNotSorted", left)) } /** * This method enables syntax such as the following: * *
     * result mustNot be (3)
     *        ^
     * 
*/ def mustNot(beWord: BeWord): ResultOfBeWordForAny[T] = new ResultOfBeWordForAny(left, false) /** * This method enables syntax such as the following: * *
     * result mustNot (be (3))
     *        ^
     * 
*/ def mustNot(rightMatcherX1: Matcher[T]) { MustMethodHelper.mustNotMatcher(left, rightMatcherX1) } /** * This method enables syntax such as the following: * *
     * result must have length (3)
     *        ^
     * result must have size (3)
     *        ^
     * 
*/ def must(haveWord: HaveWord): ResultOfHaveWordForExtent[T] = new ResultOfHaveWordForExtent(left, true) /** * This method enables syntax such as the following: * *
     * result mustBe null
     *        ^
     * 
*/ def mustBe(right: Null)(implicit ev: T <:< AnyRef) { if (left != null) { throw newTestFailedException(FailureMessages("wasNotNull", left)) } } /** * This method enables syntax such as the following: * *
     * result mustBe theSameInstanceAs (anotherObject)
     *        ^
     * 
*/ def mustBe(resultOfSameInstanceAsApplication: ResultOfTheSameInstanceAsApplication)(implicit ev: T <:< AnyRef) { if (resultOfSameInstanceAsApplication.right ne left) { throw newTestFailedException( FailureMessages( "wasNotSameInstanceAs", left, resultOfSameInstanceAsApplication.right ) ) } } // TODO: Remember to write tests for inspector shorthands uncovering the bug below, always a empty because always true true passed to matchSym /** * This method enables the following syntax: * *
     * list mustBe 'empty
     *      ^
     * 
*/ def mustBe(symbol: Symbol)(implicit ev: T <:< AnyRef) { val matcherResult = matchSymbolToPredicateMethod(left, symbol, false, true) if (!matcherResult.matches) throw newTestFailedException(matcherResult.failureMessage) } /** * This method enables the following syntax: * *
     * list mustBe a ('empty)
     *      ^
     * 
*/ def mustBe(resultOfAWordApplication: ResultOfAWordToSymbolApplication)(implicit ev: T <:< AnyRef) { val matcherResult = matchSymbolToPredicateMethod(left, resultOfAWordApplication.symbol, true, true) if (!matcherResult.matches) { throw newTestFailedException( matcherResult.failureMessage ) } } /** * This method enables the following syntax: * *
     * list mustBe an ('empty)
     *      ^
     * 
*/ def mustBe(resultOfAnWordApplication: ResultOfAnWordToSymbolApplication)(implicit ev: T <:< AnyRef) { val matcherResult = matchSymbolToPredicateMethod(left, resultOfAnWordApplication.symbol, true, false) if (!matcherResult.matches) { throw newTestFailedException( matcherResult.failureMessage ) } } /** * This method enables the following syntax, where excellentRead refers to a BePropertyMatcher[Book]: * *
     * programmingInScala mustBe excellentRead
     *                    ^
     * 
*/ def mustBe(bePropertyMatcher: BePropertyMatcher[T])(implicit ev: T <:< AnyRef) { // TODO: Try expanding this to 2.10 AnyVal val result = bePropertyMatcher(left) if (!result.matches) throw newTestFailedException(FailureMessages("wasNot", left, UnquotedString(result.propertyName))) } /** * This method enables the following syntax, where goodRead refers to a BePropertyMatcher[Book]: * *
     * programmingInScala mustBe a (goodRead)
     *                    ^
     * 
*/ def mustBe[U >: T](resultOfAWordApplication: ResultOfAWordToBePropertyMatcherApplication[U])(implicit ev: T <:< AnyRef) {// TODO: Try expanding this to 2.10 AnyVal val result = resultOfAWordApplication.bePropertyMatcher(left) if (!result.matches) { throw newTestFailedException(FailureMessages("wasNotA", left, UnquotedString(result.propertyName))) } } /** * This method enables the following syntax, where excellentRead refers to a BePropertyMatcher[Book]: * *
     * programmingInScala mustBe an (excellentRead)
     *                    ^
     * 
*/ def mustBe[U >: T](resultOfAnWordApplication: ResultOfAnWordToBePropertyMatcherApplication[U])(implicit ev: T <:< AnyRef) {// TODO: Try expanding this to 2.10 AnyVal val result = resultOfAnWordApplication.bePropertyMatcher(left) if (!result.matches) { throw newTestFailedException(FailureMessages("wasNotAn", left, UnquotedString(result.propertyName))) } } /* def mustBe[U](right: AType[U]) { if (!right.isAssignableFromClassOf(left)) { throw newTestFailedException(FailureMessages("wasNotAnInstanceOf", left, UnquotedString(right.className))) } } */ /** * This method enables syntax such as the following: * *
     * xs must contain oneOf (1, 2, 3)
     *    ^
     * 
*/ def must(containWord: ContainWord): ResultOfContainWord[T] = { new ResultOfContainWord(left, true) } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * *

* This class is used in conjunction with an implicit conversion to enable must methods to * be invoked on Strings. *

* * @author Bill Venners */ final class StringMustWrapper(val left: String) extends AnyMustWrapper(left) with StringMustWrapperForVerb { /** * This method enables syntax such as the following: * *
     * string must include regex ("hi")
     *        ^
     * 
*/ def must(includeWord: IncludeWord): ResultOfIncludeWordForString = { new ResultOfIncludeWordForString(left, true) } /** * This method enables syntax such as the following: * *
     * string must startWith regex ("hello")
     *        ^
     * 
*/ def must(startWithWord: StartWithWord): ResultOfStartWithWordForString = { new ResultOfStartWithWordForString(left, true) } /** * This method enables syntax such as the following: * *
     * string must endWith regex ("world")
     *        ^
     * 
*/ def must(endWithWord: EndWithWord): ResultOfEndWithWordForString = { new ResultOfEndWithWordForString(left, true) } /** * This method enables syntax such as the following: * *
     * string must fullyMatch regex ("""(-)?(\d+)(\.\d*)?""")
     *        ^
     * 
*/ def must(fullyMatchWord: FullyMatchWord): ResultOfFullyMatchWordForString = { new ResultOfFullyMatchWordForString(left, true) } /** * This method enables syntax such as the following: * *
     * string must not have length (3)
     *        ^
     * 
*/ override def must(notWord: NotWord): ResultOfNotWordForString = { new ResultOfNotWordForString(left, false) } /** * This method enables syntax such as the following: * *
     * string must fullyMatch regex ("a(b*)c" withGroup "bb") 
     *                                          ^
     * 
*/ def withGroup(group: String) = new RegexWithGroups(left.r, IndexedSeq(group)) /** * This method enables syntax such as the following: * *
     * string must fullyMatch regex ("a(b*)(c*)" withGroups ("bb", "cc"))
     *                                          ^
     * 
*/ def withGroups(groups: String*) = new RegexWithGroups(left.r, IndexedSeq(groups: _*)) /** * This method enables syntax such as the following: * *
     * string mustNot fullyMatch regex ("""(-)?(\d+)(\.\d*)?""")
     *        ^
     * 
*/ def mustNot(fullyMatchWord: FullyMatchWord): ResultOfFullyMatchWordForString = new ResultOfFullyMatchWordForString(left, false) /** * This method enables syntax such as the following: * *
     * string mustNot startWith regex ("hello")
     *        ^
     * 
*/ def mustNot(startWithWord: StartWithWord): ResultOfStartWithWordForString = new ResultOfStartWithWordForString(left, false) /** * This method enables syntax such as the following: * *
     * string mustNot endWith regex ("world")
     *        ^
     * 
*/ def mustNot(endWithWord: EndWithWord): ResultOfEndWithWordForString = new ResultOfEndWithWordForString(left, false) /** * This method enables syntax such as the following: * *
     * string mustNot include regex ("hi")
     *        ^
     * 
*/ def mustNot(includeWord: IncludeWord): ResultOfIncludeWordForString = new ResultOfIncludeWordForString(left, false) } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * *

* This class is used in conjunction with an implicit conversion to enable withGroup and withGroups methods to * be invoked on Regexs. *

* * @author Bill Venners */ final class RegexWrapper(regex: Regex) { /** * This method enables syntax such as the following: * *
     * regex must fullyMatch regex ("a(b*)c" withGroup "bb") 
     *                                         ^
     * 
*/ def withGroup(group: String) = new RegexWithGroups(regex, IndexedSeq(group)) /** * This method enables syntax such as the following: * *
     * regex must fullyMatch regex ("a(b*)(c*)" withGroups ("bb", "cc"))
     *                                            ^
     * 
*/ def withGroups(groups: String*) = new RegexWithGroups(regex, IndexedSeq(groups: _*)) } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * *

* This class is used in conjunction with an implicit conversion to enable must methods to * be invoked on objects of type scala.Array[T]. *

* * @author Bill Venners */ final class ArrayMustWrapper[E](left: Array[E]) extends AnyMustWrapper(left) { /** * This method enables syntax such as the following, where positiveNumber is a AMatcher: * *
     * array must contain a positiveNumber
     *       ^
     * 
*/ override def must(containWord: ContainWord) = new ResultOfContainWordForArray[E](left, true) /** * This method enables syntax such as the following: * *
     * array must not have length (3)
     *       ^
     * 
*/ override def must(notWord: NotWord): ResultOfNotWordForArray[E] = new ResultOfNotWordForArray(left, false) } // TODO: Am I doing conversions on immutable.GenTraversable and immutable.GenSeq? If so, write a test that fails and make it general. /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * *

* This class is used in conjunction with an implicit conversion to enable must methods to * be invoked on objects of type scala.collection.GenMap[K, V]. *

* * @author Bill Venners */ final class MapMustWrapper[K, V, L[_, _] <: scala.collection.GenMap[_, _]](left: L[K, V]) extends AnyMustWrapper(left) { /** * This method enables syntax such as the following: * *
     * map must contain key (10)
     *     ^
     * 
*/ override def must(containWord: ContainWord): ResultOfContainWordForMap[K, V, L] = { new ResultOfContainWordForMap(left.asInstanceOf[GenMap[K, V]], true) } /** * This method enables syntax such as the following: * *
     * map must not have size (3)
     *     ^
     * 
*/ override def must(notWord: NotWord): ResultOfNotWordForGenMap[K, V, L] = { new ResultOfNotWordForGenMap(left.asInstanceOf[L[K, V]], false) } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * *

* This class is used in conjunction with an implicit conversion to enable must methods to * be invoked on objects of type scala.Collection[T]. *

* * @author Bill Venners */ class TraversableMustWrapper[E, L[_] <: scala.collection.GenTraversable[_]](left: L[E]) extends AnyMustWrapper(left) { /** * This method enables syntax such as the following: * *
     * traversable must contain theSameElementsAs anotherTraversable
     *             ^
     * 
*/ override def must(containWord: ContainWord): ResultOfContainWordForTraversable[E, L] = new ResultOfContainWordForTraversable(left.asInstanceOf[scala.collection.GenTraversable[E]], true) /** * This method enables syntax such as the following: * *
     * traversable must not have size (3)
     *             ^
     * 
*/ override def must(notWord: NotWord): ResultOfNotWordForGenTraversable[E, L] = new ResultOfNotWordForGenTraversable(left, false) /** * This method enables syntax such as the following: * *
     * xs.loneElement must be > 9
     *    ^
     * 
*/ def loneElement: E = { if (left.size == 1) left.head.asInstanceOf[E] // Why do I need to cast? else throw newTestFailedException( FailureMessages( "notLoneElement", left, left.size) ) } } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * *

* This class is used in conjunction with an implicit conversion to enable must methods to * be invoked on objects of type java.util.Collection[T]. *

* * @author Bill Venners */ final class JavaCollectionMustWrapper[E, L[_] <: java.util.Collection[_]](left: L[E]) extends AnyMustWrapper(left) { /** * This method enables syntax such as the following: * *
     * javaCollection must contain theSameElementsAs anotherSeq
     *                ^
     * 
*/ override def must(containWord: ContainWord): ResultOfContainWordForJavaCollection[E, L] = // new ResultOfContainWordForJavaCollection(left.asInstanceOf[java.util.Collection[E]], true) new ResultOfContainWordForJavaCollection(left, true) /** * This method enables syntax such as the following: * *
     * javaCollection must not have size (3)
     *                ^
     * 
*/ override def must(notWord: NotWord): ResultOfNotWordForJavaCollection[E, L] = new ResultOfNotWordForJavaCollection(left, false) } /** * This class is part of the ScalaTest matchers DSL. Please see the documentation for Matchers for an overview of * the matchers DSL. * *

* This class is used in conjunction with an implicit conversion to enable must methods to * be invoked on objects of type java.util.Map[K, V]. *

* * @author Bill Venners */ final class JavaMapMustWrapper[K, V, L[_, _] <: java.util.Map[_, _]](left: L[K, V]) extends AnyMustWrapper(left) { /** * This method enables syntax such as the following: * *
     * javaMap must contain value (3)
     *         ^
     * 
*/ override def must(containWord: ContainWord): ResultOfContainWordForJavaMap[K, V, L] = { new ResultOfContainWordForJavaMap(left, true) } /** * This method enables syntax such as the following: * *
     * javaMap must not have length (3)
     *         ^
     * 
*/ override def must(notWord: NotWord): ResultOfNotWordForJavaMap[K, V, L] = { new ResultOfNotWordForJavaMap[K, V, L](left, false) } } /** * Implicitly converts an object of type T to a AnyMustWrapper[T], * to enable must methods to be invokable on that object. */ implicit def convertToAnyMustWrapper[T](o: T): AnyMustWrapper[T] = new AnyMustWrapper(o) /** * Implicitly converts an object of type scala.Collection[T] to a CollectionMustWrapper, * to enable must methods to be invokable on that object. */ implicit def convertToTraversableMustWrapper[E, L[_] <: scala.collection.GenTraversable[_]](o: L[E]): TraversableMustWrapper[E, L] = new TraversableMustWrapper[E, L](o) /** * Implicitly converts an object of type scala.Array[T] to a ArrayMustWrapper[T], * to enable must methods to be invokable on that object. */ implicit def convertToArrayMustWrapper[T](o: Array[T]): ArrayMustWrapper[T] = new ArrayMustWrapper[T](o) /** * Implicitly converts an object of type scala.collection.GenMap[K, V] to a MapMustWrapper[K, V], * to enable must methods to be invokable on that object. */ implicit def convertToMapMustWrapper[K, V, L[_, _] <: scala.collection.GenMap[_, _]](o: L[K, V]): MapMustWrapper[K, V, L] = new MapMustWrapper[K, V, L](o) /** * Implicitly converts an object of type java.lang.String to a StringMustWrapper, * to enable must methods to be invokable on that object. */ implicit override def convertToStringMustWrapper(o: String): StringMustWrapper = new StringMustWrapper(o) /** * Implicitly converts an object of type scala.util.matching.Regex to a RegexWrapper, * to enable withGroup and withGroups methods to be invokable on that object. */ implicit def convertToRegexWrapper(o: Regex): RegexWrapper = new RegexWrapper(o) /** * Implicitly converts an object of type java.util.Collection[T] to a JavaCollectionMustWrapper[T], * to enable must methods to be invokable on that object. */ implicit def convertToJavaCollectionMustWrapper[E, L[_] <: java.util.Collection[_]](o: L[E]): JavaCollectionMustWrapper[E, L] = new JavaCollectionMustWrapper[E, L](o) /** * Implicitly converts an object of type java.util.Map[K, V] to a JavaMapMustWrapper[K, V], * to enable must methods to be invokable on that object. */ implicit def convertToJavaMapMustWrapper[K, V, L[_, _] <: java.util.Map[_, _]](o: L[K, V]): JavaMapMustWrapper[K, V, L] = new JavaMapMustWrapper[K, V, L](o) /** * Turn off implicit conversion of LoneElement, so that if user accidentally mixin LoneElement it does conflict with convertToTraversableMustWrapper */ override def convertToTraversableLoneElementWrapper[T](xs: scala.collection.GenTraversable[T]): LoneElementTraversableWrapper[T] = new LoneElementTraversableWrapper[T](xs) } /** * Companion object that facilitates the importing of Matchers members as an alternative to mixing it the trait. One use case is to import Matchers members so you can use * them in the Scala interpreter: * *
 * $scala -classpath scalatest.jar
 * Welcome to Scala version 2.7.3.final (Java HotSpot(TM) Client VM, Java 1.5.0_16).
 * Type in expressions to have them evaluated.
 * Type :help for more information.
 * 
 * scala> import org.scalatest.Matchers._
 * import org.scalatest.Matchers._
 * 
 * scala> 1 must equal (2)
 * org.scalatest.TestFailedException: 1 did not equal 2
 * 	at org.scalatest.matchers.Helper$.newTestFailedException(Matchers.template:40)
 * 	at org.scalatest.matchers.MustMatchers$MustMethodHelper$.mustMatcher(MustMatchers.scala:826)
 * 	at org.scalatest.matchers.MustMatchers$IntMustWrapper.must(MustMatchers.scala:1123)
 * 	at .<init>(<console>:9)
 * 	at .<clinit>(<console>)
 * 	at RequestR...
 *
 * scala> "hello, world" must startWith ("hello")
 * 
 * scala> 7 must (be >= (3) and not be <= (7))
 * org.scalatest.TestFailedException: 7 was greater than or equal to 3, but 7 was less than or equal to 7
 * 	at org.scalatest.matchers.Helper$.newTestFailedException(Matchers.template:40)
 * 	at org.scalatest.matchers.MustMatchers$MustMethodHelper$.mustMatcher(MustMatchers.scala:826)
 * 	at org.scalatest.matchers.MustMatchers$IntMustWrapper.must(MustMatchers.scala:1123)
 * 	at .<init>(...
 * 
* * @author Bill Venners */ @deprecated("Please use org.scalatest.Matchers instead.") object MustMatchers extends MustMatchers




© 2015 - 2025 Weber Informatics LLC | Privacy Policy