blob: cdb90dbf8b64f0346384d59b327527095c35398d [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
Neal Norwitzeeab7da2006-01-05 06:09:13 +000012import curses, sys, tempfile, os
Neal Norwitz5e3d8622006-01-09 06:24:35 +000013import curses.panel
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000014
15# Optionally test curses module. This currently requires that the
16# 'curses' resource be given on the regrtest command line using the -u
17# option. If not available, nothing after this line will be executed.
18
Neal Norwitz9f39f682006-01-06 04:18:21 +000019from test.test_support import requires, TestSkipped
20requires('curses')
21
Mark Dickinson4fadf732010-02-21 13:40:57 +000022# skip all these tests on FreeBSD: test_curses currently hangs the
23# FreeBSD buildbots, preventing other tests from running. See issue
24# #7384.
25if 'freebsd' in sys.platform:
26 raise unittest.SkipTest('The curses module is broken on FreeBSD. See http://bugs.python.org/issue7384.')
27
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':
31 raise TestSkipped, "$TERM=%r, calling initscr() may cause exit" % term
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000032
Anthony Baxter76801852006-04-04 13:32:08 +000033if sys.platform == "cygwin":
34 raise TestSkipped("cygwin's curses mostly just hangs")
35
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:
81 raise RuntimeError, "Expected win.border() to raise TypeError"
82
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)
Neal Norwitz88bbd732006-01-10 07:05:44 +0000119 win2.overlay(win, 1, 2, 3, 3, 2, 1)
120 win2.overwrite(win, 1, 2, 3, 3, 2, 1)
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)
Andrew M. Kuchling400a49b2007-04-11 13:39:00 +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)
187 curses.putp('abc')
188 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')
194 curses.tparm('cr')
195 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'):
Anthony Baxtere94e3b42006-04-06 07:12:39 +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
224 m = curses.getmouse()
225 curses.ungetmouse(*m)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000226
Walter Dörwald4994d952006-06-19 08:07:50 +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:
243 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()
252 raise RuntimeError, 'userptr should fail since not set'
253 except curses.panel.error:
254 pass
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000255
Walter Dörwaldd391f082007-03-06 20:38:57 +0000256def test_resize_term(stdscr):
257 if hasattr(curses, 'resizeterm'):
258 lines, cols = curses.LINES, curses.COLS
259 curses.resizeterm(lines - 1, cols + 1)
Tim Petersea5962f2007-03-12 18:07:52 +0000260
Walter Dörwaldd391f082007-03-06 20:38:57 +0000261 if curses.LINES != lines - 1 or curses.COLS != cols + 1:
262 raise RuntimeError, "Expected resizeterm to update LINES and COLS"
263
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000264def main(stdscr):
265 curses.savetty()
266 try:
267 module_funcs(stdscr)
268 window_funcs(stdscr)
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000269 test_userptr_without_set(stdscr)
Walter Dörwaldd391f082007-03-06 20:38:57 +0000270 test_resize_term(stdscr)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000271 finally:
272 curses.resetty()
273
274if __name__ == '__main__':
275 curses.wrapper(main)
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000276 unit_tests()
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000277else:
R. David Murray24b19992009-11-03 14:31:53 +0000278 if not sys.__stdout__.isatty():
R. David Murray0ae237b2009-11-03 22:47:06 +0000279 raise TestSkipped("sys.__stdout__ is not a tty")
Andrew M. Kuchlingaa5e3ce2008-02-25 16:29:19 +0000280 # testing setupterm() inside initscr/endwin
281 # causes terminal breakage
282 curses.setupterm(fd=sys.__stdout__.fileno())
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000283 try:
284 stdscr = curses.initscr()
285 main(stdscr)
286 finally:
287 curses.endwin()
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000288 unit_tests()