Collections#

Collection assertion helpers for comparing sequences and containers.

Provides utilities to check sequence equality, length bounds, and emptiness beyond Python’s built-in assert statement.

are_same(actual, expected, sort=False, sort_function=None)#

Check if two sequences contain the same elements in the same order.

Can optionally specify sort so that differences in order do not cause failure.

Return type:

bool

Args:

actual: The sequence to check. expected: The sequence to compare against. sort: Sort sequences before performing comparisons. sort_function: Custom function to use when sorting.

Returns:

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.are_same(a, b)
assert not collections.are_same(b, c)
assert collections.are_same(None, None)
assert not collections.are_same(a, None)
has_length(actual, min=None, max=None)#

Check if actual value’s length falls within the inclusive range [min, max].

Return type:

bool

Args:

actual: The sequence (or collection) to check. min: Inclusive lower bound on length (len(actual) >= min). max: Inclusive upper bound on length (len(actual) <= max).

Returns:

True if the length satisfies the bounds, False otherwise. When actual is None, returns True only when both bounds are effectively zero.

Example#

from punit import collections

a = [1]
b = [1, 2]

assert collections.has_length(a, min=1)
assert collections.has_length(b, min=2)
assert collections.has_length([1, 2, 3], max=3)
assert not collections.has_length(a, min=2)
is_none_or_empty(actual)#

Check if actual value is None or empty.

Return type:

bool

Args:

actual: The sequence to check.

Returns:

True if the value is None or empty, False otherwise

Example#

from punit import collections

assert not collections.is_none_or_empty([1, 2, 3])
assert collections.is_none_or_empty([])
assert collections.is_none_or_empty(None)
areSame(actual, expected, sort=False, sort_function=None)#

Check if two sequences contain the same elements in the same order.

Can optionally specify sort so that differences in order do not cause failure.

Return type:

bool

Args:

actual: The sequence to check. expected: The sequence to compare against. sort: Sort sequences before performing comparisons. sort_function: Custom function to use when sorting.

Returns:

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.are_same(a, b)
assert not collections.are_same(b, c)
assert collections.are_same(None, None)
assert not collections.are_same(a, None)
hasLength(actual, min=None, max=None)#

Check if actual value’s length falls within the inclusive range [min, max].

Return type:

bool

Args:

actual: The sequence (or collection) to check. min: Inclusive lower bound on length (len(actual) >= min). max: Inclusive upper bound on length (len(actual) <= max).

Returns:

True if the length satisfies the bounds, False otherwise. When actual is None, returns True only when both bounds are effectively zero.

Example#

from punit import collections

a = [1]
b = [1, 2]

assert collections.has_length(a, min=1)
assert collections.has_length(b, min=2)
assert collections.has_length([1, 2, 3], max=3)
assert not collections.has_length(a, min=2)
isNoneOrEmpty(actual)#

Check if actual value is None or empty.

Return type:

bool

Args:

actual: The sequence to check.

Returns:

True if the value is None or empty, False otherwise

Example#

from punit import collections

assert not collections.is_none_or_empty([1, 2, 3])
assert collections.is_none_or_empty([])
assert collections.is_none_or_empty(None)