diff -r 03747d64c042 Lib/test/test_curses.py --- a/Lib/test/test_curses.py Fri Jun 28 18:59:52 2013 -0400 +++ b/Lib/test/test_curses.py Fri Jun 28 23:42:04 2013 -0500 @@ -2,38 +2,50 @@ # Test script for the curses module # # This script doesn't actually display anything very coherent. but it -# does call every method and function. +# does call (nearly) every method and function. # # Functions not tested: {def,reset}_{shell,prog}_mode, getch(), getstr(), # init_color() # Only called, not tested: getmouse(), ungetmouse() # -import sys, tempfile, os +import os +import sys +import tempfile +import unittest + +from test.support import requires, import_module, verbose # Optionally test curses module. This currently requires that the # 'curses' resource be given on the regrtest command line using the -u # option. If not available, nothing after this line will be executed. - -import unittest -from test.support import requires, import_module requires('curses') # If either of these don't exist, skip the tests. curses = import_module('curses') curses.panel = import_module('curses.panel') +term = os.environ.get('TERM', 'unknown') -# XXX: if newterm was supported we could use it instead of initscr and not exit -term = os.environ.get('TERM') -if not term or term == 'unknown': - raise unittest.SkipTest("$TERM=%r, calling initscr() may cause exit" % term) +@unittest.skipIf(term == 'unkwown', + "$TERM=%r, calling initscr() may cause exit" % term) +@unittest.skipIf(sys.platform == "cygwin", + "cygwin's curses mostly just hangs") +class TestCurses(unittest.TestCase): + def setUp(self): + if verbose: + # just to make the test output a little more readable + print('') + self.stdscr = curses.initscr() + curses.savetty() -if sys.platform == "cygwin": - raise unittest.SkipTest("cygwin's curses mostly just hangs") + def tearDown(self): + curses.resetty() + curses.endwin() -def window_funcs(stdscr): + def test_window_funcs(self): "Test the methods of windows" + stdscr = self.stdscr win = curses.newwin(10,10) win = curses.newwin(5,5, 5,5) win2 = curses.newwin(15,15, 5,5) @@ -71,13 +83,10 @@ 69, 70, 71, 72) win.border('|', '!', '-', '_', '+', '\\', '#', '/') - try: + with self.assertRaises(TypeError, + msg="Expected win.border() to raise TypeError"): win.border(65, 66, 67, 68, 69, [], 71, 72) - except TypeError: - pass - else: - raise RuntimeError("Expected win.border() to raise TypeError") stdscr.clearok(1) @@ -149,9 +158,9 @@ stdscr.enclose() -def module_funcs(stdscr): + def test_module_funcs(self): "Test module-level functions" - + stdscr = self.stdscr for func in [curses.baudrate, curses.beep, curses.can_change_color, curses.cbreak, curses.def_prog_mode, curses.doupdate, curses.filter, curses.flash, curses.flushinp, @@ -230,7 +239,7 @@ if hasattr(curses, 'resize_term'): curses.resize_term(*stdscr.getmaxyx()) -def unit_tests(): + def test_unctrl(self): from curses import ascii for ch, expected in [('a', 'a'), ('A', 'A'), (';', ';'), (' ', ' '), @@ -238,21 +247,19 @@ # Meta-bit characters ('\x8a', '!^J'), ('\xc1', '!A'), ]: - if ascii.unctrl(ch) != expected: - print('curses.unctrl fails on character', repr(ch)) + self.assertEqual(ascii.unctrl(ch), expected, + 'curses.unctrl fails on character %r' % ch) -def test_userptr_without_set(stdscr): + def test_userptr_without_set(self): w = curses.newwin(10, 10) p = curses.panel.new_panel(w) # try to access userptr() before calling set_userptr() -- segfaults - try: + with self.assertRaises(curses.panel.error, + msg='userptr should fail since not set'): p.userptr() - raise RuntimeError('userptr should fail since not set') - except curses.panel.error: - pass -def test_userptr_memory_leak(stdscr): + def test_userptr_memory_leak(self): w = curses.newwin(10, 10) p = curses.panel.new_panel(w) obj = object() @@ -261,32 +268,36 @@ p.set_userptr(obj) p.set_userptr(None) - if sys.getrefcount(obj) != nrefs: - raise RuntimeError("set_userptr leaked references") + self.assertEqual(sys.getrefcount(obj), nrefs, + "set_userptr leaked references") -def test_userptr_segfault(stdscr): - panel = curses.panel.new_panel(stdscr) + def test_userptr_segfault(self): + panel = curses.panel.new_panel(self.stdscr) class A: def __del__(self): panel.set_userptr(None) panel.set_userptr(A()) panel.set_userptr(None) -def test_resize_term(stdscr): - if hasattr(curses, 'resizeterm'): - lines, cols = curses.LINES, curses.COLS - curses.resizeterm(lines - 1, cols + 1) + @unittest.skipUnless(hasattr(curses, 'resizeterm'), + 'resizeterm not available') + def test_resize_term(self): + lines, cols = curses.LINES, curses.COLS + new_lines = lines - 1 + new_cols = cols + 1 + curses.resizeterm(new_lines, new_cols) - if curses.LINES != lines - 1 or curses.COLS != cols + 1: - raise RuntimeError("Expected resizeterm to update LINES and COLS") + self.assertEqual(curses.LINES, new_lines) + self.assertEqual(curses.COLS, new_cols) -def test_issue6243(stdscr): + def test_issue6243(self): curses.ungetch(1025) - stdscr.getkey() + self.stdscr.getkey() -def test_unget_wch(stdscr): - if not hasattr(curses, 'unget_wch'): - return + @unittest.skipUnless(hasattr(curses, 'unget_wch'), + 'unget_wch not available') + def test_unget_wch(self): + stdscr = self.stdscr encoding = stdscr.encoding for ch in ('a', '\xe9', '\u20ac', '\U0010FFFF'): try: @@ -296,70 +307,40 @@ try: curses.unget_wch(ch) except Exception as err: - raise Exception("unget_wch(%a) failed with encoding %s: %s" - % (ch, stdscr.encoding, err)) + self.fail("unget_wch(%a) failed with encoding %s: %s" + % (ch, stdscr.encoding, err)) read = stdscr.get_wch() - if read != ch: - raise AssertionError("%r != %r" % (read, ch)) + self.assertEqual(read, ch) code = ord(ch) curses.unget_wch(code) read = stdscr.get_wch() - if read != ch: - raise AssertionError("%r != %r" % (read, ch)) + self.assertEqual(read, ch) -def test_issue10570(): + def test_issue10570(self): b = curses.tparm(curses.tigetstr("cup"), 5, 3) - assert type(b) is bytes + self.assertIs(type(b), bytes) curses.putp(b) -def test_encoding(stdscr): + def test_encoding(self): + stdscr = self.stdscr import codecs encoding = stdscr.encoding codecs.lookup(encoding) - try: + + with self.assertRaises(TypeError): stdscr.encoding = 10 - except TypeError: - pass - else: - raise AssertionError("TypeError not raised") + stdscr.encoding = encoding - try: + with self.assertRaises(TypeError): del stdscr.encoding - except TypeError: - pass - else: - raise AssertionError("TypeError not raised") -def main(stdscr): - curses.savetty() - try: - module_funcs(stdscr) - window_funcs(stdscr) - test_userptr_without_set(stdscr) - test_userptr_memory_leak(stdscr) - test_userptr_segfault(stdscr) - test_resize_term(stdscr) - test_issue6243(stdscr) - test_unget_wch(stdscr) - test_issue10570() - test_encoding(stdscr) - finally: - curses.resetty() - -def test_main(): +def setUpModule(): if not sys.__stdout__.isatty(): raise unittest.SkipTest("sys.__stdout__ is not a tty") # testing setupterm() inside initscr/endwin # causes terminal breakage curses.setupterm(fd=sys.__stdout__.fileno()) - try: - stdscr = curses.initscr() - main(stdscr) - finally: - curses.endwin() - unit_tests() if __name__ == '__main__': - curses.wrapper(main) - unit_tests() + unittest.main()