mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #16000: Convert test_curses to use unittest
This commit is contained in:
parent
16c41ab460
commit
baf45c590c
2 changed files with 349 additions and 367 deletions
|
@ -2,21 +2,23 @@
|
|||
# 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
|
||||
import inspect
|
||||
requires('curses')
|
||||
|
||||
|
@ -24,17 +26,32 @@ requires('curses')
|
|||
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.skipUnless(sys.__stdout__.isatty(), 'sys.__stdout__ is not a tty')
|
||||
@unittest.skipIf(term == 'unknown',
|
||||
"$TERM=%r, calling initscr() may cause exit" % term)
|
||||
@unittest.skipIf(sys.platform == "cygwin",
|
||||
"cygwin's curses mostly just hangs")
|
||||
class TestCurses(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
curses.setupterm(fd=sys.__stdout__.fileno())
|
||||
|
||||
if sys.platform == "cygwin":
|
||||
raise unittest.SkipTest("cygwin's curses mostly just hangs")
|
||||
def setUp(self):
|
||||
if verbose:
|
||||
# just to make the test output a little more readable
|
||||
print()
|
||||
self.stdscr = curses.initscr()
|
||||
curses.savetty()
|
||||
|
||||
def window_funcs(stdscr):
|
||||
def tearDown(self):
|
||||
curses.resetty()
|
||||
curses.endwin()
|
||||
|
||||
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)
|
||||
|
@ -72,13 +89,10 @@ def window_funcs(stdscr):
|
|||
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)
|
||||
|
||||
|
@ -150,9 +164,9 @@ def window_funcs(stdscr):
|
|||
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,
|
||||
|
@ -231,7 +245,7 @@ def module_funcs(stdscr):
|
|||
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'),
|
||||
(';', ';'), (' ', ' '),
|
||||
|
@ -239,21 +253,19 @@ def unit_tests():
|
|||
# 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()
|
||||
|
@ -262,32 +274,36 @@ def test_userptr_memory_leak(stdscr):
|
|||
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'):
|
||||
@unittest.skipUnless(hasattr(curses, 'resizeterm'),
|
||||
'resizeterm not available')
|
||||
def test_resize_term(self):
|
||||
lines, cols = curses.LINES, curses.COLS
|
||||
curses.resizeterm(lines - 1, cols + 1)
|
||||
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:
|
||||
|
@ -297,42 +313,36 @@ def test_unget_wch(stdscr):
|
|||
try:
|
||||
curses.unget_wch(ch)
|
||||
except Exception as err:
|
||||
raise Exception("unget_wch(%a) failed with encoding %s: %s"
|
||||
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:
|
||||
stdscr.encoding = 10
|
||||
except TypeError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("TypeError not raised")
|
||||
stdscr.encoding = encoding
|
||||
try:
|
||||
del stdscr.encoding
|
||||
except TypeError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("TypeError not raised")
|
||||
|
||||
def test_issue21088(stdscr):
|
||||
with self.assertRaises(TypeError):
|
||||
stdscr.encoding = 10
|
||||
|
||||
stdscr.encoding = encoding
|
||||
with self.assertRaises(TypeError):
|
||||
del stdscr.encoding
|
||||
|
||||
def test_issue21088(self):
|
||||
stdscr = self.stdscr
|
||||
#
|
||||
# http://bugs.python.org/issue21088
|
||||
#
|
||||
|
@ -360,36 +370,6 @@ def test_issue21088(stdscr):
|
|||
offset = human_readable_signature.find("[y, x,]")
|
||||
assert offset >= 0, ""
|
||||
|
||||
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)
|
||||
test_issue21088(stdscr)
|
||||
finally:
|
||||
curses.resetty()
|
||||
|
||||
def test_main():
|
||||
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()
|
||||
|
|
|
@ -75,6 +75,8 @@ Library
|
|||
Tests
|
||||
-----
|
||||
|
||||
- Issue #16000: Convert test_curses to use unittest.
|
||||
|
||||
- Issue #21456: Skip two tests in test_urllib2net.py if _ssl module not
|
||||
present. Patch by Remi Pointel.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue