blob: 856f9c622e2b7877d07b981d23e957ecee643fad [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:
Florent Xiclunadc92ce92010-03-30 10:39:44 +000026 raise TestSkipped('The curses module is broken on FreeBSD. '
27 'See http://bugs.python.org/issue7384.')
Mark Dickinson4fadf732010-02-21 13:40:57 +000028
Neal Norwitz9f39f682006-01-06 04:18:21 +000029# XXX: if newterm was supported we could use it instead of initscr and not exit
30term = os.environ.get('TERM')
31if not term or term == 'unknown':
32 raise TestSkipped, "$TERM=%r, calling initscr() may cause exit" % term
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000033
Anthony Baxter76801852006-04-04 13:32:08 +000034if sys.platform == "cygwin":
35 raise TestSkipped("cygwin's curses mostly just hangs")
36
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000037def window_funcs(stdscr):
38 "Test the methods of windows"
39 win = curses.newwin(10,10)
40 win = curses.newwin(5,5, 5,5)
41 win2 = curses.newwin(15,15, 5,5)
42
43 for meth in [stdscr.addch, stdscr.addstr]:
44 for args in [('a'), ('a', curses.A_BOLD),
45 (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]:
Raymond Hettingerff41c482003-04-06 09:01:11 +000046 meth(*args)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000047
48 for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot,
49 stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch,
50 stdscr.deleteln, stdscr.erase, stdscr.getbegyx,
51 stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx,
52 stdscr.getparyx, stdscr.getyx, stdscr.inch,
53 stdscr.insertln, stdscr.instr, stdscr.is_wintouched,
54 win.noutrefresh, stdscr.redrawwin, stdscr.refresh,
55 stdscr.standout, stdscr.standend, stdscr.syncdown,
56 stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]:
57 meth()
58
59 stdscr.addnstr('1234', 3)
60 stdscr.addnstr('1234', 3, curses.A_BOLD)
61 stdscr.addnstr(4,4, '1234', 3)
62 stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD)
63
64 stdscr.attron(curses.A_BOLD)
65 stdscr.attroff(curses.A_BOLD)
66 stdscr.attrset(curses.A_BOLD)
67 stdscr.bkgd(' ')
68 stdscr.bkgd(' ', curses.A_REVERSE)
69 stdscr.bkgdset(' ')
70 stdscr.bkgdset(' ', curses.A_REVERSE)
71
72 win.border(65, 66, 67, 68,
73 69, 70, 71, 72)
74 win.border('|', '!', '-', '_',
75 '+', '\\', '#', '/')
76 try:
77 win.border(65, 66, 67, 68,
78 69, [], 71, 72)
79 except TypeError:
80 pass
81 else:
82 raise RuntimeError, "Expected win.border() to raise TypeError"
83
84 stdscr.clearok(1)
85
86 win4 = stdscr.derwin(2,2)
87 win4 = stdscr.derwin(1,1, 5,5)
88 win4.mvderwin(9,9)
89
90 stdscr.echochar('a')
91 stdscr.echochar('a', curses.A_BOLD)
92 stdscr.hline('-', 5)
93 stdscr.hline('-', 5, curses.A_BOLD)
94 stdscr.hline(1,1,'-', 5)
95 stdscr.hline(1,1,'-', 5, curses.A_BOLD)
96
97 stdscr.idcok(1)
98 stdscr.idlok(1)
99 stdscr.immedok(1)
100 stdscr.insch('c')
101 stdscr.insdelln(1)
102 stdscr.insnstr('abc', 3)
103 stdscr.insnstr('abc', 3, curses.A_BOLD)
104 stdscr.insnstr(5, 5, 'abc', 3)
105 stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD)
106
107 stdscr.insstr('def')
108 stdscr.insstr('def', curses.A_BOLD)
109 stdscr.insstr(5, 5, 'def')
110 stdscr.insstr(5, 5, 'def', curses.A_BOLD)
111 stdscr.is_linetouched(0)
112 stdscr.keypad(1)
113 stdscr.leaveok(1)
114 stdscr.move(3,3)
115 win.mvwin(2,2)
116 stdscr.nodelay(1)
117 stdscr.notimeout(1)
118 win2.overlay(win)
119 win2.overwrite(win)
Neal Norwitz88bbd732006-01-10 07:05:44 +0000120 win2.overlay(win, 1, 2, 3, 3, 2, 1)
121 win2.overwrite(win, 1, 2, 3, 3, 2, 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000122 stdscr.redrawln(1,2)
123
124 stdscr.scrollok(1)
125 stdscr.scroll()
126 stdscr.scroll(2)
127 stdscr.scroll(-3)
128
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000129 stdscr.move(12, 2)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000130 stdscr.setscrreg(10,15)
131 win3 = stdscr.subwin(10,10)
132 win3 = stdscr.subwin(10,10, 5,5)
133 stdscr.syncok(1)
134 stdscr.timeout(5)
135 stdscr.touchline(5,5)
136 stdscr.touchline(5,5,0)
137 stdscr.vline('a', 3)
138 stdscr.vline('a', 3, curses.A_STANDOUT)
Andrew M. Kuchling400a49b2007-04-11 13:39:00 +0000139 stdscr.chgat(5, 2, 3, curses.A_BLINK)
140 stdscr.chgat(3, curses.A_BOLD)
141 stdscr.chgat(5, 8, curses.A_UNDERLINE)
142 stdscr.chgat(curses.A_BLINK)
143 stdscr.refresh()
144
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000145 stdscr.vline(1,1, 'a', 3)
146 stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
147
148 if hasattr(curses, 'resize'):
149 stdscr.resize()
150 if hasattr(curses, 'enclose'):
151 stdscr.enclose()
152
153
154def module_funcs(stdscr):
155 "Test module-level functions"
156
157 for func in [curses.baudrate, curses.beep, curses.can_change_color,
158 curses.cbreak, curses.def_prog_mode, curses.doupdate,
159 curses.filter, curses.flash, curses.flushinp,
160 curses.has_colors, curses.has_ic, curses.has_il,
161 curses.isendwin, curses.killchar, curses.longname,
162 curses.nocbreak, curses.noecho, curses.nonl,
163 curses.noqiflush, curses.noraw,
164 curses.reset_prog_mode, curses.termattrs,
165 curses.termname, curses.erasechar, curses.getsyx]:
166 func()
167
168 # Functions that actually need arguments
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000169 if curses.tigetstr("cnorm"):
170 curses.curs_set(1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000171 curses.delay_output(1)
172 curses.echo() ; curses.echo(1)
173
174 f = tempfile.TemporaryFile()
175 stdscr.putwin(f)
176 f.seek(0)
177 curses.getwin(f)
178 f.close()
179
180 curses.halfdelay(1)
181 curses.intrflush(1)
182 curses.meta(1)
183 curses.napms(100)
184 curses.newpad(50,50)
185 win = curses.newwin(5,5)
186 win = curses.newwin(5,5, 1,1)
187 curses.nl() ; curses.nl(1)
188 curses.putp('abc')
189 curses.qiflush()
190 curses.raw() ; curses.raw(1)
191 curses.setsyx(5,5)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000192 curses.tigetflag('hc')
193 curses.tigetnum('co')
194 curses.tigetstr('cr')
195 curses.tparm('cr')
196 curses.typeahead(sys.__stdin__.fileno())
197 curses.unctrl('a')
198 curses.ungetch('a')
199 curses.use_env(1)
200
201 # Functions only available on a few platforms
202 if curses.has_colors():
203 curses.start_color()
204 curses.init_pair(2, 1,1)
205 curses.color_content(1)
206 curses.color_pair(2)
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000207 curses.pair_content(curses.COLOR_PAIRS - 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000208 curses.pair_number(0)
209
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000210 if hasattr(curses, 'use_default_colors'):
211 curses.use_default_colors()
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000212
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000213 if hasattr(curses, 'keyname'):
214 curses.keyname(13)
215
216 if hasattr(curses, 'has_key'):
217 curses.has_key(13)
218
219 if hasattr(curses, 'getmouse'):
Anthony Baxtere94e3b42006-04-06 07:12:39 +0000220 (availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED)
221 # availmask indicates that mouse stuff not available.
222 if availmask != 0:
223 curses.mouseinterval(10)
224 # just verify these don't cause errors
225 m = curses.getmouse()
226 curses.ungetmouse(*m)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000227
Walter Dörwald4994d952006-06-19 08:07:50 +0000228 if hasattr(curses, 'is_term_resized'):
229 curses.is_term_resized(*stdscr.getmaxyx())
230 if hasattr(curses, 'resizeterm'):
231 curses.resizeterm(*stdscr.getmaxyx())
232 if hasattr(curses, 'resize_term'):
233 curses.resize_term(*stdscr.getmaxyx())
234
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000235def unit_tests():
236 from curses import ascii
237 for ch, expected in [('a', 'a'), ('A', 'A'),
238 (';', ';'), (' ', ' '),
Andrew M. Kuchlinge8792c12003-08-29 18:49:05 +0000239 ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
240 # Meta-bit characters
241 ('\x8a', '!^J'), ('\xc1', '!A'),
242 ]:
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000243 if ascii.unctrl(ch) != expected:
244 print 'curses.unctrl fails on character', repr(ch)
Tim Peters58eb11c2004-01-18 20:29:55 +0000245
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000246
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000247def test_userptr_without_set(stdscr):
248 w = curses.newwin(10, 10)
249 p = curses.panel.new_panel(w)
250 # try to access userptr() before calling set_userptr() -- segfaults
251 try:
252 p.userptr()
253 raise RuntimeError, 'userptr should fail since not set'
254 except curses.panel.error:
255 pass
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000256
Walter Dörwaldd391f082007-03-06 20:38:57 +0000257def test_resize_term(stdscr):
258 if hasattr(curses, 'resizeterm'):
259 lines, cols = curses.LINES, curses.COLS
260 curses.resizeterm(lines - 1, cols + 1)
Tim Petersea5962f2007-03-12 18:07:52 +0000261
Walter Dörwaldd391f082007-03-06 20:38:57 +0000262 if curses.LINES != lines - 1 or curses.COLS != cols + 1:
263 raise RuntimeError, "Expected resizeterm to update LINES and COLS"
264
Andrew M. Kuchling47d960e2010-02-22 17:06:22 +0000265def test_issue6243(stdscr):
266 curses.ungetch(1025)
267 stdscr.getkey()
268
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000269def main(stdscr):
270 curses.savetty()
271 try:
272 module_funcs(stdscr)
273 window_funcs(stdscr)
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000274 test_userptr_without_set(stdscr)
Walter Dörwaldd391f082007-03-06 20:38:57 +0000275 test_resize_term(stdscr)
Andrew M. Kuchling47d960e2010-02-22 17:06:22 +0000276 test_issue6243(stdscr)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000277 finally:
278 curses.resetty()
279
280if __name__ == '__main__':
281 curses.wrapper(main)
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000282 unit_tests()
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000283else:
R. David Murray24b19992009-11-03 14:31:53 +0000284 if not sys.__stdout__.isatty():
R. David Murray0ae237b2009-11-03 22:47:06 +0000285 raise TestSkipped("sys.__stdout__ is not a tty")
Andrew M. Kuchlingaa5e3ce2008-02-25 16:29:19 +0000286 # testing setupterm() inside initscr/endwin
287 # causes terminal breakage
288 curses.setupterm(fd=sys.__stdout__.fileno())
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000289 try:
290 stdscr = curses.initscr()
291 main(stdscr)
292 finally:
293 curses.endwin()
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000294 unit_tests()