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