| 1 |
import functools |
| 2 |
import inspect |
| 3 |
import os |
| 4 |
import string |
| 5 |
import sys |
| 6 |
import tempfile |
| 7 |
import unittest |
| 8 |
from unittest.mock import MagicMock |
| 9 |
|
| 10 |
from test.support import (requires, verbose, SaveSignals, cpython_only, |
| 11 |
check_disallow_instantiation, MISSING_C_DOCSTRINGS, |
| 12 |
gc_collect) |
| 13 |
from test.support.import_helper import import_module |
| 14 |
|
| 15 |
# Optionally test curses module. This currently requires that the |
| 16 |
# 'curses' resource be given on the regrtest command line using the -u |
| 17 |
# option. If not available, nothing after this line will be executed. |
| 18 |
requires('curses') |
| 19 |
|
| 20 |
# If either of these don't exist, skip the tests. |
| 21 |
curses = import_module('curses') |
| 22 |
import_module('curses.ascii') |
| 23 |
import_module('curses.textpad') |
| 24 |
try: |
| 25 |
import curses.panel |
| 26 |
except ImportError: |
| 27 |
pass |
| 28 |
|
| 29 |
def requires_curses_func(name): |
| 30 |
return unittest.skipUnless(hasattr(curses, name), |
| 31 |
'requires curses.%s' % name) |
| 32 |
|
| 33 |
def requires_curses_window_meth(name): |
| 34 |
def deco(test): |
| 35 |
@functools.wraps(test) |
| 36 |
def wrapped(self, *args, **kwargs): |
| 37 |
if not hasattr(self.stdscr, name): |
| 38 |
raise unittest.SkipTest('requires curses.window.%s' % name) |
| 39 |
test(self, *args, **kwargs) |
| 40 |
return wrapped |
| 41 |
return deco |
| 42 |
|
| 43 |
|
| 44 |
def requires_colors(test): |
| 45 |
@functools.wraps(test) |
| 46 |
def wrapped(self, *args, **kwargs): |
| 47 |
if not curses.has_colors(): |
| 48 |
self.skipTest('requires colors support') |
| 49 |
curses.start_color() |
| 50 |
test(self, *args, **kwargs) |
| 51 |
return wrapped |
| 52 |
|
| 53 |
term = os.environ.get('TERM') |
| 54 |
SHORT_MAX = 0x7fff |
| 55 |
|
| 56 |
# If newterm was supported we could use it instead of initscr and not exit |
| 57 |
@unittest.skipIf(not term or term == 'unknown', |
| 58 |
"$TERM=%r, calling initscr() may cause exit" % term) |
| 59 |
@unittest.skipIf(sys.platform == "cygwin", |
| 60 |
"cygwin's curses mostly just hangs") |
| 61 |
class TestCurses(unittest.TestCase): |
| 62 |
|
| 63 |
@classmethod |
| 64 |
def setUpClass(cls): |
| 65 |
if verbose: |
| 66 |
print(f'TERM={term}', file=sys.stderr, flush=True) |
| 67 |
# testing setupterm() inside initscr/endwin |
| 68 |
# causes terminal breakage |
| 69 |
stdout_fd = sys.__stdout__.fileno() |
| 70 |
curses.setupterm(fd=stdout_fd) |
| 71 |
|
| 72 |
def setUp(self): |
| 73 |
self.isatty = True |
| 74 |
self.output = sys.__stdout__ |
| 75 |
stdout_fd = sys.__stdout__.fileno() |
| 76 |
if not sys.__stdout__.isatty(): |
| 77 |
# initstr() unconditionally uses C stdout. |
| 78 |
# If it is redirected to file or pipe, try to attach it |
| 79 |
# to terminal. |
| 80 |
# First, save a copy of the file descriptor of stdout, so it |
| 81 |
# can be restored after finishing the test. |
| 82 |
dup_fd = os.dup(stdout_fd) |
| 83 |
self.addCleanup(os.close, dup_fd) |
| 84 |
self.addCleanup(os.dup2, dup_fd, stdout_fd) |
| 85 |
|
| 86 |
if sys.__stderr__.isatty(): |
| 87 |
# If stderr is connected to terminal, use it. |
| 88 |
tmp = sys.__stderr__ |
| 89 |
self.output = sys.__stderr__ |
| 90 |
else: |
| 91 |
try: |
| 92 |
# Try to open the terminal device. |
| 93 |
tmp = open('/dev/tty', 'wb', buffering=0) |
| 94 |
except OSError: |
| 95 |
# As a fallback, use regular file to write control codes. |
| 96 |
# Some functions (like savetty) will not work, but at |
| 97 |
# least the garbage control sequences will not be mixed |
| 98 |
# with the testing report. |
| 99 |
tmp = tempfile.TemporaryFile(mode='wb', buffering=0) |
| 100 |
self.isatty = False |
| 101 |
self.addCleanup(tmp.close) |
| 102 |
self.output = None |
| 103 |
os.dup2(tmp.fileno(), stdout_fd) |
| 104 |
|
| 105 |
self.save_signals = SaveSignals() |
| 106 |
self.save_signals.save() |
| 107 |
self.addCleanup(self.save_signals.restore) |
| 108 |
if verbose and self.output is not None: |
| 109 |
# just to make the test output a little more readable |
| 110 |
sys.stderr.flush() |
| 111 |
sys.stdout.flush() |
| 112 |
print(file=self.output, flush=True) |
| 113 |
self.stdscr = curses.initscr() |
| 114 |
if self.isatty: |
| 115 |
curses.savetty() |
| 116 |
self.addCleanup(curses.endwin) |
| 117 |
self.addCleanup(curses.resetty) |
| 118 |
self.stdscr.erase() |
| 119 |
|
| 120 |
@requires_curses_func('filter') |
| 121 |
def test_filter(self): |
| 122 |
# TODO: Should be called before initscr() or newterm() are called. |
| 123 |
# TODO: nofilter() |
| 124 |
curses.filter() |
| 125 |
|
| 126 |
@requires_curses_func('use_env') |
| 127 |
def test_use_env(self): |
| 128 |
# TODO: Should be called before initscr() or newterm() are called. |
| 129 |
# TODO: use_tioctl() |
| 130 |
curses.use_env(False) |
| 131 |
curses.use_env(True) |
| 132 |
|
| 133 |
def test_create_windows(self): |
| 134 |
win = curses.newwin(5, 10) |
| 135 |
self.assertEqual(win.getbegyx(), (0, 0)) |
| 136 |
self.assertEqual(win.getparyx(), (-1, -1)) |
| 137 |
self.assertEqual(win.getmaxyx(), (5, 10)) |
| 138 |
|
| 139 |
win = curses.newwin(10, 15, 2, 5) |
| 140 |
self.assertEqual(win.getbegyx(), (2, 5)) |
| 141 |
self.assertEqual(win.getparyx(), (-1, -1)) |
| 142 |
self.assertEqual(win.getmaxyx(), (10, 15)) |
| 143 |
|
| 144 |
win2 = win.subwin(3, 7) |
| 145 |
self.assertEqual(win2.getbegyx(), (3, 7)) |
| 146 |
self.assertEqual(win2.getparyx(), (1, 2)) |
| 147 |
self.assertEqual(win2.getmaxyx(), (9, 13)) |
| 148 |
|
| 149 |
win2 = win.subwin(5, 10, 3, 7) |
| 150 |
self.assertEqual(win2.getbegyx(), (3, 7)) |
| 151 |
self.assertEqual(win2.getparyx(), (1, 2)) |
| 152 |
self.assertEqual(win2.getmaxyx(), (5, 10)) |
| 153 |
|
| 154 |
win3 = win.derwin(2, 3) |
| 155 |
self.assertEqual(win3.getbegyx(), (4, 8)) |
| 156 |
self.assertEqual(win3.getparyx(), (2, 3)) |
| 157 |
self.assertEqual(win3.getmaxyx(), (8, 12)) |
| 158 |
|
| 159 |
win3 = win.derwin(6, 11, 2, 3) |
| 160 |
self.assertEqual(win3.getbegyx(), (4, 8)) |
| 161 |
self.assertEqual(win3.getparyx(), (2, 3)) |
| 162 |
self.assertEqual(win3.getmaxyx(), (6, 11)) |
| 163 |
|
| 164 |
win.mvwin(0, 1) |
| 165 |
self.assertEqual(win.getbegyx(), (0, 1)) |
| 166 |
self.assertEqual(win.getparyx(), (-1, -1)) |
| 167 |
self.assertEqual(win.getmaxyx(), (10, 15)) |
| 168 |
self.assertEqual(win2.getbegyx(), (3, 7)) |
| 169 |
self.assertEqual(win2.getparyx(), (1, 2)) |
| 170 |
self.assertEqual(win2.getmaxyx(), (5, 10)) |
| 171 |
self.assertEqual(win3.getbegyx(), (4, 8)) |
| 172 |
self.assertEqual(win3.getparyx(), (2, 3)) |
| 173 |
self.assertEqual(win3.getmaxyx(), (6, 11)) |
| 174 |
|
| 175 |
win2.mvderwin(2, 1) |
| 176 |
self.assertEqual(win2.getbegyx(), (3, 7)) |
| 177 |
self.assertEqual(win2.getparyx(), (2, 1)) |
| 178 |
self.assertEqual(win2.getmaxyx(), (5, 10)) |
| 179 |
|
| 180 |
win3.mvderwin(2, 1) |
| 181 |
self.assertEqual(win3.getbegyx(), (4, 8)) |
| 182 |
self.assertEqual(win3.getparyx(), (2, 1)) |
| 183 |
self.assertEqual(win3.getmaxyx(), (6, 11)) |
| 184 |
|
| 185 |
def test_subwindows_references(self): |
| 186 |
win = curses.newwin(5, 10) |
| 187 |
win2 = win.subwin(3, 7) |
| 188 |
del win |
| 189 |
gc_collect() |
| 190 |
del win2 |
| 191 |
gc_collect() |
| 192 |
|
| 193 |
def test_move_cursor(self): |
| 194 |
stdscr = self.stdscr |
| 195 |
win = stdscr.subwin(10, 15, 2, 5) |
| 196 |
stdscr.move(1, 2) |
| 197 |
win.move(2, 4) |
| 198 |
self.assertEqual(stdscr.getyx(), (1, 2)) |
| 199 |
self.assertEqual(win.getyx(), (2, 4)) |
| 200 |
|
| 201 |
win.cursyncup() |
| 202 |
self.assertEqual(stdscr.getyx(), (4, 9)) |
| 203 |
|
| 204 |
def test_refresh_control(self): |
| 205 |
stdscr = self.stdscr |
| 206 |
# touchwin()/untouchwin()/is_wintouched() |
| 207 |
stdscr.refresh() |
| 208 |
self.assertIs(stdscr.is_wintouched(), False) |
| 209 |
stdscr.touchwin() |
| 210 |
self.assertIs(stdscr.is_wintouched(), True) |
| 211 |
stdscr.refresh() |
| 212 |
self.assertIs(stdscr.is_wintouched(), False) |
| 213 |
stdscr.touchwin() |
| 214 |
self.assertIs(stdscr.is_wintouched(), True) |
| 215 |
stdscr.untouchwin() |
| 216 |
self.assertIs(stdscr.is_wintouched(), False) |
| 217 |
|
| 218 |
# touchline()/untouchline()/is_linetouched() |
| 219 |
stdscr.touchline(5, 2) |
| 220 |
self.assertIs(stdscr.is_linetouched(5), True) |
| 221 |
self.assertIs(stdscr.is_linetouched(6), True) |
| 222 |
self.assertIs(stdscr.is_wintouched(), True) |
| 223 |
stdscr.touchline(5, 1, False) |
| 224 |
self.assertIs(stdscr.is_linetouched(5), False) |
| 225 |
|
| 226 |
# syncup() |
| 227 |
win = stdscr.subwin(10, 15, 2, 5) |
| 228 |
win2 = win.subwin(5, 10, 3, 7) |
| 229 |
win2.touchwin() |
| 230 |
stdscr.untouchwin() |
| 231 |
win2.syncup() |
| 232 |
self.assertIs(win.is_wintouched(), True) |
| 233 |
self.assertIs(stdscr.is_wintouched(), True) |
| 234 |
|
| 235 |
# syncdown() |
| 236 |
stdscr.touchwin() |
| 237 |
win.untouchwin() |
| 238 |
win2.untouchwin() |
| 239 |
win2.syncdown() |
| 240 |
self.assertIs(win2.is_wintouched(), True) |
| 241 |
|
| 242 |
# syncok() |
| 243 |
if hasattr(stdscr, 'syncok') and not sys.platform.startswith("sunos"): |
| 244 |
win.untouchwin() |
| 245 |
stdscr.untouchwin() |
| 246 |
for syncok in [False, True]: |
| 247 |
win2.syncok(syncok) |
| 248 |
win2.addch('a') |
| 249 |
self.assertIs(win.is_wintouched(), syncok) |
| 250 |
self.assertIs(stdscr.is_wintouched(), syncok) |
| 251 |
|
| 252 |
def test_output_character(self): |
| 253 |
stdscr = self.stdscr |
| 254 |
encoding = stdscr.encoding |
| 255 |
# addch() |
| 256 |
stdscr.refresh() |
| 257 |
stdscr.move(0, 0) |
| 258 |
stdscr.addch('A') |
| 259 |
stdscr.addch(b'A') |
| 260 |
stdscr.addch(65) |
| 261 |
c = '\u20ac' |
| 262 |
try: |
| 263 |
stdscr.addch(c) |
| 264 |
except UnicodeEncodeError: |
| 265 |
self.assertRaises(UnicodeEncodeError, c.encode, encoding) |
| 266 |
except OverflowError: |
| 267 |
encoded = c.encode(encoding) |
| 268 |
self.assertNotEqual(len(encoded), 1, repr(encoded)) |
| 269 |
stdscr.addch('A', curses.A_BOLD) |
| 270 |
stdscr.addch(1, 2, 'A') |
| 271 |
stdscr.addch(2, 3, 'A', curses.A_BOLD) |
| 272 |
self.assertIs(stdscr.is_wintouched(), True) |
| 273 |
|
| 274 |
# echochar() |
| 275 |
stdscr.refresh() |
| 276 |
stdscr.move(0, 0) |
| 277 |
stdscr.echochar('A') |
| 278 |
stdscr.echochar(b'A') |
| 279 |
stdscr.echochar(65) |
| 280 |
with self.assertRaises((UnicodeEncodeError, OverflowError)): |
| 281 |
# Unicode is not fully supported yet, but at least it does |
| 282 |
# not crash. |
| 283 |
# It is supposed to fail because either the character is |
| 284 |
# not encodable with the current encoding, or it is encoded to |
| 285 |
# a multibyte sequence. |
| 286 |
stdscr.echochar('\u0114') |
| 287 |
stdscr.echochar('A', curses.A_BOLD) |
| 288 |
self.assertIs(stdscr.is_wintouched(), False) |
| 289 |
|
| 290 |
def test_output_string(self): |
| 291 |
stdscr = self.stdscr |
| 292 |
encoding = stdscr.encoding |
| 293 |
# addstr()/insstr() |
| 294 |
for func in [stdscr.addstr, stdscr.insstr]: |
| 295 |
with self.subTest(func.__qualname__): |
| 296 |
stdscr.move(0, 0) |
| 297 |
func('abcd') |
| 298 |
func(b'abcd') |
| 299 |
s = 'à ßçđ' |
| 300 |
try: |
| 301 |
func(s) |
| 302 |
except UnicodeEncodeError: |
| 303 |
self.assertRaises(UnicodeEncodeError, s.encode, encoding) |
| 304 |
func('abcd', curses.A_BOLD) |
| 305 |
func(1, 2, 'abcd') |
| 306 |
func(2, 3, 'abcd', curses.A_BOLD) |
| 307 |
|
| 308 |
# addnstr()/insnstr() |
| 309 |
for func in [stdscr.addnstr, stdscr.insnstr]: |
| 310 |
with self.subTest(func.__qualname__): |
| 311 |
stdscr.move(0, 0) |
| 312 |
func('1234', 3) |
| 313 |
func(b'1234', 3) |
| 314 |
s = '\u0661\u0662\u0663\u0664' |
| 315 |
try: |
| 316 |
func(s, 3) |
| 317 |
except UnicodeEncodeError: |
| 318 |
self.assertRaises(UnicodeEncodeError, s.encode, encoding) |
| 319 |
func('1234', 5) |
| 320 |
func('1234', 3, curses.A_BOLD) |
| 321 |
func(1, 2, '1234', 3) |
| 322 |
func(2, 3, '1234', 3, curses.A_BOLD) |
| 323 |
|
| 324 |
def test_output_string_embedded_null_chars(self): |
| 325 |
# reject embedded null bytes and characters |
| 326 |
stdscr = self.stdscr |
| 327 |
for arg in ['a\0', b'a\0']: |
| 328 |
with self.subTest(arg=arg): |
| 329 |
self.assertRaises(ValueError, stdscr.addstr, arg) |
| 330 |
self.assertRaises(ValueError, stdscr.addnstr, arg, 1) |
| 331 |
self.assertRaises(ValueError, stdscr.insstr, arg) |
| 332 |
self.assertRaises(ValueError, stdscr.insnstr, arg, 1) |
| 333 |
|
| 334 |
def test_read_from_window(self): |
| 335 |
stdscr = self.stdscr |
| 336 |
stdscr.addstr(0, 1, 'ABCD', curses.A_BOLD) |
| 337 |
# inch() |
| 338 |
stdscr.move(0, 1) |
| 339 |
self.assertEqual(stdscr.inch(), 65 | curses.A_BOLD) |
| 340 |
self.assertEqual(stdscr.inch(0, 3), 67 | curses.A_BOLD) |
| 341 |
stdscr.move(0, 0) |
| 342 |
# instr() |
| 343 |
self.assertEqual(stdscr.instr()[:6], b' ABCD ') |
| 344 |
self.assertEqual(stdscr.instr(3)[:6], b' AB') |
| 345 |
self.assertEqual(stdscr.instr(0, 2)[:4], b'BCD ') |
| 346 |
self.assertEqual(stdscr.instr(0, 2, 4), b'BCD ') |
| 347 |
self.assertRaises(ValueError, stdscr.instr, -2) |
| 348 |
self.assertRaises(ValueError, stdscr.instr, 0, 2, -2) |
| 349 |
|
| 350 |
def test_getch(self): |
| 351 |
win = curses.newwin(5, 12, 5, 2) |
| 352 |
|
| 353 |
# TODO: Test with real input by writing to master fd. |
| 354 |
for c in 'spam\n'[::-1]: |
| 355 |
curses.ungetch(c) |
| 356 |
self.assertEqual(win.getch(3, 1), b's'[0]) |
| 357 |
self.assertEqual(win.getyx(), (3, 1)) |
| 358 |
self.assertEqual(win.getch(3, 4), b'p'[0]) |
| 359 |
self.assertEqual(win.getyx(), (3, 4)) |
| 360 |
self.assertEqual(win.getch(), b'a'[0]) |
| 361 |
self.assertEqual(win.getyx(), (3, 4)) |
| 362 |
self.assertEqual(win.getch(), b'm'[0]) |
| 363 |
self.assertEqual(win.getch(), b'\n'[0]) |
| 364 |
|
| 365 |
def test_getstr(self): |
| 366 |
win = curses.newwin(5, 12, 5, 2) |
| 367 |
curses.echo() |
| 368 |
self.addCleanup(curses.noecho) |
| 369 |
|
| 370 |
self.assertRaises(ValueError, win.getstr, -400) |
| 371 |
self.assertRaises(ValueError, win.getstr, 2, 3, -400) |
| 372 |
|
| 373 |
# TODO: Test with real input by writing to master fd. |
| 374 |
for c in 'Lorem\nipsum\ndolor\nsit\namet\n'[::-1]: |
| 375 |
curses.ungetch(c) |
| 376 |
self.assertEqual(win.getstr(3, 1, 2), b'Lo') |
| 377 |
self.assertEqual(win.instr(3, 0), b' Lo ') |
| 378 |
self.assertEqual(win.getstr(3, 5, 10), b'ipsum') |
| 379 |
self.assertEqual(win.instr(3, 0), b' Lo ipsum ') |
| 380 |
self.assertEqual(win.getstr(1, 5), b'dolor') |
| 381 |
self.assertEqual(win.instr(1, 0), b' dolor ') |
| 382 |
self.assertEqual(win.getstr(2), b'si') |
| 383 |
self.assertEqual(win.instr(1, 0), b'si dolor ') |
| 384 |
self.assertEqual(win.getstr(), b'amet') |
| 385 |
self.assertEqual(win.instr(1, 0), b'amet dolor ') |
| 386 |
|
| 387 |
def test_clear(self): |
| 388 |
win = curses.newwin(5, 15, 5, 2) |
| 389 |
lorem_ipsum(win) |
| 390 |
|
| 391 |
win.move(0, 8) |
| 392 |
win.clrtoeol() |
| 393 |
self.assertEqual(win.instr(0, 0).rstrip(), b'Lorem ip') |
| 394 |
self.assertEqual(win.instr(1, 0).rstrip(), b'dolor sit amet,') |
| 395 |
|
| 396 |
win.move(0, 3) |
| 397 |
win.clrtobot() |
| 398 |
self.assertEqual(win.instr(0, 0).rstrip(), b'Lor') |
| 399 |
self.assertEqual(win.instr(1, 0).rstrip(), b'') |
| 400 |
|
| 401 |
for func in [win.erase, win.clear]: |
| 402 |
lorem_ipsum(win) |
| 403 |
func() |
| 404 |
self.assertEqual(win.instr(0, 0).rstrip(), b'') |
| 405 |
self.assertEqual(win.instr(1, 0).rstrip(), b'') |
| 406 |
|
| 407 |
def test_insert_delete(self): |
| 408 |
win = curses.newwin(5, 15, 5, 2) |
| 409 |
lorem_ipsum(win) |
| 410 |
|
| 411 |
win.move(0, 2) |
| 412 |
win.delch() |
| 413 |
self.assertEqual(win.instr(0, 0), b'Loem ipsum ') |
| 414 |
win.delch(0, 7) |
| 415 |
self.assertEqual(win.instr(0, 0), b'Loem ipum ') |
| 416 |
|
| 417 |
win.move(1, 5) |
| 418 |
win.deleteln() |
| 419 |
self.assertEqual(win.instr(0, 0), b'Loem ipum ') |
| 420 |
self.assertEqual(win.instr(1, 0), b'consectetur ') |
| 421 |
self.assertEqual(win.instr(2, 0), b'adipiscing elit') |
| 422 |
self.assertEqual(win.instr(3, 0), b'sed do eiusmod ') |
| 423 |
self.assertEqual(win.instr(4, 0), b' ') |
| 424 |
|
| 425 |
win.move(1, 5) |
| 426 |
win.insertln() |
| 427 |
self.assertEqual(win.instr(0, 0), b'Loem ipum ') |
| 428 |
self.assertEqual(win.instr(1, 0), b' ') |
| 429 |
self.assertEqual(win.instr(2, 0), b'consectetur ') |
| 430 |
|
| 431 |
win.clear() |
| 432 |
lorem_ipsum(win) |
| 433 |
win.move(1, 5) |
| 434 |
win.insdelln(2) |
| 435 |
self.assertEqual(win.instr(0, 0), b'Lorem ipsum ') |
| 436 |
self.assertEqual(win.instr(1, 0), b' ') |
| 437 |
self.assertEqual(win.instr(2, 0), b' ') |
| 438 |
self.assertEqual(win.instr(3, 0), b'dolor sit amet,') |
| 439 |
|
| 440 |
win.clear() |
| 441 |
lorem_ipsum(win) |
| 442 |
win.move(1, 5) |
| 443 |
win.insdelln(-2) |
| 444 |
self.assertEqual(win.instr(0, 0), b'Lorem ipsum ') |
| 445 |
self.assertEqual(win.instr(1, 0), b'adipiscing elit') |
| 446 |
self.assertEqual(win.instr(2, 0), b'sed do eiusmod ') |
| 447 |
self.assertEqual(win.instr(3, 0), b' ') |
| 448 |
|
| 449 |
def test_scroll(self): |
| 450 |
win = curses.newwin(5, 15, 5, 2) |
| 451 |
lorem_ipsum(win) |
| 452 |
win.scrollok(True) |
| 453 |
win.scroll() |
| 454 |
self.assertEqual(win.instr(0, 0), b'dolor sit amet,') |
| 455 |
win.scroll(2) |
| 456 |
self.assertEqual(win.instr(0, 0), b'adipiscing elit') |
| 457 |
win.scroll(-3) |
| 458 |
self.assertEqual(win.instr(0, 0), b' ') |
| 459 |
self.assertEqual(win.instr(2, 0), b' ') |
| 460 |
self.assertEqual(win.instr(3, 0), b'adipiscing elit') |
| 461 |
win.scrollok(False) |
| 462 |
|
| 463 |
def test_attributes(self): |
| 464 |
# TODO: attr_get(), attr_set(), ... |
| 465 |
win = curses.newwin(5, 15, 5, 2) |
| 466 |
win.attron(curses.A_BOLD) |
| 467 |
win.attroff(curses.A_BOLD) |
| 468 |
win.attrset(curses.A_BOLD) |
| 469 |
|
| 470 |
win.standout() |
| 471 |
win.standend() |
| 472 |
|
| 473 |
@requires_curses_window_meth('chgat') |
| 474 |
def test_chgat(self): |
| 475 |
win = curses.newwin(5, 15, 5, 2) |
| 476 |
win.addstr(2, 0, 'Lorem ipsum') |
| 477 |
win.addstr(3, 0, 'dolor sit amet') |
| 478 |
|
| 479 |
win.move(2, 8) |
| 480 |
win.chgat(curses.A_BLINK) |
| 481 |
self.assertEqual(win.inch(2, 7), b'p'[0]) |
| 482 |
self.assertEqual(win.inch(2, 8), b's'[0] | curses.A_BLINK) |
| 483 |
self.assertEqual(win.inch(2, 14), b' '[0] | curses.A_BLINK) |
| 484 |
|
| 485 |
win.move(2, 1) |
| 486 |
win.chgat(3, curses.A_BOLD) |
| 487 |
self.assertEqual(win.inch(2, 0), b'L'[0]) |
| 488 |
self.assertEqual(win.inch(2, 1), b'o'[0] | curses.A_BOLD) |
| 489 |
self.assertEqual(win.inch(2, 3), b'e'[0] | curses.A_BOLD) |
| 490 |
self.assertEqual(win.inch(2, 4), b'm'[0]) |
| 491 |
|
| 492 |
win.chgat(3, 2, curses.A_UNDERLINE) |
| 493 |
self.assertEqual(win.inch(3, 1), b'o'[0]) |
| 494 |
self.assertEqual(win.inch(3, 2), b'l'[0] | curses.A_UNDERLINE) |
| 495 |
self.assertEqual(win.inch(3, 14), b' '[0] | curses.A_UNDERLINE) |
| 496 |
|
| 497 |
win.chgat(3, 4, 7, curses.A_BLINK) |
| 498 |
self.assertEqual(win.inch(3, 3), b'o'[0] | curses.A_UNDERLINE) |
| 499 |
self.assertEqual(win.inch(3, 4), b'r'[0] | curses.A_BLINK) |
| 500 |
self.assertEqual(win.inch(3, 10), b'a'[0] | curses.A_BLINK) |
| 501 |
self.assertEqual(win.inch(3, 11), b'm'[0] | curses.A_UNDERLINE) |
| 502 |
self.assertEqual(win.inch(3, 14), b' '[0] | curses.A_UNDERLINE) |
| 503 |
|
| 504 |
def test_background(self): |
| 505 |
win = curses.newwin(5, 15, 5, 2) |
| 506 |
win.addstr(0, 0, 'Lorem ipsum') |
| 507 |
|
| 508 |
self.assertIn(win.getbkgd(), (0, 32)) |
| 509 |
|
| 510 |
# bkgdset() |
| 511 |
win.bkgdset('_') |
| 512 |
self.assertEqual(win.getbkgd(), b'_'[0]) |
| 513 |
win.bkgdset(b'#') |
| 514 |
self.assertEqual(win.getbkgd(), b'#'[0]) |
| 515 |
win.bkgdset(65) |
| 516 |
self.assertEqual(win.getbkgd(), 65) |
| 517 |
win.bkgdset(0) |
| 518 |
self.assertEqual(win.getbkgd(), 32) |
| 519 |
|
| 520 |
win.bkgdset('#', curses.A_REVERSE) |
| 521 |
self.assertEqual(win.getbkgd(), b'#'[0] | curses.A_REVERSE) |
| 522 |
self.assertEqual(win.inch(0, 0), b'L'[0]) |
| 523 |
self.assertEqual(win.inch(0, 5), b' '[0]) |
| 524 |
win.bkgdset(0) |
| 525 |
|
| 526 |
# bkgd() |
| 527 |
win.bkgd('_') |
| 528 |
self.assertEqual(win.getbkgd(), b'_'[0]) |
| 529 |
self.assertEqual(win.inch(0, 0), b'L'[0]) |
| 530 |
self.assertEqual(win.inch(0, 5), b'_'[0]) |
| 531 |
|
| 532 |
win.bkgd('#', curses.A_REVERSE) |
| 533 |
self.assertEqual(win.getbkgd(), b'#'[0] | curses.A_REVERSE) |
| 534 |
self.assertEqual(win.inch(0, 0), b'L'[0] | curses.A_REVERSE) |
| 535 |
self.assertEqual(win.inch(0, 5), b'#'[0] | curses.A_REVERSE) |
| 536 |
|
| 537 |
def test_overlay(self): |
| 538 |
srcwin = curses.newwin(5, 18, 3, 4) |
| 539 |
lorem_ipsum(srcwin) |
| 540 |
dstwin = curses.newwin(7, 17, 5, 7) |
| 541 |
for i in range(6): |
| 542 |
dstwin.addstr(i, 0, '_'*17) |
| 543 |
|
| 544 |
srcwin.overlay(dstwin) |
| 545 |
self.assertEqual(dstwin.instr(0, 0), b'sectetur_________') |
| 546 |
self.assertEqual(dstwin.instr(1, 0), b'piscing_elit,____') |
| 547 |
self.assertEqual(dstwin.instr(2, 0), b'_do_eiusmod______') |
| 548 |
self.assertEqual(dstwin.instr(3, 0), b'_________________') |
| 549 |
|
| 550 |
srcwin.overwrite(dstwin) |
| 551 |
self.assertEqual(dstwin.instr(0, 0), b'sectetur __') |
| 552 |
self.assertEqual(dstwin.instr(1, 0), b'piscing elit, __') |
| 553 |
self.assertEqual(dstwin.instr(2, 0), b' do eiusmod __') |
| 554 |
self.assertEqual(dstwin.instr(3, 0), b'_________________') |
| 555 |
|
| 556 |
srcwin.overlay(dstwin, 1, 4, 3, 2, 4, 11) |
| 557 |
self.assertEqual(dstwin.instr(3, 0), b'__r_sit_amet_____') |
| 558 |
self.assertEqual(dstwin.instr(4, 0), b'__ectetur________') |
| 559 |
self.assertEqual(dstwin.instr(5, 0), b'_________________') |
| 560 |
|
| 561 |
srcwin.overwrite(dstwin, 1, 4, 3, 2, 4, 11) |
| 562 |
self.assertEqual(dstwin.instr(3, 0), b'__r sit amet_____') |
| 563 |
self.assertEqual(dstwin.instr(4, 0), b'__ectetur _____') |
| 564 |
self.assertEqual(dstwin.instr(5, 0), b'_________________') |
| 565 |
|
| 566 |
def test_refresh(self): |
| 567 |
win = curses.newwin(5, 15, 2, 5) |
| 568 |
win.noutrefresh() |
| 569 |
win.redrawln(1, 2) |
| 570 |
win.redrawwin() |
| 571 |
win.refresh() |
| 572 |
curses.doupdate() |
| 573 |
|
| 574 |
@requires_curses_window_meth('resize') |
| 575 |
def test_resize(self): |
| 576 |
win = curses.newwin(5, 15, 2, 5) |
| 577 |
win.resize(4, 20) |
| 578 |
self.assertEqual(win.getmaxyx(), (4, 20)) |
| 579 |
win.resize(5, 15) |
| 580 |
self.assertEqual(win.getmaxyx(), (5, 15)) |
| 581 |
|
| 582 |
@requires_curses_window_meth('enclose') |
| 583 |
def test_enclose(self): |
| 584 |
win = curses.newwin(5, 15, 2, 5) |
| 585 |
self.assertIs(win.enclose(2, 5), True) |
| 586 |
self.assertIs(win.enclose(1, 5), False) |
| 587 |
self.assertIs(win.enclose(2, 4), False) |
| 588 |
self.assertIs(win.enclose(6, 19), True) |
| 589 |
self.assertIs(win.enclose(7, 19), False) |
| 590 |
self.assertIs(win.enclose(6, 20), False) |
| 591 |
|
| 592 |
def test_putwin(self): |
| 593 |
win = curses.newwin(5, 12, 1, 2) |
| 594 |
win.addstr(2, 1, 'Lorem ipsum') |
| 595 |
with tempfile.TemporaryFile() as f: |
| 596 |
win.putwin(f) |
| 597 |
del win |
| 598 |
f.seek(0) |
| 599 |
win = curses.getwin(f) |
| 600 |
self.assertEqual(win.getbegyx(), (1, 2)) |
| 601 |
self.assertEqual(win.getmaxyx(), (5, 12)) |
| 602 |
self.assertEqual(win.instr(2, 0), b' Lorem ipsum') |
| 603 |
|
| 604 |
def test_borders_and_lines(self): |
| 605 |
win = curses.newwin(5, 10, 5, 2) |
| 606 |
win.border('|', '!', '-', '_', |
| 607 |
'+', '\\', '#', '/') |
| 608 |
self.assertEqual(win.instr(0, 0), b'+--------\\') |
| 609 |
self.assertEqual(win.instr(1, 0), b'| !') |
| 610 |
self.assertEqual(win.instr(4, 0), b'#________/') |
| 611 |
win.border(b'|', b'!', b'-', b'_', |
| 612 |
b'+', b'\\', b'#', b'/') |
| 613 |
win.border(65, 66, 67, 68, |
| 614 |
69, 70, 71, 72) |
| 615 |
self.assertRaises(TypeError, win.border, |
| 616 |
65, 66, 67, 68, 69, [], 71, 72) |
| 617 |
self.assertRaises(TypeError, win.border, |
| 618 |
65, 66, 67, 68, 69, 70, 71, 72, 73) |
| 619 |
self.assertRaises(TypeError, win.border, |
| 620 |
65, 66, 67, 68, 69, 70, 71, 72, 73) |
| 621 |
win.border(65, 66, 67, 68, 69, 70, 71) |
| 622 |
win.border(65, 66, 67, 68, 69, 70) |
| 623 |
win.border(65, 66, 67, 68, 69) |
| 624 |
win.border(65, 66, 67, 68) |
| 625 |
win.border(65, 66, 67) |
| 626 |
win.border(65, 66) |
| 627 |
win.border(65) |
| 628 |
win.border() |
| 629 |
|
| 630 |
win.box(':', '~') |
| 631 |
self.assertEqual(win.instr(0, 1, 8), b'~~~~~~~~') |
| 632 |
self.assertEqual(win.instr(1, 0), b': :') |
| 633 |
self.assertEqual(win.instr(4, 1, 8), b'~~~~~~~~') |
| 634 |
win.box(b':', b'~') |
| 635 |
win.box(65, 67) |
| 636 |
self.assertRaises(TypeError, win.box, 65, 66, 67) |
| 637 |
self.assertRaises(TypeError, win.box, 65) |
| 638 |
win.box() |
| 639 |
|
| 640 |
win.move(1, 2) |
| 641 |
win.hline('-', 5) |
| 642 |
self.assertEqual(win.instr(1, 1, 7), b' ----- ') |
| 643 |
win.hline(b'-', 5) |
| 644 |
win.hline(45, 5) |
| 645 |
win.hline('-', 5, curses.A_BOLD) |
| 646 |
win.hline(1, 1, '-', 5) |
| 647 |
win.hline(1, 1, '-', 5, curses.A_BOLD) |
| 648 |
|
| 649 |
win.move(1, 2) |
| 650 |
win.vline('a', 3) |
| 651 |
win.vline(b'a', 3) |
| 652 |
win.vline(97, 3) |
| 653 |
win.vline('a', 3, curses.A_STANDOUT) |
| 654 |
win.vline(1, 1, 'a', 3) |
| 655 |
win.vline(1, 1, ';', 2, curses.A_STANDOUT) |
| 656 |
self.assertEqual(win.inch(1, 1), b';'[0] | curses.A_STANDOUT) |
| 657 |
self.assertEqual(win.inch(2, 1), b';'[0] | curses.A_STANDOUT) |
| 658 |
self.assertEqual(win.inch(3, 1), b'a'[0]) |
| 659 |
|
| 660 |
def test_unctrl(self): |
| 661 |
# TODO: wunctrl() |
| 662 |
self.assertEqual(curses.unctrl(b'A'), b'A') |
| 663 |
self.assertEqual(curses.unctrl('A'), b'A') |
| 664 |
self.assertEqual(curses.unctrl(65), b'A') |
| 665 |
self.assertEqual(curses.unctrl(b'\n'), b'^J') |
| 666 |
self.assertEqual(curses.unctrl('\n'), b'^J') |
| 667 |
self.assertEqual(curses.unctrl(10), b'^J') |
| 668 |
self.assertRaises(TypeError, curses.unctrl, b'') |
| 669 |
self.assertRaises(TypeError, curses.unctrl, b'AB') |
| 670 |
self.assertRaises(TypeError, curses.unctrl, '') |
| 671 |
self.assertRaises(TypeError, curses.unctrl, 'AB') |
| 672 |
self.assertRaises(OverflowError, curses.unctrl, 2**64) |
| 673 |
|
| 674 |
def test_endwin(self): |
| 675 |
if not self.isatty: |
| 676 |
self.skipTest('requires terminal') |
| 677 |
self.assertIs(curses.isendwin(), False) |
| 678 |
curses.endwin() |
| 679 |
self.assertIs(curses.isendwin(), True) |
| 680 |
curses.doupdate() |
| 681 |
self.assertIs(curses.isendwin(), False) |
| 682 |
|
| 683 |
def test_terminfo(self): |
| 684 |
self.assertIsInstance(curses.tigetflag('hc'), int) |
| 685 |
self.assertEqual(curses.tigetflag('cols'), -1) |
| 686 |
self.assertEqual(curses.tigetflag('cr'), -1) |
| 687 |
|
| 688 |
self.assertIsInstance(curses.tigetnum('cols'), int) |
| 689 |
self.assertEqual(curses.tigetnum('hc'), -2) |
| 690 |
self.assertEqual(curses.tigetnum('cr'), -2) |
| 691 |
|
| 692 |
self.assertIsInstance(curses.tigetstr('cr'), (bytes, type(None))) |
| 693 |
self.assertIsNone(curses.tigetstr('hc')) |
| 694 |
self.assertIsNone(curses.tigetstr('cols')) |
| 695 |
|
| 696 |
cud = curses.tigetstr('cud') |
| 697 |
if cud is not None: |
| 698 |
# See issue10570. |
| 699 |
self.assertIsInstance(cud, bytes) |
| 700 |
curses.tparm(cud, 2) |
| 701 |
cud_2 = curses.tparm(cud, 2) |
| 702 |
self.assertIsInstance(cud_2, bytes) |
| 703 |
curses.putp(cud_2) |
| 704 |
|
| 705 |
curses.putp(b'abc\n') |
| 706 |
|
| 707 |
def test_misc_module_funcs(self): |
| 708 |
curses.delay_output(1) |
| 709 |
curses.flushinp() |
| 710 |
|
| 711 |
curses.doupdate() |
| 712 |
self.assertIs(curses.isendwin(), False) |
| 713 |
|
| 714 |
curses.napms(100) |
| 715 |
|
| 716 |
curses.newpad(50, 50) |
| 717 |
|
| 718 |
def test_env_queries(self): |
| 719 |
# TODO: term_attrs(), erasewchar(), killwchar() |
| 720 |
self.assertIsInstance(curses.termname(), bytes) |
| 721 |
self.assertIsInstance(curses.longname(), bytes) |
| 722 |
self.assertIsInstance(curses.baudrate(), int) |
| 723 |
self.assertIsInstance(curses.has_ic(), bool) |
| 724 |
self.assertIsInstance(curses.has_il(), bool) |
| 725 |
self.assertIsInstance(curses.termattrs(), int) |
| 726 |
|
| 727 |
c = curses.killchar() |
| 728 |
self.assertIsInstance(c, bytes) |
| 729 |
self.assertEqual(len(c), 1) |
| 730 |
c = curses.erasechar() |
| 731 |
self.assertIsInstance(c, bytes) |
| 732 |
self.assertEqual(len(c), 1) |
| 733 |
|
| 734 |
def test_output_options(self): |
| 735 |
stdscr = self.stdscr |
| 736 |
|
| 737 |
stdscr.clearok(True) |
| 738 |
stdscr.clearok(False) |
| 739 |
|
| 740 |
stdscr.idcok(True) |
| 741 |
stdscr.idcok(False) |
| 742 |
|
| 743 |
stdscr.idlok(False) |
| 744 |
stdscr.idlok(True) |
| 745 |
|
| 746 |
if hasattr(stdscr, 'immedok'): |
| 747 |
stdscr.immedok(True) |
| 748 |
stdscr.immedok(False) |
| 749 |
|
| 750 |
stdscr.leaveok(True) |
| 751 |
stdscr.leaveok(False) |
| 752 |
|
| 753 |
stdscr.scrollok(True) |
| 754 |
stdscr.scrollok(False) |
| 755 |
|
| 756 |
stdscr.setscrreg(5, 10) |
| 757 |
|
| 758 |
curses.nonl() |
| 759 |
curses.nl(True) |
| 760 |
curses.nl(False) |
| 761 |
curses.nl() |
| 762 |
|
| 763 |
def test_input_options(self): |
| 764 |
stdscr = self.stdscr |
| 765 |
|
| 766 |
if self.isatty: |
| 767 |
curses.nocbreak() |
| 768 |
curses.cbreak() |
| 769 |
curses.cbreak(False) |
| 770 |
curses.cbreak(True) |
| 771 |
|
| 772 |
curses.intrflush(True) |
| 773 |
curses.intrflush(False) |
| 774 |
|
| 775 |
curses.raw() |
| 776 |
curses.raw(False) |
| 777 |
curses.raw(True) |
| 778 |
curses.noraw() |
| 779 |
|
| 780 |
curses.noecho() |
| 781 |
curses.echo() |
| 782 |
curses.echo(False) |
| 783 |
curses.echo(True) |
| 784 |
|
| 785 |
curses.halfdelay(255) |
| 786 |
curses.halfdelay(1) |
| 787 |
|
| 788 |
stdscr.keypad(True) |
| 789 |
stdscr.keypad(False) |
| 790 |
|
| 791 |
curses.meta(True) |
| 792 |
curses.meta(False) |
| 793 |
|
| 794 |
stdscr.nodelay(True) |
| 795 |
stdscr.nodelay(False) |
| 796 |
|
| 797 |
curses.noqiflush() |
| 798 |
curses.qiflush(True) |
| 799 |
curses.qiflush(False) |
| 800 |
curses.qiflush() |
| 801 |
|
| 802 |
stdscr.notimeout(True) |
| 803 |
stdscr.notimeout(False) |
| 804 |
|
| 805 |
stdscr.timeout(-1) |
| 806 |
stdscr.timeout(0) |
| 807 |
stdscr.timeout(5) |
| 808 |
|
| 809 |
@requires_curses_func('typeahead') |
| 810 |
def test_typeahead(self): |
| 811 |
curses.typeahead(sys.__stdin__.fileno()) |
| 812 |
curses.typeahead(-1) |
| 813 |
|
| 814 |
def test_prog_mode(self): |
| 815 |
if not self.isatty: |
| 816 |
self.skipTest('requires terminal') |
| 817 |
curses.def_prog_mode() |
| 818 |
curses.reset_prog_mode() |
| 819 |
|
| 820 |
def test_beep(self): |
| 821 |
if (curses.tigetstr("bel") is not None |
| 822 |
or curses.tigetstr("flash") is not None): |
| 823 |
curses.beep() |
| 824 |
else: |
| 825 |
try: |
| 826 |
curses.beep() |
| 827 |
except curses.error: |
| 828 |
self.skipTest('beep() failed') |
| 829 |
|
| 830 |
def test_flash(self): |
| 831 |
if (curses.tigetstr("bel") is not None |
| 832 |
or curses.tigetstr("flash") is not None): |
| 833 |
curses.flash() |
| 834 |
else: |
| 835 |
try: |
| 836 |
curses.flash() |
| 837 |
except curses.error: |
| 838 |
self.skipTest('flash() failed') |
| 839 |
|
| 840 |
def test_curs_set(self): |
| 841 |
for vis, cap in [(0, 'civis'), (2, 'cvvis'), (1, 'cnorm')]: |
| 842 |
if curses.tigetstr(cap) is not None: |
| 843 |
curses.curs_set(vis) |
| 844 |
else: |
| 845 |
try: |
| 846 |
curses.curs_set(vis) |
| 847 |
except curses.error: |
| 848 |
pass |
| 849 |
|
| 850 |
@requires_curses_func('get_escdelay') |
| 851 |
def test_escdelay(self): |
| 852 |
escdelay = curses.get_escdelay() |
| 853 |
self.assertIsInstance(escdelay, int) |
| 854 |
curses.set_escdelay(25) |
| 855 |
self.assertEqual(curses.get_escdelay(), 25) |
| 856 |
curses.set_escdelay(escdelay) |
| 857 |
|
| 858 |
@requires_curses_func('get_tabsize') |
| 859 |
def test_tabsize(self): |
| 860 |
tabsize = curses.get_tabsize() |
| 861 |
self.assertIsInstance(tabsize, int) |
| 862 |
curses.set_tabsize(4) |
| 863 |
self.assertEqual(curses.get_tabsize(), 4) |
| 864 |
curses.set_tabsize(tabsize) |
| 865 |
|
| 866 |
@requires_curses_func('getsyx') |
| 867 |
def test_getsyx(self): |
| 868 |
y, x = curses.getsyx() |
| 869 |
self.assertIsInstance(y, int) |
| 870 |
self.assertIsInstance(x, int) |
| 871 |
curses.setsyx(4, 5) |
| 872 |
self.assertEqual(curses.getsyx(), (4, 5)) |
| 873 |
|
| 874 |
def bad_colors(self): |
| 875 |
return (-1, curses.COLORS, -2**31 - 1, 2**31, -2**63 - 1, 2**63, 2**64) |
| 876 |
|
| 877 |
def bad_colors2(self): |
| 878 |
return (curses.COLORS, 2**31, 2**63, 2**64) |
| 879 |
|
| 880 |
def bad_pairs(self): |
| 881 |
return (-1, -2**31 - 1, 2**31, -2**63 - 1, 2**63, 2**64) |
| 882 |
|
| 883 |
def test_has_colors(self): |
| 884 |
self.assertIsInstance(curses.has_colors(), bool) |
| 885 |
self.assertIsInstance(curses.can_change_color(), bool) |
| 886 |
|
| 887 |
def test_start_color(self): |
| 888 |
if not curses.has_colors(): |
| 889 |
self.skipTest('requires colors support') |
| 890 |
curses.start_color() |
| 891 |
if verbose: |
| 892 |
print(f'COLORS = {curses.COLORS}', file=sys.stderr) |
| 893 |
print(f'COLOR_PAIRS = {curses.COLOR_PAIRS}', file=sys.stderr) |
| 894 |
|
| 895 |
@requires_colors |
| 896 |
def test_color_content(self): |
| 897 |
self.assertEqual(curses.color_content(curses.COLOR_BLACK), (0, 0, 0)) |
| 898 |
curses.color_content(0) |
| 899 |
maxcolor = curses.COLORS - 1 |
| 900 |
curses.color_content(maxcolor) |
| 901 |
|
| 902 |
for color in self.bad_colors(): |
| 903 |
self.assertRaises(ValueError, curses.color_content, color) |
| 904 |
|
| 905 |
@requires_colors |
| 906 |
def test_init_color(self): |
| 907 |
if not curses.can_change_color(): |
| 908 |
self.skipTest('cannot change color') |
| 909 |
|
| 910 |
old = curses.color_content(0) |
| 911 |
try: |
| 912 |
curses.init_color(0, *old) |
| 913 |
except curses.error: |
| 914 |
self.skipTest('cannot change color (init_color() failed)') |
| 915 |
self.addCleanup(curses.init_color, 0, *old) |
| 916 |
curses.init_color(0, 0, 0, 0) |
| 917 |
self.assertEqual(curses.color_content(0), (0, 0, 0)) |
| 918 |
curses.init_color(0, 1000, 1000, 1000) |
| 919 |
self.assertEqual(curses.color_content(0), (1000, 1000, 1000)) |
| 920 |
|
| 921 |
maxcolor = curses.COLORS - 1 |
| 922 |
old = curses.color_content(maxcolor) |
| 923 |
curses.init_color(maxcolor, *old) |
| 924 |
self.addCleanup(curses.init_color, maxcolor, *old) |
| 925 |
curses.init_color(maxcolor, 0, 500, 1000) |
| 926 |
self.assertEqual(curses.color_content(maxcolor), (0, 500, 1000)) |
| 927 |
|
| 928 |
for color in self.bad_colors(): |
| 929 |
self.assertRaises(ValueError, curses.init_color, color, 0, 0, 0) |
| 930 |
for comp in (-1, 1001): |
| 931 |
self.assertRaises(ValueError, curses.init_color, 0, comp, 0, 0) |
| 932 |
self.assertRaises(ValueError, curses.init_color, 0, 0, comp, 0) |
| 933 |
self.assertRaises(ValueError, curses.init_color, 0, 0, 0, comp) |
| 934 |
|
| 935 |
def get_pair_limit(self): |
| 936 |
pair_limit = curses.COLOR_PAIRS |
| 937 |
if hasattr(curses, 'ncurses_version'): |
| 938 |
if curses.has_extended_color_support(): |
| 939 |
pair_limit += 2*curses.COLORS + 1 |
| 940 |
if (not curses.has_extended_color_support() |
| 941 |
or (6, 1) <= curses.ncurses_version < (6, 2)): |
| 942 |
pair_limit = min(pair_limit, SHORT_MAX) |
| 943 |
# If use_default_colors() is called, the upper limit of the extended |
| 944 |
# range may be restricted, so we need to check if the limit is still |
| 945 |
# correct |
| 946 |
try: |
| 947 |
curses.init_pair(pair_limit - 1, 0, 0) |
| 948 |
except ValueError: |
| 949 |
pair_limit = curses.COLOR_PAIRS |
| 950 |
return pair_limit |
| 951 |
|
| 952 |
@requires_colors |
| 953 |
def test_pair_content(self): |
| 954 |
curses.pair_content(0) |
| 955 |
maxpair = self.get_pair_limit() - 1 |
| 956 |
if maxpair > 0: |
| 957 |
curses.pair_content(maxpair) |
| 958 |
|
| 959 |
for pair in self.bad_pairs(): |
| 960 |
self.assertRaises(ValueError, curses.pair_content, pair) |
| 961 |
|
| 962 |
@requires_colors |
| 963 |
def test_init_pair(self): |
| 964 |
old = curses.pair_content(1) |
| 965 |
curses.init_pair(1, *old) |
| 966 |
self.addCleanup(curses.init_pair, 1, *old) |
| 967 |
|
| 968 |
curses.init_pair(1, 0, 0) |
| 969 |
self.assertEqual(curses.pair_content(1), (0, 0)) |
| 970 |
maxcolor = curses.COLORS - 1 |
| 971 |
curses.init_pair(1, maxcolor, 0) |
| 972 |
self.assertEqual(curses.pair_content(1), (maxcolor, 0)) |
| 973 |
curses.init_pair(1, 0, maxcolor) |
| 974 |
self.assertEqual(curses.pair_content(1), (0, maxcolor)) |
| 975 |
maxpair = self.get_pair_limit() - 1 |
| 976 |
if maxpair > 1: |
| 977 |
curses.init_pair(maxpair, 0, 0) |
| 978 |
self.assertEqual(curses.pair_content(maxpair), (0, 0)) |
| 979 |
|
| 980 |
for pair in self.bad_pairs(): |
| 981 |
self.assertRaises(ValueError, curses.init_pair, pair, 0, 0) |
| 982 |
for color in self.bad_colors2(): |
| 983 |
self.assertRaises(ValueError, curses.init_pair, 1, color, 0) |
| 984 |
self.assertRaises(ValueError, curses.init_pair, 1, 0, color) |
| 985 |
|
| 986 |
@requires_colors |
| 987 |
def test_color_attrs(self): |
| 988 |
for pair in 0, 1, 255: |
| 989 |
attr = curses.color_pair(pair) |
| 990 |
self.assertEqual(curses.pair_number(attr), pair, attr) |
| 991 |
self.assertEqual(curses.pair_number(attr | curses.A_BOLD), pair) |
| 992 |
self.assertEqual(curses.color_pair(0), 0) |
| 993 |
self.assertEqual(curses.pair_number(0), 0) |
| 994 |
|
| 995 |
@requires_curses_func('use_default_colors') |
| 996 |
@requires_colors |
| 997 |
def test_use_default_colors(self): |
| 998 |
try: |
| 999 |
curses.use_default_colors() |
| 1000 |
except curses.error: |
| 1001 |
self.skipTest('cannot change color (use_default_colors() failed)') |
| 1002 |
self.assertEqual(curses.pair_content(0), (-1, -1)) |
| 1003 |
|
| 1004 |
@requires_curses_func('assume_default_colors') |
| 1005 |
@requires_colors |
| 1006 |
def test_assume_default_colors(self): |
| 1007 |
try: |
| 1008 |
curses.assume_default_colors(-1, -1) |
| 1009 |
except curses.error: |
| 1010 |
self.skipTest('cannot change color (assume_default_colors() failed)') |
| 1011 |
self.assertEqual(curses.pair_content(0), (-1, -1)) |
| 1012 |
curses.assume_default_colors(curses.COLOR_YELLOW, curses.COLOR_BLUE) |
| 1013 |
self.assertEqual(curses.pair_content(0), (curses.COLOR_YELLOW, curses.COLOR_BLUE)) |
| 1014 |
curses.assume_default_colors(curses.COLOR_RED, -1) |
| 1015 |
self.assertEqual(curses.pair_content(0), (curses.COLOR_RED, -1)) |
| 1016 |
curses.assume_default_colors(-1, curses.COLOR_GREEN) |
| 1017 |
self.assertEqual(curses.pair_content(0), (-1, curses.COLOR_GREEN)) |
| 1018 |
curses.assume_default_colors(-1, -1) |
| 1019 |
|
| 1020 |
def test_keyname(self): |
| 1021 |
# TODO: key_name() |
| 1022 |
self.assertEqual(curses.keyname(65), b'A') |
| 1023 |
self.assertEqual(curses.keyname(13), b'^M') |
| 1024 |
self.assertEqual(curses.keyname(127), b'^?') |
| 1025 |
self.assertEqual(curses.keyname(0), b'^@') |
| 1026 |
self.assertRaises(ValueError, curses.keyname, -1) |
| 1027 |
self.assertIsInstance(curses.keyname(256), bytes) |
| 1028 |
|
| 1029 |
@requires_curses_func('has_key') |
| 1030 |
def test_has_key(self): |
| 1031 |
curses.has_key(13) |
| 1032 |
|
| 1033 |
@requires_curses_func('getmouse') |
| 1034 |
def test_getmouse(self): |
| 1035 |
(availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED) |
| 1036 |
if availmask == 0: |
| 1037 |
self.skipTest('mouse stuff not available') |
| 1038 |
curses.mouseinterval(10) |
| 1039 |
# just verify these don't cause errors |
| 1040 |
curses.ungetmouse(0, 0, 0, 0, curses.BUTTON1_PRESSED) |
| 1041 |
m = curses.getmouse() |
| 1042 |
|
| 1043 |
@requires_curses_func('panel') |
| 1044 |
def test_userptr_without_set(self): |
| 1045 |
w = curses.newwin(10, 10) |
| 1046 |
p = curses.panel.new_panel(w) |
| 1047 |
# try to access userptr() before calling set_userptr() -- segfaults |
| 1048 |
with self.assertRaises(curses.panel.error, |
| 1049 |
msg='userptr should fail since not set'): |
| 1050 |
p.userptr() |
| 1051 |
|
| 1052 |
@requires_curses_func('panel') |
| 1053 |
def test_userptr_memory_leak(self): |
| 1054 |
w = curses.newwin(10, 10) |
| 1055 |
p = curses.panel.new_panel(w) |
| 1056 |
obj = object() |
| 1057 |
nrefs = sys.getrefcount(obj) |
| 1058 |
for i in range(100): |
| 1059 |
p.set_userptr(obj) |
| 1060 |
|
| 1061 |
p.set_userptr(None) |
| 1062 |
self.assertEqual(sys.getrefcount(obj), nrefs, |
| 1063 |
"set_userptr leaked references") |
| 1064 |
|
| 1065 |
@requires_curses_func('panel') |
| 1066 |
def test_userptr_segfault(self): |
| 1067 |
w = curses.newwin(10, 10) |
| 1068 |
panel = curses.panel.new_panel(w) |
| 1069 |
class A: |
| 1070 |
def __del__(self): |
| 1071 |
panel.set_userptr(None) |
| 1072 |
panel.set_userptr(A()) |
| 1073 |
panel.set_userptr(None) |
| 1074 |
|
| 1075 |
@cpython_only |
| 1076 |
@requires_curses_func('panel') |
| 1077 |
def test_disallow_instantiation(self): |
| 1078 |
# Ensure that the type disallows instantiation (bpo-43916) |
| 1079 |
w = curses.newwin(10, 10) |
| 1080 |
panel = curses.panel.new_panel(w) |
| 1081 |
check_disallow_instantiation(self, type(panel)) |
| 1082 |
|
| 1083 |
@requires_curses_func('is_term_resized') |
| 1084 |
def test_is_term_resized(self): |
| 1085 |
lines, cols = curses.LINES, curses.COLS |
| 1086 |
self.assertIs(curses.is_term_resized(lines, cols), False) |
| 1087 |
self.assertIs(curses.is_term_resized(lines-1, cols-1), True) |
| 1088 |
|
| 1089 |
@requires_curses_func('resize_term') |
| 1090 |
def test_resize_term(self): |
| 1091 |
curses.update_lines_cols() |
| 1092 |
lines, cols = curses.LINES, curses.COLS |
| 1093 |
new_lines = lines - 1 |
| 1094 |
new_cols = cols + 1 |
| 1095 |
curses.resize_term(new_lines, new_cols) |
| 1096 |
self.assertEqual(curses.LINES, new_lines) |
| 1097 |
self.assertEqual(curses.COLS, new_cols) |
| 1098 |
|
| 1099 |
curses.resize_term(lines, cols) |
| 1100 |
self.assertEqual(curses.LINES, lines) |
| 1101 |
self.assertEqual(curses.COLS, cols) |
| 1102 |
|
| 1103 |
with self.assertRaises(OverflowError): |
| 1104 |
curses.resize_term(35000, 1) |
| 1105 |
with self.assertRaises(OverflowError): |
| 1106 |
curses.resize_term(1, 35000) |
| 1107 |
# GH-120378: Overflow failure in resize_term() causes refresh to fail |
| 1108 |
tmp = curses.initscr() |
| 1109 |
tmp.erase() |
| 1110 |
|
| 1111 |
@requires_curses_func('resizeterm') |
| 1112 |
def test_resizeterm(self): |
| 1113 |
curses.update_lines_cols() |
| 1114 |
lines, cols = curses.LINES, curses.COLS |
| 1115 |
new_lines = lines - 1 |
| 1116 |
new_cols = cols + 1 |
| 1117 |
curses.resizeterm(new_lines, new_cols) |
| 1118 |
self.assertEqual(curses.LINES, new_lines) |
| 1119 |
self.assertEqual(curses.COLS, new_cols) |
| 1120 |
|
| 1121 |
curses.resizeterm(lines, cols) |
| 1122 |
self.assertEqual(curses.LINES, lines) |
| 1123 |
self.assertEqual(curses.COLS, cols) |
| 1124 |
|
| 1125 |
with self.assertRaises(OverflowError): |
| 1126 |
curses.resizeterm(35000, 1) |
| 1127 |
with self.assertRaises(OverflowError): |
| 1128 |
curses.resizeterm(1, 35000) |
| 1129 |
# GH-120378: Overflow failure in resizeterm() causes refresh to fail |
| 1130 |
tmp = curses.initscr() |
| 1131 |
tmp.erase() |
| 1132 |
|
| 1133 |
def test_ungetch(self): |
| 1134 |
curses.ungetch(b'A') |
| 1135 |
self.assertEqual(self.stdscr.getkey(), 'A') |
| 1136 |
curses.ungetch('B') |
| 1137 |
self.assertEqual(self.stdscr.getkey(), 'B') |
| 1138 |
curses.ungetch(67) |
| 1139 |
self.assertEqual(self.stdscr.getkey(), 'C') |
| 1140 |
|
| 1141 |
def test_issue6243(self): |
| 1142 |
curses.ungetch(1025) |
| 1143 |
self.stdscr.getkey() |
| 1144 |
|
| 1145 |
@requires_curses_func('unget_wch') |
| 1146 |
@unittest.skipIf(getattr(curses, 'ncurses_version', (99,)) < (5, 8), |
| 1147 |
"unget_wch is broken in ncurses 5.7 and earlier") |
| 1148 |
def test_unget_wch(self): |
| 1149 |
stdscr = self.stdscr |
| 1150 |
encoding = stdscr.encoding |
| 1151 |
for ch in ('a', '\xe9', '\u20ac', '\U0010FFFF'): |
| 1152 |
try: |
| 1153 |
ch.encode(encoding) |
| 1154 |
except UnicodeEncodeError: |
| 1155 |
continue |
| 1156 |
try: |
| 1157 |
curses.unget_wch(ch) |
| 1158 |
except Exception as err: |
| 1159 |
self.fail("unget_wch(%a) failed with encoding %s: %s" |
| 1160 |
% (ch, stdscr.encoding, err)) |
| 1161 |
read = stdscr.get_wch() |
| 1162 |
self.assertEqual(read, ch) |
| 1163 |
|
| 1164 |
code = ord(ch) |
| 1165 |
curses.unget_wch(code) |
| 1166 |
read = stdscr.get_wch() |
| 1167 |
self.assertEqual(read, ch) |
| 1168 |
|
| 1169 |
def test_encoding(self): |
| 1170 |
stdscr = self.stdscr |
| 1171 |
import codecs |
| 1172 |
encoding = stdscr.encoding |
| 1173 |
codecs.lookup(encoding) |
| 1174 |
with self.assertRaises(TypeError): |
| 1175 |
stdscr.encoding = 10 |
| 1176 |
stdscr.encoding = encoding |
| 1177 |
with self.assertRaises(TypeError): |
| 1178 |
del stdscr.encoding |
| 1179 |
|
| 1180 |
@unittest.skipIf(MISSING_C_DOCSTRINGS, |
| 1181 |
"Signature information for builtins requires docstrings") |
| 1182 |
def test_issue21088(self): |
| 1183 |
stdscr = self.stdscr |
| 1184 |
# |
| 1185 |
# http://bugs.python.org/issue21088 |
| 1186 |
# |
| 1187 |
# the bug: |
| 1188 |
# when converting curses.window.addch to Argument Clinic |
| 1189 |
# the first two parameters were switched. |
| 1190 |
|
| 1191 |
# if someday we can represent the signature of addch |
| 1192 |
# we will need to rewrite this test. |
| 1193 |
try: |
| 1194 |
signature = inspect.signature(stdscr.addch) |
| 1195 |
self.assertFalse(signature) |
| 1196 |
except ValueError: |
| 1197 |
# not generating a signature is fine. |
| 1198 |
pass |
| 1199 |
|
| 1200 |
# So. No signature for addch. |
| 1201 |
# But Argument Clinic gave us a human-readable equivalent |
| 1202 |
# as the first line of the docstring. So we parse that, |
| 1203 |
# and ensure that the parameters appear in the correct order. |
| 1204 |
# Since this is parsing output from Argument Clinic, we can |
| 1205 |
# be reasonably certain the generated parsing code will be |
| 1206 |
# correct too. |
| 1207 |
human_readable_signature = stdscr.addch.__doc__.split("\n")[0] |
| 1208 |
self.assertIn("[y, x,]", human_readable_signature) |
| 1209 |
|
| 1210 |
@requires_curses_window_meth('resize') |
| 1211 |
def test_issue13051(self): |
| 1212 |
win = curses.newwin(5, 15, 2, 5) |
| 1213 |
box = curses.textpad.Textbox(win, insert_mode=True) |
| 1214 |
lines, cols = win.getmaxyx() |
| 1215 |
win.resize(lines-2, cols-2) |
| 1216 |
# this may cause infinite recursion, leading to a RuntimeError |
| 1217 |
box._insert_printable_char('a') |
| 1218 |
|
| 1219 |
|
| 1220 |
class MiscTests(unittest.TestCase): |
| 1221 |
|
| 1222 |
@requires_curses_func('update_lines_cols') |
| 1223 |
def test_update_lines_cols(self): |
| 1224 |
curses.update_lines_cols() |
| 1225 |
lines, cols = curses.LINES, curses.COLS |
| 1226 |
curses.LINES = curses.COLS = 0 |
| 1227 |
curses.update_lines_cols() |
| 1228 |
self.assertEqual(curses.LINES, lines) |
| 1229 |
self.assertEqual(curses.COLS, cols) |
| 1230 |
|
| 1231 |
@requires_curses_func('ncurses_version') |
| 1232 |
def test_ncurses_version(self): |
| 1233 |
v = curses.ncurses_version |
| 1234 |
if verbose: |
| 1235 |
print(f'ncurses_version = {curses.ncurses_version}', flush=True) |
| 1236 |
self.assertIsInstance(v[:], tuple) |
| 1237 |
self.assertEqual(len(v), 3) |
| 1238 |
self.assertIsInstance(v[0], int) |
| 1239 |
self.assertIsInstance(v[1], int) |
| 1240 |
self.assertIsInstance(v[2], int) |
| 1241 |
self.assertIsInstance(v.major, int) |
| 1242 |
self.assertIsInstance(v.minor, int) |
| 1243 |
self.assertIsInstance(v.patch, int) |
| 1244 |
self.assertEqual(v[0], v.major) |
| 1245 |
self.assertEqual(v[1], v.minor) |
| 1246 |
self.assertEqual(v[2], v.patch) |
| 1247 |
self.assertGreaterEqual(v.major, 0) |
| 1248 |
self.assertGreaterEqual(v.minor, 0) |
| 1249 |
self.assertGreaterEqual(v.patch, 0) |
| 1250 |
|
| 1251 |
def test_has_extended_color_support(self): |
| 1252 |
r = curses.has_extended_color_support() |
| 1253 |
self.assertIsInstance(r, bool) |
| 1254 |
|
| 1255 |
|
| 1256 |
class TestAscii(unittest.TestCase): |
| 1257 |
|
| 1258 |
def test_controlnames(self): |
| 1259 |
for name in curses.ascii.controlnames: |
| 1260 |
self.assertHasAttr(curses.ascii, name) |
| 1261 |
|
| 1262 |
def test_ctypes(self): |
| 1263 |
def check(func, expected): |
| 1264 |
with self.subTest(ch=c, func=func): |
| 1265 |
self.assertEqual(func(i), expected) |
| 1266 |
self.assertEqual(func(c), expected) |
| 1267 |
|
| 1268 |
for i in range(256): |
| 1269 |
c = chr(i) |
| 1270 |
b = bytes([i]) |
| 1271 |
check(curses.ascii.isalnum, b.isalnum()) |
| 1272 |
check(curses.ascii.isalpha, b.isalpha()) |
| 1273 |
check(curses.ascii.isdigit, b.isdigit()) |
| 1274 |
check(curses.ascii.islower, b.islower()) |
| 1275 |
check(curses.ascii.isspace, b.isspace()) |
| 1276 |
check(curses.ascii.isupper, b.isupper()) |
| 1277 |
|
| 1278 |
check(curses.ascii.isascii, i < 128) |
| 1279 |
check(curses.ascii.ismeta, i >= 128) |
| 1280 |
check(curses.ascii.isctrl, i < 32) |
| 1281 |
check(curses.ascii.iscntrl, i < 32 or i == 127) |
| 1282 |
check(curses.ascii.isblank, c in ' \t') |
| 1283 |
check(curses.ascii.isgraph, 32 < i <= 126) |
| 1284 |
check(curses.ascii.isprint, 32 <= i <= 126) |
| 1285 |
check(curses.ascii.ispunct, c in string.punctuation) |
| 1286 |
check(curses.ascii.isxdigit, c in string.hexdigits) |
| 1287 |
|
| 1288 |
for i in (-2, -1, 256, sys.maxunicode, sys.maxunicode+1): |
| 1289 |
self.assertFalse(curses.ascii.isalnum(i)) |
| 1290 |
self.assertFalse(curses.ascii.isalpha(i)) |
| 1291 |
self.assertFalse(curses.ascii.isdigit(i)) |
| 1292 |
self.assertFalse(curses.ascii.islower(i)) |
| 1293 |
self.assertFalse(curses.ascii.isspace(i)) |
| 1294 |
self.assertFalse(curses.ascii.isupper(i)) |
| 1295 |
|
| 1296 |
self.assertFalse(curses.ascii.isascii(i)) |
| 1297 |
self.assertFalse(curses.ascii.isctrl(i)) |
| 1298 |
self.assertFalse(curses.ascii.iscntrl(i)) |
| 1299 |
self.assertFalse(curses.ascii.isblank(i)) |
| 1300 |
self.assertFalse(curses.ascii.isgraph(i)) |
| 1301 |
self.assertFalse(curses.ascii.isprint(i)) |
| 1302 |
self.assertFalse(curses.ascii.ispunct(i)) |
| 1303 |
self.assertFalse(curses.ascii.isxdigit(i)) |
| 1304 |
|
| 1305 |
self.assertFalse(curses.ascii.ismeta(-1)) |
| 1306 |
|
| 1307 |
def test_ascii(self): |
| 1308 |
ascii = curses.ascii.ascii |
| 1309 |
self.assertEqual(ascii('\xc1'), 'A') |
| 1310 |
self.assertEqual(ascii('A'), 'A') |
| 1311 |
self.assertEqual(ascii(ord('\xc1')), ord('A')) |
| 1312 |
|
| 1313 |
def test_ctrl(self): |
| 1314 |
ctrl = curses.ascii.ctrl |
| 1315 |
self.assertEqual(ctrl('J'), '\n') |
| 1316 |
self.assertEqual(ctrl('\n'), '\n') |
| 1317 |
self.assertEqual(ctrl('@'), '\0') |
| 1318 |
self.assertEqual(ctrl(ord('J')), ord('\n')) |
| 1319 |
|
| 1320 |
def test_alt(self): |
| 1321 |
alt = curses.ascii.alt |
| 1322 |
self.assertEqual(alt('\n'), '\x8a') |
| 1323 |
self.assertEqual(alt('A'), '\xc1') |
| 1324 |
self.assertEqual(alt(ord('A')), 0xc1) |
| 1325 |
|
| 1326 |
def test_unctrl(self): |
| 1327 |
unctrl = curses.ascii.unctrl |
| 1328 |
self.assertEqual(unctrl('a'), 'a') |
| 1329 |
self.assertEqual(unctrl('A'), 'A') |
| 1330 |
self.assertEqual(unctrl(';'), ';') |
| 1331 |
self.assertEqual(unctrl(' '), ' ') |
| 1332 |
self.assertEqual(unctrl('\x7f'), '^?') |
| 1333 |
self.assertEqual(unctrl('\n'), '^J') |
| 1334 |
self.assertEqual(unctrl('\0'), '^@') |
| 1335 |
self.assertEqual(unctrl(ord('A')), 'A') |
| 1336 |
self.assertEqual(unctrl(ord('\n')), '^J') |
| 1337 |
# Meta-bit characters |
| 1338 |
self.assertEqual(unctrl('\x8a'), '!^J') |
| 1339 |
self.assertEqual(unctrl('\xc1'), '!A') |
| 1340 |
self.assertEqual(unctrl(ord('\x8a')), '!^J') |
| 1341 |
self.assertEqual(unctrl(ord('\xc1')), '!A') |
| 1342 |
|
| 1343 |
|
| 1344 |
def lorem_ipsum(win): |
| 1345 |
text = [ |
| 1346 |
'Lorem ipsum', |
| 1347 |
'dolor sit amet,', |
| 1348 |
'consectetur', |
| 1349 |
'adipiscing elit,', |
| 1350 |
'sed do eiusmod', |
| 1351 |
'tempor incididunt', |
| 1352 |
'ut labore et', |
| 1353 |
'dolore magna', |
| 1354 |
'aliqua.', |
| 1355 |
] |
| 1356 |
maxy, maxx = win.getmaxyx() |
| 1357 |
for y, line in enumerate(text[:maxy]): |
| 1358 |
win.addstr(y, 0, line[:maxx - (y == maxy - 1)]) |
| 1359 |
|
| 1360 |
|
| 1361 |
class TextboxTest(unittest.TestCase): |
| 1362 |
def setUp(self): |
| 1363 |
self.mock_win = MagicMock(spec=curses.window) |
| 1364 |
self.mock_win.getyx.return_value = (1, 1) |
| 1365 |
self.mock_win.getmaxyx.return_value = (10, 20) |
| 1366 |
self.textbox = curses.textpad.Textbox(self.mock_win) |
| 1367 |
|
| 1368 |
def test_init(self): |
| 1369 |
"""Test textbox initialization.""" |
| 1370 |
self.mock_win.reset_mock() |
| 1371 |
tb = curses.textpad.Textbox(self.mock_win) |
| 1372 |
self.mock_win.getmaxyx.assert_called_once_with() |
| 1373 |
self.mock_win.keypad.assert_called_once_with(1) |
| 1374 |
self.assertEqual(tb.insert_mode, False) |
| 1375 |
self.assertEqual(tb.stripspaces, 1) |
| 1376 |
self.assertIsNone(tb.lastcmd) |
| 1377 |
self.mock_win.reset_mock() |
| 1378 |
|
| 1379 |
def test_insert(self): |
| 1380 |
"""Test inserting a printable character.""" |
| 1381 |
self.mock_win.reset_mock() |
| 1382 |
self.textbox.do_command(ord('a')) |
| 1383 |
self.mock_win.addch.assert_called_with(ord('a')) |
| 1384 |
self.textbox.do_command(ord('b')) |
| 1385 |
self.mock_win.addch.assert_called_with(ord('b')) |
| 1386 |
self.textbox.do_command(ord('c')) |
| 1387 |
self.mock_win.addch.assert_called_with(ord('c')) |
| 1388 |
self.mock_win.reset_mock() |
| 1389 |
|
| 1390 |
def test_delete(self): |
| 1391 |
"""Test deleting a character.""" |
| 1392 |
self.mock_win.reset_mock() |
| 1393 |
self.textbox.do_command(curses.ascii.BS) |
| 1394 |
self.textbox.do_command(curses.KEY_BACKSPACE) |
| 1395 |
self.textbox.do_command(curses.ascii.DEL) |
| 1396 |
assert self.mock_win.delch.call_count == 3 |
| 1397 |
self.mock_win.reset_mock() |
| 1398 |
|
| 1399 |
def test_move_left(self): |
| 1400 |
"""Test moving the cursor left.""" |
| 1401 |
self.mock_win.reset_mock() |
| 1402 |
self.textbox.do_command(curses.KEY_LEFT) |
| 1403 |
self.mock_win.move.assert_called_with(1, 0) |
| 1404 |
self.mock_win.reset_mock() |
| 1405 |
|
| 1406 |
def test_move_right(self): |
| 1407 |
"""Test moving the cursor right.""" |
| 1408 |
self.mock_win.reset_mock() |
| 1409 |
self.textbox.do_command(curses.KEY_RIGHT) |
| 1410 |
self.mock_win.move.assert_called_with(1, 2) |
| 1411 |
self.mock_win.reset_mock() |
| 1412 |
|
| 1413 |
def test_move_left_and_right(self): |
| 1414 |
"""Test moving the cursor left and then right.""" |
| 1415 |
self.mock_win.reset_mock() |
| 1416 |
self.textbox.do_command(curses.KEY_LEFT) |
| 1417 |
self.mock_win.move.assert_called_with(1, 0) |
| 1418 |
self.textbox.do_command(curses.KEY_RIGHT) |
| 1419 |
self.mock_win.move.assert_called_with(1, 2) |
| 1420 |
self.mock_win.reset_mock() |
| 1421 |
|
| 1422 |
def test_move_up(self): |
| 1423 |
"""Test moving the cursor up.""" |
| 1424 |
self.mock_win.reset_mock() |
| 1425 |
self.textbox.do_command(curses.KEY_UP) |
| 1426 |
self.mock_win.move.assert_called_with(0, 1) |
| 1427 |
self.mock_win.reset_mock() |
| 1428 |
|
| 1429 |
def test_move_down(self): |
| 1430 |
"""Test moving the cursor down.""" |
| 1431 |
self.mock_win.reset_mock() |
| 1432 |
self.textbox.do_command(curses.KEY_DOWN) |
| 1433 |
self.mock_win.move.assert_called_with(2, 1) |
| 1434 |
self.mock_win.reset_mock() |
| 1435 |
|
| 1436 |
|
| 1437 |
if __name__ == '__main__': |
| 1438 |
unittest.main() |