def test_safe_representation(): class TestObject(object): """A test object that has neither __str__ nor __repr__.""" def __str__(self): return NotImplementedError() def __repr__(self): return NotImplementedError() assert_eq("2", qcore.safe_str(2)) assert_eq("2....", qcore.safe_str("2.192842", max_length=5)) assert_is_substring("<n/a: str(...) raised", qcore.safe_str(TestObject())) assert_eq("'a'", qcore.safe_repr("a")) assert_eq("'...", qcore.safe_repr("2.192842", max_length=4)) assert_eq("<n/a: repr...", qcore.safe_repr(TestObject(), max_length=13))
def test_string_assertions(): assert_is_substring("a", "bca") with AssertRaises(AssertionError): assert_is_substring("a", "bc") assert_is_not_substring("a", "bc") with AssertRaises(AssertionError): assert_is_not_substring("a", "bca") assert_startswith("a", "abc bcd") with AssertRaises(AssertionError): assert_startswith("b", "abc bcd") assert_endswith("d", "abc bcd") with AssertRaises(AssertionError): assert_endswith("c", "abc bcd")
def test_complex_assertions(): with AssertRaises(AssertionError): with AssertRaises(AssertionError): pass with AssertRaises(RuntimeError): raise RuntimeError() assert_unordered_list_eq([1, 2, 2], [2, 1, 2]) with AssertRaises(AssertionError): try: assert_unordered_list_eq([1, 2, 2], [2, 1]) except AssertionError as e: print(repr(e)) raise assert_is_substring("a", "bca") with AssertRaises(AssertionError): assert_is_substring("a", "bc") assert_is_not_substring("a", "bc") with AssertRaises(AssertionError): assert_is_not_substring("a", "bca")
def test_format_stack(): def foo(): return qcore.debug.format_stack() st = foo() assert_is_substring("in foo\n", st)