blob: e137d318a8b16f76b9074182f62ccf606d3b9f3e [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
Neal Norwitzeeab7da2006-01-05 06:09:13 +000011import curses, sys, tempfile, os
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000012
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
Neal Norwitz9f39f682006-01-06 04:18:21 +000017from test.test_support import requires, TestSkipped
18requires('curses')
19
20# XXX: if newterm was supported we could use it instead of initscr and not exit
21term = os.environ.get('TERM')
22if not term or term == 'unknown':
23 raise TestSkipped, "$TERM=%r, calling initscr() may cause exit" % term
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000024
25def window_funcs(stdscr):
26 "Test the methods of windows"
27 win = curses.newwin(10,10)
28 win = curses.newwin(5,5, 5,5)
29 win2 = curses.newwin(15,15, 5,5)
30
31 for meth in [stdscr.addch, stdscr.addstr]:
32 for args in [('a'), ('a', curses.A_BOLD),
33 (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]:
Raymond Hettingerff41c482003-04-06 09:01:11 +000034 meth(*args)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000035
36 for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot,
37 stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch,
38 stdscr.deleteln, stdscr.erase, stdscr.getbegyx,
39 stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx,
40 stdscr.getparyx, stdscr.getyx, stdscr.inch,
41 stdscr.insertln, stdscr.instr, stdscr.is_wintouched,
42 win.noutrefresh, stdscr.redrawwin, stdscr.refresh,
43 stdscr.standout, stdscr.standend, stdscr.syncdown,
44 stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]:
45 meth()
46
47 stdscr.addnstr('1234', 3)
48 stdscr.addnstr('1234', 3, curses.A_BOLD)
49 stdscr.addnstr(4,4, '1234', 3)
50 stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD)
51
52 stdscr.attron(curses.A_BOLD)
53 stdscr.attroff(curses.A_BOLD)
54 stdscr.attrset(curses.A_BOLD)
55 stdscr.bkgd(' ')
56 stdscr.bkgd(' ', curses.A_REVERSE)
57 stdscr.bkgdset(' ')
58 stdscr.bkgdset(' ', curses.A_REVERSE)
59
60 win.border(65, 66, 67, 68,
61 69, 70, 71, 72)
62 win.border('|', '!', '-', '_',
63 '+', '\\', '#', '/')
64 try:
65 win.border(65, 66, 67, 68,
66 69, [], 71, 72)
67 except TypeError:
68 pass
69 else:
70 raise RuntimeError, "Expected win.border() to raise TypeError"
71
72 stdscr.clearok(1)
73
74 win4 = stdscr.derwin(2,2)
75 win4 = stdscr.derwin(1,1, 5,5)
76 win4.mvderwin(9,9)
77
78 stdscr.echochar('a')
79 stdscr.echochar('a', curses.A_BOLD)
80 stdscr.hline('-', 5)
81 stdscr.hline('-', 5, curses.A_BOLD)
82 stdscr.hline(1,1,'-', 5)
83 stdscr.hline(1,1,'-', 5, curses.A_BOLD)
84
85 stdscr.idcok(1)
86 stdscr.idlok(1)
87 stdscr.immedok(1)
88 stdscr.insch('c')
89 stdscr.insdelln(1)
90 stdscr.insnstr('abc', 3)
91 stdscr.insnstr('abc', 3, curses.A_BOLD)
92 stdscr.insnstr(5, 5, 'abc', 3)
93 stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD)
94
95 stdscr.insstr('def')
96 stdscr.insstr('def', curses.A_BOLD)
97 stdscr.insstr(5, 5, 'def')
98 stdscr.insstr(5, 5, 'def', curses.A_BOLD)
99 stdscr.is_linetouched(0)
100 stdscr.keypad(1)
101 stdscr.leaveok(1)
102 stdscr.move(3,3)
103 win.mvwin(2,2)
104 stdscr.nodelay(1)
105 stdscr.notimeout(1)
106 win2.overlay(win)
107 win2.overwrite(win)
108 stdscr.redrawln(1,2)
109
110 stdscr.scrollok(1)
111 stdscr.scroll()
112 stdscr.scroll(2)
113 stdscr.scroll(-3)
114
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000115 stdscr.move(12, 2)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000116 stdscr.setscrreg(10,15)
117 win3 = stdscr.subwin(10,10)
118 win3 = stdscr.subwin(10,10, 5,5)
119 stdscr.syncok(1)
120 stdscr.timeout(5)
121 stdscr.touchline(5,5)
122 stdscr.touchline(5,5,0)
123 stdscr.vline('a', 3)
124 stdscr.vline('a', 3, curses.A_STANDOUT)
125 stdscr.vline(1,1, 'a', 3)
126 stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
127
128 if hasattr(curses, 'resize'):
129 stdscr.resize()
130 if hasattr(curses, 'enclose'):
131 stdscr.enclose()
132
133
134def module_funcs(stdscr):
135 "Test module-level functions"
136
137 for func in [curses.baudrate, curses.beep, curses.can_change_color,
138 curses.cbreak, curses.def_prog_mode, curses.doupdate,
139 curses.filter, curses.flash, curses.flushinp,
140 curses.has_colors, curses.has_ic, curses.has_il,
141 curses.isendwin, curses.killchar, curses.longname,
142 curses.nocbreak, curses.noecho, curses.nonl,
143 curses.noqiflush, curses.noraw,
144 curses.reset_prog_mode, curses.termattrs,
145 curses.termname, curses.erasechar, curses.getsyx]:
146 func()
147
148 # Functions that actually need arguments
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000149 if curses.tigetstr("cnorm"):
150 curses.curs_set(1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000151 curses.delay_output(1)
152 curses.echo() ; curses.echo(1)
153
154 f = tempfile.TemporaryFile()
155 stdscr.putwin(f)
156 f.seek(0)
157 curses.getwin(f)
158 f.close()
159
160 curses.halfdelay(1)
161 curses.intrflush(1)
162 curses.meta(1)
163 curses.napms(100)
164 curses.newpad(50,50)
165 win = curses.newwin(5,5)
166 win = curses.newwin(5,5, 1,1)
167 curses.nl() ; curses.nl(1)
168 curses.putp('abc')
169 curses.qiflush()
170 curses.raw() ; curses.raw(1)
171 curses.setsyx(5,5)
172 curses.setupterm(fd=sys.__stdout__.fileno())
173 curses.tigetflag('hc')
174 curses.tigetnum('co')
175 curses.tigetstr('cr')
176 curses.tparm('cr')
177 curses.typeahead(sys.__stdin__.fileno())
178 curses.unctrl('a')
179 curses.ungetch('a')
180 curses.use_env(1)
181
182 # Functions only available on a few platforms
183 if curses.has_colors():
184 curses.start_color()
185 curses.init_pair(2, 1,1)
186 curses.color_content(1)
187 curses.color_pair(2)
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000188 curses.pair_content(curses.COLOR_PAIRS - 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000189 curses.pair_number(0)
190
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000191 if hasattr(curses, 'use_default_colors'):
192 curses.use_default_colors()
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000193
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000194 if hasattr(curses, 'keyname'):
195 curses.keyname(13)
196
197 if hasattr(curses, 'has_key'):
198 curses.has_key(13)
199
200 if hasattr(curses, 'getmouse'):
201 curses.mousemask(curses.BUTTON1_PRESSED)
202 curses.mouseinterval(10)
203
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000204def unit_tests():
205 from curses import ascii
206 for ch, expected in [('a', 'a'), ('A', 'A'),
207 (';', ';'), (' ', ' '),
Andrew M. Kuchlinge8792c12003-08-29 18:49:05 +0000208 ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
209 # Meta-bit characters
210 ('\x8a', '!^J'), ('\xc1', '!A'),
211 ]:
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000212 if ascii.unctrl(ch) != expected:
213 print 'curses.unctrl fails on character', repr(ch)
Tim Peters58eb11c2004-01-18 20:29:55 +0000214
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000215
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000216
217def main(stdscr):
218 curses.savetty()
219 try:
220 module_funcs(stdscr)
221 window_funcs(stdscr)
222 finally:
223 curses.resetty()
224
Tim Peters58eb11c2004-01-18 20:29:55 +0000225
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000226if __name__ == '__main__':
227 curses.wrapper(main)
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000228 unit_tests()
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000229else:
230 try:
231 stdscr = curses.initscr()
232 main(stdscr)
233 finally:
234 curses.endwin()
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000235
236 unit_tests()