blob: e13c553027f7d755dc62165a3d3b39e62696cf82 [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
R. David Murray3db8a342009-03-30 23:05:48 +000018from test.test_support import requires, import_module
Neal Norwitz9f39f682006-01-06 04:18:21 +000019requires('curses')
R. David Murray3db8a342009-03-30 23:05:48 +000020curses = import_module('curses')
21curses.panel = import_module('curses.panel')
Neal Norwitz9f39f682006-01-06 04:18:21 +000022
23# XXX: if newterm was supported we could use it instead of initscr and not exit
24term = os.environ.get('TERM')
25if not term or term == 'unknown':
Benjamin Petersonbec087f2009-03-26 21:10:30 +000026 raise unittest.SkipTest, "$TERM=%r, calling initscr() may cause exit" % term
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000027
Anthony Baxter76801852006-04-04 13:32:08 +000028if sys.platform == "cygwin":
Benjamin Petersonbec087f2009-03-26 21:10:30 +000029 raise unittest.SkipTest("cygwin's curses mostly just hangs")
Anthony Baxter76801852006-04-04 13:32:08 +000030
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000031def window_funcs(stdscr):
32 "Test the methods of windows"
33 win = curses.newwin(10,10)
34 win = curses.newwin(5,5, 5,5)
35 win2 = curses.newwin(15,15, 5,5)
36
37 for meth in [stdscr.addch, stdscr.addstr]:
38 for args in [('a'), ('a', curses.A_BOLD),
39 (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]:
Raymond Hettingerff41c482003-04-06 09:01:11 +000040 meth(*args)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000041
42 for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot,
43 stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch,
44 stdscr.deleteln, stdscr.erase, stdscr.getbegyx,
45 stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx,
46 stdscr.getparyx, stdscr.getyx, stdscr.inch,
47 stdscr.insertln, stdscr.instr, stdscr.is_wintouched,
48 win.noutrefresh, stdscr.redrawwin, stdscr.refresh,
49 stdscr.standout, stdscr.standend, stdscr.syncdown,
50 stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]:
51 meth()
52
53 stdscr.addnstr('1234', 3)
54 stdscr.addnstr('1234', 3, curses.A_BOLD)
55 stdscr.addnstr(4,4, '1234', 3)
56 stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD)
57
58 stdscr.attron(curses.A_BOLD)
59 stdscr.attroff(curses.A_BOLD)
60 stdscr.attrset(curses.A_BOLD)
61 stdscr.bkgd(' ')
62 stdscr.bkgd(' ', curses.A_REVERSE)
63 stdscr.bkgdset(' ')
64 stdscr.bkgdset(' ', curses.A_REVERSE)
65
66 win.border(65, 66, 67, 68,
67 69, 70, 71, 72)
68 win.border('|', '!', '-', '_',
69 '+', '\\', '#', '/')
70 try:
71 win.border(65, 66, 67, 68,
72 69, [], 71, 72)
73 except TypeError:
74 pass
75 else:
76 raise RuntimeError, "Expected win.border() to raise TypeError"
77
78 stdscr.clearok(1)
79
80 win4 = stdscr.derwin(2,2)
81 win4 = stdscr.derwin(1,1, 5,5)
82 win4.mvderwin(9,9)
83
84 stdscr.echochar('a')
85 stdscr.echochar('a', curses.A_BOLD)
86 stdscr.hline('-', 5)
87 stdscr.hline('-', 5, curses.A_BOLD)
88 stdscr.hline(1,1,'-', 5)
89 stdscr.hline(1,1,'-', 5, curses.A_BOLD)
90
91 stdscr.idcok(1)
92 stdscr.idlok(1)
93 stdscr.immedok(1)
94 stdscr.insch('c')
95 stdscr.insdelln(1)
96 stdscr.insnstr('abc', 3)
97 stdscr.insnstr('abc', 3, curses.A_BOLD)
98 stdscr.insnstr(5, 5, 'abc', 3)
99 stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD)
100
101 stdscr.insstr('def')
102 stdscr.insstr('def', curses.A_BOLD)
103 stdscr.insstr(5, 5, 'def')
104 stdscr.insstr(5, 5, 'def', curses.A_BOLD)
105 stdscr.is_linetouched(0)
106 stdscr.keypad(1)
107 stdscr.leaveok(1)
108 stdscr.move(3,3)
109 win.mvwin(2,2)
110 stdscr.nodelay(1)
111 stdscr.notimeout(1)
112 win2.overlay(win)
113 win2.overwrite(win)
Neal Norwitz88bbd732006-01-10 07:05:44 +0000114 win2.overlay(win, 1, 2, 3, 3, 2, 1)
115 win2.overwrite(win, 1, 2, 3, 3, 2, 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000116 stdscr.redrawln(1,2)
117
118 stdscr.scrollok(1)
119 stdscr.scroll()
120 stdscr.scroll(2)
121 stdscr.scroll(-3)
122
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000123 stdscr.move(12, 2)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000124 stdscr.setscrreg(10,15)
125 win3 = stdscr.subwin(10,10)
126 win3 = stdscr.subwin(10,10, 5,5)
127 stdscr.syncok(1)
128 stdscr.timeout(5)
129 stdscr.touchline(5,5)
130 stdscr.touchline(5,5,0)
131 stdscr.vline('a', 3)
132 stdscr.vline('a', 3, curses.A_STANDOUT)
Andrew M. Kuchling400a49b2007-04-11 13:39:00 +0000133 stdscr.chgat(5, 2, 3, curses.A_BLINK)
134 stdscr.chgat(3, curses.A_BOLD)
135 stdscr.chgat(5, 8, curses.A_UNDERLINE)
136 stdscr.chgat(curses.A_BLINK)
137 stdscr.refresh()
138
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000139 stdscr.vline(1,1, 'a', 3)
140 stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
141
142 if hasattr(curses, 'resize'):
143 stdscr.resize()
144 if hasattr(curses, 'enclose'):
145 stdscr.enclose()
146
147
148def module_funcs(stdscr):
149 "Test module-level functions"
150
151 for func in [curses.baudrate, curses.beep, curses.can_change_color,
152 curses.cbreak, curses.def_prog_mode, curses.doupdate,
153 curses.filter, curses.flash, curses.flushinp,
154 curses.has_colors, curses.has_ic, curses.has_il,
155 curses.isendwin, curses.killchar, curses.longname,
156 curses.nocbreak, curses.noecho, curses.nonl,
157 curses.noqiflush, curses.noraw,
158 curses.reset_prog_mode, curses.termattrs,
159 curses.termname, curses.erasechar, curses.getsyx]:
160 func()
161
162 # Functions that actually need arguments
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000163 if curses.tigetstr("cnorm"):
164 curses.curs_set(1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000165 curses.delay_output(1)
166 curses.echo() ; curses.echo(1)
167
168 f = tempfile.TemporaryFile()
169 stdscr.putwin(f)
170 f.seek(0)
171 curses.getwin(f)
172 f.close()
173
174 curses.halfdelay(1)
175 curses.intrflush(1)
176 curses.meta(1)
177 curses.napms(100)
178 curses.newpad(50,50)
179 win = curses.newwin(5,5)
180 win = curses.newwin(5,5, 1,1)
181 curses.nl() ; curses.nl(1)
182 curses.putp('abc')
183 curses.qiflush()
184 curses.raw() ; curses.raw(1)
185 curses.setsyx(5,5)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000186 curses.tigetflag('hc')
187 curses.tigetnum('co')
188 curses.tigetstr('cr')
189 curses.tparm('cr')
190 curses.typeahead(sys.__stdin__.fileno())
191 curses.unctrl('a')
192 curses.ungetch('a')
193 curses.use_env(1)
194
195 # Functions only available on a few platforms
196 if curses.has_colors():
197 curses.start_color()
198 curses.init_pair(2, 1,1)
199 curses.color_content(1)
200 curses.color_pair(2)
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000201 curses.pair_content(curses.COLOR_PAIRS - 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000202 curses.pair_number(0)
203
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000204 if hasattr(curses, 'use_default_colors'):
205 curses.use_default_colors()
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000206
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000207 if hasattr(curses, 'keyname'):
208 curses.keyname(13)
209
210 if hasattr(curses, 'has_key'):
211 curses.has_key(13)
212
213 if hasattr(curses, 'getmouse'):
Anthony Baxtere94e3b42006-04-06 07:12:39 +0000214 (availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED)
215 # availmask indicates that mouse stuff not available.
216 if availmask != 0:
217 curses.mouseinterval(10)
218 # just verify these don't cause errors
219 m = curses.getmouse()
220 curses.ungetmouse(*m)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000221
Walter Dörwald4994d952006-06-19 08:07:50 +0000222 if hasattr(curses, 'is_term_resized'):
223 curses.is_term_resized(*stdscr.getmaxyx())
224 if hasattr(curses, 'resizeterm'):
225 curses.resizeterm(*stdscr.getmaxyx())
226 if hasattr(curses, 'resize_term'):
227 curses.resize_term(*stdscr.getmaxyx())
228
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000229def unit_tests():
230 from curses import ascii
231 for ch, expected in [('a', 'a'), ('A', 'A'),
232 (';', ';'), (' ', ' '),
Andrew M. Kuchlinge8792c12003-08-29 18:49:05 +0000233 ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
234 # Meta-bit characters
235 ('\x8a', '!^J'), ('\xc1', '!A'),
236 ]:
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000237 if ascii.unctrl(ch) != expected:
238 print 'curses.unctrl fails on character', repr(ch)
Tim Peters58eb11c2004-01-18 20:29:55 +0000239
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000240
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000241def test_userptr_without_set(stdscr):
242 w = curses.newwin(10, 10)
243 p = curses.panel.new_panel(w)
244 # try to access userptr() before calling set_userptr() -- segfaults
245 try:
246 p.userptr()
247 raise RuntimeError, 'userptr should fail since not set'
248 except curses.panel.error:
249 pass
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000250
Walter Dörwaldd391f082007-03-06 20:38:57 +0000251def test_resize_term(stdscr):
252 if hasattr(curses, 'resizeterm'):
253 lines, cols = curses.LINES, curses.COLS
254 curses.resizeterm(lines - 1, cols + 1)
Tim Petersea5962f2007-03-12 18:07:52 +0000255
Walter Dörwaldd391f082007-03-06 20:38:57 +0000256 if curses.LINES != lines - 1 or curses.COLS != cols + 1:
257 raise RuntimeError, "Expected resizeterm to update LINES and COLS"
258
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000259def main(stdscr):
260 curses.savetty()
261 try:
262 module_funcs(stdscr)
263 window_funcs(stdscr)
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000264 test_userptr_without_set(stdscr)
Walter Dörwaldd391f082007-03-06 20:38:57 +0000265 test_resize_term(stdscr)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000266 finally:
267 curses.resetty()
268
269if __name__ == '__main__':
270 curses.wrapper(main)
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000271 unit_tests()
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000272else:
Andrew M. Kuchlingaa5e3ce2008-02-25 16:29:19 +0000273 # testing setupterm() inside initscr/endwin
274 # causes terminal breakage
275 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()