2 # Test script for the curses module
4 # This script doesn't actually display anything very coherent. but it
5 # does call every method and function.
7 # Functions not tested: {def,reset}_{shell,prog}_mode, getch(), getstr(),
9 # Only called, not tested: getmouse(), ungetmouse()
12 import curses, sys, tempfile, os
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.
19 from test.test_support import requires, TestSkipped
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':
25 raise TestSkipped, "$TERM=%r, calling initscr() may cause exit" % term
27 if sys.platform == "cygwin":
28 raise TestSkipped("cygwin's curses mostly just hangs")
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)
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)]:
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]:
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)
57 stdscr.attron(curses.A_BOLD)
58 stdscr.attroff(curses.A_BOLD)
59 stdscr.attrset(curses.A_BOLD)
61 stdscr.bkgd(' ', curses.A_REVERSE)
63 stdscr.bkgdset(' ', curses.A_REVERSE)
65 win.border(65, 66, 67, 68,
67 win.border('|', '!', '-', '_',
70 win.border(65, 66, 67, 68,
75 raise RuntimeError, "Expected win.border() to raise TypeError"
79 win4 = stdscr.derwin(2,2)
80 win4 = stdscr.derwin(1,1, 5,5)
84 stdscr.echochar('a', curses.A_BOLD)
86 stdscr.hline('-', 5, curses.A_BOLD)
87 stdscr.hline(1,1,'-', 5)
88 stdscr.hline(1,1,'-', 5, curses.A_BOLD)
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)
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)
113 win2.overlay(win, 1, 2, 3, 3, 2, 1)
114 win2.overwrite(win, 1, 2, 3, 3, 2, 1)
123 stdscr.setscrreg(10,15)
124 win3 = stdscr.subwin(10,10)
125 win3 = stdscr.subwin(10,10, 5,5)
128 stdscr.touchline(5,5)
129 stdscr.touchline(5,5,0)
131 stdscr.vline('a', 3, curses.A_STANDOUT)
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)
138 stdscr.vline(1,1, 'a', 3)
139 stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
141 if hasattr(curses, 'resize'):
143 if hasattr(curses, 'enclose'):
147 def module_funcs(stdscr):
148 "Test module-level functions"
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]:
161 # Functions that actually need arguments
162 if curses.tigetstr("cnorm"):
164 curses.delay_output(1)
165 curses.echo() ; curses.echo(1)
167 f = tempfile.TemporaryFile()
178 win = curses.newwin(5,5)
179 win = curses.newwin(5,5, 1,1)
180 curses.nl() ; curses.nl(1)
183 curses.raw() ; curses.raw(1)
185 curses.tigetflag('hc')
186 curses.tigetnum('co')
187 curses.tigetstr('cr')
189 curses.typeahead(sys.__stdin__.fileno())
194 # Functions only available on a few platforms
195 if curses.has_colors():
197 curses.init_pair(2, 1,1)
198 curses.color_content(1)
200 curses.pair_content(curses.COLOR_PAIRS - 1)
201 curses.pair_number(0)
203 if hasattr(curses, 'use_default_colors'):
204 curses.use_default_colors()
206 if hasattr(curses, 'keyname'):
209 if hasattr(curses, 'has_key'):
212 if hasattr(curses, 'getmouse'):
213 (availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED)
214 # availmask indicates that mouse stuff not available.
216 curses.mouseinterval(10)
217 # just verify these don't cause errors
218 m = curses.getmouse()
219 curses.ungetmouse(*m)
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())
229 from curses import ascii
230 for ch, expected in [('a', 'a'), ('A', 'A'),
231 (';', ';'), (' ', ' '),
232 ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
233 # Meta-bit characters
234 ('\x8a', '!^J'), ('\xc1', '!A'),
236 if ascii.unctrl(ch) != expected:
237 print 'curses.unctrl fails on character', repr(ch)
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
246 raise RuntimeError, 'userptr should fail since not set'
247 except curses.panel.error:
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)
255 if curses.LINES != lines - 1 or curses.COLS != cols + 1:
256 raise RuntimeError, "Expected resizeterm to update LINES and COLS"
263 test_userptr_without_set(stdscr)
264 test_resize_term(stdscr)
268 if __name__ == '__main__':
272 # testing setupterm() inside initscr/endwin
273 # causes terminal breakage
274 curses.setupterm(fd=sys.__stdout__.fileno())
276 stdscr = curses.initscr()