blob: b9ff34644b6b2cfb191f290c07d4c7b6d4d055d2 [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
Neal Norwitz9f39f682006-01-06 04:18:21 +000027# XXX: if newterm was supported we could use it instead of initscr and not exit
28term = os.environ.get('TERM')
29if not term or term == 'unknown':
Benjamin Petersone549ead2009-03-28 21:42:05 +000030 raise unittest.SkipTest("$TERM=%r, calling initscr() may cause exit" % term)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000031
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000032if sys.platform == "cygwin":
Benjamin Petersone549ead2009-03-28 21:42:05 +000033 raise unittest.SkipTest("cygwin's curses mostly just hangs")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000035def window_funcs(stdscr):
36 "Test the methods of windows"
37 win = curses.newwin(10,10)
38 win = curses.newwin(5,5, 5,5)
39 win2 = curses.newwin(15,15, 5,5)
40
41 for meth in [stdscr.addch, stdscr.addstr]:
42 for args in [('a'), ('a', curses.A_BOLD),
43 (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]:
Raymond Hettingerff41c482003-04-06 09:01:11 +000044 meth(*args)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000045
46 for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot,
47 stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch,
48 stdscr.deleteln, stdscr.erase, stdscr.getbegyx,
49 stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx,
50 stdscr.getparyx, stdscr.getyx, stdscr.inch,
51 stdscr.insertln, stdscr.instr, stdscr.is_wintouched,
52 win.noutrefresh, stdscr.redrawwin, stdscr.refresh,
53 stdscr.standout, stdscr.standend, stdscr.syncdown,
54 stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]:
55 meth()
56
57 stdscr.addnstr('1234', 3)
58 stdscr.addnstr('1234', 3, curses.A_BOLD)
59 stdscr.addnstr(4,4, '1234', 3)
60 stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD)
61
62 stdscr.attron(curses.A_BOLD)
63 stdscr.attroff(curses.A_BOLD)
64 stdscr.attrset(curses.A_BOLD)
65 stdscr.bkgd(' ')
66 stdscr.bkgd(' ', curses.A_REVERSE)
67 stdscr.bkgdset(' ')
68 stdscr.bkgdset(' ', curses.A_REVERSE)
69
70 win.border(65, 66, 67, 68,
71 69, 70, 71, 72)
72 win.border('|', '!', '-', '_',
73 '+', '\\', '#', '/')
74 try:
75 win.border(65, 66, 67, 68,
76 69, [], 71, 72)
77 except TypeError:
78 pass
79 else:
Collin Winter3add4d72007-08-29 23:37:32 +000080 raise RuntimeError("Expected win.border() to raise TypeError")
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000081
82 stdscr.clearok(1)
83
84 win4 = stdscr.derwin(2,2)
85 win4 = stdscr.derwin(1,1, 5,5)
86 win4.mvderwin(9,9)
87
88 stdscr.echochar('a')
89 stdscr.echochar('a', curses.A_BOLD)
90 stdscr.hline('-', 5)
91 stdscr.hline('-', 5, curses.A_BOLD)
92 stdscr.hline(1,1,'-', 5)
93 stdscr.hline(1,1,'-', 5, curses.A_BOLD)
94
95 stdscr.idcok(1)
96 stdscr.idlok(1)
97 stdscr.immedok(1)
98 stdscr.insch('c')
99 stdscr.insdelln(1)
100 stdscr.insnstr('abc', 3)
101 stdscr.insnstr('abc', 3, curses.A_BOLD)
102 stdscr.insnstr(5, 5, 'abc', 3)
103 stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD)
104
105 stdscr.insstr('def')
106 stdscr.insstr('def', curses.A_BOLD)
107 stdscr.insstr(5, 5, 'def')
108 stdscr.insstr(5, 5, 'def', curses.A_BOLD)
109 stdscr.is_linetouched(0)
110 stdscr.keypad(1)
111 stdscr.leaveok(1)
112 stdscr.move(3,3)
113 win.mvwin(2,2)
114 stdscr.nodelay(1)
115 stdscr.notimeout(1)
116 win2.overlay(win)
117 win2.overwrite(win)
Neal Norwitz88bbd732006-01-10 07:05:44 +0000118 win2.overlay(win, 1, 2, 3, 3, 2, 1)
119 win2.overwrite(win, 1, 2, 3, 3, 2, 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000120 stdscr.redrawln(1,2)
121
122 stdscr.scrollok(1)
123 stdscr.scroll()
124 stdscr.scroll(2)
125 stdscr.scroll(-3)
126
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000127 stdscr.move(12, 2)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000128 stdscr.setscrreg(10,15)
129 win3 = stdscr.subwin(10,10)
130 win3 = stdscr.subwin(10,10, 5,5)
131 stdscr.syncok(1)
132 stdscr.timeout(5)
133 stdscr.touchline(5,5)
134 stdscr.touchline(5,5,0)
135 stdscr.vline('a', 3)
136 stdscr.vline('a', 3, curses.A_STANDOUT)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000137 stdscr.chgat(5, 2, 3, curses.A_BLINK)
138 stdscr.chgat(3, curses.A_BOLD)
139 stdscr.chgat(5, 8, curses.A_UNDERLINE)
140 stdscr.chgat(curses.A_BLINK)
141 stdscr.refresh()
142
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000143 stdscr.vline(1,1, 'a', 3)
144 stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
145
146 if hasattr(curses, 'resize'):
147 stdscr.resize()
148 if hasattr(curses, 'enclose'):
149 stdscr.enclose()
150
151
152def module_funcs(stdscr):
153 "Test module-level functions"
154
155 for func in [curses.baudrate, curses.beep, curses.can_change_color,
156 curses.cbreak, curses.def_prog_mode, curses.doupdate,
157 curses.filter, curses.flash, curses.flushinp,
158 curses.has_colors, curses.has_ic, curses.has_il,
159 curses.isendwin, curses.killchar, curses.longname,
160 curses.nocbreak, curses.noecho, curses.nonl,
161 curses.noqiflush, curses.noraw,
162 curses.reset_prog_mode, curses.termattrs,
163 curses.termname, curses.erasechar, curses.getsyx]:
164 func()
165
166 # Functions that actually need arguments
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000167 if curses.tigetstr("cnorm"):
168 curses.curs_set(1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000169 curses.delay_output(1)
170 curses.echo() ; curses.echo(1)
171
172 f = tempfile.TemporaryFile()
173 stdscr.putwin(f)
174 f.seek(0)
175 curses.getwin(f)
176 f.close()
177
178 curses.halfdelay(1)
179 curses.intrflush(1)
180 curses.meta(1)
181 curses.napms(100)
182 curses.newpad(50,50)
183 win = curses.newwin(5,5)
184 win = curses.newwin(5,5, 1,1)
185 curses.nl() ; curses.nl(1)
186 curses.putp('abc')
187 curses.qiflush()
188 curses.raw() ; curses.raw(1)
189 curses.setsyx(5,5)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000190 curses.tigetflag('hc')
191 curses.tigetnum('co')
192 curses.tigetstr('cr')
193 curses.tparm('cr')
194 curses.typeahead(sys.__stdin__.fileno())
195 curses.unctrl('a')
196 curses.ungetch('a')
197 curses.use_env(1)
198
199 # Functions only available on a few platforms
200 if curses.has_colors():
201 curses.start_color()
202 curses.init_pair(2, 1,1)
203 curses.color_content(1)
204 curses.color_pair(2)
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000205 curses.pair_content(curses.COLOR_PAIRS - 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000206 curses.pair_number(0)
207
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000208 if hasattr(curses, 'use_default_colors'):
209 curses.use_default_colors()
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000210
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000211 if hasattr(curses, 'keyname'):
212 curses.keyname(13)
213
214 if hasattr(curses, 'has_key'):
215 curses.has_key(13)
216
217 if hasattr(curses, 'getmouse'):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000218 (availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED)
219 # availmask indicates that mouse stuff not available.
220 if availmask != 0:
221 curses.mouseinterval(10)
222 # just verify these don't cause errors
Mark Dickinsonaf43e9a2010-08-07 12:33:36 +0000223 curses.ungetmouse(0, 0, 0, 0, curses.BUTTON1_PRESSED)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000224 m = curses.getmouse()
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000225
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000226 if hasattr(curses, 'is_term_resized'):
227 curses.is_term_resized(*stdscr.getmaxyx())
228 if hasattr(curses, 'resizeterm'):
229 curses.resizeterm(*stdscr.getmaxyx())
230 if hasattr(curses, 'resize_term'):
231 curses.resize_term(*stdscr.getmaxyx())
232
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000233def unit_tests():
234 from curses import ascii
235 for ch, expected in [('a', 'a'), ('A', 'A'),
236 (';', ';'), (' ', ' '),
Andrew M. Kuchlinge8792c12003-08-29 18:49:05 +0000237 ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
238 # Meta-bit characters
239 ('\x8a', '!^J'), ('\xc1', '!A'),
240 ]:
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000241 if ascii.unctrl(ch) != expected:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000242 print('curses.unctrl fails on character', repr(ch))
Tim Peters58eb11c2004-01-18 20:29:55 +0000243
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000244
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000245def test_userptr_without_set(stdscr):
246 w = curses.newwin(10, 10)
247 p = curses.panel.new_panel(w)
248 # try to access userptr() before calling set_userptr() -- segfaults
249 try:
250 p.userptr()
Collin Winter3add4d72007-08-29 23:37:32 +0000251 raise RuntimeError('userptr should fail since not set')
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000252 except curses.panel.error:
253 pass
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000254
Guido van Rossumd8faa362007-04-27 19:54:29 +0000255def test_resize_term(stdscr):
256 if hasattr(curses, 'resizeterm'):
257 lines, cols = curses.LINES, curses.COLS
258 curses.resizeterm(lines - 1, cols + 1)
259
260 if curses.LINES != lines - 1 or curses.COLS != cols + 1:
Collin Winter3add4d72007-08-29 23:37:32 +0000261 raise RuntimeError("Expected resizeterm to update LINES and COLS")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000262
Benjamin Peterson7e2ef572009-10-04 20:40:17 +0000263def test_issue6243(stdscr):
264 curses.ungetch(1025)
265 stdscr.getkey()
266
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000267def main(stdscr):
268 curses.savetty()
269 try:
270 module_funcs(stdscr)
271 window_funcs(stdscr)
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000272 test_userptr_without_set(stdscr)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000273 test_resize_term(stdscr)
Benjamin Peterson7e2ef572009-10-04 20:40:17 +0000274 test_issue6243(stdscr)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000275 finally:
276 curses.resetty()
277
Alexandre Vassalotti5ff02352009-07-22 21:27:53 +0000278def test_main():
R. David Murray09386442009-10-19 16:04:44 +0000279 if not sys.stdout.isatty():
280 raise unittest.SkipTest("sys.stdout is not a tty")
Christian Heimes836baa52008-02-26 08:18:30 +0000281 # testing setupterm() inside initscr/endwin
282 # causes terminal breakage
Alexandre Vassalotti5ff02352009-07-22 21:27:53 +0000283 curses.setupterm(fd=sys.stdout.fileno())
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000284 try:
285 stdscr = curses.initscr()
286 main(stdscr)
287 finally:
288 curses.endwin()
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000289 unit_tests()
Alexandre Vassalotti5ff02352009-07-22 21:27:53 +0000290
291if __name__ == '__main__':
292 curses.wrapper(main)
293 unit_tests()