tests: Add more tests to improve coverage, mostly testing exceptions.
This commit is contained in:
parent
d292a81e95
commit
d007cb8903
17
tests/basics/builtin_issubclass.py
Normal file
17
tests/basics/builtin_issubclass.py
Normal 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')
|
@ -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')
|
||||
|
13
tests/basics/builtin_type.py
Normal file
13
tests/basics/builtin_type.py
Normal 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')
|
@ -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')
|
||||
|
15
tests/basics/bytes_subscr.py
Normal file
15
tests/basics/bytes_subscr.py
Normal 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')
|
@ -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')
|
||||
|
@ -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)
|
||||
|
@ -14,3 +14,9 @@ class Sub(Base):
|
||||
|
||||
a = Sub()
|
||||
a.meth()
|
||||
|
||||
# printing super
|
||||
class A:
|
||||
def p(self):
|
||||
print(str(super())[:18])
|
||||
A().p()
|
||||
|
@ -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')
|
||||
|
12
tests/basics/set_unop.py
Normal file
12
tests/basics/set_unop.py
Normal 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')
|
@ -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'))
|
||||
|
@ -46,3 +46,8 @@ def t():
|
||||
return True
|
||||
|
||||
print("0000".count('0', t()))
|
||||
|
||||
try:
|
||||
'abc'.count(1)
|
||||
except TypeError:
|
||||
print('TypeError')
|
||||
|
@ -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')
|
||||
|
@ -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')
|
||||
|
@ -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')
|
||||
|
@ -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')
|
||||
|
@ -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"))
|
||||
|
@ -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')
|
||||
|
@ -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')
|
||||
|
Loading…
Reference in New Issue
Block a user