blob: 10ebe09243e25e96154bdb9597b9f55a5bb843f2 [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(),
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
Barry Warsaw04f357c2002-07-23 19:04:11 +000017from test import test_support
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000018test_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)]:
Raymond Hettingerff41c482003-04-06 09:01:11 +000029 meth(*args)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000030
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
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000184 if hasattr(curses, 'use_default_colors'):
185 curses.use_default_colors()
186
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000187 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
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000197def unit_tests():
198 from curses import ascii
199 for ch, expected in [('a', 'a'), ('A', 'A'),
200 (';', ';'), (' ', ' '),
Andrew M. Kuchlinge8792c12003-08-29 18:49:05 +0000201 ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
202 # Meta-bit characters
203 ('\x8a', '!^J'), ('\xc1', '!A'),
204 ]:
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000205 if ascii.unctrl(ch) != expected:
206 print 'curses.unctrl fails on character', repr(ch)
207
208
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000209
210def main(stdscr):
211 curses.savetty()
212 try:
213 module_funcs(stdscr)
214 window_funcs(stdscr)
215 finally:
216 curses.resetty()
217
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000218
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000219if __name__ == '__main__':
220 curses.wrapper(main)
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000221 unit_tests()
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000222else:
223 try:
224 stdscr = curses.initscr()
225 main(stdscr)
226 finally:
227 curses.endwin()
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000228
229 unit_tests()