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 Java Map for which
an implicit Collecting[org.scalatest.Entry, java.util.Map] is available.
Wrapper class that adds a loneElement method to String 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.
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:
set.loneElement should be > 9Or, using an assertion instead of a matcher expression:
assert(set.loneElement > 9)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: