Assertion Helper Functions
- ornithology.in_order(items, expected)[source]
Attention
This function never asserts on its own! You must assert some condition on its return value.
Given an iterable of items and a list of expected items, return
True
if and only if the items occur in exactly the given order. Extra items may appear between expected items, but expected items may not appear out of order.When this function returns
False
, it emits a detailed log message at ERROR level showing a “match” display for debugging.
- ornithology.track_quantity(items, increment_condition, decrement_condition, initial_quantity=0, min_quantity=None, max_quantity=None, expected_quantity=None)[source]
Attention
This function never asserts on its own! You must assert some condition on its return value.
Given an iterable of items, track the value of a “quantity” as it changes over the items. The increment and decrement conditions are given as functions that will receive a single item as their argument and should return a boolean. If the increment (decrement) condition returns
True
for an item, the quantity is incremented (decremented).Both conditions are checked for every item, so an item may cause the quantity to both increment and decrement (in which case it would be reported as staying the same).
The parameters
min_quantity
,max_quantity
, andexpected_quantity
do not effect the return value, but they can cause an ERROR-level logging message to be printed when their condition is not satisfied at the end of tracking. The message shows the item-by-item tracking of the quantity.- Parameters:
items (
Iterable
[TypeVar
(T
)]) – The items to track a quantity over.increment_condition (
Callable
[[TypeVar
(T
)],bool
]) – If this condition evaluates toTrue
on an item, the quantity will be incremented.decrement_condition (
Callable
[[TypeVar
(T
)],bool
]) – If this condition evaluates toTrue
on an item, the quantity will be decremented.min_quantity (
Optional
[int
]) – If given, and the quantity is ever lower than this value, print the debug message.max_quantity (
Optional
[int
]) – If given, and the quantity is ever higher this value, print the debug message.expected_quantity (
Optional
[int
]) – If given, and does not appear in the quantity history, print the debug message.
- Returns:
history – The “history” of the tracked quantity, one element for each entry in the items.
- Return type:
List[int]