Wrapper class that adds a loneElement method to any collection type C for which
an implicit Collecting[C] is available.
Wrapper class that adds a loneElement method to any collection type C for which
an implicit Collecting[C] is available.
Through the implicit conversion provided by trait LoneElement, this class allows you to make statements like:
trav.loneElement should be > 9
the element type of the collection on which to add the loneElement method
the "collection type constructor" for the collection on which to add the loneElement method
Wrapper class that adds a loneElement method to Java Map for which
an implicit Collecting[org.scalactic.Entry, java.util.Map] is available.
Wrapper class that adds a loneElement method to Java Map for which
an implicit Collecting[org.scalactic.Entry, java.util.Map] is available.
Through the implicit conversion provided by trait LoneElement, this class allows you to make statements like:
jmap.loneElement.getKey should be > 9
the element type of the Java Map key on which to add the loneElement method
the element type of the Java Map value on which to add the loneElement method
the "Java Map type constructor" for the collection on which to add the loneElement method
Wrapper class that adds a loneElement method to String for which an
implicit Collecting[C] is available.
Wrapper class that adds a loneElement method to String for which an
implicit Collecting[C] is available.
Through the implicit conversion provided by trait LoneElement, this class allows you to make statements like:
"9".loneElement should be ('9')
Implicit conversion that adds a loneElement method to any collection type C for which an
implicit Collecting[C] is available.
Implicit conversion that adds a loneElement method to any collection type C for which an
implicit Collecting[C] is available.
the element type of the collection on which to add the loneElement method
the "collection type constructor" for the collection on which to add the loneElement method
the collection on which to add the loneElement method
a typeclass that enables the loneElement syntax
Implicit conversion that adds a loneElement method to String for which an
implicit Collecting[C] is available.
Implicit conversion that adds a loneElement method to String for which an
implicit Collecting[C] is available.
the String to wrap
a typeclass that enables the loneElement syntax
Trait that provides an implicit conversion that adds to collection types a
loneElementmethod, which will return the value of the lone element if the collection does indeed contain one and only one element, or throwTestFailedExceptionif not.This construct allows you to express in one statement that a collection should contain one and only one element and that the element value should meet some expectation. Here's an example:
Or, using an assertion instead of a matcher expression:
The
loneElementsyntax can be used with any collection typeCfor which an implicitCollecting[C]is available. ScalaTest provides implicitCollectinginstances forscala.collection.GenTraversable,Array, andjava.util.Collection. You can enable theloneElementsyntax on other collection types by defining an implicitCollectinginstances for those types.If you want to use
loneElementwith ajava.util.Map, first transform it to a set of entries withentrySet, and if helpful, use ScalaTest'sEntryclass:scala> import org.scalatest._ import org.scalatest._ scala> import LoneElement._ import LoneElement._ scala> import Matchers._ import Matchers._ scala> val jmap = new java.util.HashMap[String, Int] jmap: java.util.HashMap[String,Int] = {} scala> jmap.put("one", 1) res0: Int = 0 scala> jmap.entrySet.loneElement should be (Entry("one", 1))