Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 1 | # |
| 2 | # Test script for the curses module |
| 3 | # |
| 4 | # This script doesn't actually display anything very coherent. but it |
| 5 | # does call every method and function. |
| 6 | # |
| 7 | # Functions not tested: {def,reset}_{shell,prog}_mode, getch(), getstr(), |
Neal Norwitz | 88bbd73 | 2006-01-10 07:05:44 +0000 | [diff] [blame] | 8 | # init_color() |
| 9 | # Only called, not tested: getmouse(), ungetmouse() |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 10 | # |
| 11 | |
Neal Norwitz | eeab7da | 2006-01-05 06:09:13 +0000 | [diff] [blame] | 12 | import curses, sys, tempfile, os |
Neal Norwitz | 5e3d862 | 2006-01-09 06:24:35 +0000 | [diff] [blame] | 13 | import curses.panel |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 19 | from test.support import requires, TestSkipped |
Neal Norwitz | 9f39f68 | 2006-01-06 04:18:21 +0000 | [diff] [blame] | 20 | requires('curses') |
| 21 | |
| 22 | # XXX: if newterm was supported we could use it instead of initscr and not exit |
| 23 | term = os.environ.get('TERM') |
| 24 | if not term or term == 'unknown': |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 25 | raise TestSkipped("$TERM=%r, calling initscr() may cause exit" % term) |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 26 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 27 | if sys.platform == "cygwin": |
| 28 | raise TestSkipped("cygwin's curses mostly just hangs") |
| 29 | |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 30 | def window_funcs(stdscr): |
| 31 | "Test the methods of windows" |
| 32 | win = curses.newwin(10,10) |
| 33 | win = curses.newwin(5,5, 5,5) |
| 34 | win2 = curses.newwin(15,15, 5,5) |
| 35 | |
| 36 | for meth in [stdscr.addch, stdscr.addstr]: |
| 37 | for args in [('a'), ('a', curses.A_BOLD), |
| 38 | (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]: |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 39 | meth(*args) |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 40 | |
| 41 | for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot, |
| 42 | stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch, |
| 43 | stdscr.deleteln, stdscr.erase, stdscr.getbegyx, |
| 44 | stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx, |
| 45 | stdscr.getparyx, stdscr.getyx, stdscr.inch, |
| 46 | stdscr.insertln, stdscr.instr, stdscr.is_wintouched, |
| 47 | win.noutrefresh, stdscr.redrawwin, stdscr.refresh, |
| 48 | stdscr.standout, stdscr.standend, stdscr.syncdown, |
| 49 | stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]: |
| 50 | meth() |
| 51 | |
| 52 | stdscr.addnstr('1234', 3) |
| 53 | stdscr.addnstr('1234', 3, curses.A_BOLD) |
| 54 | stdscr.addnstr(4,4, '1234', 3) |
| 55 | stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD) |
| 56 | |
| 57 | stdscr.attron(curses.A_BOLD) |
| 58 | stdscr.attroff(curses.A_BOLD) |
| 59 | stdscr.attrset(curses.A_BOLD) |
| 60 | stdscr.bkgd(' ') |
| 61 | stdscr.bkgd(' ', curses.A_REVERSE) |
| 62 | stdscr.bkgdset(' ') |
| 63 | stdscr.bkgdset(' ', curses.A_REVERSE) |
| 64 | |
| 65 | win.border(65, 66, 67, 68, |
| 66 | 69, 70, 71, 72) |
| 67 | win.border('|', '!', '-', '_', |
| 68 | '+', '\\', '#', '/') |
| 69 | try: |
| 70 | win.border(65, 66, 67, 68, |
| 71 | 69, [], 71, 72) |
| 72 | except TypeError: |
| 73 | pass |
| 74 | else: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 75 | raise RuntimeError("Expected win.border() to raise TypeError") |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 76 | |
| 77 | stdscr.clearok(1) |
| 78 | |
| 79 | win4 = stdscr.derwin(2,2) |
| 80 | win4 = stdscr.derwin(1,1, 5,5) |
| 81 | win4.mvderwin(9,9) |
| 82 | |
| 83 | stdscr.echochar('a') |
| 84 | stdscr.echochar('a', curses.A_BOLD) |
| 85 | stdscr.hline('-', 5) |
| 86 | stdscr.hline('-', 5, curses.A_BOLD) |
| 87 | stdscr.hline(1,1,'-', 5) |
| 88 | stdscr.hline(1,1,'-', 5, curses.A_BOLD) |
| 89 | |
| 90 | stdscr.idcok(1) |
| 91 | stdscr.idlok(1) |
| 92 | stdscr.immedok(1) |
| 93 | stdscr.insch('c') |
| 94 | stdscr.insdelln(1) |
| 95 | stdscr.insnstr('abc', 3) |
| 96 | stdscr.insnstr('abc', 3, curses.A_BOLD) |
| 97 | stdscr.insnstr(5, 5, 'abc', 3) |
| 98 | stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD) |
| 99 | |
| 100 | stdscr.insstr('def') |
| 101 | stdscr.insstr('def', curses.A_BOLD) |
| 102 | stdscr.insstr(5, 5, 'def') |
| 103 | stdscr.insstr(5, 5, 'def', curses.A_BOLD) |
| 104 | stdscr.is_linetouched(0) |
| 105 | stdscr.keypad(1) |
| 106 | stdscr.leaveok(1) |
| 107 | stdscr.move(3,3) |
| 108 | win.mvwin(2,2) |
| 109 | stdscr.nodelay(1) |
| 110 | stdscr.notimeout(1) |
| 111 | win2.overlay(win) |
| 112 | win2.overwrite(win) |
Neal Norwitz | 88bbd73 | 2006-01-10 07:05:44 +0000 | [diff] [blame] | 113 | win2.overlay(win, 1, 2, 3, 3, 2, 1) |
| 114 | win2.overwrite(win, 1, 2, 3, 3, 2, 1) |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 115 | stdscr.redrawln(1,2) |
| 116 | |
| 117 | stdscr.scrollok(1) |
| 118 | stdscr.scroll() |
| 119 | stdscr.scroll(2) |
| 120 | stdscr.scroll(-3) |
| 121 | |
Andrew M. Kuchling | d1badac | 2005-06-15 18:44:23 +0000 | [diff] [blame] | 122 | stdscr.move(12, 2) |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 123 | stdscr.setscrreg(10,15) |
| 124 | win3 = stdscr.subwin(10,10) |
| 125 | win3 = stdscr.subwin(10,10, 5,5) |
| 126 | stdscr.syncok(1) |
| 127 | stdscr.timeout(5) |
| 128 | stdscr.touchline(5,5) |
| 129 | stdscr.touchline(5,5,0) |
| 130 | stdscr.vline('a', 3) |
| 131 | stdscr.vline('a', 3, curses.A_STANDOUT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 132 | stdscr.chgat(5, 2, 3, curses.A_BLINK) |
| 133 | stdscr.chgat(3, curses.A_BOLD) |
| 134 | stdscr.chgat(5, 8, curses.A_UNDERLINE) |
| 135 | stdscr.chgat(curses.A_BLINK) |
| 136 | stdscr.refresh() |
| 137 | |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 138 | stdscr.vline(1,1, 'a', 3) |
| 139 | stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT) |
| 140 | |
| 141 | if hasattr(curses, 'resize'): |
| 142 | stdscr.resize() |
| 143 | if hasattr(curses, 'enclose'): |
| 144 | stdscr.enclose() |
| 145 | |
| 146 | |
| 147 | def module_funcs(stdscr): |
| 148 | "Test module-level functions" |
| 149 | |
| 150 | for func in [curses.baudrate, curses.beep, curses.can_change_color, |
| 151 | curses.cbreak, curses.def_prog_mode, curses.doupdate, |
| 152 | curses.filter, curses.flash, curses.flushinp, |
| 153 | curses.has_colors, curses.has_ic, curses.has_il, |
| 154 | curses.isendwin, curses.killchar, curses.longname, |
| 155 | curses.nocbreak, curses.noecho, curses.nonl, |
| 156 | curses.noqiflush, curses.noraw, |
| 157 | curses.reset_prog_mode, curses.termattrs, |
| 158 | curses.termname, curses.erasechar, curses.getsyx]: |
| 159 | func() |
| 160 | |
| 161 | # Functions that actually need arguments |
Michael W. Hudson | 2b3feec | 2004-08-07 15:27:16 +0000 | [diff] [blame] | 162 | if curses.tigetstr("cnorm"): |
| 163 | curses.curs_set(1) |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 164 | curses.delay_output(1) |
| 165 | curses.echo() ; curses.echo(1) |
| 166 | |
| 167 | f = tempfile.TemporaryFile() |
| 168 | stdscr.putwin(f) |
| 169 | f.seek(0) |
| 170 | curses.getwin(f) |
| 171 | f.close() |
| 172 | |
| 173 | curses.halfdelay(1) |
| 174 | curses.intrflush(1) |
| 175 | curses.meta(1) |
| 176 | curses.napms(100) |
| 177 | curses.newpad(50,50) |
| 178 | win = curses.newwin(5,5) |
| 179 | win = curses.newwin(5,5, 1,1) |
| 180 | curses.nl() ; curses.nl(1) |
| 181 | curses.putp('abc') |
| 182 | curses.qiflush() |
| 183 | curses.raw() ; curses.raw(1) |
| 184 | curses.setsyx(5,5) |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 185 | curses.tigetflag('hc') |
| 186 | curses.tigetnum('co') |
| 187 | curses.tigetstr('cr') |
| 188 | curses.tparm('cr') |
| 189 | curses.typeahead(sys.__stdin__.fileno()) |
| 190 | curses.unctrl('a') |
| 191 | curses.ungetch('a') |
| 192 | curses.use_env(1) |
| 193 | |
| 194 | # Functions only available on a few platforms |
| 195 | if curses.has_colors(): |
| 196 | curses.start_color() |
| 197 | curses.init_pair(2, 1,1) |
| 198 | curses.color_content(1) |
| 199 | curses.color_pair(2) |
Andrew M. Kuchling | d1badac | 2005-06-15 18:44:23 +0000 | [diff] [blame] | 200 | curses.pair_content(curses.COLOR_PAIRS - 1) |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 201 | curses.pair_number(0) |
| 202 | |
Michael W. Hudson | 2b3feec | 2004-08-07 15:27:16 +0000 | [diff] [blame] | 203 | if hasattr(curses, 'use_default_colors'): |
| 204 | curses.use_default_colors() |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 205 | |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 206 | if hasattr(curses, 'keyname'): |
| 207 | curses.keyname(13) |
| 208 | |
| 209 | if hasattr(curses, 'has_key'): |
| 210 | curses.has_key(13) |
| 211 | |
| 212 | if hasattr(curses, 'getmouse'): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 213 | (availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED) |
| 214 | # availmask indicates that mouse stuff not available. |
| 215 | if availmask != 0: |
| 216 | curses.mouseinterval(10) |
| 217 | # just verify these don't cause errors |
| 218 | m = curses.getmouse() |
| 219 | curses.ungetmouse(*m) |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 220 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 221 | if hasattr(curses, 'is_term_resized'): |
| 222 | curses.is_term_resized(*stdscr.getmaxyx()) |
| 223 | if hasattr(curses, 'resizeterm'): |
| 224 | curses.resizeterm(*stdscr.getmaxyx()) |
| 225 | if hasattr(curses, 'resize_term'): |
| 226 | curses.resize_term(*stdscr.getmaxyx()) |
| 227 | |
Andrew M. Kuchling | e752e20 | 2003-08-29 18:37:37 +0000 | [diff] [blame] | 228 | def unit_tests(): |
| 229 | from curses import ascii |
| 230 | for ch, expected in [('a', 'a'), ('A', 'A'), |
| 231 | (';', ';'), (' ', ' '), |
Andrew M. Kuchling | e8792c1 | 2003-08-29 18:49:05 +0000 | [diff] [blame] | 232 | ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'), |
| 233 | # Meta-bit characters |
| 234 | ('\x8a', '!^J'), ('\xc1', '!A'), |
| 235 | ]: |
Andrew M. Kuchling | e752e20 | 2003-08-29 18:37:37 +0000 | [diff] [blame] | 236 | if ascii.unctrl(ch) != expected: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 237 | print('curses.unctrl fails on character', repr(ch)) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 238 | |
Andrew M. Kuchling | e752e20 | 2003-08-29 18:37:37 +0000 | [diff] [blame] | 239 | |
Neal Norwitz | 5e3d862 | 2006-01-09 06:24:35 +0000 | [diff] [blame] | 240 | def test_userptr_without_set(stdscr): |
| 241 | w = curses.newwin(10, 10) |
| 242 | p = curses.panel.new_panel(w) |
| 243 | # try to access userptr() before calling set_userptr() -- segfaults |
| 244 | try: |
| 245 | p.userptr() |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 246 | raise RuntimeError('userptr should fail since not set') |
Neal Norwitz | 5e3d862 | 2006-01-09 06:24:35 +0000 | [diff] [blame] | 247 | except curses.panel.error: |
| 248 | pass |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 250 | def test_resize_term(stdscr): |
| 251 | if hasattr(curses, 'resizeterm'): |
| 252 | lines, cols = curses.LINES, curses.COLS |
| 253 | curses.resizeterm(lines - 1, cols + 1) |
| 254 | |
| 255 | if curses.LINES != lines - 1 or curses.COLS != cols + 1: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 256 | raise RuntimeError("Expected resizeterm to update LINES and COLS") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 257 | |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 258 | def main(stdscr): |
| 259 | curses.savetty() |
| 260 | try: |
| 261 | module_funcs(stdscr) |
| 262 | window_funcs(stdscr) |
Neal Norwitz | 5e3d862 | 2006-01-09 06:24:35 +0000 | [diff] [blame] | 263 | test_userptr_without_set(stdscr) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 264 | test_resize_term(stdscr) |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 265 | finally: |
| 266 | curses.resetty() |
| 267 | |
| 268 | if __name__ == '__main__': |
| 269 | curses.wrapper(main) |
Andrew M. Kuchling | e752e20 | 2003-08-29 18:37:37 +0000 | [diff] [blame] | 270 | unit_tests() |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 271 | else: |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 272 | # testing setupterm() inside initscr/endwin |
| 273 | # causes terminal breakage |
| 274 | curses.setupterm(fd=sys.__stdout__.fileno()) |
Andrew M. Kuchling | 2158df0 | 2001-10-22 15:26:09 +0000 | [diff] [blame] | 275 | try: |
| 276 | stdscr = curses.initscr() |
| 277 | main(stdscr) |
| 278 | finally: |
| 279 | curses.endwin() |
Andrew M. Kuchling | e752e20 | 2003-08-29 18:37:37 +0000 | [diff] [blame] | 280 | unit_tests() |