blob: 57d4e8b1612f800867a766d93d01670680995314 [file] [log] [blame]
Andrew M. Kuchling2158df02001-10-22 15:26:09 +00001#
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 Norwitz88bbd732006-01-10 07:05:44 +00008# init_color()
9# Only called, not tested: getmouse(), ungetmouse()
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000010#
11
R. David Murraya21e4ca2009-03-31 23:16:50 +000012import sys, tempfile, os
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000013
14# Optionally test curses module. This currently requires that the
15# 'curses' resource be given on the regrtest command line using the -u
16# option. If not available, nothing after this line will be executed.
17
Amaury Forgeot d'Arcf6b88722009-06-09 23:51:56 +000018import unittest
R. David Murraya21e4ca2009-03-31 23:16:50 +000019from test.support import requires, import_module
Neal Norwitz9f39f682006-01-06 04:18:21 +000020requires('curses')
21
R. David Murraya21e4ca2009-03-31 23:16:50 +000022# If either of these don't exist, skip the tests.
23curses = import_module('curses')
24curses.panel = import_module('curses.panel')
25
Mark Dickinson945e2422010-02-21 13:42:03 +000026# skip all these tests on FreeBSD: test_curses currently hangs the
27# FreeBSD buildbots, preventing other tests from running. See issue
28# #7384.
29if 'freebsd' in sys.platform:
30 raise unittest.SkipTest('The curses module is broken on FreeBSD. See http://bugs.python.org/issue7384.')
31
Neal Norwitz9f39f682006-01-06 04:18:21 +000032# XXX: if newterm was supported we could use it instead of initscr and not exit
33term = os.environ.get('TERM')
34if not term or term == 'unknown':
Benjamin Petersone549ead2009-03-28 21:42:05 +000035 raise unittest.SkipTest("$TERM=%r, calling initscr() may cause exit" % term)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000036
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037if sys.platform == "cygwin":
Benjamin Petersone549ead2009-03-28 21:42:05 +000038 raise unittest.SkipTest("cygwin's curses mostly just hangs")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000040def window_funcs(stdscr):
41 "Test the methods of windows"
42 win = curses.newwin(10,10)
43 win = curses.newwin(5,5, 5,5)
44 win2 = curses.newwin(15,15, 5,5)
45
46 for meth in [stdscr.addch, stdscr.addstr]:
47 for args in [('a'), ('a', curses.A_BOLD),
48 (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]:
Raymond Hettingerff41c482003-04-06 09:01:11 +000049 meth(*args)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000050
51 for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot,
52 stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch,
53 stdscr.deleteln, stdscr.erase, stdscr.getbegyx,
54 stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx,
55 stdscr.getparyx, stdscr.getyx, stdscr.inch,
56 stdscr.insertln, stdscr.instr, stdscr.is_wintouched,
57 win.noutrefresh, stdscr.redrawwin, stdscr.refresh,
58 stdscr.standout, stdscr.standend, stdscr.syncdown,
59 stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]:
60 meth()
61
62 stdscr.addnstr('1234', 3)
63 stdscr.addnstr('1234', 3, curses.A_BOLD)
64 stdscr.addnstr(4,4, '1234', 3)
65 stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD)
66
67 stdscr.attron(curses.A_BOLD)
68 stdscr.attroff(curses.A_BOLD)
69 stdscr.attrset(curses.A_BOLD)
70 stdscr.bkgd(' ')
71 stdscr.bkgd(' ', curses.A_REVERSE)
72 stdscr.bkgdset(' ')
73 stdscr.bkgdset(' ', curses.A_REVERSE)
74
75 win.border(65, 66, 67, 68,
76 69, 70, 71, 72)
77 win.border('|', '!', '-', '_',
78 '+', '\\', '#', '/')
79 try:
80 win.border(65, 66, 67, 68,
81 69, [], 71, 72)
82 except TypeError:
83 pass
84 else:
Collin Winter3add4d72007-08-29 23:37:32 +000085 raise RuntimeError("Expected win.border() to raise TypeError")
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000086
87 stdscr.clearok(1)
88
89 win4 = stdscr.derwin(2,2)
90 win4 = stdscr.derwin(1,1, 5,5)
91 win4.mvderwin(9,9)
92
93 stdscr.echochar('a')
94 stdscr.echochar('a', curses.A_BOLD)
95 stdscr.hline('-', 5)
96 stdscr.hline('-', 5, curses.A_BOLD)
97 stdscr.hline(1,1,'-', 5)
98 stdscr.hline(1,1,'-', 5, curses.A_BOLD)
99
100 stdscr.idcok(1)
101 stdscr.idlok(1)
102 stdscr.immedok(1)
103 stdscr.insch('c')
104 stdscr.insdelln(1)
105 stdscr.insnstr('abc', 3)
106 stdscr.insnstr('abc', 3, curses.A_BOLD)
107 stdscr.insnstr(5, 5, 'abc', 3)
108 stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD)
109
110 stdscr.insstr('def')
111 stdscr.insstr('def', curses.A_BOLD)
112 stdscr.insstr(5, 5, 'def')
113 stdscr.insstr(5, 5, 'def', curses.A_BOLD)
114 stdscr.is_linetouched(0)
115 stdscr.keypad(1)
116 stdscr.leaveok(1)
117 stdscr.move(3,3)
118 win.mvwin(2,2)
119 stdscr.nodelay(1)
120 stdscr.notimeout(1)
121 win2.overlay(win)
122 win2.overwrite(win)
Neal Norwitz88bbd732006-01-10 07:05:44 +0000123 win2.overlay(win, 1, 2, 3, 3, 2, 1)
124 win2.overwrite(win, 1, 2, 3, 3, 2, 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000125 stdscr.redrawln(1,2)
126
127 stdscr.scrollok(1)
128 stdscr.scroll()
129 stdscr.scroll(2)
130 stdscr.scroll(-3)
131
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000132 stdscr.move(12, 2)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000133 stdscr.setscrreg(10,15)
134 win3 = stdscr.subwin(10,10)
135 win3 = stdscr.subwin(10,10, 5,5)
136 stdscr.syncok(1)
137 stdscr.timeout(5)
138 stdscr.touchline(5,5)
139 stdscr.touchline(5,5,0)
140 stdscr.vline('a', 3)
141 stdscr.vline('a', 3, curses.A_STANDOUT)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000142 stdscr.chgat(5, 2, 3, curses.A_BLINK)
143 stdscr.chgat(3, curses.A_BOLD)
144 stdscr.chgat(5, 8, curses.A_UNDERLINE)
145 stdscr.chgat(curses.A_BLINK)
146 stdscr.refresh()
147
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000148 stdscr.vline(1,1, 'a', 3)
149 stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
150
151 if hasattr(curses, 'resize'):
152 stdscr.resize()
153 if hasattr(curses, 'enclose'):
154 stdscr.enclose()
155
156
157def module_funcs(stdscr):
158 "Test module-level functions"
159
160 for func in [curses.baudrate, curses.beep, curses.can_change_color,
161 curses.cbreak, curses.def_prog_mode, curses.doupdate,
162 curses.filter, curses.flash, curses.flushinp,
163 curses.has_colors, curses.has_ic, curses.has_il,
164 curses.isendwin, curses.killchar, curses.longname,
165 curses.nocbreak, curses.noecho, curses.nonl,
166 curses.noqiflush, curses.noraw,
167 curses.reset_prog_mode, curses.termattrs,
168 curses.termname, curses.erasechar, curses.getsyx]:
169 func()
170
171 # Functions that actually need arguments
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000172 if curses.tigetstr("cnorm"):
173 curses.curs_set(1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000174 curses.delay_output(1)
175 curses.echo() ; curses.echo(1)
176
177 f = tempfile.TemporaryFile()
178 stdscr.putwin(f)
179 f.seek(0)
180 curses.getwin(f)
181 f.close()
182
183 curses.halfdelay(1)
184 curses.intrflush(1)
185 curses.meta(1)
186 curses.napms(100)
187 curses.newpad(50,50)
188 win = curses.newwin(5,5)
189 win = curses.newwin(5,5, 1,1)
190 curses.nl() ; curses.nl(1)
191 curses.putp('abc')
192 curses.qiflush()
193 curses.raw() ; curses.raw(1)
194 curses.setsyx(5,5)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000195 curses.tigetflag('hc')
196 curses.tigetnum('co')
197 curses.tigetstr('cr')
198 curses.tparm('cr')
199 curses.typeahead(sys.__stdin__.fileno())
200 curses.unctrl('a')
201 curses.ungetch('a')
202 curses.use_env(1)
203
204 # Functions only available on a few platforms
205 if curses.has_colors():
206 curses.start_color()
207 curses.init_pair(2, 1,1)
208 curses.color_content(1)
209 curses.color_pair(2)
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000210 curses.pair_content(curses.COLOR_PAIRS - 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000211 curses.pair_number(0)
212
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000213 if hasattr(curses, 'use_default_colors'):
214 curses.use_default_colors()
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000215
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000216 if hasattr(curses, 'keyname'):
217 curses.keyname(13)
218
219 if hasattr(curses, 'has_key'):
220 curses.has_key(13)
221
222 if hasattr(curses, 'getmouse'):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000223 (availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED)
224 # availmask indicates that mouse stuff not available.
225 if availmask != 0:
226 curses.mouseinterval(10)
227 # just verify these don't cause errors
228 m = curses.getmouse()
229 curses.ungetmouse(*m)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000230
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000231 if hasattr(curses, 'is_term_resized'):
232 curses.is_term_resized(*stdscr.getmaxyx())
233 if hasattr(curses, 'resizeterm'):
234 curses.resizeterm(*stdscr.getmaxyx())
235 if hasattr(curses, 'resize_term'):
236 curses.resize_term(*stdscr.getmaxyx())
237
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000238def unit_tests():
239 from curses import ascii
240 for ch, expected in [('a', 'a'), ('A', 'A'),
241 (';', ';'), (' ', ' '),
Andrew M. Kuchlinge8792c12003-08-29 18:49:05 +0000242 ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
243 # Meta-bit characters
244 ('\x8a', '!^J'), ('\xc1', '!A'),
245 ]:
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000246 if ascii.unctrl(ch) != expected:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000247 print('curses.unctrl fails on character', repr(ch))
Tim Peters58eb11c2004-01-18 20:29:55 +0000248
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000249
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000250def test_userptr_without_set(stdscr):
251 w = curses.newwin(10, 10)
252 p = curses.panel.new_panel(w)
253 # try to access userptr() before calling set_userptr() -- segfaults
254 try:
255 p.userptr()
Collin Winter3add4d72007-08-29 23:37:32 +0000256 raise RuntimeError('userptr should fail since not set')
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000257 except curses.panel.error:
258 pass
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000259
Guido van Rossumd8faa362007-04-27 19:54:29 +0000260def test_resize_term(stdscr):
261 if hasattr(curses, 'resizeterm'):
262 lines, cols = curses.LINES, curses.COLS
263 curses.resizeterm(lines - 1, cols + 1)
264
265 if curses.LINES != lines - 1 or curses.COLS != cols + 1:
Collin Winter3add4d72007-08-29 23:37:32 +0000266 raise RuntimeError("Expected resizeterm to update LINES and COLS")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000267
Benjamin Peterson7e2ef572009-10-04 20:40:17 +0000268def test_issue6243(stdscr):
269 curses.ungetch(1025)
270 stdscr.getkey()
271
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000272def main(stdscr):
273 curses.savetty()
274 try:
275 module_funcs(stdscr)
276 window_funcs(stdscr)
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000277 test_userptr_without_set(stdscr)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000278 test_resize_term(stdscr)
Benjamin Peterson7e2ef572009-10-04 20:40:17 +0000279 test_issue6243(stdscr)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000280 finally:
281 curses.resetty()
282
Alexandre Vassalotti5ff02352009-07-22 21:27:53 +0000283def test_main():
R. David Murray09386442009-10-19 16:04:44 +0000284 if not sys.stdout.isatty():
285 raise unittest.SkipTest("sys.stdout is not a tty")
Christian Heimes836baa52008-02-26 08:18:30 +0000286 # testing setupterm() inside initscr/endwin
287 # causes terminal breakage
Alexandre Vassalotti5ff02352009-07-22 21:27:53 +0000288 curses.setupterm(fd=sys.stdout.fileno())
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000289 try:
290 stdscr = curses.initscr()
291 main(stdscr)
292 finally:
293 curses.endwin()
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000294 unit_tests()
Alexandre Vassalotti5ff02352009-07-22 21:27:53 +0000295
296if __name__ == '__main__':
297 curses.wrapper(main)
298 unit_tests()