Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fcf4435

Browse files
committed
Improve test coverage. Hope the test_file changes work the same on windows.
1 parent 307021f commit fcf4435

5 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,11 @@ def test_compile(self):
238238
self.assertRaises(TypeError, compile)
239239
self.assertRaises(ValueError, compile, 'print 42\n', '<string>', 'badmode')
240240
self.assertRaises(ValueError, compile, 'print 42\n', '<string>', 'single', 0xff)
241+
self.assertRaises(TypeError, compile, chr(0), 'f', 'exec')
241242
if have_unicode:
242243
compile(unicode('print u"\xc3\xa5"\n', 'utf8'), '', 'exec')
244+
self.assertRaises(TypeError, compile, unichr(0), 'f', 'exec')
245+
self.assertRaises(ValueError, compile, unicode('a = 1'), 'f', 'bad')
243246

244247
def test_delattr(self):
245248
import sys
@@ -421,6 +424,7 @@ def __setitem__(self, key, value):
421424

422425
unlink(TESTFN)
423426
self.assertRaises(TypeError, execfile)
427+
self.assertRaises(TypeError, execfile, TESTFN, {}, ())
424428
import os
425429
self.assertRaises(IOError, execfile, os.curdir)
426430
self.assertRaises(IOError, execfile, "I_dont_exist")
@@ -1008,6 +1012,9 @@ class BadSeq:
10081012
def __getitem__(self, index):
10091013
raise ValueError
10101014
self.assertRaises(ValueError, map, lambda x: x, BadSeq())
1015+
def badfunc(x):
1016+
raise RuntimeError
1017+
self.assertRaises(RuntimeError, map, badfunc, range(5))
10111018

10121019
def test_max(self):
10131020
self.assertEqual(max('123123'), '3')
@@ -1239,6 +1246,12 @@ def test_range(self):
12391246
self.assertRaises(TypeError, range)
12401247
self.assertRaises(TypeError, range, 1, 2, 3, 4)
12411248
self.assertRaises(ValueError, range, 1, 2, 0)
1249+
self.assertRaises(ValueError, range, a, a + 1, long(0))
1250+
1251+
class badzero(int):
1252+
def __cmp__(self, other):
1253+
raise RuntimeError
1254+
self.assertRaises(RuntimeError, range, a, a + 1, badzero(1))
12421255

12431256
# Reject floats when it would require PyLongs to represent.
12441257
# (smaller floats still accepted, but deprecated)

Lib/test/test_file.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,39 @@ class NonString: pass
100100
print "writelines accepted sequence of non-string objects"
101101
f.close()
102102

103+
try:
104+
sys.stdin.seek(0)
105+
except IOError:
106+
pass
107+
else:
108+
print "should not be able to seek on sys.stdin"
109+
110+
try:
111+
sys.stdin.tell()
112+
except IOError:
113+
pass
114+
else:
115+
print "should not be able to seek on sys.stdin"
116+
117+
try:
118+
sys.stdin.truncate()
119+
except IOError:
120+
pass
121+
else:
122+
print "should not be able to truncate on sys.stdin"
123+
124+
# verify repr works
125+
f = open(TESTFN)
126+
if not repr(f).startswith("<open file '" + TESTFN):
127+
print "repr(file) failed"
128+
f.close()
129+
130+
# verify repr works for unicode too
131+
f = open(unicode(TESTFN))
132+
if not repr(f).startswith("<open file u'" + TESTFN):
133+
print "repr(file with unicode name) failed"
134+
f.close()
135+
103136
# verify that we get a sensible error message for bad mode argument
104137
bad_mode = "qwerty"
105138
try:

Lib/test/test_generators.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,11 @@ def printsolution(self, x):
15891589
...
15901590
ValueError: 7
15911591
1592+
>>> f().throw("abc") # throw on just-opened generator
1593+
Traceback (most recent call last):
1594+
...
1595+
TypeError: exceptions must be classes, or instances, not str
1596+
15921597
15931598
Now let's try closing a generator:
15941599

Lib/test/test_set.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,8 @@ def test_empty_difference_rev(self):
606606
def test_iteration(self):
607607
for v in self.set:
608608
self.assert_(v in self.values)
609+
setiter = iter(self.set)
610+
self.assertEqual(setiter._length_cue(), len(self.set))
609611

610612
def test_pickling(self):
611613
p = pickle.dumps(self.set)
@@ -693,6 +695,16 @@ def test_instancesWithoutException(self):
693695
set('abc')
694696
set(gooditer())
695697

698+
def test_changingSizeWhileIterating(self):
699+
s = set([1,2,3])
700+
try:
701+
for i in s:
702+
s.update([4])
703+
except RuntimeError:
704+
pass
705+
else:
706+
self.fail("no exception when changing size during iteration")
707+
696708
#==============================================================================
697709

698710
class TestSetOfSets(unittest.TestCase):

Lib/test/test_syntax.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ def test_global_err_then_warn(self):
4242
self._check_error(source, "global")
4343
warnings.filters.pop(0)
4444

45+
def test_break_outside_loop(self):
46+
self._check_error("break", "outside loop")
47+
48+
def test_delete_deref(self):
49+
source = re.sub('(?m)^ *:', '', """\
50+
:def foo(x):
51+
: def bar():
52+
: print x
53+
: del x
54+
:""")
55+
self._check_error(source, "nested scope")
56+
4557
def test_main():
4658
test_support.run_unittest(SyntaxTestCase)
4759

0 commit comments

Comments
 (0)