From d007cb890394d9d26c6fafb133532a5175d91eb2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 21 Aug 2015 11:56:14 +0100 Subject: [PATCH] tests: Add more tests to improve coverage, mostly testing exceptions. --- tests/basics/builtin_issubclass.py | 17 +++++++ tests/basics/builtin_property.py | 17 +++++++ tests/basics/builtin_type.py | 13 +++++ tests/basics/bytes_construct.py | 6 +++ tests/basics/bytes_subscr.py | 15 ++++++ tests/basics/class2.py | 9 ++++ tests/basics/class_new.py | 5 ++ tests/basics/class_super.py | 6 +++ tests/basics/set_binop.py | 9 ++++ tests/basics/set_unop.py | 12 +++++ tests/basics/string1.py | 8 +++- tests/basics/string_count.py | 5 ++ tests/basics/string_find.py | 5 ++ tests/basics/string_format.py | 72 +++++++++++++++++++++++++++- tests/basics/string_format_modulo.py | 25 ++++++++++ tests/basics/string_replace.py | 10 ++++ tests/basics/string_rsplit.py | 14 +++++- tests/basics/subclass_native1.py | 7 +++ tests/float/string_format.py | 9 ++++ 19 files changed, 260 insertions(+), 4 deletions(-) create mode 100644 tests/basics/builtin_issubclass.py create mode 100644 tests/basics/builtin_type.py create mode 100644 tests/basics/bytes_subscr.py create mode 100644 tests/basics/set_unop.py diff --git a/tests/basics/builtin_issubclass.py b/tests/basics/builtin_issubclass.py new file mode 100644 index 0000000000..6febc03682 --- /dev/null +++ b/tests/basics/builtin_issubclass.py @@ -0,0 +1,17 @@ +# test builtin issubclass + +class A: + pass + +print(issubclass(A, A)) +print(issubclass(A, (A,))) + +try: + issubclass(A, 1) +except TypeError: + print('TypeError') + +try: + issubclass('a', 1) +except TypeError: + print('TypeError') diff --git a/tests/basics/builtin_property.py b/tests/basics/builtin_property.py index 4df9842a3e..3b3b32d166 100644 --- a/tests/basics/builtin_property.py +++ b/tests/basics/builtin_property.py @@ -76,3 +76,20 @@ print(c.x) c.x = 6 print(c.x) del c.x + +# a property that has no get, set or del +class D: + prop = property() +d = D() +try: + d.prop +except AttributeError: + print('AttributeError') +try: + d.prop = 1 +except AttributeError: + print('AttributeError') +try: + del d.prop +except AttributeError: + print('AttributeError') diff --git a/tests/basics/builtin_type.py b/tests/basics/builtin_type.py new file mode 100644 index 0000000000..83c45c64b9 --- /dev/null +++ b/tests/basics/builtin_type.py @@ -0,0 +1,13 @@ +# test builtin type + +print(type(int)) + +try: + type() +except TypeError: + print('TypeError') + +try: + type(1, 2) +except TypeError: + print('TypeError') diff --git a/tests/basics/bytes_construct.py b/tests/basics/bytes_construct.py index de6b5d9545..0cf0540c7a 100644 --- a/tests/basics/bytes_construct.py +++ b/tests/basics/bytes_construct.py @@ -14,3 +14,9 @@ print(bytes(array('I', [1, 2]))) # long ints print(ord(bytes([14953042807679334000 & 0xff]))) + +# error in construction +try: + a = bytes([1, 2, 3], 1) +except TypeError: + print('TypeError') diff --git a/tests/basics/bytes_subscr.py b/tests/basics/bytes_subscr.py new file mode 100644 index 0000000000..d6ab7bdc7d --- /dev/null +++ b/tests/basics/bytes_subscr.py @@ -0,0 +1,15 @@ +# test [...] of bytes + +print(b'123'[0]) +print(b'123'[1]) +print(b'123'[-1]) + +try: + b'123'[1] = 4 +except TypeError: + print('TypeError') + +try: + del b'123'[1] +except TypeError: + print('TypeError') diff --git a/tests/basics/class2.py b/tests/basics/class2.py index 64f1f62b96..8ab3ef2b35 100644 --- a/tests/basics/class2.py +++ b/tests/basics/class2.py @@ -15,3 +15,12 @@ class C2: c2 = C2(4) print(type(c2) == C2) print(c2.x) + +# __init__ should return None +class C3: + def __init__(self): + return 10 +try: + C3() +except TypeError: + print('TypeError') diff --git a/tests/basics/class_new.py b/tests/basics/class_new.py index 7e84dccf40..a6a34c5811 100644 --- a/tests/basics/class_new.py +++ b/tests/basics/class_new.py @@ -25,3 +25,8 @@ a.meth() # __new__ should automatically be a staticmethod, so this should work a = a.__new__(A) a.meth() + +class B: + def __new__(self, v1, v2): + None +B(1, 2) diff --git a/tests/basics/class_super.py b/tests/basics/class_super.py index 0f2852a479..4b052d8f3c 100644 --- a/tests/basics/class_super.py +++ b/tests/basics/class_super.py @@ -14,3 +14,9 @@ class Sub(Base): a = Sub() a.meth() + +# printing super +class A: + def p(self): + print(str(super())[:18]) +A().p() diff --git a/tests/basics/set_binop.py b/tests/basics/set_binop.py index df82aadd9b..a3657d84bd 100644 --- a/tests/basics/set_binop.py +++ b/tests/basics/set_binop.py @@ -1,3 +1,5 @@ +# test set binary operations + sets = [set(), {1}, {1, 2}, {1, 2, 3}, {2, 3}, {2, 3, 5}, {5}, {7}] for s in sets: for t in sets: @@ -24,3 +26,10 @@ for s in sets: print(sorted(s), '>=', sorted(t), '=', s >= t) print(sorted(s), '<', sorted(t), '=', s < t) print(sorted(s), '<=', sorted(t), '=', s <= t) + +print(set('abc') == 1) + +try: + set('abc') * 2 +except TypeError: + print('TypeError') diff --git a/tests/basics/set_unop.py b/tests/basics/set_unop.py new file mode 100644 index 0000000000..1f96deed50 --- /dev/null +++ b/tests/basics/set_unop.py @@ -0,0 +1,12 @@ +# test set unary operations + +print(bool(set())) +print(bool(set('abc'))) + +print(len(set())) +print(len(set('abc'))) + +try: + hash(set('abc')) +except TypeError: + print('TypeError') diff --git a/tests/basics/string1.py b/tests/basics/string1.py index 8d5f4618dc..b8ca15c344 100644 --- a/tests/basics/string1.py +++ b/tests/basics/string1.py @@ -20,6 +20,10 @@ print(x) # binary ops print('123' + "456") print('123' * 5) +try: + '123' * '1' +except TypeError: + print('TypeError') # subscription print('abc'[1]) @@ -27,11 +31,11 @@ print('abc'[-1]) try: 'abc'[100] except IndexError: - print('caught') + print('IndexError') try: 'abc'[-4] except IndexError: - print('caught2') + print('IndexError') # iter print(list('str')) diff --git a/tests/basics/string_count.py b/tests/basics/string_count.py index 0da1b1fcae..462ccb8299 100644 --- a/tests/basics/string_count.py +++ b/tests/basics/string_count.py @@ -46,3 +46,8 @@ def t(): return True print("0000".count('0', t())) + +try: + 'abc'.count(1) +except TypeError: + print('TypeError') diff --git a/tests/basics/string_find.py b/tests/basics/string_find.py index df65fd6e5a..4a206eb0e0 100644 --- a/tests/basics/string_find.py +++ b/tests/basics/string_find.py @@ -21,3 +21,8 @@ print("0000".find('-1', 3)) print("0000".find('1', 3)) print("0000".find('1', 4)) print("0000".find('1', 5)) + +try: + 'abc'.find(1) +except TypeError: + print('TypeError') diff --git a/tests/basics/string_format.py b/tests/basics/string_format.py index 5398c6e236..d0518243cf 100644 --- a/tests/basics/string_format.py +++ b/tests/basics/string_format.py @@ -123,4 +123,74 @@ if full_tests: for str in ('', 'a', 'bcd', 'This is a test with a longer string'): test_fmt(conv, fill, alignment, '', '', width, '', 's', str) -# TODO Add tests for erroneous format strings. +# tests for errors in format string + +try: + '{0:0}'.format('zzz') +except (ValueError): + print('ValueError') + +try: + '{1:}'.format(1) +except IndexError: + print('IndexError') + +try: + '}'.format('zzzz') +except ValueError: + print('ValueError') + +try: + 'abc{!d}'.format('1') +except ValueError: + print('ValueError') + +try: + '{abc'.format('zzzz') +except ValueError: + print('ValueError') + +try: + '{!s :}'.format(2) +except ValueError: + print('ValueError') + +try: + '{}{0}'.format(1, 2) +except ValueError: + print('ValueError') + +try: + '{1:}'.format(1) +except IndexError: + print('IndexError') + +try: + '{ 0 :*^10}'.format(12) +except KeyError: + print('KeyError') + +try: + '{0}{}'.format(1) +except ValueError: + print('ValueError') + +try: + '{}{}'.format(1) +except IndexError: + print('IndexError') + +try: + '{0:+s}'.format('1') +except ValueError: + print('ValueError') + +try: + '{0:+c}'.format(1) +except ValueError: + print('ValueError') + +try: + '{0:s}'.format(1) +except ValueError: + print('ValueError') diff --git a/tests/basics/string_format_modulo.py b/tests/basics/string_format_modulo.py index a1c31def90..05b00ef14f 100644 --- a/tests/basics/string_format_modulo.py +++ b/tests/basics/string_format_modulo.py @@ -86,3 +86,28 @@ try: print("%(foo)*s" % {"foo": "bar"}) except TypeError: print("TypeError") + +try: + '%(a' % {'a':1} +except ValueError: + print('ValueError') + +try: + '%.*d %.*d' % (20, 5) +except TypeError: + print('TypeError') + +try: + a = '%*' % 1 +except (ValueError): + print('ValueError') + +try: + '%c' % 'aa' +except TypeError: + print('TypeError') + +try: + '%l' % 1 +except ValueError: + print('ValueError') diff --git a/tests/basics/string_replace.py b/tests/basics/string_replace.py index 95e2ba9bc8..f03b38966b 100644 --- a/tests/basics/string_replace.py +++ b/tests/basics/string_replace.py @@ -12,3 +12,13 @@ print("".replace("", "1")) print("A".replace("", "1")) print("AB".replace("", "1")) print("AB".replace("", "12")) + +try: + 'abc'.replace(1, 2) +except TypeError: + print('TypeError') + +try: + 'abc'.replace('1', 2) +except TypeError: + print('TypeError') diff --git a/tests/basics/string_rsplit.py b/tests/basics/string_rsplit.py index cc6c0fd062..563b64f1c9 100644 --- a/tests/basics/string_rsplit.py +++ b/tests/basics/string_rsplit.py @@ -7,12 +7,24 @@ print("a b".rsplit()) #print(" a b c ".rsplit(None, 0)) #print(" a b c ".rsplit(None, -1)) -# empty separator should fail +# empty separator should fail (this actually delegates to .split()) try: "abc".rsplit('') except ValueError: print("ValueError") +# empty separator should fail (error handled in .rsplit()) +try: + 'a a a a'.rsplit('', 5) +except ValueError: + print('ValueError') + +# bad separator type +try: + 'a a a a'.rsplit(1) +except TypeError: + print('TypeError') + # non-empty separator print("abc".rsplit("a")) print("abc".rsplit("b")) diff --git a/tests/basics/subclass_native1.py b/tests/basics/subclass_native1.py index a176893425..288a686d1a 100644 --- a/tests/basics/subclass_native1.py +++ b/tests/basics/subclass_native1.py @@ -29,3 +29,10 @@ try: pass except TypeError: print("TypeError") + +# multiple bases with layout conflict +try: + class A(type, tuple): + None +except TypeError: + print('TypeError') diff --git a/tests/float/string_format.py b/tests/float/string_format.py index 9ffbec4ff7..b605f20978 100644 --- a/tests/float/string_format.py +++ b/tests/float/string_format.py @@ -6,12 +6,14 @@ full_tests = False def test(fmt, *args): print('{:8s}'.format(fmt) + '>' + fmt.format(*args) + '<') +test("{:10.4}", 123.456) test("{:10.4e}", 123.456) test("{:10.4e}", -123.456) test("{:10.4f}", 123.456) test("{:10.4f}", -123.456) test("{:10.4g}", 123.456) test("{:10.4g}", -123.456) +test("{:10.4n}", 123.456) test("{:e}", 100) test("{:f}", 200) test("{:g}", 300) @@ -128,3 +130,10 @@ else: # We don't currently test a type of '' with floats (see the detailed comment # in objstr.c) + +# tests for errors in format string + +try: + '{:10.1b}'.format(0.0) +except ValueError: + print('ValueError')