forked from exaloop/codon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunittest.codon
More file actions
133 lines (108 loc) · 4.55 KB
/
unittest.codon
File metadata and controls
133 lines (108 loc) · 4.55 KB
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
75
76
77
78
79
80
81
82
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
# Copyright (C) 2022-2026 Exaloop Inc. <https://exaloop.io>
# Simplified version of Python's unittest.TestCase to allow
# copy/pasting tests directly from CPython's test suite.
class TestCase:
def fail(self, standard_message: str, special_message: str = ""):
print("TEST FAILED:", special_message if special_message else standard_message)
def assertTrue(self, obj, msg=""):
if not bool(obj):
self.fail(f"expected object to be true: {obj}", msg)
def assertFalse(self, obj, msg=""):
if bool(obj):
self.fail(f"expected object to be false: {obj}", msg)
def assertEqual(self, first, second, msg=""):
result = first == second
if not result:
self.fail(f"expected equality of:\n 1: {first}\n 2: {second}", msg)
def assertNotEqual(self, first, second, msg=""):
result = first != second
if not result:
self.fail(f"expected inequality of:\n 1: {first}\n 2: {second}", msg)
def assertSequenceEqual(self, seq1, seq2, msg=""):
len1 = len(seq1)
len2 = len(seq2)
if len1 != len2:
self.fail(
f"expected equality of sequences (len1={len1}, len2={len2}):\n 1: {seq1}\n 2: {seq2}",
msg,
)
for i in range(len1):
a, b = seq1[i], seq2[i]
if a != b:
self.fail(
f"expected equality of sequences (diff at elem {i}):\n 1: {seq1}\n 2: {seq2}",
msg,
)
def assertIn(self, member, container, msg=""):
if member not in container:
self.fail(f"expected {member} to be in {container}", msg)
def assertNotIn(self, member, container, msg=""):
if member in container:
self.fail(f"expected {member} to not be in {container}", msg)
def assertIs(self, expr1, expr2, msg=""):
if expr1 is not expr2:
self.fail(f"expected {expr1} to be identical to {expr2}", msg)
def assertIsNot(self, expr1, expr2, msg=""):
if expr1 is expr2:
self.fail(f"expected {expr1} to not be identical to {expr2}", msg)
def assertIsNot(self, expr1, expr2, msg=""):
if expr1 is expr2:
self.fail(f"expected {expr1} to not be identical to {expr2}", msg)
def assertCountEqual(self, first, second, msg=""):
from collections import Counter
first_seq, second_seq = list(first), list(second)
first_counter = Counter(first_seq)
second_counter = Counter(second_seq)
if first_counter != second_counter:
self.fail(f"expected equal counts:\n 1: {first}\n 2: {second}", msg)
def assertLess(self, a, b, msg=""):
if not (a < b):
self.fail(f"expected less-than:\n 1: {a}\n 2: {b}", msg)
def assertLessEqual(self, a, b, msg=""):
if not (a <= b):
self.fail(f"expected less-than-or-equal:\n 1: {a}\n 2: {b}", msg)
def assertGreater(self, a, b, msg=""):
if not (a > b):
self.fail(f"expected greater-than:\n 1: {a}\n 2: {b}", msg)
def assertGreaterEqual(self, a, b, msg=""):
if not (a >= b):
self.fail(f"expected greater-than-or-equal:\n 1: {a}\n 2: {b}", msg)
def assertIsNone(self, obj, msg=""):
if obj is not None:
self.fail(f"expected {obj} to be None", msg)
def assertIsNotNone(self, obj, msg=""):
if obj is None:
self.fail(f"expected {obj} to not be None", msg)
def assertRaises(self, exception: type, function, *args, **kwargs):
try:
function(*args, **kwargs)
except exception:
return
except:
pass
self.fail(f"call to function did not raise the given exception")
def assertAlmostEqual(
self, first, second, places: int = 0, msg="", delta=None
):
if first == second:
# shortcut
return
if places <= 0 and delta is None:
raise ValueError("specify delta or places not both")
standard_msg = ""
diff = abs(first - second)
if delta is not None:
if diff <= delta:
return
standard_msg = (
f"{first} != {second} within {delta} delta ({diff} difference)"
)
else:
if places <= 0:
places = 7
if round(diff, places) == 0:
return
standard_msg = (
f"{first} != {second} within {places} places ({diff} difference)"
)
self.fail(standard_msg, msg)