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')
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
)]:
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
]:
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
)
63 stdscr
.bkgd(' ', curses
.A_REVERSE
)
65 stdscr
.bkgdset(' ', curses
.A_REVERSE
)
67 win
.border(65, 66, 67, 68,
69 win
.border('|', '!', '-', '_',
72 win
.border(65, 66, 67, 68,
77 raise RuntimeError, "Expected win.border() to raise TypeError"
81 win4
= stdscr
.derwin(2,2)
82 win4
= stdscr
.derwin(1,1, 5,5)
86 stdscr
.echochar('a', curses
.A_BOLD
)
88 stdscr
.hline('-', 5, curses
.A_BOLD
)
89 stdscr
.hline(1,1,'-', 5)
90 stdscr
.hline(1,1,'-', 5, curses
.A_BOLD
)
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
)
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)
115 win2
.overlay(win
, 1, 2, 3, 3, 2, 1)
116 win2
.overwrite(win
, 1, 2, 3, 3, 2, 1)
125 stdscr
.setscrreg(10,15)
126 win3
= stdscr
.subwin(10,10)
127 win3
= stdscr
.subwin(10,10, 5,5)
130 stdscr
.touchline(5,5)
131 stdscr
.touchline(5,5,0)
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
)
140 stdscr
.vline(1,1, 'a', 3)
141 stdscr
.vline(1,1, 'a', 3, curses
.A_STANDOUT
)
143 if hasattr(curses
, 'resize'):
145 if hasattr(curses
, '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
]:
163 # Functions that actually need arguments
164 if curses
.tigetstr("cnorm"):
166 curses
.delay_output(1)
167 curses
.echo() ; curses
.echo(1)
169 f
= tempfile
.TemporaryFile()
180 win
= curses
.newwin(5,5)
181 win
= curses
.newwin(5,5, 1,1)
182 curses
.nl() ; curses
.nl(1)
185 curses
.raw() ; curses
.raw(1)
187 curses
.tigetflag('hc')
188 curses
.tigetnum('co')
189 curses
.tigetstr('cr')
191 curses
.typeahead(sys
.__stdin
__.fileno())
196 # Functions only available on a few platforms
197 if curses
.has_colors():
199 curses
.init_pair(2, 1,1)
200 curses
.color_content(1)
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'):
211 if hasattr(curses
, 'has_key'):
214 if hasattr(curses
, 'getmouse'):
215 (availmask
, oldmask
) = curses
.mousemask(curses
.BUTTON1_PRESSED
)
216 # availmask indicates that mouse stuff not available.
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())
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
248 raise RuntimeError, 'userptr should fail since not set'
249 except curses
.panel
.error
:
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
):
269 test_userptr_without_set(stdscr
)
270 test_resize_term(stdscr
)
271 test_issue6243(stdscr
)
275 if __name__
== '__main__':
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())
285 stdscr
= curses
.initscr()