The silencing of DeprecationWarning was not taking -3 into consideration. Since
[python.git] / Lib / test / test_curses.py
blob414228211a14e9908dcc5555ff58516010db9648
2 # Test script for the curses module
4 # This script doesn't actually display anything very coherent. but it
5 # does call every method and function.
7 # Functions not tested: {def,reset}_{shell,prog}_mode, getch(), getstr(),
8 # init_color()
9 # Only called, not tested: getmouse(), ungetmouse()
12 import sys, tempfile, os
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.
18 import unittest
19 from test.test_support import requires, import_module
20 requires('curses')
21 curses = import_module('curses')
22 curses.panel = import_module('curses.panel')
24 # XXX: if newterm was supported we could use it instead of initscr and not exit
25 term = os.environ.get('TERM')
26 if not term or term == 'unknown':
27 raise unittest.SkipTest, "$TERM=%r, calling initscr() may cause exit" % term
29 if sys.platform == "cygwin":
30 raise unittest.SkipTest("cygwin's curses mostly just hangs")
32 def window_funcs(stdscr):
33 "Test the methods of windows"
34 win = curses.newwin(10,10)
35 win = curses.newwin(5,5, 5,5)
36 win2 = curses.newwin(15,15, 5,5)
38 for meth in [stdscr.addch, stdscr.addstr]:
39 for args in [('a'), ('a', curses.A_BOLD),
40 (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]:
41 meth(*args)
43 for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot,
44 stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch,
45 stdscr.deleteln, stdscr.erase, stdscr.getbegyx,
46 stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx,
47 stdscr.getparyx, stdscr.getyx, stdscr.inch,
48 stdscr.insertln, stdscr.instr, stdscr.is_wintouched,
49 win.noutrefresh, stdscr.redrawwin, stdscr.refresh,
50 stdscr.standout, stdscr.standend, stdscr.syncdown,
51 stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]:
52 meth()
54 stdscr.addnstr('1234', 3)
55 stdscr.addnstr('1234', 3, curses.A_BOLD)
56 stdscr.addnstr(4,4, '1234', 3)
57 stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD)
59 stdscr.attron(curses.A_BOLD)
60 stdscr.attroff(curses.A_BOLD)
61 stdscr.attrset(curses.A_BOLD)
62 stdscr.bkgd(' ')
63 stdscr.bkgd(' ', curses.A_REVERSE)
64 stdscr.bkgdset(' ')
65 stdscr.bkgdset(' ', curses.A_REVERSE)
67 win.border(65, 66, 67, 68,
68 69, 70, 71, 72)
69 win.border('|', '!', '-', '_',
70 '+', '\\', '#', '/')
71 try:
72 win.border(65, 66, 67, 68,
73 69, [], 71, 72)
74 except TypeError:
75 pass
76 else:
77 raise RuntimeError, "Expected win.border() to raise TypeError"
79 stdscr.clearok(1)
81 win4 = stdscr.derwin(2,2)
82 win4 = stdscr.derwin(1,1, 5,5)
83 win4.mvderwin(9,9)
85 stdscr.echochar('a')
86 stdscr.echochar('a', curses.A_BOLD)
87 stdscr.hline('-', 5)
88 stdscr.hline('-', 5, curses.A_BOLD)
89 stdscr.hline(1,1,'-', 5)
90 stdscr.hline(1,1,'-', 5, curses.A_BOLD)
92 stdscr.idcok(1)
93 stdscr.idlok(1)
94 stdscr.immedok(1)
95 stdscr.insch('c')
96 stdscr.insdelln(1)
97 stdscr.insnstr('abc', 3)
98 stdscr.insnstr('abc', 3, curses.A_BOLD)
99 stdscr.insnstr(5, 5, 'abc', 3)
100 stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD)
102 stdscr.insstr('def')
103 stdscr.insstr('def', curses.A_BOLD)
104 stdscr.insstr(5, 5, 'def')
105 stdscr.insstr(5, 5, 'def', curses.A_BOLD)
106 stdscr.is_linetouched(0)
107 stdscr.keypad(1)
108 stdscr.leaveok(1)
109 stdscr.move(3,3)
110 win.mvwin(2,2)
111 stdscr.nodelay(1)
112 stdscr.notimeout(1)
113 win2.overlay(win)
114 win2.overwrite(win)
115 win2.overlay(win, 1, 2, 3, 3, 2, 1)
116 win2.overwrite(win, 1, 2, 3, 3, 2, 1)
117 stdscr.redrawln(1,2)
119 stdscr.scrollok(1)
120 stdscr.scroll()
121 stdscr.scroll(2)
122 stdscr.scroll(-3)
124 stdscr.move(12, 2)
125 stdscr.setscrreg(10,15)
126 win3 = stdscr.subwin(10,10)
127 win3 = stdscr.subwin(10,10, 5,5)
128 stdscr.syncok(1)
129 stdscr.timeout(5)
130 stdscr.touchline(5,5)
131 stdscr.touchline(5,5,0)
132 stdscr.vline('a', 3)
133 stdscr.vline('a', 3, curses.A_STANDOUT)
134 stdscr.chgat(5, 2, 3, curses.A_BLINK)
135 stdscr.chgat(3, curses.A_BOLD)
136 stdscr.chgat(5, 8, curses.A_UNDERLINE)
137 stdscr.chgat(curses.A_BLINK)
138 stdscr.refresh()
140 stdscr.vline(1,1, 'a', 3)
141 stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
143 if hasattr(curses, 'resize'):
144 stdscr.resize()
145 if hasattr(curses, 'enclose'):
146 stdscr.enclose()
149 def module_funcs(stdscr):
150 "Test module-level functions"
152 for func in [curses.baudrate, curses.beep, curses.can_change_color,
153 curses.cbreak, curses.def_prog_mode, curses.doupdate,
154 curses.filter, curses.flash, curses.flushinp,
155 curses.has_colors, curses.has_ic, curses.has_il,
156 curses.isendwin, curses.killchar, curses.longname,
157 curses.nocbreak, curses.noecho, curses.nonl,
158 curses.noqiflush, curses.noraw,
159 curses.reset_prog_mode, curses.termattrs,
160 curses.termname, curses.erasechar, curses.getsyx]:
161 func()
163 # Functions that actually need arguments
164 if curses.tigetstr("cnorm"):
165 curses.curs_set(1)
166 curses.delay_output(1)
167 curses.echo() ; curses.echo(1)
169 f = tempfile.TemporaryFile()
170 stdscr.putwin(f)
171 f.seek(0)
172 curses.getwin(f)
173 f.close()
175 curses.halfdelay(1)
176 curses.intrflush(1)
177 curses.meta(1)
178 curses.napms(100)
179 curses.newpad(50,50)
180 win = curses.newwin(5,5)
181 win = curses.newwin(5,5, 1,1)
182 curses.nl() ; curses.nl(1)
183 curses.putp('abc')
184 curses.qiflush()
185 curses.raw() ; curses.raw(1)
186 curses.setsyx(5,5)
187 curses.tigetflag('hc')
188 curses.tigetnum('co')
189 curses.tigetstr('cr')
190 curses.tparm('cr')
191 curses.typeahead(sys.__stdin__.fileno())
192 curses.unctrl('a')
193 curses.ungetch('a')
194 curses.use_env(1)
196 # Functions only available on a few platforms
197 if curses.has_colors():
198 curses.start_color()
199 curses.init_pair(2, 1,1)
200 curses.color_content(1)
201 curses.color_pair(2)
202 curses.pair_content(curses.COLOR_PAIRS - 1)
203 curses.pair_number(0)
205 if hasattr(curses, 'use_default_colors'):
206 curses.use_default_colors()
208 if hasattr(curses, 'keyname'):
209 curses.keyname(13)
211 if hasattr(curses, 'has_key'):
212 curses.has_key(13)
214 if hasattr(curses, 'getmouse'):
215 (availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED)
216 # availmask indicates that mouse stuff not available.
217 if availmask != 0:
218 curses.mouseinterval(10)
219 # just verify these don't cause errors
220 m = curses.getmouse()
221 curses.ungetmouse(*m)
223 if hasattr(curses, 'is_term_resized'):
224 curses.is_term_resized(*stdscr.getmaxyx())
225 if hasattr(curses, 'resizeterm'):
226 curses.resizeterm(*stdscr.getmaxyx())
227 if hasattr(curses, 'resize_term'):
228 curses.resize_term(*stdscr.getmaxyx())
230 def unit_tests():
231 from curses import ascii
232 for ch, expected in [('a', 'a'), ('A', 'A'),
233 (';', ';'), (' ', ' '),
234 ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
235 # Meta-bit characters
236 ('\x8a', '!^J'), ('\xc1', '!A'),
238 if ascii.unctrl(ch) != expected:
239 print 'curses.unctrl fails on character', repr(ch)
242 def test_userptr_without_set(stdscr):
243 w = curses.newwin(10, 10)
244 p = curses.panel.new_panel(w)
245 # try to access userptr() before calling set_userptr() -- segfaults
246 try:
247 p.userptr()
248 raise RuntimeError, 'userptr should fail since not set'
249 except curses.panel.error:
250 pass
252 def test_resize_term(stdscr):
253 if hasattr(curses, 'resizeterm'):
254 lines, cols = curses.LINES, curses.COLS
255 curses.resizeterm(lines - 1, cols + 1)
257 if curses.LINES != lines - 1 or curses.COLS != cols + 1:
258 raise RuntimeError, "Expected resizeterm to update LINES and COLS"
260 def test_issue6243(stdscr):
261 curses.ungetch(1025)
262 stdscr.getkey()
264 def main(stdscr):
265 curses.savetty()
266 try:
267 module_funcs(stdscr)
268 window_funcs(stdscr)
269 test_userptr_without_set(stdscr)
270 test_resize_term(stdscr)
271 test_issue6243(stdscr)
272 finally:
273 curses.resetty()
275 if __name__ == '__main__':
276 curses.wrapper(main)
277 unit_tests()
278 else:
279 if not sys.__stdout__.isatty():
280 raise unittest.SkipTest("sys.__stdout__ is not a tty")
281 # testing setupterm() inside initscr/endwin
282 # causes terminal breakage
283 curses.setupterm(fd=sys.__stdout__.fileno())
284 try:
285 stdscr = curses.initscr()
286 main(stdscr)
287 finally:
288 curses.endwin()
289 unit_tests()