tests: Add more tests to improve coverage, mostly testing exceptions.

This commit is contained in:
Damien George 2015-08-21 11:56:14 +01:00
parent d292a81e95
commit d007cb8903
19 changed files with 260 additions and 4 deletions

View File

@ -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')

View File

@ -76,3 +76,20 @@ print(c.x)
c.x = 6 c.x = 6
print(c.x) print(c.x)
del 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')

View File

@ -0,0 +1,13 @@
# test builtin type
print(type(int))
try:
type()
except TypeError:
print('TypeError')
try:
type(1, 2)
except TypeError:
print('TypeError')

View File

@ -14,3 +14,9 @@ print(bytes(array('I', [1, 2])))
# long ints # long ints
print(ord(bytes([14953042807679334000 & 0xff]))) print(ord(bytes([14953042807679334000 & 0xff])))
# error in construction
try:
a = bytes([1, 2, 3], 1)
except TypeError:
print('TypeError')

View File

@ -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')

View File

@ -15,3 +15,12 @@ class C2:
c2 = C2(4) c2 = C2(4)
print(type(c2) == C2) print(type(c2) == C2)
print(c2.x) print(c2.x)
# __init__ should return None
class C3:
def __init__(self):
return 10
try:
C3()
except TypeError:
print('TypeError')

View File

@ -25,3 +25,8 @@ a.meth()
# __new__ should automatically be a staticmethod, so this should work # __new__ should automatically be a staticmethod, so this should work
a = a.__new__(A) a = a.__new__(A)
a.meth() a.meth()
class B:
def __new__(self, v1, v2):
None
B(1, 2)

View File

@ -14,3 +14,9 @@ class Sub(Base):
a = Sub() a = Sub()
a.meth() a.meth()
# printing super
class A:
def p(self):
print(str(super())[:18])
A().p()

View File

@ -1,3 +1,5 @@
# test set binary operations
sets = [set(), {1}, {1, 2}, {1, 2, 3}, {2, 3}, {2, 3, 5}, {5}, {7}] sets = [set(), {1}, {1, 2}, {1, 2, 3}, {2, 3}, {2, 3, 5}, {5}, {7}]
for s in sets: for s in sets:
for t 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(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')

12
tests/basics/set_unop.py Normal file
View File

@ -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')

View File

@ -20,6 +20,10 @@ print(x)
# binary ops # binary ops
print('123' + "456") print('123' + "456")
print('123' * 5) print('123' * 5)
try:
'123' * '1'
except TypeError:
print('TypeError')
# subscription # subscription
print('abc'[1]) print('abc'[1])
@ -27,11 +31,11 @@ print('abc'[-1])
try: try:
'abc'[100] 'abc'[100]
except IndexError: except IndexError:
print('caught') print('IndexError')
try: try:
'abc'[-4] 'abc'[-4]
except IndexError: except IndexError:
print('caught2') print('IndexError')
# iter # iter
print(list('str')) print(list('str'))

View File

@ -46,3 +46,8 @@ def t():
return True return True
print("0000".count('0', t())) print("0000".count('0', t()))
try:
'abc'.count(1)
except TypeError:
print('TypeError')

View File

@ -21,3 +21,8 @@ print("0000".find('-1', 3))
print("0000".find('1', 3)) print("0000".find('1', 3))
print("0000".find('1', 4)) print("0000".find('1', 4))
print("0000".find('1', 5)) print("0000".find('1', 5))
try:
'abc'.find(1)
except TypeError:
print('TypeError')

View File

@ -123,4 +123,74 @@ if full_tests:
for str in ('', 'a', 'bcd', 'This is a test with a longer string'): for str in ('', 'a', 'bcd', 'This is a test with a longer string'):
test_fmt(conv, fill, alignment, '', '', width, '', 's', str) 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')

View File

@ -86,3 +86,28 @@ try:
print("%(foo)*s" % {"foo": "bar"}) print("%(foo)*s" % {"foo": "bar"})
except TypeError: except TypeError:
print("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')

View File

@ -12,3 +12,13 @@ print("".replace("", "1"))
print("A".replace("", "1")) print("A".replace("", "1"))
print("AB".replace("", "1")) print("AB".replace("", "1"))
print("AB".replace("", "12")) print("AB".replace("", "12"))
try:
'abc'.replace(1, 2)
except TypeError:
print('TypeError')
try:
'abc'.replace('1', 2)
except TypeError:
print('TypeError')

View File

@ -7,12 +7,24 @@ print("a b".rsplit())
#print(" a b c ".rsplit(None, 0)) #print(" a b c ".rsplit(None, 0))
#print(" a b c ".rsplit(None, -1)) #print(" a b c ".rsplit(None, -1))
# empty separator should fail # empty separator should fail (this actually delegates to .split())
try: try:
"abc".rsplit('') "abc".rsplit('')
except ValueError: except ValueError:
print("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 # non-empty separator
print("abc".rsplit("a")) print("abc".rsplit("a"))
print("abc".rsplit("b")) print("abc".rsplit("b"))

View File

@ -29,3 +29,10 @@ try:
pass pass
except TypeError: except TypeError:
print("TypeError") print("TypeError")
# multiple bases with layout conflict
try:
class A(type, tuple):
None
except TypeError:
print('TypeError')

View File

@ -6,12 +6,14 @@ full_tests = False
def test(fmt, *args): def test(fmt, *args):
print('{:8s}'.format(fmt) + '>' + fmt.format(*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.4e}", -123.456) test("{:10.4e}", -123.456)
test("{:10.4f}", 123.456) test("{:10.4f}", 123.456)
test("{:10.4f}", -123.456) test("{:10.4f}", -123.456)
test("{:10.4g}", 123.456) test("{:10.4g}", 123.456)
test("{:10.4g}", -123.456) test("{:10.4g}", -123.456)
test("{:10.4n}", 123.456)
test("{:e}", 100) test("{:e}", 100)
test("{:f}", 200) test("{:f}", 200)
test("{:g}", 300) test("{:g}", 300)
@ -128,3 +130,10 @@ else:
# We don't currently test a type of '' with floats (see the detailed comment # We don't currently test a type of '' with floats (see the detailed comment
# in objstr.c) # in objstr.c)
# tests for errors in format string
try:
'{:10.1b}'.format(0.0)
except ValueError:
print('ValueError')