Collection assertions¶
Assertions and the collection pipeline for list, tuple, set, and frozenset values.
Collection assertions mixin.
is_iterable ¶
Asserts that val is iterable.
Examples:
Usage:
assert_that('foo').is_iterable()
assert_that(['a', 'b']).is_iterable()
assert_that((1, 2, 3)).is_iterable()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not iterable |
Source code in assertpy2/collection.py
is_not_iterable ¶
Asserts that val is not iterable.
Examples:
Usage:
assert_that(1).is_not_iterable()
assert_that(123.4).is_not_iterable()
assert_that(True).is_not_iterable()
assert_that(None).is_not_iterable()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is iterable |
Source code in assertpy2/collection.py
is_subset_of ¶
Asserts that val is iterable and a subset of the given superset (or supersets).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*supersets
|
object
|
the expected superset (or supersets) |
()
|
Examples:
Usage:
assert_that('foo').is_subset_of('abcdefghijklmnopqrstuvwxyz')
assert_that(['a', 'b']).is_subset_of(['a', 'b', 'c'])
assert_that((1, 2, 3)).is_subset_of([1, 2, 3, 4])
assert_that({'a': 1, 'b': 2}).is_subset_of({'a': 1, 'b': 2, 'c': 3})
assert_that({'a', 'b'}).is_subset_of({'a', 'b', 'c'})
# or multiple supersets (as comma-separated args)
assert_that('aBc').is_subset_of('abc', 'ABC')
assert_that((1, 2, 3)).is_subset_of([1, 3, 5], [2, 4, 6])
assert_that({'a': 1, 'b': 2}).is_subset_of({'a': 1, 'c': 3}) # fails
# Expected <{'a': 1, 'b': 2}> to be subset of <{'a': 1, 'c': 3}>, but <{'b': 2}> was missing.
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not subset of given superset (or supersets) |
Source code in assertpy2/collection.py
is_sorted ¶
Asserts that val is iterable and is sorted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
function
|
the one-arg function to extract the sort comparison key. Defaults to
|
lambda item: item
|
reverse
|
bool
|
if |
False
|
Examples:
Usage:
assert_that(['a', 'b', 'c']).is_sorted()
assert_that((1, 2, 3)).is_sorted()
# with a key function
assert_that('aBc').is_sorted(key=str.lower)
# reverse order
assert_that(['c', 'b', 'a']).is_sorted(reverse=True)
assert_that((3, 2, 1)).is_sorted(reverse=True)
assert_that((1, 2, 3, 4, -5, 6)).is_sorted() # fails
# Expected <(1, 2, 3, 4, -5, 6)> to be sorted, but subset <4, -5> at index 3 is not.
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not sorted |
Source code in assertpy2/collection.py
has_same_size_as ¶
Asserts that val has the same length as other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
a sized object whose |
required |
Examples:
Usage:
assert_that([1, 2, 3]).has_same_size_as((4, 5, 6))
assert_that('foo').has_same_size_as([1, 2, 3])
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val and other do not have the same length |
TypeError
|
if other is not a sized object |
Source code in assertpy2/collection.py
filtered_on ¶
Returns a new builder with elements matching the predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[Any], bool]
|
callable or Matcher. If a Matcher, uses |
required |
Examples:
Usage:
assert_that([1, -2, 3]).filtered_on(lambda x: x > 0).is_length(2)
assert_that(items).filtered_on(match.is_positive()).is_not_empty()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns a new instance with the filtered list as val |
Source code in assertpy2/collection.py
mapped ¶
Returns a new builder with each element transformed by func.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[Any], Any]
|
callable applied to each element. |
required |
Examples:
Usage:
assert_that(["a", "b"]).mapped(str.upper).contains("A")
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns a new instance with the mapped list as val |
Source code in assertpy2/collection.py
flat_mapped ¶
Returns a new builder with each element expanded and flattened by func.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[Any], Iterable[Any]]
|
callable returning an iterable for each element. |
required |
Examples:
Usage:
assert_that(["ab", "cd"]).flat_mapped(list).contains("a", "c")
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns a new instance with the flattened list as val |
Source code in assertpy2/collection.py
first ¶
Returns a new builder with the first element of val.
Examples:
Usage:
assert_that([10, 20, 30]).first().is_equal_to(10)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns a new instance with the first element as val |
Raises:
| Type | Description |
|---|---|
ValueError
|
if val is empty |
Source code in assertpy2/collection.py
last ¶
Returns a new builder with the last element of val.
Examples:
Usage:
assert_that([10, 20, 30]).last().is_equal_to(30)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns a new instance with the last element as val |
Raises:
| Type | Description |
|---|---|
ValueError
|
if val is empty |
Source code in assertpy2/collection.py
element ¶
Returns a new builder with the element at the given index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
zero-based index. |
required |
Examples:
Usage:
assert_that([10, 20, 30]).element(1).is_equal_to(20)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns a new instance with the selected element as val |
Raises:
| Type | Description |
|---|---|
IndexError
|
if index is out of range |
Source code in assertpy2/collection.py
single ¶
Returns a new builder with the only element of val.
Examples:
Usage:
assert_that([42]).single().is_equal_to(42)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns a new instance with the single element as val |
Raises:
| Type | Description |
|---|---|
ValueError
|
if val is empty or has more than one element |