1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
| | #! /usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Copyright Red Hat
# Author: David Gibson <david@gibson.dropbear.id.au>
"""
Test writer facing interface to avocodo-classless
"""
from collections import Counter
from .manifest import manifest_add
def test(func):
"""Function decorator to mark a function as a classless test"""
manifest_add(func.__module__, func.__name__, func)
return func
def test_output(*tests):
"""Decorate a function to test its output with a list of other functions"""
def dec(func):
for testfn in tests:
manifest_add(func.__module__,
f"{testfn.__name__}∘{func.__name__}",
lambda testfn=testfn: testfn(func()))
return func
return dec
#
# Assertion helpers without unnecessary OOP nonsense
#
def assert_eq(left, right):
"""assert_eq(left, right)
If left != right, fail with message showing both values
"""
assert (left == right), f'{left} != {right}'
def assert_in(member, group):
"""assert_in(member, group)
If member not in group, fail with a message showing both values
"""
assert member in group, f'{member} not in {group}'
def assert_eq_unordered(left, right):
"""assert_eq_unordered(left, right)
Fails if sequences left and right are different, ignoring order.
"""
assert Counter(left) == Counter(right), \
f'{left} != {right} (ignoring order)'
def assert_raises(exc, func, *args, **kwargs):
"""assert_raises(exc, func, *args, **kwargs)
If func(*args, **kwargs) does not raise exc, fail
"""
try:
func(*args, **kwargs)
raise AssertionError(f'Expected {exc.__name__} exception')
except exc:
pass
|