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) |
Note
Where one element of val is not found, the failure says what is nearest to it: the closest row among the items of the superset, and on a line under the message why it is not there, where one fact about it says so.
Source code in assertpy2/collection.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
is_sorted ¶
is_sorted(
key: Callable[[Any], Any] = lambda item: item,
reverse: bool = False,
*,
allow_empty: bool = False,
) -> Self
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
has_size_greater_than ¶
Asserts that val has a length strictly greater than the given size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
SupportsIndex
|
the size val's length must exceed |
required |
Examples:
Usage:
assert_that([1, 2, 3]).has_size_greater_than(2)
assert_that('foo').has_size_greater_than(1)
assert_that({'a': 1, 'b': 2}).has_size_greater_than(1)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val's length is not greater than the given size |
TypeError
|
if the given arg is not an int |
ValueError
|
if the given arg is negative |
Source code in assertpy2/collection.py
has_size_less_than ¶
Asserts that val has a length strictly less than the given size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
SupportsIndex
|
the size val's length must stay under |
required |
Examples:
Usage:
assert_that([1, 2, 3]).has_size_less_than(4)
assert_that('foo').has_size_less_than(5)
assert_that({'a': 1, 'b': 2}).has_size_less_than(3)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val's length is not less than the given size |
TypeError
|
if the given arg is not an int |
ValueError
|
if the given arg is negative |
Source code in assertpy2/collection.py
has_size_between ¶
Asserts that val has a length between low and high (both inclusive).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
low
|
SupportsIndex
|
the inclusive lower size bound |
required |
high
|
SupportsIndex
|
the inclusive upper size bound |
required |
Examples:
Usage:
assert_that([1, 2, 3]).has_size_between(1, 5)
assert_that('foo').has_size_between(3, 3)
assert_that({'a': 1, 'b': 2}).has_size_between(0, 2)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val's length is not between low and high |
TypeError
|
if a given arg is not an int |
ValueError
|
if a given arg is negative, or low is greater than high |
Source code in assertpy2/collection.py
filtered_on ¶
Returns a new builder with elements matching the predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Matcher[Any] | 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
|
SupportsIndex
|
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 |