blob: 69271de847412e3b25efa33f386bce43db86bbdd [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 Murray3db8a342009-03-30 23:05:48 +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'Arc5217c082009-06-09 23:37:11 +000018import unittest
R. David Murray3db8a342009-03-30 23:05:48 +000019from test.test_support import requires, import_module
Neal Norwitz9f39f682006-01-06 04:18:21 +000020requires('curses')
R. David Murray3db8a342009-03-30 23:05:48 +000021curses = import_module('curses')
22curses.panel = import_module('curses.panel')
Neal Norwitz9f39f682006-01-06 04:18:21 +000023
Mark Dickinson45ad8012010-02-21 13:37:53 +000024# skip all these tests on FreeBSD: test_curses currently hangs the
25# FreeBSD buildbots, preventing other tests from running. See issue
26# #7384.
27if 'freebsd' in sys.platform:
28 raise unittest.SkipTest('The curses module is broken on FreeBSD. See http://bugs.python.org/issue7384.')
29
Neal Norwitz9f39f682006-01-06 04:18:21 +000030# XXX: if newterm was supported we could use it instead of initscr and not exit
31term = os.environ.get('TERM')
32if not term or term == 'unknown':
Benjamin Petersonbec087f2009-03-26 21:10:30 +000033 raise unittest.SkipTest, "$TERM=%r, calling initscr() may cause exit" % term
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000034
Anthony Baxter76801852006-04-04 13:32:08 +000035if sys.platform == "cygwin":
Benjamin Petersonbec087f2009-03-26 21:10:30 +000036 raise unittest.SkipTest("cygwin's curses mostly just hangs")
Anthony Baxter76801852006-04-04 13:32:08 +000037
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000038def window_funcs(stdscr):
39 "Test the methods of windows"
40 win = curses.newwin(10,10)
41 win = curses.newwin(5,5, 5,5)
42 win2 = curses.newwin(15,15, 5,5)
43
44 for meth in [stdscr.addch, stdscr.addstr]:
45 for args in [('a'), ('a', curses.A_BOLD),
46 (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]:
Raymond Hettingerff41c482003-04-06 09:01:11 +000047 meth(*args)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000048
49 for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot,
50 stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch,
51 stdscr.deleteln, stdscr.erase, stdscr.getbegyx,
52 stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx,
53 stdscr.getparyx, stdscr.getyx, stdscr.inch,
54 stdscr.insertln, stdscr.instr, stdscr.is_wintouched,
55 win.noutrefresh, stdscr.redrawwin, stdscr.refresh,
56 stdscr.standout, stdscr.standend, stdscr.syncdown,
57 stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]:
58 meth()
59
60 stdscr.addnstr('1234', 3)
61 stdscr.addnstr('1234', 3, curses.A_BOLD)
62 stdscr.addnstr(4,4, '1234', 3)
63 stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD)
64
65 stdscr.attron(curses.A_BOLD)
66 stdscr.attroff(curses.A_BOLD)
67 stdscr.attrset(curses.A_BOLD)
68 stdscr.bkgd(' ')
69 stdscr.bkgd(' ', curses.A_REVERSE)
70 stdscr.bkgdset(' ')
71 stdscr.bkgdset(' ', curses.A_REVERSE)
72
73 win.border(65, 66, 67, 68,
74 69, 70, 71, 72)
75 win.border('|', '!', '-', '_',
76 '+', '\\', '#', '/')
77 try:
78 win.border(65, 66, 67, 68,
79 69, [], 71, 72)
80 except TypeError:
81 pass
82 else:
83 raise RuntimeError, "Expected win.border() to raise TypeError"
84
85 stdscr.clearok(1)
86
87 win4 = stdscr.derwin(2,2)
88 win4 = stdscr.derwin(1,1, 5,5)
89 win4.mvderwin(9,9)
90
91 stdscr.echochar('a')
92 stdscr.echochar('a', curses.A_BOLD)
93 stdscr.hline('-', 5)
94 stdscr.hline('-', 5, curses.A_BOLD)
95 stdscr.hline(1,1,'-', 5)
96 stdscr.hline(1,1,'-', 5, curses.A_BOLD)
97
98 stdscr.idcok(1)
99 stdscr.idlok(1)
100 stdscr.immedok(1)
101 stdscr.insch('c')
102 stdscr.insdelln(1)
103 stdscr.insnstr('abc', 3)
104 stdscr.insnstr('abc', 3, curses.A_BOLD)
105 stdscr.insnstr(5, 5, 'abc', 3)
106 stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD)
107
108 stdscr.insstr('def')
109 stdscr.insstr('def', curses.A_BOLD)
110 stdscr.insstr(5, 5, 'def')
111 stdscr.insstr(5, 5, 'def', curses.A_BOLD)
112 stdscr.is_linetouched(0)
113 stdscr.keypad(1)
114 stdscr.leaveok(1)
115 stdscr.move(3,3)
116 win.mvwin(2,2)
117 stdscr.nodelay(1)
118 stdscr.notimeout(1)
119 win2.overlay(win)
120 win2.overwrite(win)
Neal Norwitz88bbd732006-01-10 07:05:44 +0000121 win2.overlay(win, 1, 2, 3, 3, 2, 1)
122 win2.overwrite(win, 1, 2, 3, 3, 2, 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000123 stdscr.redrawln(1,2)
124
125 stdscr.scrollok(1)
126 stdscr.scroll()
127 stdscr.scroll(2)
128 stdscr.scroll(-3)
129
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000130 stdscr.move(12, 2)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000131 stdscr.setscrreg(10,15)
132 win3 = stdscr.subwin(10,10)
133 win3 = stdscr.subwin(10,10, 5,5)
134 stdscr.syncok(1)
135 stdscr.timeout(5)
136 stdscr.touchline(5,5)
137 stdscr.touchline(5,5,0)
138 stdscr.vline('a', 3)
139 stdscr.vline('a', 3, curses.A_STANDOUT)
Andrew M. Kuchling400a49b2007-04-11 13:39:00 +0000140 stdscr.chgat(5, 2, 3, curses.A_BLINK)
141 stdscr.chgat(3, curses.A_BOLD)
142 stdscr.chgat(5, 8, curses.A_UNDERLINE)
143 stdscr.chgat(curses.A_BLINK)
144 stdscr.refresh()
145
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000146 stdscr.vline(1,1, 'a', 3)
147 stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
148
149 if hasattr(curses, 'resize'):
150 stdscr.resize()
151 if hasattr(curses, 'enclose'):
152 stdscr.enclose()
153
154
155def module_funcs(stdscr):
156 "Test module-level functions"
157
158 for func in [curses.baudrate, curses.beep, curses.can_change_color,
159 curses.cbreak, curses.def_prog_mode, curses.doupdate,
160 curses.filter, curses.flash, curses.flushinp,
161 curses.has_colors, curses.has_ic, curses.has_il,
162 curses.isendwin, curses.killchar, curses.longname,
163 curses.nocbreak, curses.noecho, curses.nonl,
164 curses.noqiflush, curses.noraw,
165 curses.reset_prog_mode, curses.termattrs,
166 curses.termname, curses.erasechar, curses.getsyx]:
167 func()
168
169 # Functions that actually need arguments
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000170 if curses.tigetstr("cnorm"):
171 curses.curs_set(1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000172 curses.delay_output(1)
173 curses.echo() ; curses.echo(1)
174
175 f = tempfile.TemporaryFile()
176 stdscr.putwin(f)
177 f.seek(0)
178 curses.getwin(f)
179 f.close()
180
181 curses.halfdelay(1)
182 curses.intrflush(1)
183 curses.meta(1)
184 curses.napms(100)
185 curses.newpad(50,50)
186 win = curses.newwin(5,5)
187 win = curses.newwin(5,5, 1,1)
188 curses.nl() ; curses.nl(1)
189 curses.putp('abc')
190 curses.qiflush()
191 curses.raw() ; curses.raw(1)
192 curses.setsyx(5,5)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000193 curses.tigetflag('hc')
194 curses.tigetnum('co')
195 curses.tigetstr('cr')
196 curses.tparm('cr')
197 curses.typeahead(sys.__stdin__.fileno())
198 curses.unctrl('a')
199 curses.ungetch('a')
200 curses.use_env(1)
201
202 # Functions only available on a few platforms
203 if curses.has_colors():
204 curses.start_color()
205 curses.init_pair(2, 1,1)
206 curses.color_content(1)
207 curses.color_pair(2)
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000208 curses.pair_content(curses.COLOR_PAIRS - 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000209 curses.pair_number(0)
210
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000211 if hasattr(curses, 'use_default_colors'):
212 curses.use_default_colors()
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000213
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000214 if hasattr(curses, 'keyname'):
215 curses.keyname(13)
216
217 if hasattr(curses, 'has_key'):
218 curses.has_key(13)
219
220 if hasattr(curses, 'getmouse'):
Anthony Baxtere94e3b42006-04-06 07:12:39 +0000221 (availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED)
222 # availmask indicates that mouse stuff not available.
223 if availmask != 0:
224 curses.mouseinterval(10)
225 # just verify these don't cause errors
226 m = curses.getmouse()
227 curses.ungetmouse(*m)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000228
Walter Dörwald4994d952006-06-19 08:07:50 +0000229 if hasattr(curses, 'is_term_resized'):
230 curses.is_term_resized(*stdscr.getmaxyx())
231 if hasattr(curses, 'resizeterm'):
232 curses.resizeterm(*stdscr.getmaxyx())
233 if hasattr(curses, 'resize_term'):
234 curses.resize_term(*stdscr.getmaxyx())
235
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000236def unit_tests():
237 from curses import ascii
238 for ch, expected in [('a', 'a'), ('A', 'A'),
239 (';', ';'), (' ', ' '),
Andrew M. Kuchlinge8792c12003-08-29 18:49:05 +0000240 ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
241 # Meta-bit characters
242 ('\x8a', '!^J'), ('\xc1', '!A'),
243 ]:
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000244 if ascii.unctrl(ch) != expected:
245 print 'curses.unctrl fails on character', repr(ch)
Tim Peters58eb11c2004-01-18 20:29:55 +0000246
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000247
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000248def test_userptr_without_set(stdscr):
249 w = curses.newwin(10, 10)
250 p = curses.panel.new_panel(w)
251 # try to access userptr() before calling set_userptr() -- segfaults
252 try:
253 p.userptr()
254 raise RuntimeError, 'userptr should fail since not set'
255 except curses.panel.error:
256 pass
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000257
Walter Dörwaldd391f082007-03-06 20:38:57 +0000258def test_resize_term(stdscr):
259 if hasattr(curses, 'resizeterm'):
260 lines, cols = curses.LINES, curses.COLS
261 curses.resizeterm(lines - 1, cols + 1)
Tim Petersea5962f2007-03-12 18:07:52 +0000262
Walter Dörwaldd391f082007-03-06 20:38:57 +0000263 if curses.LINES != lines - 1 or curses.COLS != cols + 1:
264 raise RuntimeError, "Expected resizeterm to update LINES and COLS"
265
Andrew M. Kuchlingb49e53e2009-09-25 22:23:54 +0000266def test_issue6243(stdscr):
267 curses.ungetch(1025)
268 stdscr.getkey()
269
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000270def main(stdscr):
271 curses.savetty()
272 try:
273 module_funcs(stdscr)
274 window_funcs(stdscr)
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000275 test_userptr_without_set(stdscr)
Walter Dörwaldd391f082007-03-06 20:38:57 +0000276 test_resize_term(stdscr)
Andrew M. Kuchlingb49e53e2009-09-25 22:23:54 +0000277 test_issue6243(stdscr)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000278 finally:
279 curses.resetty()
280
281if __name__ == '__main__':
282 curses.wrapper(main)
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000283 unit_tests()
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000284else:
R. David Murrayd9f19442009-10-19 16:01:28 +0000285 if not sys.__stdout__.isatty():
286 raise unittest.SkipTest("sys.__stdout__ is not a tty")
Andrew M. Kuchlingaa5e3ce2008-02-25 16:29:19 +0000287 # testing setupterm() inside initscr/endwin
288 # causes terminal breakage
289 curses.setupterm(fd=sys.__stdout__.fileno())
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000290 try:
291 stdscr = curses.initscr()
292 main(stdscr)
293 finally:
294 curses.endwin()
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000295 unit_tests()