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(),
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.
19 from test
.test_support
import requires
, import_module
21 curses
= import_module('curses')
22 curses
.panel
= import_module('curses.panel')
25 # XXX: if newterm was supported we could use it instead of initscr and not exit
26 term
= os
.environ
.get('TERM')
27 if not term
or term
== 'unknown':
28 raise unittest
.SkipTest
, "$TERM=%r, calling initscr() may cause exit" % term
30 if sys
.platform
== "cygwin":
31 raise unittest
.SkipTest("cygwin's curses mostly just hangs")
33 def window_funcs(stdscr
):
34 "Test the methods of windows"
35 win
= curses
.newwin(10,10)
36 win
= curses
.newwin(5,5, 5,5)
37 win2
= curses
.newwin(15,15, 5,5)
39 for meth
in [stdscr
.addch
, stdscr
.addstr
]:
40 for args
in [('a'), ('a', curses
.A_BOLD
),
41 (4,4, 'a'), (5,5, 'a', curses
.A_BOLD
)]:
44 for meth
in [stdscr
.box
, stdscr
.clear
, stdscr
.clrtobot
,
45 stdscr
.clrtoeol
, stdscr
.cursyncup
, stdscr
.delch
,
46 stdscr
.deleteln
, stdscr
.erase
, stdscr
.getbegyx
,
47 stdscr
.getbkgd
, stdscr
.getkey
, stdscr
.getmaxyx
,
48 stdscr
.getparyx
, stdscr
.getyx
, stdscr
.inch
,
49 stdscr
.insertln
, stdscr
.instr
, stdscr
.is_wintouched
,
50 win
.noutrefresh
, stdscr
.redrawwin
, stdscr
.refresh
,
51 stdscr
.standout
, stdscr
.standend
, stdscr
.syncdown
,
52 stdscr
.syncup
, stdscr
.touchwin
, stdscr
.untouchwin
]:
55 stdscr
.addnstr('1234', 3)
56 stdscr
.addnstr('1234', 3, curses
.A_BOLD
)
57 stdscr
.addnstr(4,4, '1234', 3)
58 stdscr
.addnstr(5,5, '1234', 3, curses
.A_BOLD
)
60 stdscr
.attron(curses
.A_BOLD
)
61 stdscr
.attroff(curses
.A_BOLD
)
62 stdscr
.attrset(curses
.A_BOLD
)
64 stdscr
.bkgd(' ', curses
.A_REVERSE
)
66 stdscr
.bkgdset(' ', curses
.A_REVERSE
)
68 win
.border(65, 66, 67, 68,
70 win
.border('|', '!', '-', '_',
73 win
.border(65, 66, 67, 68,
78 raise RuntimeError, "Expected win.border() to raise TypeError"
82 win4
= stdscr
.derwin(2,2)
83 win4
= stdscr
.derwin(1,1, 5,5)
87 stdscr
.echochar('a', curses
.A_BOLD
)
89 stdscr
.hline('-', 5, curses
.A_BOLD
)
90 stdscr
.hline(1,1,'-', 5)
91 stdscr
.hline(1,1,'-', 5, curses
.A_BOLD
)
98 stdscr
.insnstr('abc', 3)
99 stdscr
.insnstr('abc', 3, curses
.A_BOLD
)
100 stdscr
.insnstr(5, 5, 'abc', 3)
101 stdscr
.insnstr(5, 5, 'abc', 3, curses
.A_BOLD
)
104 stdscr
.insstr('def', curses
.A_BOLD
)
105 stdscr
.insstr(5, 5, 'def')
106 stdscr
.insstr(5, 5, 'def', curses
.A_BOLD
)
107 stdscr
.is_linetouched(0)
116 win2
.overlay(win
, 1, 2, 3, 3, 2, 1)
117 win2
.overwrite(win
, 1, 2, 3, 3, 2, 1)
126 stdscr
.setscrreg(10,15)
127 win3
= stdscr
.subwin(10,10)
128 win3
= stdscr
.subwin(10,10, 5,5)
131 stdscr
.touchline(5,5)
132 stdscr
.touchline(5,5,0)
134 stdscr
.vline('a', 3, curses
.A_STANDOUT
)
135 stdscr
.chgat(5, 2, 3, curses
.A_BLINK
)
136 stdscr
.chgat(3, curses
.A_BOLD
)
137 stdscr
.chgat(5, 8, curses
.A_UNDERLINE
)
138 stdscr
.chgat(curses
.A_BLINK
)
141 stdscr
.vline(1,1, 'a', 3)
142 stdscr
.vline(1,1, 'a', 3, curses
.A_STANDOUT
)
144 if hasattr(curses
, 'resize'):
146 if hasattr(curses
, 'enclose'):
150 def module_funcs(stdscr
):
151 "Test module-level functions"
153 for func
in [curses
.baudrate
, curses
.beep
, curses
.can_change_color
,
154 curses
.cbreak
, curses
.def_prog_mode
, curses
.doupdate
,
155 curses
.filter, curses
.flash
, curses
.flushinp
,
156 curses
.has_colors
, curses
.has_ic
, curses
.has_il
,
157 curses
.isendwin
, curses
.killchar
, curses
.longname
,
158 curses
.nocbreak
, curses
.noecho
, curses
.nonl
,
159 curses
.noqiflush
, curses
.noraw
,
160 curses
.reset_prog_mode
, curses
.termattrs
,
161 curses
.termname
, curses
.erasechar
, curses
.getsyx
]:
164 # Functions that actually need arguments
165 if curses
.tigetstr("cnorm"):
167 curses
.delay_output(1)
168 curses
.echo() ; curses
.echo(1)
170 f
= tempfile
.TemporaryFile()
181 win
= curses
.newwin(5,5)
182 win
= curses
.newwin(5,5, 1,1)
183 curses
.nl() ; curses
.nl(1)
186 curses
.raw() ; curses
.raw(1)
188 curses
.tigetflag('hc')
189 curses
.tigetnum('co')
190 curses
.tigetstr('cr')
192 curses
.typeahead(sys
.__stdin
__.fileno())
197 # Functions only available on a few platforms
198 if curses
.has_colors():
200 curses
.init_pair(2, 1,1)
201 curses
.color_content(1)
203 curses
.pair_content(curses
.COLOR_PAIRS
- 1)
204 curses
.pair_number(0)
206 if hasattr(curses
, 'use_default_colors'):
207 curses
.use_default_colors()
209 if hasattr(curses
, 'keyname'):
212 if hasattr(curses
, 'has_key'):
215 if hasattr(curses
, 'getmouse'):
216 (availmask
, oldmask
) = curses
.mousemask(curses
.BUTTON1_PRESSED
)
217 # availmask indicates that mouse stuff not available.
219 curses
.mouseinterval(10)
220 # just verify these don't cause errors
221 m
= curses
.getmouse()
222 curses
.ungetmouse(*m
)
224 if hasattr(curses
, 'is_term_resized'):
225 curses
.is_term_resized(*stdscr
.getmaxyx())
226 if hasattr(curses
, 'resizeterm'):
227 curses
.resizeterm(*stdscr
.getmaxyx())
228 if hasattr(curses
, 'resize_term'):
229 curses
.resize_term(*stdscr
.getmaxyx())
232 from curses
import ascii
233 for ch
, expected
in [('a', 'a'), ('A', 'A'),
234 (';', ';'), (' ', ' '),
235 ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
236 # Meta-bit characters
237 ('\x8a', '!^J'), ('\xc1', '!A'),
239 if ascii
.unctrl(ch
) != expected
:
240 print 'curses.unctrl fails on character', repr(ch
)
243 def test_userptr_without_set(stdscr
):
244 w
= curses
.newwin(10, 10)
245 p
= curses
.panel
.new_panel(w
)
246 # try to access userptr() before calling set_userptr() -- segfaults
249 raise RuntimeError, 'userptr should fail since not set'
250 except curses
.panel
.error
:
253 def test_resize_term(stdscr
):
254 if hasattr(curses
, 'resizeterm'):
255 lines
, cols
= curses
.LINES
, curses
.COLS
256 curses
.resizeterm(lines
- 1, cols
+ 1)
258 if curses
.LINES
!= lines
- 1 or curses
.COLS
!= cols
+ 1:
259 raise RuntimeError, "Expected resizeterm to update LINES and COLS"
261 def test_issue6243(stdscr
):
270 test_userptr_without_set(stdscr
)
271 test_resize_term(stdscr
)
272 test_issue6243(stdscr
)
276 if __name__
== '__main__':
280 if not sys
.__stdout
__.isatty():
281 raise unittest
.SkipTest("sys.__stdout__ is not a tty")
282 # testing setupterm() inside initscr/endwin
283 # causes terminal breakage
284 curses
.setupterm(fd
=sys
.__stdout
__.fileno())
286 stdscr
= curses
.initscr()