blob: a4a45a7302dfd7dbf9a1f4f5d152374573dc140e [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(),
Neal Norwitz88bbd732006-01-10 07:05:44 +00008# init_color()
9# Only called, not tested: getmouse(), ungetmouse()
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000010#
11
Neal Norwitzeeab7da2006-01-05 06:09:13 +000012import curses, sys, tempfile, os
Neal Norwitz5e3d8622006-01-09 06:24:35 +000013import curses.panel
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000014
15# Optionally test curses module. This currently requires that the
16# 'curses' resource be given on the regrtest command line using the -u
17# option. If not available, nothing after this line will be executed.
18
Neal Norwitz9f39f682006-01-06 04:18:21 +000019from test.test_support import requires, TestSkipped
20requires('curses')
21
22# XXX: if newterm was supported we could use it instead of initscr and not exit
23term = os.environ.get('TERM')
24if not term or term == 'unknown':
25 raise TestSkipped, "$TERM=%r, calling initscr() may cause exit" % term
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000026
27def window_funcs(stdscr):
28 "Test the methods of windows"
29 win = curses.newwin(10,10)
30 win = curses.newwin(5,5, 5,5)
31 win2 = curses.newwin(15,15, 5,5)
32
33 for meth in [stdscr.addch, stdscr.addstr]:
34 for args in [('a'), ('a', curses.A_BOLD),
35 (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]:
Raymond Hettingerff41c482003-04-06 09:01:11 +000036 meth(*args)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +000037
38 for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot,
39 stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch,
40 stdscr.deleteln, stdscr.erase, stdscr.getbegyx,
41 stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx,
42 stdscr.getparyx, stdscr.getyx, stdscr.inch,
43 stdscr.insertln, stdscr.instr, stdscr.is_wintouched,
44 win.noutrefresh, stdscr.redrawwin, stdscr.refresh,
45 stdscr.standout, stdscr.standend, stdscr.syncdown,
46 stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]:
47 meth()
48
49 stdscr.addnstr('1234', 3)
50 stdscr.addnstr('1234', 3, curses.A_BOLD)
51 stdscr.addnstr(4,4, '1234', 3)
52 stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD)
53
54 stdscr.attron(curses.A_BOLD)
55 stdscr.attroff(curses.A_BOLD)
56 stdscr.attrset(curses.A_BOLD)
57 stdscr.bkgd(' ')
58 stdscr.bkgd(' ', curses.A_REVERSE)
59 stdscr.bkgdset(' ')
60 stdscr.bkgdset(' ', curses.A_REVERSE)
61
62 win.border(65, 66, 67, 68,
63 69, 70, 71, 72)
64 win.border('|', '!', '-', '_',
65 '+', '\\', '#', '/')
66 try:
67 win.border(65, 66, 67, 68,
68 69, [], 71, 72)
69 except TypeError:
70 pass
71 else:
72 raise RuntimeError, "Expected win.border() to raise TypeError"
73
74 stdscr.clearok(1)
75
76 win4 = stdscr.derwin(2,2)
77 win4 = stdscr.derwin(1,1, 5,5)
78 win4.mvderwin(9,9)
79
80 stdscr.echochar('a')
81 stdscr.echochar('a', curses.A_BOLD)
82 stdscr.hline('-', 5)
83 stdscr.hline('-', 5, curses.A_BOLD)
84 stdscr.hline(1,1,'-', 5)
85 stdscr.hline(1,1,'-', 5, curses.A_BOLD)
86
87 stdscr.idcok(1)
88 stdscr.idlok(1)
89 stdscr.immedok(1)
90 stdscr.insch('c')
91 stdscr.insdelln(1)
92 stdscr.insnstr('abc', 3)
93 stdscr.insnstr('abc', 3, curses.A_BOLD)
94 stdscr.insnstr(5, 5, 'abc', 3)
95 stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD)
96
97 stdscr.insstr('def')
98 stdscr.insstr('def', curses.A_BOLD)
99 stdscr.insstr(5, 5, 'def')
100 stdscr.insstr(5, 5, 'def', curses.A_BOLD)
101 stdscr.is_linetouched(0)
102 stdscr.keypad(1)
103 stdscr.leaveok(1)
104 stdscr.move(3,3)
105 win.mvwin(2,2)
106 stdscr.nodelay(1)
107 stdscr.notimeout(1)
108 win2.overlay(win)
109 win2.overwrite(win)
Neal Norwitz88bbd732006-01-10 07:05:44 +0000110 win2.overlay(win, 1, 2, 3, 3, 2, 1)
111 win2.overwrite(win, 1, 2, 3, 3, 2, 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000112 stdscr.redrawln(1,2)
113
114 stdscr.scrollok(1)
115 stdscr.scroll()
116 stdscr.scroll(2)
117 stdscr.scroll(-3)
118
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000119 stdscr.move(12, 2)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000120 stdscr.setscrreg(10,15)
121 win3 = stdscr.subwin(10,10)
122 win3 = stdscr.subwin(10,10, 5,5)
123 stdscr.syncok(1)
124 stdscr.timeout(5)
125 stdscr.touchline(5,5)
126 stdscr.touchline(5,5,0)
127 stdscr.vline('a', 3)
128 stdscr.vline('a', 3, curses.A_STANDOUT)
129 stdscr.vline(1,1, 'a', 3)
130 stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
131
132 if hasattr(curses, 'resize'):
133 stdscr.resize()
134 if hasattr(curses, 'enclose'):
135 stdscr.enclose()
136
137
138def module_funcs(stdscr):
139 "Test module-level functions"
140
141 for func in [curses.baudrate, curses.beep, curses.can_change_color,
142 curses.cbreak, curses.def_prog_mode, curses.doupdate,
143 curses.filter, curses.flash, curses.flushinp,
144 curses.has_colors, curses.has_ic, curses.has_il,
145 curses.isendwin, curses.killchar, curses.longname,
146 curses.nocbreak, curses.noecho, curses.nonl,
147 curses.noqiflush, curses.noraw,
148 curses.reset_prog_mode, curses.termattrs,
149 curses.termname, curses.erasechar, curses.getsyx]:
150 func()
151
152 # Functions that actually need arguments
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000153 if curses.tigetstr("cnorm"):
154 curses.curs_set(1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000155 curses.delay_output(1)
156 curses.echo() ; curses.echo(1)
157
158 f = tempfile.TemporaryFile()
159 stdscr.putwin(f)
160 f.seek(0)
161 curses.getwin(f)
162 f.close()
163
164 curses.halfdelay(1)
165 curses.intrflush(1)
166 curses.meta(1)
167 curses.napms(100)
168 curses.newpad(50,50)
169 win = curses.newwin(5,5)
170 win = curses.newwin(5,5, 1,1)
171 curses.nl() ; curses.nl(1)
172 curses.putp('abc')
173 curses.qiflush()
174 curses.raw() ; curses.raw(1)
175 curses.setsyx(5,5)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000176 curses.tigetflag('hc')
177 curses.tigetnum('co')
178 curses.tigetstr('cr')
179 curses.tparm('cr')
180 curses.typeahead(sys.__stdin__.fileno())
181 curses.unctrl('a')
182 curses.ungetch('a')
183 curses.use_env(1)
184
185 # Functions only available on a few platforms
186 if curses.has_colors():
187 curses.start_color()
188 curses.init_pair(2, 1,1)
189 curses.color_content(1)
190 curses.color_pair(2)
Andrew M. Kuchlingd1badac2005-06-15 18:44:23 +0000191 curses.pair_content(curses.COLOR_PAIRS - 1)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000192 curses.pair_number(0)
193
Michael W. Hudson2b3feec2004-08-07 15:27:16 +0000194 if hasattr(curses, 'use_default_colors'):
195 curses.use_default_colors()
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000196
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000197 if hasattr(curses, 'keyname'):
198 curses.keyname(13)
199
200 if hasattr(curses, 'has_key'):
201 curses.has_key(13)
202
203 if hasattr(curses, 'getmouse'):
204 curses.mousemask(curses.BUTTON1_PRESSED)
205 curses.mouseinterval(10)
Neal Norwitz88bbd732006-01-10 07:05:44 +0000206 # just verify these don't cause errors
207 m = curses.getmouse()
208 curses.ungetmouse(*m)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000209
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000210def unit_tests():
211 from curses import ascii
212 for ch, expected in [('a', 'a'), ('A', 'A'),
213 (';', ';'), (' ', ' '),
Andrew M. Kuchlinge8792c12003-08-29 18:49:05 +0000214 ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
215 # Meta-bit characters
216 ('\x8a', '!^J'), ('\xc1', '!A'),
217 ]:
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000218 if ascii.unctrl(ch) != expected:
219 print 'curses.unctrl fails on character', repr(ch)
Tim Peters58eb11c2004-01-18 20:29:55 +0000220
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000221
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000222def test_userptr_without_set(stdscr):
223 w = curses.newwin(10, 10)
224 p = curses.panel.new_panel(w)
225 # try to access userptr() before calling set_userptr() -- segfaults
226 try:
227 p.userptr()
228 raise RuntimeError, 'userptr should fail since not set'
229 except curses.panel.error:
230 pass
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000231
232def main(stdscr):
233 curses.savetty()
234 try:
235 module_funcs(stdscr)
236 window_funcs(stdscr)
Neal Norwitz5e3d8622006-01-09 06:24:35 +0000237 test_userptr_without_set(stdscr)
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000238 finally:
239 curses.resetty()
240
241if __name__ == '__main__':
242 curses.wrapper(main)
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000243 unit_tests()
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000244else:
245 try:
Georg Brandl05f5ba92006-01-12 15:41:05 +0000246 # testing setupterm() inside initscr/endwin
247 # causes terminal breakage
248 curses.setupterm(fd=sys.__stdout__.fileno())
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000249 stdscr = curses.initscr()
250 main(stdscr)
251 finally:
252 curses.endwin()
Andrew M. Kuchlinge752e202003-08-29 18:37:37 +0000253
254 unit_tests()