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