blob: 3f43c11edd6ab62f7132d256113cbbe9d2fa2adf [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
R. David Murraya21e4ca2009-03-31 23:16:50 +000018from test.support import requires, import_module
Neal Norwitz9f39f682006-01-06 04:18:21 +000019requires('curses')
20
R. David Murraya21e4ca2009-03-31 23:16:50 +000021# If either of these don't exist, skip the tests.
22curses = import_module('curses')
23curses.panel = import_module('curses.panel')
24
Neal Norwitz9f39f682006-01-06 04:18:21 +000025# XXX: if newterm was supported we could use it instead of initscr and not exit
26term = os.environ.get('TERM')
27if not term or term == 'unknown':
Benjamin Petersone549ead2009-03-28 21:42:05 +000028 raise unittest.SkipTest("$TERM=%r, calling initscr() may cause exit" % term)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000029
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030if sys.platform == "cygwin":
Benjamin Petersone549ead2009-03-28 21:42:05 +000031 raise unittest.SkipTest("cygwin's curses mostly just hangs")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000032
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000033def window_funcs(stdscr):
34 "Test the methods of windows"
35 win = curses.newwin(10,10)
36 win = curses.newwin(5,5, 5,5)
37 win2 = curses.newwin(15,15, 5,5)
38
39 for meth in [stdscr.addch, stdscr.addstr]:
40 for args in [('a'), ('a', curses.A_BOLD),
41 (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]:
Raymond Hettingerff41c482003-04-06 09:01:11 +000042 meth(*args)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000043
44 for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot,
45 stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch,
46 stdscr.deleteln, stdscr.erase, stdscr.getbegyx,
47 stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx,
48 stdscr.getparyx, stdscr.getyx, stdscr.inch,
49 stdscr.insertln, stdscr.instr, stdscr.is_wintouched,
50 win.noutrefresh, stdscr.redrawwin, stdscr.refresh,
51 stdscr.standout, stdscr.standend, stdscr.syncdown,
52 stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]:
53 meth()
54
55 stdscr.addnstr('1234', 3)
56 stdscr.addnstr('1234', 3, curses.A_BOLD)
57 stdscr.addnstr(4,4, '1234', 3)
58 stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD)
59
60 stdscr.attron(curses.A_BOLD)
61 stdscr.attroff(curses.A_BOLD)
62 stdscr.attrset(curses.A_BOLD)
63 stdscr.bkgd(' ')
64 stdscr.bkgd(' ', curses.A_REVERSE)
65 stdscr.bkgdset(' ')
66 stdscr.bkgdset(' ', curses.A_REVERSE)
67
68 win.border(65, 66, 67, 68,
69 69, 70, 71, 72)
70 win.border('|', '!', '-', '_',
71 '+', '\\', '#', '/')
72 try:
73 win.border(65, 66, 67, 68,
74 69, [], 71, 72)
75 except TypeError:
76 pass
77 else:
Collin Winter3add4d72007-08-29 23:37:32 +000078 raise RuntimeError("Expected win.border() to raise TypeError")
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000079
80 stdscr.clearok(1)
81
82 win4 = stdscr.derwin(2,2)
83 win4 = stdscr.derwin(1,1, 5,5)
84 win4.mvderwin(9,9)
85
86 stdscr.echochar('a')
87 stdscr.echochar('a', curses.A_BOLD)
88 stdscr.hline('-', 5)
89 stdscr.hline('-', 5, curses.A_BOLD)
90 stdscr.hline(1,1,'-', 5)
91 stdscr.hline(1,1,'-', 5, curses.A_BOLD)
92
93 stdscr.idcok(1)
94 stdscr.idlok(1)
95 stdscr.immedok(1)
96 stdscr.insch('c')
97 stdscr.insdelln(1)
98 stdscr.insnstr('abc', 3)
99 stdscr.insnstr('abc', 3, curses.A_BOLD)
100 stdscr.insnstr(5, 5, 'abc', 3)
101 stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD)
102
103 stdscr.insstr('def')
104 stdscr.insstr('def', curses.A_BOLD)
105 stdscr.insstr(5, 5, 'def')
106 stdscr.insstr(5, 5, 'def', curses.A_BOLD)
107 stdscr.is_linetouched(0)
108 stdscr.keypad(1)
109 stdscr.leaveok(1)
110 stdscr.move(3,3)
111 win.mvwin(2,2)
112 stdscr.nodelay(1)
113 stdscr.notimeout(1)
114 win2.overlay(win)
115 win2.overwrite(win)
Neal Norwitz88bbd732006-01-10 07:05:44 +0000116 win2.overlay(win, 1, 2, 3, 3, 2, 1)
117 win2.overwrite(win, 1, 2, 3, 3, 2, 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000118 stdscr.redrawln(1,2)
119
120 stdscr.scrollok(1)
121 stdscr.scroll()
122 stdscr.scroll(2)
123 stdscr.scroll(-3)
124
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000125 stdscr.move(12, 2)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000126 stdscr.setscrreg(10,15)
127 win3 = stdscr.subwin(10,10)
128 win3 = stdscr.subwin(10,10, 5,5)
129 stdscr.syncok(1)
130 stdscr.timeout(5)
131 stdscr.touchline(5,5)
132 stdscr.touchline(5,5,0)
133 stdscr.vline('a', 3)
134 stdscr.vline('a', 3, curses.A_STANDOUT)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000135 stdscr.chgat(5, 2, 3, curses.A_BLINK)
136 stdscr.chgat(3, curses.A_BOLD)
137 stdscr.chgat(5, 8, curses.A_UNDERLINE)
138 stdscr.chgat(curses.A_BLINK)
139 stdscr.refresh()
140
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000141 stdscr.vline(1,1, 'a', 3)
142 stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
143
144 if hasattr(curses, 'resize'):
145 stdscr.resize()
146 if hasattr(curses, 'enclose'):
147 stdscr.enclose()
148
149
150def module_funcs(stdscr):
151 "Test module-level functions"
152
153 for func in [curses.baudrate, curses.beep, curses.can_change_color,
154 curses.cbreak, curses.def_prog_mode, curses.doupdate,
155 curses.filter, curses.flash, curses.flushinp,
156 curses.has_colors, curses.has_ic, curses.has_il,
157 curses.isendwin, curses.killchar, curses.longname,
158 curses.nocbreak, curses.noecho, curses.nonl,
159 curses.noqiflush, curses.noraw,
160 curses.reset_prog_mode, curses.termattrs,
161 curses.termname, curses.erasechar, curses.getsyx]:
162 func()
163
164 # Functions that actually need arguments
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000165 if curses.tigetstr("cnorm"):
166 curses.curs_set(1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000167 curses.delay_output(1)
168 curses.echo() ; curses.echo(1)
169
170 f = tempfile.TemporaryFile()
171 stdscr.putwin(f)
172 f.seek(0)
173 curses.getwin(f)
174 f.close()
175
176 curses.halfdelay(1)
177 curses.intrflush(1)
178 curses.meta(1)
179 curses.napms(100)
180 curses.newpad(50,50)
181 win = curses.newwin(5,5)
182 win = curses.newwin(5,5, 1,1)
183 curses.nl() ; curses.nl(1)
184 curses.putp('abc')
185 curses.qiflush()
186 curses.raw() ; curses.raw(1)
187 curses.setsyx(5,5)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000188 curses.tigetflag('hc')
189 curses.tigetnum('co')
190 curses.tigetstr('cr')
191 curses.tparm('cr')
192 curses.typeahead(sys.__stdin__.fileno())
193 curses.unctrl('a')
194 curses.ungetch('a')
195 curses.use_env(1)
196
197 # Functions only available on a few platforms
198 if curses.has_colors():
199 curses.start_color()
200 curses.init_pair(2, 1,1)
201 curses.color_content(1)
202 curses.color_pair(2)
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000203 curses.pair_content(curses.COLOR_PAIRS - 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000204 curses.pair_number(0)
205
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000206 if hasattr(curses, 'use_default_colors'):
207 curses.use_default_colors()
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000208
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000209 if hasattr(curses, 'keyname'):
210 curses.keyname(13)
211
212 if hasattr(curses, 'has_key'):
213 curses.has_key(13)
214
215 if hasattr(curses, 'getmouse'):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000216 (availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED)
217 # availmask indicates that mouse stuff not available.
218 if availmask != 0:
219 curses.mouseinterval(10)
220 # just verify these don't cause errors
221 m = curses.getmouse()
222 curses.ungetmouse(*m)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000223
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000224 if hasattr(curses, 'is_term_resized'):
225 curses.is_term_resized(*stdscr.getmaxyx())
226 if hasattr(curses, 'resizeterm'):
227 curses.resizeterm(*stdscr.getmaxyx())
228 if hasattr(curses, 'resize_term'):
229 curses.resize_term(*stdscr.getmaxyx())
230
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000231def unit_tests():
232 from curses import ascii
233 for ch, expected in [('a', 'a'), ('A', 'A'),
234 (';', ';'), (' ', ' '),
Andrew M. Kuchlinge8792c12003-08-29 18:49:05 +0000235 ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
236 # Meta-bit characters
237 ('\x8a', '!^J'), ('\xc1', '!A'),
238 ]:
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000239 if ascii.unctrl(ch) != expected:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000240 print('curses.unctrl fails on character', repr(ch))
Tim Peters58eb11c2004-01-18 20:29:55 +0000241
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000242
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000243def test_userptr_without_set(stdscr):
244 w = curses.newwin(10, 10)
245 p = curses.panel.new_panel(w)
246 # try to access userptr() before calling set_userptr() -- segfaults
247 try:
248 p.userptr()
Collin Winter3add4d72007-08-29 23:37:32 +0000249 raise RuntimeError('userptr should fail since not set')
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000250 except curses.panel.error:
251 pass
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000252
Guido van Rossumd8faa362007-04-27 19:54:29 +0000253def test_resize_term(stdscr):
254 if hasattr(curses, 'resizeterm'):
255 lines, cols = curses.LINES, curses.COLS
256 curses.resizeterm(lines - 1, cols + 1)
257
258 if curses.LINES != lines - 1 or curses.COLS != cols + 1:
Collin Winter3add4d72007-08-29 23:37:32 +0000259 raise RuntimeError("Expected resizeterm to update LINES and COLS")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000260
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000261def main(stdscr):
262 curses.savetty()
263 try:
264 module_funcs(stdscr)
265 window_funcs(stdscr)
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000266 test_userptr_without_set(stdscr)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000267 test_resize_term(stdscr)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000268 finally:
269 curses.resetty()
270
271if __name__ == '__main__':
272 curses.wrapper(main)
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000273 unit_tests()
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000274else:
Christian Heimes836baa52008-02-26 08:18:30 +0000275 # testing setupterm() inside initscr/endwin
276 # causes terminal breakage
277 curses.setupterm(fd=sys.__stdout__.fileno())
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000278 try:
279 stdscr = curses.initscr()
280 main(stdscr)
281 finally:
282 curses.endwin()
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000283 unit_tests()