test_curses.py revision 69f31eb80c86f74cf35ae313870f87b6611293fa
1#
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(),
8# getmouse(), ungetmouse(), init_color()
9#
10
11import curses, sys, tempfile
12
13# Optionally test curses module.  This currently requires that the
14# 'curses' resource be given on the regrtest command line using the -u
15# option.  If not available, nothing after this line will be executed.
16
17from test import test_support
18test_support.requires('curses')
19
20def window_funcs(stdscr):
21    "Test the methods of windows"
22    win = curses.newwin(10,10)
23    win = curses.newwin(5,5, 5,5)
24    win2 = curses.newwin(15,15, 5,5)
25
26    for meth in [stdscr.addch, stdscr.addstr]:
27        for args in [('a'), ('a', curses.A_BOLD),
28                     (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]:
29            meth(*args)
30
31    for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot,
32                 stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch,
33                 stdscr.deleteln, stdscr.erase, stdscr.getbegyx,
34                 stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx,
35                 stdscr.getparyx, stdscr.getyx, stdscr.inch,
36                 stdscr.insertln, stdscr.instr, stdscr.is_wintouched,
37                 win.noutrefresh, stdscr.redrawwin, stdscr.refresh,
38                 stdscr.standout, stdscr.standend, stdscr.syncdown,
39                 stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]:
40        meth()
41
42    stdscr.addnstr('1234', 3)
43    stdscr.addnstr('1234', 3, curses.A_BOLD)
44    stdscr.addnstr(4,4, '1234', 3)
45    stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD)
46
47    stdscr.attron(curses.A_BOLD)
48    stdscr.attroff(curses.A_BOLD)
49    stdscr.attrset(curses.A_BOLD)
50    stdscr.bkgd(' ')
51    stdscr.bkgd(' ', curses.A_REVERSE)
52    stdscr.bkgdset(' ')
53    stdscr.bkgdset(' ', curses.A_REVERSE)
54
55    win.border(65, 66, 67, 68,
56               69, 70, 71, 72)
57    win.border('|', '!', '-', '_',
58               '+', '\\', '#', '/')
59    try:
60        win.border(65, 66, 67, 68,
61                   69, [], 71, 72)
62    except TypeError:
63        pass
64    else:
65        raise RuntimeError, "Expected win.border() to raise TypeError"
66
67    stdscr.clearok(1)
68
69    win4 = stdscr.derwin(2,2)
70    win4 = stdscr.derwin(1,1, 5,5)
71    win4.mvderwin(9,9)
72
73    stdscr.echochar('a')
74    stdscr.echochar('a', curses.A_BOLD)
75    stdscr.hline('-', 5)
76    stdscr.hline('-', 5, curses.A_BOLD)
77    stdscr.hline(1,1,'-', 5)
78    stdscr.hline(1,1,'-', 5, curses.A_BOLD)
79
80    stdscr.idcok(1)
81    stdscr.idlok(1)
82    stdscr.immedok(1)
83    stdscr.insch('c')
84    stdscr.insdelln(1)
85    stdscr.insnstr('abc', 3)
86    stdscr.insnstr('abc', 3, curses.A_BOLD)
87    stdscr.insnstr(5, 5, 'abc', 3)
88    stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD)
89
90    stdscr.insstr('def')
91    stdscr.insstr('def', curses.A_BOLD)
92    stdscr.insstr(5, 5, 'def')
93    stdscr.insstr(5, 5, 'def', curses.A_BOLD)
94    stdscr.is_linetouched(0)
95    stdscr.keypad(1)
96    stdscr.leaveok(1)
97    stdscr.move(3,3)
98    win.mvwin(2,2)
99    stdscr.nodelay(1)
100    stdscr.notimeout(1)
101    win2.overlay(win)
102    win2.overwrite(win)
103    stdscr.redrawln(1,2)
104
105    stdscr.scrollok(1)
106    stdscr.scroll()
107    stdscr.scroll(2)
108    stdscr.scroll(-3)
109
110    stdscr.setscrreg(10,15)
111    win3 = stdscr.subwin(10,10)
112    win3 = stdscr.subwin(10,10, 5,5)
113    stdscr.syncok(1)
114    stdscr.timeout(5)
115    stdscr.touchline(5,5)
116    stdscr.touchline(5,5,0)
117    stdscr.vline('a', 3)
118    stdscr.vline('a', 3, curses.A_STANDOUT)
119    stdscr.vline(1,1, 'a', 3)
120    stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
121
122    if hasattr(curses, 'resize'):
123        stdscr.resize()
124    if hasattr(curses, 'enclose'):
125        stdscr.enclose()
126
127
128def module_funcs(stdscr):
129    "Test module-level functions"
130
131    for func in [curses.baudrate, curses.beep, curses.can_change_color,
132                 curses.cbreak, curses.def_prog_mode, curses.doupdate,
133                 curses.filter, curses.flash, curses.flushinp,
134                 curses.has_colors, curses.has_ic, curses.has_il,
135                 curses.isendwin, curses.killchar, curses.longname,
136                 curses.nocbreak, curses.noecho, curses.nonl,
137                 curses.noqiflush, curses.noraw,
138                 curses.reset_prog_mode, curses.termattrs,
139                 curses.termname, curses.erasechar, curses.getsyx]:
140        func()
141
142    # Functions that actually need arguments
143    curses.curs_set(1)
144    curses.delay_output(1)
145    curses.echo() ; curses.echo(1)
146
147    f = tempfile.TemporaryFile()
148    stdscr.putwin(f)
149    f.seek(0)
150    curses.getwin(f)
151    f.close()
152
153    curses.halfdelay(1)
154    curses.intrflush(1)
155    curses.meta(1)
156    curses.napms(100)
157    curses.newpad(50,50)
158    win = curses.newwin(5,5)
159    win = curses.newwin(5,5, 1,1)
160    curses.nl() ; curses.nl(1)
161    curses.putp('abc')
162    curses.qiflush()
163    curses.raw() ; curses.raw(1)
164    curses.setsyx(5,5)
165    curses.setupterm(fd=sys.__stdout__.fileno())
166    curses.tigetflag('hc')
167    curses.tigetnum('co')
168    curses.tigetstr('cr')
169    curses.tparm('cr')
170    curses.typeahead(sys.__stdin__.fileno())
171    curses.unctrl('a')
172    curses.ungetch('a')
173    curses.use_env(1)
174
175    # Functions only available on a few platforms
176    if curses.has_colors():
177        curses.start_color()
178        curses.init_pair(2, 1,1)
179        curses.color_content(1)
180        curses.color_pair(2)
181        curses.pair_content(curses.COLOR_PAIRS)
182        curses.pair_number(0)
183
184    if hasattr(curses, 'use_default_colors'):
185        curses.use_default_colors()
186
187    if hasattr(curses, 'keyname'):
188        curses.keyname(13)
189
190    if hasattr(curses, 'has_key'):
191        curses.has_key(13)
192
193    if hasattr(curses, 'getmouse'):
194        curses.mousemask(curses.BUTTON1_PRESSED)
195        curses.mouseinterval(10)
196
197
198def main(stdscr):
199    curses.savetty()
200    try:
201        module_funcs(stdscr)
202        window_funcs(stdscr)
203    finally:
204        curses.resetty()
205
206if __name__ == '__main__':
207    curses.wrapper(main)
208else:
209    try:
210        stdscr = curses.initscr()
211        main(stdscr)
212    finally:
213        curses.endwin()
214