10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Test script for the curses module 30a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# This script doesn't actually display anything very coherent. but it 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# does call every method and function. 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Functions not tested: {def,reset}_{shell,prog}_mode, getch(), getstr(), 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# init_color() 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Only called, not tested: getmouse(), ungetmouse() 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys, tempfile, os 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Optionally test curses module. This currently requires that the 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 'curses' resource be given on the regrtest command line using the -u 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# option. If not available, nothing after this line will be executed. 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test.test_support import requires, import_module 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gaorequires('curses') 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gaocurses = import_module('curses') 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gaocurses.panel = import_module('curses.panel') 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# XXX: if newterm was supported we could use it instead of initscr and not exit 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoterm = os.environ.get('TERM') 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif not term or term == 'unknown': 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise unittest.SkipTest, "$TERM=%r, calling initscr() may cause exit" % term 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif sys.platform == "cygwin": 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise unittest.SkipTest("cygwin's curses mostly just hangs") 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef window_funcs(stdscr): 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "Test the methods of windows" 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win = curses.newwin(10,10) 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win = curses.newwin(5,5, 5,5) 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win2 = curses.newwin(15,15, 5,5) 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for meth in [stdscr.addch, stdscr.addstr]: 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for args in [('a'), ('a', curses.A_BOLD), 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]: 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao meth(*args) 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot, 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch, 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.deleteln, stdscr.erase, stdscr.getbegyx, 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx, 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.getparyx, stdscr.getyx, stdscr.inch, 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.insertln, stdscr.instr, stdscr.is_wintouched, 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win.noutrefresh, stdscr.redrawwin, stdscr.refresh, 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.standout, stdscr.standend, stdscr.syncdown, 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]: 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao meth() 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.addnstr('1234', 3) 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.addnstr('1234', 3, curses.A_BOLD) 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.addnstr(4,4, '1234', 3) 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD) 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.attron(curses.A_BOLD) 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.attroff(curses.A_BOLD) 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.attrset(curses.A_BOLD) 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.bkgd(' ') 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.bkgd(' ', curses.A_REVERSE) 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.bkgdset(' ') 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.bkgdset(' ', curses.A_REVERSE) 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win.border(65, 66, 67, 68, 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 69, 70, 71, 72) 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win.border('|', '!', '-', '_', 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '+', '\\', '#', '/') 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win.border(65, 66, 67, 68, 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 69, [], 71, 72) 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except TypeError: 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise RuntimeError, "Expected win.border() to raise TypeError" 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.clearok(1) 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win4 = stdscr.derwin(2,2) 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win4 = stdscr.derwin(1,1, 5,5) 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win4.mvderwin(9,9) 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.echochar('a') 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.echochar('a', curses.A_BOLD) 880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.hline('-', 5) 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.hline('-', 5, curses.A_BOLD) 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.hline(1,1,'-', 5) 910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.hline(1,1,'-', 5, curses.A_BOLD) 920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.idcok(1) 940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.idlok(1) 950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.immedok(1) 960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.insch('c') 970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.insdelln(1) 980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.insnstr('abc', 3) 990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.insnstr('abc', 3, curses.A_BOLD) 1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.insnstr(5, 5, 'abc', 3) 1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD) 1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.insstr('def') 1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.insstr('def', curses.A_BOLD) 1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.insstr(5, 5, 'def') 1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.insstr(5, 5, 'def', curses.A_BOLD) 1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.is_linetouched(0) 1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.keypad(1) 1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.leaveok(1) 1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.move(3,3) 1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win.mvwin(2,2) 1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.nodelay(1) 1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.notimeout(1) 1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win2.overlay(win) 1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win2.overwrite(win) 1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win2.overlay(win, 1, 2, 3, 3, 2, 1) 1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win2.overwrite(win, 1, 2, 3, 3, 2, 1) 1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.redrawln(1,2) 1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.scrollok(1) 1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.scroll() 1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.scroll(2) 1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.scroll(-3) 1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.move(12, 2) 1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.setscrreg(10,15) 1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win3 = stdscr.subwin(10,10) 1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win3 = stdscr.subwin(10,10, 5,5) 1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.syncok(1) 1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.timeout(5) 1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.touchline(5,5) 1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.touchline(5,5,0) 1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.vline('a', 3) 1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.vline('a', 3, curses.A_STANDOUT) 1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.chgat(5, 2, 3, curses.A_BLINK) 1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.chgat(3, curses.A_BOLD) 1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.chgat(5, 8, curses.A_UNDERLINE) 1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.chgat(curses.A_BLINK) 1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.refresh() 1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.vline(1,1, 'a', 3) 1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT) 1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(curses, 'resize'): 1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.resize() 1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(curses, 'enclose'): 1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.enclose() 1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef module_funcs(stdscr): 1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "Test module-level functions" 1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for func in [curses.baudrate, curses.beep, curses.can_change_color, 1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.cbreak, curses.def_prog_mode, curses.doupdate, 1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.filter, curses.flash, curses.flushinp, 1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.has_colors, curses.has_ic, curses.has_il, 1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.isendwin, curses.killchar, curses.longname, 1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.nocbreak, curses.noecho, curses.nonl, 1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.noqiflush, curses.noraw, 1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.reset_prog_mode, curses.termattrs, 1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.termname, curses.erasechar, curses.getsyx]: 1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao func() 1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Functions that actually need arguments 1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if curses.tigetstr("cnorm"): 1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.curs_set(1) 1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.delay_output(1) 1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.echo() ; curses.echo(1) 1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fx = tempfile.TemporaryFile() 1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # cf tempfile.py TemporaryFile vs NamedTemporaryFile 1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if os.name != 'posix' or os.sys.platform == 'cygwin': 1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f = fx.file 1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f = fx 1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.putwin(f) 1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f.seek(0) 1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.getwin(f) 1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fx.close() 1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.halfdelay(1) 1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.intrflush(1) 1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.meta(1) 1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.napms(100) 1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.newpad(50,50) 1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win = curses.newwin(5,5) 1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao win = curses.newwin(5,5, 1,1) 1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.nl() ; curses.nl(1) 1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.putp('abc') 1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.qiflush() 1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.raw() ; curses.raw(1) 1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.setsyx(5,5) 1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.tigetflag('hc') 1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.tigetnum('co') 1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.tigetstr('cr') 1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.tparm('cr') 1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.typeahead(sys.__stdin__.fileno()) 1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.unctrl('a') 1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.ungetch('a') 2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.use_env(1) 2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Functions only available on a few platforms 2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if curses.has_colors(): 2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.start_color() 2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.init_pair(2, 1,1) 2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.color_content(1) 2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.color_pair(2) 2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.pair_content(curses.COLOR_PAIRS - 1) 2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.pair_number(0) 2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(curses, 'use_default_colors'): 2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.use_default_colors() 2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(curses, 'keyname'): 2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.keyname(13) 2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(curses, 'has_key'): 2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.has_key(13) 2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(curses, 'getmouse'): 2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED) 2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # availmask indicates that mouse stuff not available. 2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if availmask != 0: 2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.mouseinterval(10) 2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # just verify these don't cause errors 2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.ungetmouse(0, 0, 0, 0, curses.BUTTON1_PRESSED) 2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m = curses.getmouse() 2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(curses, 'is_term_resized'): 2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.is_term_resized(*stdscr.getmaxyx()) 2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(curses, 'resizeterm'): 2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.resizeterm(*stdscr.getmaxyx()) 2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(curses, 'resize_term'): 2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.resize_term(*stdscr.getmaxyx()) 2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef unit_tests(): 2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao from curses import ascii 2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for ch, expected in [('a', 'a'), ('A', 'A'), 2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (';', ';'), (' ', ' '), 2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'), 2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Meta-bit characters 2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ('\x8a', '!^J'), ('\xc1', '!A'), 2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ]: 2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ascii.unctrl(ch) != expected: 2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print 'curses.unctrl fails on character', repr(ch) 2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_userptr_without_set(stdscr): 2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao w = curses.newwin(10, 10) 2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p = curses.panel.new_panel(w) 2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # try to access userptr() before calling set_userptr() -- segfaults 2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p.userptr() 2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise RuntimeError, 'userptr should fail since not set' 2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except curses.panel.error: 2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_resize_term(stdscr): 2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(curses, 'resizeterm'): 2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lines, cols = curses.LINES, curses.COLS 2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.resizeterm(lines - 1, cols + 1) 2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if curses.LINES != lines - 1 or curses.COLS != cols + 1: 2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise RuntimeError, "Expected resizeterm to update LINES and COLS" 2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_issue6243(stdscr): 2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.ungetch(1025) 2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr.getkey() 2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef main(stdscr): 2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.savetty() 2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao module_funcs(stdscr) 2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao window_funcs(stdscr) 2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_userptr_without_set(stdscr) 2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_resize_term(stdscr) 2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_issue6243(stdscr) 2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao finally: 2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.resetty() 2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == '__main__': 2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.wrapper(main) 2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao unit_tests() 2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoelse: 2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not sys.__stdout__.isatty(): 2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise unittest.SkipTest("sys.__stdout__ is not a tty") 2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # testing setupterm() inside initscr/endwin 2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # causes terminal breakage 2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.setupterm(fd=sys.__stdout__.fileno()) 2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao stdscr = curses.initscr() 2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao main(stdscr) 2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao finally: 2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao curses.endwin() 2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao unit_tests() 296