Home | History | Annotate | Download | only in idle_test
      1 '''Test warnings replacement in PyShell.py and run.py.
      2 
      3 This file could be expanded to include traceback overrides
      4 (in same two modules). If so, change name.
      5 Revise if output destination changes (http://bugs.python.org/issue18318).
      6 Make sure warnings module is left unaltered (http://bugs.python.org/issue18081).
      7 '''
      8 
      9 import unittest
     10 from test.test_support import captured_stderr
     11 
     12 import warnings
     13 # Try to capture default showwarning before Idle modules are imported.
     14 showwarning = warnings.showwarning
     15 # But if we run this file within idle, we are in the middle of the run.main loop
     16 # and default showwarnings has already been replaced.
     17 running_in_idle = 'idle' in showwarning.__name__
     18 
     19 from idlelib import run
     20 from idlelib import PyShell as shell
     21 
     22 # The following was generated from PyShell.idle_formatwarning
     23 # and checked as matching expectation.
     24 idlemsg = '''
     25 Warning (from warnings module):
     26   File "test_warning.py", line 99
     27     Line of code
     28 UserWarning: Test
     29 '''
     30 shellmsg = idlemsg + ">>> "
     31 
     32 class RunWarnTest(unittest.TestCase):
     33 
     34     @unittest.skipIf(running_in_idle, "Does not work when run within Idle.")
     35     def test_showwarnings(self):
     36         self.assertIs(warnings.showwarning, showwarning)
     37         run.capture_warnings(True)
     38         self.assertIs(warnings.showwarning, run.idle_showwarning_subproc)
     39         run.capture_warnings(False)
     40         self.assertIs(warnings.showwarning, showwarning)
     41 
     42     def test_run_show(self):
     43         with captured_stderr() as f:
     44             run.idle_showwarning_subproc(
     45                     'Test', UserWarning, 'test_warning.py', 99, f, 'Line of code')
     46             # The following uses .splitlines to erase line-ending differences
     47             self.assertEqual(idlemsg.splitlines(), f.getvalue().splitlines())
     48 
     49 class ShellWarnTest(unittest.TestCase):
     50 
     51     @unittest.skipIf(running_in_idle, "Does not work when run within Idle.")
     52     def test_showwarnings(self):
     53         self.assertIs(warnings.showwarning, showwarning)
     54         shell.capture_warnings(True)
     55         self.assertIs(warnings.showwarning, shell.idle_showwarning)
     56         shell.capture_warnings(False)
     57         self.assertIs(warnings.showwarning, showwarning)
     58 
     59     def test_idle_formatter(self):
     60         # Will fail if format changed without regenerating idlemsg
     61         s = shell.idle_formatwarning(
     62                 'Test', UserWarning, 'test_warning.py', 99, 'Line of code')
     63         self.assertEqual(idlemsg, s)
     64 
     65     def test_shell_show(self):
     66         with captured_stderr() as f:
     67             shell.idle_showwarning(
     68                     'Test', UserWarning, 'test_warning.py', 99, f, 'Line of code')
     69             self.assertEqual(shellmsg.splitlines(), f.getvalue().splitlines())
     70 
     71 
     72 if __name__ == '__main__':
     73     unittest.main(verbosity=2, exit=False)
     74