blob: ad10f620b3605464ce82147ef4c203bf0ec4f1bd [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
Larry Hastings9147a962014-05-04 04:41:18 -070020import inspect
Neal Norwitz9f39f682006-01-06 04:18:21 +000021requires('curses')
22
R. David Murraya21e4ca2009-03-31 23:16:50 +000023# If either of these don't exist, skip the tests.
24curses = import_module('curses')
25curses.panel = import_module('curses.panel')
26
Mark Dickinson945e2422010-02-21 13:42:03 +000027
Neal Norwitz9f39f682006-01-06 04:18:21 +000028# XXX: if newterm was supported we could use it instead of initscr and not exit
29term = os.environ.get('TERM')
30if not term or term == 'unknown':
Benjamin Petersone549ead2009-03-28 21:42:05 +000031 raise unittest.SkipTest("$TERM=%r, calling initscr() may cause exit" % term)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000032
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033if sys.platform == "cygwin":
Benjamin Petersone549ead2009-03-28 21:42:05 +000034 raise unittest.SkipTest("cygwin's curses mostly just hangs")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000035
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000036def window_funcs(stdscr):
37 "Test the methods of windows"
38 win = curses.newwin(10,10)
39 win = curses.newwin(5,5, 5,5)
40 win2 = curses.newwin(15,15, 5,5)
41
42 for meth in [stdscr.addch, stdscr.addstr]:
43 for args in [('a'), ('a', curses.A_BOLD),
44 (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]:
Raymond Hettingerff41c482003-04-06 09:01:11 +000045 meth(*args)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000046
47 for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot,
48 stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch,
49 stdscr.deleteln, stdscr.erase, stdscr.getbegyx,
50 stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx,
51 stdscr.getparyx, stdscr.getyx, stdscr.inch,
52 stdscr.insertln, stdscr.instr, stdscr.is_wintouched,
53 win.noutrefresh, stdscr.redrawwin, stdscr.refresh,
54 stdscr.standout, stdscr.standend, stdscr.syncdown,
55 stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]:
56 meth()
57
58 stdscr.addnstr('1234', 3)
59 stdscr.addnstr('1234', 3, curses.A_BOLD)
60 stdscr.addnstr(4,4, '1234', 3)
61 stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD)
62
63 stdscr.attron(curses.A_BOLD)
64 stdscr.attroff(curses.A_BOLD)
65 stdscr.attrset(curses.A_BOLD)
66 stdscr.bkgd(' ')
67 stdscr.bkgd(' ', curses.A_REVERSE)
68 stdscr.bkgdset(' ')
69 stdscr.bkgdset(' ', curses.A_REVERSE)
70
71 win.border(65, 66, 67, 68,
72 69, 70, 71, 72)
73 win.border('|', '!', '-', '_',
74 '+', '\\', '#', '/')
75 try:
76 win.border(65, 66, 67, 68,
77 69, [], 71, 72)
78 except TypeError:
79 pass
80 else:
Collin Winter3add4d72007-08-29 23:37:32 +000081 raise RuntimeError("Expected win.border() to raise TypeError")
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000082
83 stdscr.clearok(1)
84
85 win4 = stdscr.derwin(2,2)
86 win4 = stdscr.derwin(1,1, 5,5)
87 win4.mvderwin(9,9)
88
89 stdscr.echochar('a')
90 stdscr.echochar('a', curses.A_BOLD)
91 stdscr.hline('-', 5)
92 stdscr.hline('-', 5, curses.A_BOLD)
93 stdscr.hline(1,1,'-', 5)
94 stdscr.hline(1,1,'-', 5, curses.A_BOLD)
95
96 stdscr.idcok(1)
97 stdscr.idlok(1)
98 stdscr.immedok(1)
99 stdscr.insch('c')
100 stdscr.insdelln(1)
101 stdscr.insnstr('abc', 3)
102 stdscr.insnstr('abc', 3, curses.A_BOLD)
103 stdscr.insnstr(5, 5, 'abc', 3)
104 stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD)
105
106 stdscr.insstr('def')
107 stdscr.insstr('def', curses.A_BOLD)
108 stdscr.insstr(5, 5, 'def')
109 stdscr.insstr(5, 5, 'def', curses.A_BOLD)
110 stdscr.is_linetouched(0)
111 stdscr.keypad(1)
112 stdscr.leaveok(1)
113 stdscr.move(3,3)
114 win.mvwin(2,2)
115 stdscr.nodelay(1)
116 stdscr.notimeout(1)
117 win2.overlay(win)
118 win2.overwrite(win)
Larry Hastings23e37aa2014-01-25 22:19:47 -0800119 win2.overlay(win, 1, 2, 2, 1, 3, 3)
120 win2.overwrite(win, 1, 2, 2, 1, 3, 3)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000121 stdscr.redrawln(1,2)
122
123 stdscr.scrollok(1)
124 stdscr.scroll()
125 stdscr.scroll(2)
126 stdscr.scroll(-3)
127
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000128 stdscr.move(12, 2)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000129 stdscr.setscrreg(10,15)
130 win3 = stdscr.subwin(10,10)
131 win3 = stdscr.subwin(10,10, 5,5)
132 stdscr.syncok(1)
133 stdscr.timeout(5)
134 stdscr.touchline(5,5)
135 stdscr.touchline(5,5,0)
136 stdscr.vline('a', 3)
137 stdscr.vline('a', 3, curses.A_STANDOUT)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000138 stdscr.chgat(5, 2, 3, curses.A_BLINK)
139 stdscr.chgat(3, curses.A_BOLD)
140 stdscr.chgat(5, 8, curses.A_UNDERLINE)
141 stdscr.chgat(curses.A_BLINK)
142 stdscr.refresh()
143
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000144 stdscr.vline(1,1, 'a', 3)
145 stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
146
147 if hasattr(curses, 'resize'):
148 stdscr.resize()
149 if hasattr(curses, 'enclose'):
150 stdscr.enclose()
151
152
153def module_funcs(stdscr):
154 "Test module-level functions"
155
156 for func in [curses.baudrate, curses.beep, curses.can_change_color,
157 curses.cbreak, curses.def_prog_mode, curses.doupdate,
158 curses.filter, curses.flash, curses.flushinp,
159 curses.has_colors, curses.has_ic, curses.has_il,
160 curses.isendwin, curses.killchar, curses.longname,
161 curses.nocbreak, curses.noecho, curses.nonl,
162 curses.noqiflush, curses.noraw,
163 curses.reset_prog_mode, curses.termattrs,
164 curses.termname, curses.erasechar, curses.getsyx]:
165 func()
166
167 # Functions that actually need arguments
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000168 if curses.tigetstr("cnorm"):
169 curses.curs_set(1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000170 curses.delay_output(1)
171 curses.echo() ; curses.echo(1)
172
173 f = tempfile.TemporaryFile()
174 stdscr.putwin(f)
175 f.seek(0)
176 curses.getwin(f)
177 f.close()
178
179 curses.halfdelay(1)
180 curses.intrflush(1)
181 curses.meta(1)
182 curses.napms(100)
183 curses.newpad(50,50)
184 win = curses.newwin(5,5)
185 win = curses.newwin(5,5, 1,1)
186 curses.nl() ; curses.nl(1)
Victor Stinnerb3bc7e72011-11-03 20:35:40 +0100187 curses.putp(b'abc')
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000188 curses.qiflush()
189 curses.raw() ; curses.raw(1)
190 curses.setsyx(5,5)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000191 curses.tigetflag('hc')
192 curses.tigetnum('co')
193 curses.tigetstr('cr')
Victor Stinner26621332011-11-02 23:45:29 +0100194 curses.tparm(b'cr')
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000195 curses.typeahead(sys.__stdin__.fileno())
196 curses.unctrl('a')
197 curses.ungetch('a')
198 curses.use_env(1)
199
200 # Functions only available on a few platforms
201 if curses.has_colors():
202 curses.start_color()
203 curses.init_pair(2, 1,1)
204 curses.color_content(1)
205 curses.color_pair(2)
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000206 curses.pair_content(curses.COLOR_PAIRS - 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000207 curses.pair_number(0)
208
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000209 if hasattr(curses, 'use_default_colors'):
210 curses.use_default_colors()
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000211
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000212 if hasattr(curses, 'keyname'):
213 curses.keyname(13)
214
215 if hasattr(curses, 'has_key'):
216 curses.has_key(13)
217
218 if hasattr(curses, 'getmouse'):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000219 (availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED)
220 # availmask indicates that mouse stuff not available.
221 if availmask != 0:
222 curses.mouseinterval(10)
223 # just verify these don't cause errors
Mark Dickinsonaf43e9a2010-08-07 12:33:36 +0000224 curses.ungetmouse(0, 0, 0, 0, curses.BUTTON1_PRESSED)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000225 m = curses.getmouse()
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000226
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000227 if hasattr(curses, 'is_term_resized'):
228 curses.is_term_resized(*stdscr.getmaxyx())
229 if hasattr(curses, 'resizeterm'):
230 curses.resizeterm(*stdscr.getmaxyx())
231 if hasattr(curses, 'resize_term'):
232 curses.resize_term(*stdscr.getmaxyx())
233
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000234def unit_tests():
235 from curses import ascii
236 for ch, expected in [('a', 'a'), ('A', 'A'),
237 (';', ';'), (' ', ' '),
Andrew M. Kuchlinge8792c12003-08-29 18:49:05 +0000238 ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
239 # Meta-bit characters
240 ('\x8a', '!^J'), ('\xc1', '!A'),
241 ]:
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000242 if ascii.unctrl(ch) != expected:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000243 print('curses.unctrl fails on character', repr(ch))
Tim Peters58eb11c2004-01-18 20:29:55 +0000244
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000245
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000246def test_userptr_without_set(stdscr):
247 w = curses.newwin(10, 10)
248 p = curses.panel.new_panel(w)
249 # try to access userptr() before calling set_userptr() -- segfaults
250 try:
251 p.userptr()
Collin Winter3add4d72007-08-29 23:37:32 +0000252 raise RuntimeError('userptr should fail since not set')
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000253 except curses.panel.error:
254 pass
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000255
Andrew Kuchling53e5ea72013-06-15 14:04:04 -0400256def test_userptr_memory_leak(stdscr):
257 w = curses.newwin(10, 10)
258 p = curses.panel.new_panel(w)
259 obj = object()
260 nrefs = sys.getrefcount(obj)
261 for i in range(100):
262 p.set_userptr(obj)
263
264 p.set_userptr(None)
265 if sys.getrefcount(obj) != nrefs:
266 raise RuntimeError("set_userptr leaked references")
267
Andrew Kuchling9290dd12013-06-22 14:50:56 -0400268def test_userptr_segfault(stdscr):
269 panel = curses.panel.new_panel(stdscr)
270 class A:
271 def __del__(self):
272 panel.set_userptr(None)
273 panel.set_userptr(A())
274 panel.set_userptr(None)
275
Guido van Rossumd8faa362007-04-27 19:54:29 +0000276def test_resize_term(stdscr):
277 if hasattr(curses, 'resizeterm'):
278 lines, cols = curses.LINES, curses.COLS
279 curses.resizeterm(lines - 1, cols + 1)
280
281 if curses.LINES != lines - 1 or curses.COLS != cols + 1:
Collin Winter3add4d72007-08-29 23:37:32 +0000282 raise RuntimeError("Expected resizeterm to update LINES and COLS")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000283
Benjamin Peterson7e2ef572009-10-04 20:40:17 +0000284def test_issue6243(stdscr):
285 curses.ungetch(1025)
286 stdscr.getkey()
287
Victor Stinner71e44cb2011-09-06 01:53:03 +0200288def test_unget_wch(stdscr):
Victor Stinner900c2922011-09-06 10:08:28 +0200289 if not hasattr(curses, 'unget_wch'):
290 return
Victor Stinnerca2b6462012-08-29 01:40:57 +0200291 encoding = stdscr.encoding
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100292 for ch in ('a', '\xe9', '\u20ac', '\U0010FFFF'):
Victor Stinner756c6ec2011-11-27 00:19:53 +0100293 try:
Victor Stinnerd1b097f2011-11-28 07:26:19 +0100294 ch.encode(encoding)
295 except UnicodeEncodeError:
296 continue
297 try:
Victor Stinner756c6ec2011-11-27 00:19:53 +0100298 curses.unget_wch(ch)
299 except Exception as err:
Victor Stinnerca2b6462012-08-29 01:40:57 +0200300 raise Exception("unget_wch(%a) failed with encoding %s: %s"
301 % (ch, stdscr.encoding, err))
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100302 read = stdscr.get_wch()
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100303 if read != ch:
304 raise AssertionError("%r != %r" % (read, ch))
Victor Stinner71e44cb2011-09-06 01:53:03 +0200305
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100306 code = ord(ch)
307 curses.unget_wch(code)
308 read = stdscr.get_wch()
Victor Stinnerca2b6462012-08-29 01:40:57 +0200309 if read != ch:
310 raise AssertionError("%r != %r" % (read, ch))
Victor Stinner71e44cb2011-09-06 01:53:03 +0200311
Victor Stinner26621332011-11-02 23:45:29 +0100312def test_issue10570():
313 b = curses.tparm(curses.tigetstr("cup"), 5, 3)
314 assert type(b) is bytes
Victor Stinnerb3bc7e72011-11-03 20:35:40 +0100315 curses.putp(b)
Victor Stinner26621332011-11-02 23:45:29 +0100316
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100317def test_encoding(stdscr):
318 import codecs
319 encoding = stdscr.encoding
320 codecs.lookup(encoding)
321 try:
322 stdscr.encoding = 10
323 except TypeError:
324 pass
325 else:
326 raise AssertionError("TypeError not raised")
327 stdscr.encoding = encoding
328 try:
329 del stdscr.encoding
330 except TypeError:
331 pass
332 else:
333 raise AssertionError("TypeError not raised")
334
Larry Hastings9147a962014-05-04 04:41:18 -0700335def test_issue21088(stdscr):
336 #
337 # http://bugs.python.org/issue21088
338 #
339 # the bug:
340 # when converting curses.window.addch to Argument Clinic
341 # the first two parameters were switched.
342
343 # if someday we can represent the signature of addch
344 # we will need to rewrite this test.
345 try:
346 signature = inspect.signature(stdscr.addch)
347 self.assertFalse(signature)
348 except ValueError:
349 # not generating a signature is fine.
350 pass
351
352 # So. No signature for addch.
353 # But Argument Clinic gave us a human-readable equivalent
354 # as the first line of the docstring. So we parse that,
355 # and ensure that the parameters appear in the correct order.
356 # Since this is parsing output from Argument Clinic, we can
357 # be reasonably certain the generated parsing code will be
358 # correct too.
359 human_readable_signature = stdscr.addch.__doc__.split("\n")[0]
360 offset = human_readable_signature.find("[y, x,]")
361 assert offset >= 0, ""
362
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000363def main(stdscr):
364 curses.savetty()
365 try:
366 module_funcs(stdscr)
367 window_funcs(stdscr)
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000368 test_userptr_without_set(stdscr)
Andrew Kuchling53e5ea72013-06-15 14:04:04 -0400369 test_userptr_memory_leak(stdscr)
Andrew Kuchling9290dd12013-06-22 14:50:56 -0400370 test_userptr_segfault(stdscr)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000371 test_resize_term(stdscr)
Benjamin Peterson7e2ef572009-10-04 20:40:17 +0000372 test_issue6243(stdscr)
Victor Stinner71e44cb2011-09-06 01:53:03 +0200373 test_unget_wch(stdscr)
Victor Stinner26621332011-11-02 23:45:29 +0100374 test_issue10570()
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100375 test_encoding(stdscr)
Larry Hastings9147a962014-05-04 04:41:18 -0700376 test_issue21088(stdscr)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000377 finally:
378 curses.resetty()
379
Alexandre Vassalotti5ff02352009-07-22 21:27:53 +0000380def test_main():
Nadeem Vawda780199e2011-08-13 15:43:49 +0200381 if not sys.__stdout__.isatty():
382 raise unittest.SkipTest("sys.__stdout__ is not a tty")
Christian Heimes836baa52008-02-26 08:18:30 +0000383 # testing setupterm() inside initscr/endwin
384 # causes terminal breakage
Nadeem Vawda780199e2011-08-13 15:43:49 +0200385 curses.setupterm(fd=sys.__stdout__.fileno())
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000386 try:
387 stdscr = curses.initscr()
388 main(stdscr)
389 finally:
390 curses.endwin()
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000391 unit_tests()
Alexandre Vassalotti5ff02352009-07-22 21:27:53 +0000392
393if __name__ == '__main__':
394 curses.wrapper(main)
395 unit_tests()