Collection Helpers
- areSame(actual: Sequence, expected: Sequence, sort: bool = False, sort_function: Callable[[Any], Any] = None) bool
Use
areSame()to assert that two sequences contain the same elements in the same order.Check if two sequences contain the same elements in the same order.
- Parameters:
actaul (Sequence[Any]|None) – The sequence to check
expected (Sequence[Any]|None) – The sequence to compare against
sort (Optional[bool]) – Sort sequences before performing comparisons.
sort_function (Optional[Callable[[Any], Any]]) – Custom function to use when sorting.
- Returns bool:
True if the sequences contain the same elements in the same order, False otherwise.
Example
from punit import collections
a = [1,2,3]
b = [1,2,3]
c = [3,2,1]
assert collections.areSame(a, b)
assert not collections.areSame(b, c)
# and some less obvious behaviors
assert collections.areSame(a, a)
assert collections.areSame(None, None)
assert not collections.areSame(a, None)
assert not collections.areSame(None, b)
- hasLength(sequence: Sequence, expected: int) bool
Check if a sequence has the expected number of elements.
- Parameters:
sequence (Sequence[Any]|None) – The sequence to check
expected (int|None) – The expected number of elements
- Returns bool:
True if the sequence has exactly the expected number of elements, False otherwise
Example
from punit import collections
a = [1]
b = [1,2]
c = [3,2,1]
assert collections.hasLength(a, 1)
assert collections.hasLength(b, 2)
assert collections.hasLength(c, 3)
assert not collections.hasLength(a, 2)
assert not collections.hasLength(b, 3)
- isNoneOrEmpty(sequence: Sequence) bool
Check if a sequence is None or empty.
- Parameters:
sequence (Sequence[Any]|None) – The sequence to check
- Returns bool:
True if the sequence is None or empty, False otherwise
Example
from punit import collections
a = [1,2,3]
b = []
c = None
assert collections.isNoneOrEmpty(a)
assert collections.isNoneOrEmpty(b)
assert collections.isNoneOrEmpty(c)
assert not collections.isNoneOrEmpty(a)
assert not collections.isNoneOrEmpty(b)
assert not collections.isNoneOrEmpty(c)