##// END OF EJS Templates
Use AssertPrints in tests for autoreload extension.
Thomas Kluyver -
Show More
@@ -7,6 +7,7 b' import time'
7 7 from StringIO import StringIO
8 8
9 9 import nose.tools as nt
10 import IPython.testing.tools as tt
10 11
11 12 from IPython.extensions.autoreload import AutoreloadInterface
12 13 from IPython.core.hooks import TryNext
@@ -197,19 +198,11 b" class Bar: # old-style class: weakref doesn't work for it on Python < 2.7"
197 198 a syntax error
198 199 """)
199 200
200 old_stderr = sys.stderr
201 new_stderr = StringIO()
202 sys.stderr = new_stderr
203 try:
201 with tt.AssertPrints(('[autoreload of %s failed:' % mod_name), channel='stderr'):
204 202 self.shell.run_code("pass") # trigger reload
203 with tt.AssertNotPrints(('[autoreload of %s failed:' % mod_name), channel='stderr'):
205 204 self.shell.run_code("pass") # trigger another reload
206 check_module_contents()
207 finally:
208 sys.stderr = old_stderr
209
210 nt.assert_true(('[autoreload of %s failed:' % mod_name) in
211 new_stderr.getvalue())
212 nt.assert_equal(new_stderr.getvalue().count('[autoreload of'), 1)
205 check_module_contents()
213 206
214 207 #
215 208 # Rewrite module (this time reload should succeed)
@@ -342,22 +342,23 b' class AssertPrints(object):'
342 342
343 343 Examples
344 344 --------
345 >>> with AssertPrints("abc"):
345 >>> with AssertPrints("abc", suppress=False):
346 346 ... print "abcd"
347 347 ... print "def"
348 348 ...
349 349 abcd
350 350 def
351 351 """
352 def __init__(self, s, channel='stdout'):
352 def __init__(self, s, channel='stdout', suppress=True):
353 353 self.s = s
354 354 self.channel = channel
355 self.suppress = suppress
355 356
356 357 def __enter__(self):
357 358 self.orig_stream = getattr(sys, self.channel)
358 359 self.buffer = MyStringIO()
359 360 self.tee = Tee(self.buffer, channel=self.channel)
360 setattr(sys, self.channel, self.tee)
361 setattr(sys, self.channel, self.buffer if self.suppress else self.tee)
361 362
362 363 def __exit__(self, etype, value, traceback):
363 364 self.tee.flush()
@@ -365,6 +366,17 b' class AssertPrints(object):'
365 366 printed = self.buffer.getvalue()
366 367 assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed)
367 368 return False
369
370 class AssertNotPrints(AssertPrints):
371 """Context manager for checking that certain output *isn't* produced.
372
373 Counterpart of AssertPrints"""
374 def __exit__(self, etype, value, traceback):
375 self.tee.flush()
376 setattr(sys, self.channel, self.orig_stream)
377 printed = self.buffer.getvalue()
378 assert self.s not in printed, notprinted_msg.format(self.s, self.channel, printed)
379 return False
368 380
369 381 @contextmanager
370 382 def mute_warn():
General Comments 0
You need to be logged in to leave comments. Login now