Wrapper class that adds a loneElement
method to any collection type C
for which
an implicit Collection[C]
is available.
Implicit conversion that adds a loneElement
method to any collection type C
for which an
implicit Collection[C]
is available.
Implicit conversion that adds a loneElement
method to any collection type C
for which an
implicit Collection[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
Trait that provides an implicit conversion that adds to collection types a
loneElement
method, which will return the value of the lone element if the collection does indeed contain one and only one element, or throwTestFailedException
if 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 > 9
Or, using an assertion instead of a matcher expression:
assert(set.loneElement > 9)
The
loneElement
syntax can be used with any collection typeC
for which an implicitCollecting[C]
is available. ScalaTest provides implicitCollecting
instances forscala.collection.GenTraversable
,Array
, andjava.util.Collection
. You can enable theloneElement
syntax on other collection types by defining an implicitCollecting
instances for those types.If you want to use
loneElement
with ajava.util.Map
, first transform it to a set of entries withentrySet
, and if helpful, use ScalaTest'sEntry
class: