codon/stdlib/unittest.codon

134 lines
4.5 KiB
Python
Raw Normal View History

Dynamic Polymorphism (#58) * Use Static[] for static inheritance * Support .seq extension * Fix #36 * Polymorphic typechecking; vtables [wip] * v-table dispatch [wip] * vtable routing [wip; bug] * vtable routing [MVP] * Fix texts * Add union type support * Update FAQs * Clarify * Add BSL license * Add makeUnion * Add IR UnionType * Update union representation in LLVM * Update README * Update README.md * Update README * Update README.md * Add benchmarks * Add more benchmarks and README * Add primes benchmark * Update benchmarks * Fix cpp * Clean up list * Update faq.md * Add binary trees benchmark * Add fannkuch benchmark * Fix paths * Add PyPy * Abort on fail * More benchmarks * Add cpp word_count * Update set_partition cpp * Add nbody cpp * Add TAQ cpp; fix word_count timing * Update CODEOWNERS * Update README * Update README.md * Update CODEOWNERS * Fix bench script * Update binary_trees.cpp * Update taq.cpp * Fix primes benchmark * Add mandelbrot benchmark * Fix OpenMP init * Add Module::unsafeGetUnionType * UnionType [wip] [skip ci] * Integrate IR unions and Union * UnionType refactor [skip ci] * Update README.md * Update docs * UnionType [wip] [skip ci] * UnionType and automatic unions * Add Slack * Update faq.md * Refactor types * New error reporting [wip] * New error reporting [wip] * peglib updates [wip] [skip_ci] * Fix parsing issues * Fix parsing issues * Fix error reporting issues * Make sure random module matches Python * Update releases.md * Fix tests * Fix #59 * Fix #57 * Fix #50 * Fix #49 * Fix #26; Fix #51; Fix #47; Fix #49 * Fix collection extension methods * Fix #62 * Handle *args/**kwargs with Callable[]; Fix #43 * Fix #43 * Fix Ptr.__sub__; Fix polymorphism issues * Add typeinfo * clang-format * Upgrade fmtlib to v9; Use CPM for fmtlib; format spec support; __format__ support * Use CPM for semver and toml++ * Remove extension check * Revamp str methods * Update str.zfill * Fix thunk crashes [wip] [skip_ci] * Fix str.__reversed__ * Fix count_with_max * Fix vtable memory allocation issues * Add poly AST tests * Use PDQsort when stability does not matter * Fix dotted imports; Fix issues * Fix kwargs passing to Python * Fix #61 * Fix #37 * Add isinstance support for unions; Union methods return Union type if different * clang-format * Nicely format error tracebacks * Fix build issues; clang-format * Fix OpenMP init * Fix OpenMP init * Update README.md * Fix tests * Update license [skip ci] * Update license [ci skip] * Add copyright header to all source files * Fix super(); Fix error recovery in ClassStmt * Clean up whitespace [ci skip] * Use Python 3.9 on CI * Print info in random test * Fix single unions * Update random_test.codon * Fix polymorhic thunk instantiation * Fix random test * Add operator.attrgetter and operator.methodcaller * Add code documentation * Update documentation * Update README.md * Fix tests * Fix random init Co-authored-by: A. R. Shajii <ars@ars.me>
2022-12-05 08:45:21 +08:00
# Copyright (C) 2022 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:
2022-02-16 23:51:16 +08:00
def fail(self, standard_message: str, special_message: str = ""):
2022-01-24 18:28:05 +08:00
print("TEST FAILED:", special_message if special_message else standard_message)
2022-02-16 23:51:16 +08:00
def assertTrue(self, obj, msg=""):
if not bool(obj):
2022-01-24 18:28:05 +08:00
self.fail(f"expected object to be true: {obj}", msg)
2022-02-16 23:51:16 +08:00
def assertFalse(self, obj, msg=""):
if bool(obj):
2022-01-24 18:28:05 +08:00
self.fail(f"expected object to be false: {obj}", msg)
2022-02-16 23:51:16 +08:00
def assertEqual(self, first, second, msg=""):
2022-01-24 18:28:05 +08:00
result = first == second
if not result:
2022-01-24 18:28:05 +08:00
self.fail(f"expected equality of:\n 1: {first}\n 2: {second}", msg)
2022-02-16 23:51:16 +08:00
def assertNotEqual(self, first, second, msg=""):
2022-01-24 18:28:05 +08:00
result = first != second
if not result:
2022-01-24 18:28:05 +08:00
self.fail(f"expected inequality of:\n 1: {first}\n 2: {second}", msg)
2022-02-16 23:51:16 +08:00
def assertSequenceEqual(self, seq1, seq2, msg=""):
len1 = len(seq1)
len2 = len(seq2)
if len1 != len2:
2022-01-24 18:28:05 +08:00
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:
2022-01-24 18:28:05 +08:00
self.fail(
f"expected equality of sequences (diff at elem {i}):\n 1: {seq1}\n 2: {seq2}",
msg,
)
2022-02-16 23:51:16 +08:00
def assertIn(self, member, container, msg=""):
if member not in container:
2022-01-24 18:28:05 +08:00
self.fail(f"expected {member} to be in {container}", msg)
2022-02-16 23:51:16 +08:00
def assertNotIn(self, member, container, msg=""):
if member in container:
2022-01-24 18:28:05 +08:00
self.fail(f"expected {member} to not be in {container}", msg)
2022-02-16 23:51:16 +08:00
def assertIs(self, expr1, expr2, msg=""):
if expr1 is not expr2:
2022-01-24 18:28:05 +08:00
self.fail(f"expected {expr1} to be identical to {expr2}", msg)
2022-02-16 23:51:16 +08:00
def assertIsNot(self, expr1, expr2, msg=""):
if expr1 is expr2:
2022-01-24 18:28:05 +08:00
self.fail(f"expected {expr1} to not be identical to {expr2}", msg)
2022-02-16 23:51:16 +08:00
def assertIsNot(self, expr1, expr2, msg=""):
if expr1 is expr2:
2022-01-24 18:28:05 +08:00
self.fail(f"expected {expr1} to not be identical to {expr2}", msg)
2022-02-16 23:51:16 +08:00
def assertCountEqual(self, first, second, msg=""):
from collections import Counter
2022-01-24 18:28:05 +08:00
first_seq, second_seq = list(first), list(second)
first_counter = Counter(first_seq)
second_counter = Counter(second_seq)
if first_counter != second_counter:
2022-01-24 18:28:05 +08:00
self.fail(f"expected equal counts:\n 1: {first}\n 2: {second}", msg)
2022-02-16 23:51:16 +08:00
def assertLess(self, a, b, msg=""):
if not (a < b):
2022-01-24 18:28:05 +08:00
self.fail(f"expected less-than:\n 1: {a}\n 2: {b}", msg)
2022-02-16 23:51:16 +08:00
def assertLessEqual(self, a, b, msg=""):
if not (a <= b):
2022-01-24 18:28:05 +08:00
self.fail(f"expected less-than-or-equal:\n 1: {a}\n 2: {b}", msg)
2022-02-16 23:51:16 +08:00
def assertGreater(self, a, b, msg=""):
if not (a > b):
2022-01-24 18:28:05 +08:00
self.fail(f"expected greater-than:\n 1: {a}\n 2: {b}", msg)
2022-02-16 23:51:16 +08:00
def assertGreaterEqual(self, a, b, msg=""):
if not (a >= b):
2022-01-24 18:28:05 +08:00
self.fail(f"expected greater-than-or-equal:\n 1: {a}\n 2: {b}", msg)
2022-02-16 23:51:16 +08:00
def assertIsNone(self, obj, msg=""):
if obj is not None:
2022-01-24 18:28:05 +08:00
self.fail(f"expected {obj} to be None", msg)
2022-02-16 23:51:16 +08:00
def assertIsNotNone(self, obj, msg=""):
if obj is None:
2022-01-24 18:28:05 +08:00
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
2022-01-24 18:28:05 +08:00
self.fail(f"call to function did not raise the given exception")
2022-01-24 18:28:05 +08:00
def assertAlmostEqual(
self, first, second, places: int = 0, msg="", delta=None
2022-02-16 23:51:16 +08:00
):
if first == second:
# shortcut
return
if places <= 0 and delta is None:
2022-01-24 18:28:05 +08:00
raise ValueError("specify delta or places not both")
2022-01-24 18:28:05 +08:00
standard_msg = ""
diff = abs(first - second)
if delta is not None:
if diff <= delta:
return
2022-01-24 18:28:05 +08:00
standard_msg = (
f"{first} != {second} within {delta} delta ({diff} difference)"
)
else:
if places <= 0:
places = 7
if round(diff, places) == 0:
return
2022-01-24 18:28:05 +08:00
standard_msg = (
f"{first} != {second} within {places} places ({diff} difference)"
)
self.fail(standard_msg, msg)