1 | n/a | # |
---|
2 | n/a | # Test script for the curses module |
---|
3 | n/a | # |
---|
4 | n/a | # This script doesn't actually display anything very coherent. but it |
---|
5 | n/a | # does call (nearly) every method and function. |
---|
6 | n/a | # |
---|
7 | n/a | # Functions not tested: {def,reset}_{shell,prog}_mode, getch(), getstr(), |
---|
8 | n/a | # init_color() |
---|
9 | n/a | # Only called, not tested: getmouse(), ungetmouse() |
---|
10 | n/a | # |
---|
11 | n/a | |
---|
12 | n/a | import os |
---|
13 | n/a | import string |
---|
14 | n/a | import sys |
---|
15 | n/a | import tempfile |
---|
16 | n/a | import unittest |
---|
17 | n/a | |
---|
18 | n/a | from test.support import requires, import_module, verbose |
---|
19 | n/a | |
---|
20 | n/a | # Optionally test curses module. This currently requires that the |
---|
21 | n/a | # 'curses' resource be given on the regrtest command line using the -u |
---|
22 | n/a | # option. If not available, nothing after this line will be executed. |
---|
23 | n/a | import inspect |
---|
24 | n/a | requires('curses') |
---|
25 | n/a | |
---|
26 | n/a | # If either of these don't exist, skip the tests. |
---|
27 | n/a | curses = import_module('curses') |
---|
28 | n/a | import_module('curses.panel') |
---|
29 | n/a | import_module('curses.ascii') |
---|
30 | n/a | import_module('curses.textpad') |
---|
31 | n/a | |
---|
32 | n/a | def requires_curses_func(name): |
---|
33 | n/a | return unittest.skipUnless(hasattr(curses, name), |
---|
34 | n/a | 'requires curses.%s' % name) |
---|
35 | n/a | |
---|
36 | n/a | term = os.environ.get('TERM') |
---|
37 | n/a | |
---|
38 | n/a | # If newterm was supported we could use it instead of initscr and not exit |
---|
39 | n/a | @unittest.skipIf(not term or term == 'unknown', |
---|
40 | n/a | "$TERM=%r, calling initscr() may cause exit" % term) |
---|
41 | n/a | @unittest.skipIf(sys.platform == "cygwin", |
---|
42 | n/a | "cygwin's curses mostly just hangs") |
---|
43 | n/a | class TestCurses(unittest.TestCase): |
---|
44 | n/a | |
---|
45 | n/a | @classmethod |
---|
46 | n/a | def setUpClass(cls): |
---|
47 | n/a | if not sys.__stdout__.isatty(): |
---|
48 | n/a | # Temporary skip tests on non-tty |
---|
49 | n/a | raise unittest.SkipTest('sys.__stdout__ is not a tty') |
---|
50 | n/a | cls.tmp = tempfile.TemporaryFile() |
---|
51 | n/a | fd = cls.tmp.fileno() |
---|
52 | n/a | else: |
---|
53 | n/a | cls.tmp = None |
---|
54 | n/a | fd = sys.__stdout__.fileno() |
---|
55 | n/a | # testing setupterm() inside initscr/endwin |
---|
56 | n/a | # causes terminal breakage |
---|
57 | n/a | curses.setupterm(fd=fd) |
---|
58 | n/a | |
---|
59 | n/a | @classmethod |
---|
60 | n/a | def tearDownClass(cls): |
---|
61 | n/a | if cls.tmp: |
---|
62 | n/a | cls.tmp.close() |
---|
63 | n/a | del cls.tmp |
---|
64 | n/a | |
---|
65 | n/a | def setUp(self): |
---|
66 | n/a | if verbose: |
---|
67 | n/a | # just to make the test output a little more readable |
---|
68 | n/a | print() |
---|
69 | n/a | self.stdscr = curses.initscr() |
---|
70 | n/a | curses.savetty() |
---|
71 | n/a | |
---|
72 | n/a | def tearDown(self): |
---|
73 | n/a | curses.resetty() |
---|
74 | n/a | curses.endwin() |
---|
75 | n/a | |
---|
76 | n/a | def test_window_funcs(self): |
---|
77 | n/a | "Test the methods of windows" |
---|
78 | n/a | stdscr = self.stdscr |
---|
79 | n/a | win = curses.newwin(10,10) |
---|
80 | n/a | win = curses.newwin(5,5, 5,5) |
---|
81 | n/a | win2 = curses.newwin(15,15, 5,5) |
---|
82 | n/a | |
---|
83 | n/a | for meth in [stdscr.addch, stdscr.addstr]: |
---|
84 | n/a | for args in [('a'), ('a', curses.A_BOLD), |
---|
85 | n/a | (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]: |
---|
86 | n/a | with self.subTest(meth=meth.__qualname__, args=args): |
---|
87 | n/a | meth(*args) |
---|
88 | n/a | |
---|
89 | n/a | for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot, |
---|
90 | n/a | stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch, |
---|
91 | n/a | stdscr.deleteln, stdscr.erase, stdscr.getbegyx, |
---|
92 | n/a | stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx, |
---|
93 | n/a | stdscr.getparyx, stdscr.getyx, stdscr.inch, |
---|
94 | n/a | stdscr.insertln, stdscr.instr, stdscr.is_wintouched, |
---|
95 | n/a | win.noutrefresh, stdscr.redrawwin, stdscr.refresh, |
---|
96 | n/a | stdscr.standout, stdscr.standend, stdscr.syncdown, |
---|
97 | n/a | stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]: |
---|
98 | n/a | with self.subTest(meth=meth.__qualname__): |
---|
99 | n/a | meth() |
---|
100 | n/a | |
---|
101 | n/a | stdscr.addnstr('1234', 3) |
---|
102 | n/a | stdscr.addnstr('1234', 3, curses.A_BOLD) |
---|
103 | n/a | stdscr.addnstr(4,4, '1234', 3) |
---|
104 | n/a | stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD) |
---|
105 | n/a | |
---|
106 | n/a | stdscr.attron(curses.A_BOLD) |
---|
107 | n/a | stdscr.attroff(curses.A_BOLD) |
---|
108 | n/a | stdscr.attrset(curses.A_BOLD) |
---|
109 | n/a | stdscr.bkgd(' ') |
---|
110 | n/a | stdscr.bkgd(' ', curses.A_REVERSE) |
---|
111 | n/a | stdscr.bkgdset(' ') |
---|
112 | n/a | stdscr.bkgdset(' ', curses.A_REVERSE) |
---|
113 | n/a | |
---|
114 | n/a | win.border(65, 66, 67, 68, |
---|
115 | n/a | 69, 70, 71, 72) |
---|
116 | n/a | win.border('|', '!', '-', '_', |
---|
117 | n/a | '+', '\\', '#', '/') |
---|
118 | n/a | with self.assertRaises(TypeError, |
---|
119 | n/a | msg="Expected win.border() to raise TypeError"): |
---|
120 | n/a | win.border(65, 66, 67, 68, |
---|
121 | n/a | 69, [], 71, 72) |
---|
122 | n/a | |
---|
123 | n/a | stdscr.clearok(1) |
---|
124 | n/a | |
---|
125 | n/a | win4 = stdscr.derwin(2,2) |
---|
126 | n/a | win4 = stdscr.derwin(1,1, 5,5) |
---|
127 | n/a | win4.mvderwin(9,9) |
---|
128 | n/a | |
---|
129 | n/a | stdscr.echochar('a') |
---|
130 | n/a | stdscr.echochar('a', curses.A_BOLD) |
---|
131 | n/a | stdscr.hline('-', 5) |
---|
132 | n/a | stdscr.hline('-', 5, curses.A_BOLD) |
---|
133 | n/a | stdscr.hline(1,1,'-', 5) |
---|
134 | n/a | stdscr.hline(1,1,'-', 5, curses.A_BOLD) |
---|
135 | n/a | |
---|
136 | n/a | stdscr.idcok(1) |
---|
137 | n/a | stdscr.idlok(1) |
---|
138 | n/a | stdscr.immedok(1) |
---|
139 | n/a | stdscr.insch('c') |
---|
140 | n/a | stdscr.insdelln(1) |
---|
141 | n/a | stdscr.insnstr('abc', 3) |
---|
142 | n/a | stdscr.insnstr('abc', 3, curses.A_BOLD) |
---|
143 | n/a | stdscr.insnstr(5, 5, 'abc', 3) |
---|
144 | n/a | stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD) |
---|
145 | n/a | |
---|
146 | n/a | stdscr.insstr('def') |
---|
147 | n/a | stdscr.insstr('def', curses.A_BOLD) |
---|
148 | n/a | stdscr.insstr(5, 5, 'def') |
---|
149 | n/a | stdscr.insstr(5, 5, 'def', curses.A_BOLD) |
---|
150 | n/a | stdscr.is_linetouched(0) |
---|
151 | n/a | stdscr.keypad(1) |
---|
152 | n/a | stdscr.leaveok(1) |
---|
153 | n/a | stdscr.move(3,3) |
---|
154 | n/a | win.mvwin(2,2) |
---|
155 | n/a | stdscr.nodelay(1) |
---|
156 | n/a | stdscr.notimeout(1) |
---|
157 | n/a | win2.overlay(win) |
---|
158 | n/a | win2.overwrite(win) |
---|
159 | n/a | win2.overlay(win, 1, 2, 2, 1, 3, 3) |
---|
160 | n/a | win2.overwrite(win, 1, 2, 2, 1, 3, 3) |
---|
161 | n/a | stdscr.redrawln(1,2) |
---|
162 | n/a | |
---|
163 | n/a | stdscr.scrollok(1) |
---|
164 | n/a | stdscr.scroll() |
---|
165 | n/a | stdscr.scroll(2) |
---|
166 | n/a | stdscr.scroll(-3) |
---|
167 | n/a | |
---|
168 | n/a | stdscr.move(12, 2) |
---|
169 | n/a | stdscr.setscrreg(10,15) |
---|
170 | n/a | win3 = stdscr.subwin(10,10) |
---|
171 | n/a | win3 = stdscr.subwin(10,10, 5,5) |
---|
172 | n/a | stdscr.syncok(1) |
---|
173 | n/a | stdscr.timeout(5) |
---|
174 | n/a | stdscr.touchline(5,5) |
---|
175 | n/a | stdscr.touchline(5,5,0) |
---|
176 | n/a | stdscr.vline('a', 3) |
---|
177 | n/a | stdscr.vline('a', 3, curses.A_STANDOUT) |
---|
178 | n/a | stdscr.chgat(5, 2, 3, curses.A_BLINK) |
---|
179 | n/a | stdscr.chgat(3, curses.A_BOLD) |
---|
180 | n/a | stdscr.chgat(5, 8, curses.A_UNDERLINE) |
---|
181 | n/a | stdscr.chgat(curses.A_BLINK) |
---|
182 | n/a | stdscr.refresh() |
---|
183 | n/a | |
---|
184 | n/a | stdscr.vline(1,1, 'a', 3) |
---|
185 | n/a | stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT) |
---|
186 | n/a | |
---|
187 | n/a | if hasattr(curses, 'resize'): |
---|
188 | n/a | stdscr.resize() |
---|
189 | n/a | if hasattr(curses, 'enclose'): |
---|
190 | n/a | stdscr.enclose() |
---|
191 | n/a | |
---|
192 | n/a | self.assertRaises(ValueError, stdscr.getstr, -400) |
---|
193 | n/a | self.assertRaises(ValueError, stdscr.getstr, 2, 3, -400) |
---|
194 | n/a | self.assertRaises(ValueError, stdscr.instr, -2) |
---|
195 | n/a | self.assertRaises(ValueError, stdscr.instr, 2, 3, -2) |
---|
196 | n/a | |
---|
197 | n/a | |
---|
198 | n/a | def test_module_funcs(self): |
---|
199 | n/a | "Test module-level functions" |
---|
200 | n/a | for func in [curses.baudrate, curses.beep, curses.can_change_color, |
---|
201 | n/a | curses.cbreak, curses.def_prog_mode, curses.doupdate, |
---|
202 | n/a | curses.filter, curses.flash, curses.flushinp, |
---|
203 | n/a | curses.has_colors, curses.has_ic, curses.has_il, |
---|
204 | n/a | curses.isendwin, curses.killchar, curses.longname, |
---|
205 | n/a | curses.nocbreak, curses.noecho, curses.nonl, |
---|
206 | n/a | curses.noqiflush, curses.noraw, |
---|
207 | n/a | curses.reset_prog_mode, curses.termattrs, |
---|
208 | n/a | curses.termname, curses.erasechar, curses.getsyx]: |
---|
209 | n/a | with self.subTest(func=func.__qualname__): |
---|
210 | n/a | func() |
---|
211 | n/a | |
---|
212 | n/a | # Functions that actually need arguments |
---|
213 | n/a | if curses.tigetstr("cnorm"): |
---|
214 | n/a | curses.curs_set(1) |
---|
215 | n/a | curses.delay_output(1) |
---|
216 | n/a | curses.echo() ; curses.echo(1) |
---|
217 | n/a | |
---|
218 | n/a | with tempfile.TemporaryFile() as f: |
---|
219 | n/a | self.stdscr.putwin(f) |
---|
220 | n/a | f.seek(0) |
---|
221 | n/a | curses.getwin(f) |
---|
222 | n/a | |
---|
223 | n/a | curses.halfdelay(1) |
---|
224 | n/a | curses.intrflush(1) |
---|
225 | n/a | curses.meta(1) |
---|
226 | n/a | curses.napms(100) |
---|
227 | n/a | curses.newpad(50,50) |
---|
228 | n/a | win = curses.newwin(5,5) |
---|
229 | n/a | win = curses.newwin(5,5, 1,1) |
---|
230 | n/a | curses.nl() ; curses.nl(1) |
---|
231 | n/a | curses.putp(b'abc') |
---|
232 | n/a | curses.qiflush() |
---|
233 | n/a | curses.raw() ; curses.raw(1) |
---|
234 | n/a | curses.setsyx(5,5) |
---|
235 | n/a | curses.tigetflag('hc') |
---|
236 | n/a | curses.tigetnum('co') |
---|
237 | n/a | curses.tigetstr('cr') |
---|
238 | n/a | curses.tparm(b'cr') |
---|
239 | n/a | curses.typeahead(sys.__stdin__.fileno()) |
---|
240 | n/a | curses.unctrl('a') |
---|
241 | n/a | curses.ungetch('a') |
---|
242 | n/a | curses.use_env(1) |
---|
243 | n/a | |
---|
244 | n/a | # Functions only available on a few platforms |
---|
245 | n/a | def test_colors_funcs(self): |
---|
246 | n/a | if not curses.has_colors(): |
---|
247 | n/a | self.skipTest('requires colors support') |
---|
248 | n/a | curses.start_color() |
---|
249 | n/a | curses.init_pair(2, 1,1) |
---|
250 | n/a | curses.color_content(1) |
---|
251 | n/a | curses.color_pair(2) |
---|
252 | n/a | curses.pair_content(curses.COLOR_PAIRS - 1) |
---|
253 | n/a | curses.pair_number(0) |
---|
254 | n/a | |
---|
255 | n/a | if hasattr(curses, 'use_default_colors'): |
---|
256 | n/a | curses.use_default_colors() |
---|
257 | n/a | |
---|
258 | n/a | @requires_curses_func('keyname') |
---|
259 | n/a | def test_keyname(self): |
---|
260 | n/a | curses.keyname(13) |
---|
261 | n/a | |
---|
262 | n/a | @requires_curses_func('has_key') |
---|
263 | n/a | def test_has_key(self): |
---|
264 | n/a | curses.has_key(13) |
---|
265 | n/a | |
---|
266 | n/a | @requires_curses_func('getmouse') |
---|
267 | n/a | def test_getmouse(self): |
---|
268 | n/a | (availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED) |
---|
269 | n/a | if availmask == 0: |
---|
270 | n/a | self.skipTest('mouse stuff not available') |
---|
271 | n/a | curses.mouseinterval(10) |
---|
272 | n/a | # just verify these don't cause errors |
---|
273 | n/a | curses.ungetmouse(0, 0, 0, 0, curses.BUTTON1_PRESSED) |
---|
274 | n/a | m = curses.getmouse() |
---|
275 | n/a | |
---|
276 | n/a | def test_userptr_without_set(self): |
---|
277 | n/a | w = curses.newwin(10, 10) |
---|
278 | n/a | p = curses.panel.new_panel(w) |
---|
279 | n/a | # try to access userptr() before calling set_userptr() -- segfaults |
---|
280 | n/a | with self.assertRaises(curses.panel.error, |
---|
281 | n/a | msg='userptr should fail since not set'): |
---|
282 | n/a | p.userptr() |
---|
283 | n/a | |
---|
284 | n/a | def test_userptr_memory_leak(self): |
---|
285 | n/a | w = curses.newwin(10, 10) |
---|
286 | n/a | p = curses.panel.new_panel(w) |
---|
287 | n/a | obj = object() |
---|
288 | n/a | nrefs = sys.getrefcount(obj) |
---|
289 | n/a | for i in range(100): |
---|
290 | n/a | p.set_userptr(obj) |
---|
291 | n/a | |
---|
292 | n/a | p.set_userptr(None) |
---|
293 | n/a | self.assertEqual(sys.getrefcount(obj), nrefs, |
---|
294 | n/a | "set_userptr leaked references") |
---|
295 | n/a | |
---|
296 | n/a | def test_userptr_segfault(self): |
---|
297 | n/a | panel = curses.panel.new_panel(self.stdscr) |
---|
298 | n/a | class A: |
---|
299 | n/a | def __del__(self): |
---|
300 | n/a | panel.set_userptr(None) |
---|
301 | n/a | panel.set_userptr(A()) |
---|
302 | n/a | panel.set_userptr(None) |
---|
303 | n/a | |
---|
304 | n/a | def test_new_curses_panel(self): |
---|
305 | n/a | panel = curses.panel.new_panel(self.stdscr) |
---|
306 | n/a | self.assertRaises(TypeError, type(panel)) |
---|
307 | n/a | |
---|
308 | n/a | @requires_curses_func('is_term_resized') |
---|
309 | n/a | def test_is_term_resized(self): |
---|
310 | n/a | curses.is_term_resized(*self.stdscr.getmaxyx()) |
---|
311 | n/a | |
---|
312 | n/a | @requires_curses_func('resize_term') |
---|
313 | n/a | def test_resize_term(self): |
---|
314 | n/a | curses.resize_term(*self.stdscr.getmaxyx()) |
---|
315 | n/a | |
---|
316 | n/a | @requires_curses_func('resizeterm') |
---|
317 | n/a | def test_resizeterm(self): |
---|
318 | n/a | stdscr = self.stdscr |
---|
319 | n/a | lines, cols = curses.LINES, curses.COLS |
---|
320 | n/a | new_lines = lines - 1 |
---|
321 | n/a | new_cols = cols + 1 |
---|
322 | n/a | curses.resizeterm(new_lines, new_cols) |
---|
323 | n/a | |
---|
324 | n/a | self.assertEqual(curses.LINES, new_lines) |
---|
325 | n/a | self.assertEqual(curses.COLS, new_cols) |
---|
326 | n/a | |
---|
327 | n/a | def test_issue6243(self): |
---|
328 | n/a | curses.ungetch(1025) |
---|
329 | n/a | self.stdscr.getkey() |
---|
330 | n/a | |
---|
331 | n/a | @requires_curses_func('unget_wch') |
---|
332 | n/a | def test_unget_wch(self): |
---|
333 | n/a | stdscr = self.stdscr |
---|
334 | n/a | encoding = stdscr.encoding |
---|
335 | n/a | for ch in ('a', '\xe9', '\u20ac', '\U0010FFFF'): |
---|
336 | n/a | try: |
---|
337 | n/a | ch.encode(encoding) |
---|
338 | n/a | except UnicodeEncodeError: |
---|
339 | n/a | continue |
---|
340 | n/a | try: |
---|
341 | n/a | curses.unget_wch(ch) |
---|
342 | n/a | except Exception as err: |
---|
343 | n/a | self.fail("unget_wch(%a) failed with encoding %s: %s" |
---|
344 | n/a | % (ch, stdscr.encoding, err)) |
---|
345 | n/a | read = stdscr.get_wch() |
---|
346 | n/a | self.assertEqual(read, ch) |
---|
347 | n/a | |
---|
348 | n/a | code = ord(ch) |
---|
349 | n/a | curses.unget_wch(code) |
---|
350 | n/a | read = stdscr.get_wch() |
---|
351 | n/a | self.assertEqual(read, ch) |
---|
352 | n/a | |
---|
353 | n/a | def test_issue10570(self): |
---|
354 | n/a | b = curses.tparm(curses.tigetstr("cup"), 5, 3) |
---|
355 | n/a | self.assertIs(type(b), bytes) |
---|
356 | n/a | |
---|
357 | n/a | def test_encoding(self): |
---|
358 | n/a | stdscr = self.stdscr |
---|
359 | n/a | import codecs |
---|
360 | n/a | encoding = stdscr.encoding |
---|
361 | n/a | codecs.lookup(encoding) |
---|
362 | n/a | with self.assertRaises(TypeError): |
---|
363 | n/a | stdscr.encoding = 10 |
---|
364 | n/a | stdscr.encoding = encoding |
---|
365 | n/a | with self.assertRaises(TypeError): |
---|
366 | n/a | del stdscr.encoding |
---|
367 | n/a | |
---|
368 | n/a | def test_issue21088(self): |
---|
369 | n/a | stdscr = self.stdscr |
---|
370 | n/a | # |
---|
371 | n/a | # http://bugs.python.org/issue21088 |
---|
372 | n/a | # |
---|
373 | n/a | # the bug: |
---|
374 | n/a | # when converting curses.window.addch to Argument Clinic |
---|
375 | n/a | # the first two parameters were switched. |
---|
376 | n/a | |
---|
377 | n/a | # if someday we can represent the signature of addch |
---|
378 | n/a | # we will need to rewrite this test. |
---|
379 | n/a | try: |
---|
380 | n/a | signature = inspect.signature(stdscr.addch) |
---|
381 | n/a | self.assertFalse(signature) |
---|
382 | n/a | except ValueError: |
---|
383 | n/a | # not generating a signature is fine. |
---|
384 | n/a | pass |
---|
385 | n/a | |
---|
386 | n/a | # So. No signature for addch. |
---|
387 | n/a | # But Argument Clinic gave us a human-readable equivalent |
---|
388 | n/a | # as the first line of the docstring. So we parse that, |
---|
389 | n/a | # and ensure that the parameters appear in the correct order. |
---|
390 | n/a | # Since this is parsing output from Argument Clinic, we can |
---|
391 | n/a | # be reasonably certain the generated parsing code will be |
---|
392 | n/a | # correct too. |
---|
393 | n/a | human_readable_signature = stdscr.addch.__doc__.split("\n")[0] |
---|
394 | n/a | self.assertIn("[y, x,]", human_readable_signature) |
---|
395 | n/a | |
---|
396 | n/a | def test_issue13051(self): |
---|
397 | n/a | stdscr = self.stdscr |
---|
398 | n/a | box = curses.textpad.Textbox(stdscr, insert_mode=True) |
---|
399 | n/a | lines, cols = stdscr.getmaxyx() |
---|
400 | n/a | stdscr.resize(lines-2, cols-2) |
---|
401 | n/a | # this may cause infinite recursion, leading to a RuntimeError |
---|
402 | n/a | box._insert_printable_char('a') |
---|
403 | n/a | |
---|
404 | n/a | |
---|
405 | n/a | class MiscTests(unittest.TestCase): |
---|
406 | n/a | |
---|
407 | n/a | @requires_curses_func('update_lines_cols') |
---|
408 | n/a | def test_update_lines_cols(self): |
---|
409 | n/a | # this doesn't actually test that LINES and COLS are updated, |
---|
410 | n/a | # because we can't automate changing them. See Issue #4254 for |
---|
411 | n/a | # a manual test script. We can only test that the function |
---|
412 | n/a | # can be called. |
---|
413 | n/a | curses.update_lines_cols() |
---|
414 | n/a | |
---|
415 | n/a | |
---|
416 | n/a | class TestAscii(unittest.TestCase): |
---|
417 | n/a | |
---|
418 | n/a | def test_controlnames(self): |
---|
419 | n/a | for name in curses.ascii.controlnames: |
---|
420 | n/a | self.assertTrue(hasattr(curses.ascii, name), name) |
---|
421 | n/a | |
---|
422 | n/a | def test_ctypes(self): |
---|
423 | n/a | def check(func, expected): |
---|
424 | n/a | with self.subTest(ch=c, func=func): |
---|
425 | n/a | self.assertEqual(func(i), expected) |
---|
426 | n/a | self.assertEqual(func(c), expected) |
---|
427 | n/a | |
---|
428 | n/a | for i in range(256): |
---|
429 | n/a | c = chr(i) |
---|
430 | n/a | b = bytes([i]) |
---|
431 | n/a | check(curses.ascii.isalnum, b.isalnum()) |
---|
432 | n/a | check(curses.ascii.isalpha, b.isalpha()) |
---|
433 | n/a | check(curses.ascii.isdigit, b.isdigit()) |
---|
434 | n/a | check(curses.ascii.islower, b.islower()) |
---|
435 | n/a | check(curses.ascii.isspace, b.isspace()) |
---|
436 | n/a | check(curses.ascii.isupper, b.isupper()) |
---|
437 | n/a | |
---|
438 | n/a | check(curses.ascii.isascii, i < 128) |
---|
439 | n/a | check(curses.ascii.ismeta, i >= 128) |
---|
440 | n/a | check(curses.ascii.isctrl, i < 32) |
---|
441 | n/a | check(curses.ascii.iscntrl, i < 32 or i == 127) |
---|
442 | n/a | check(curses.ascii.isblank, c in ' \t') |
---|
443 | n/a | check(curses.ascii.isgraph, 32 < i <= 126) |
---|
444 | n/a | check(curses.ascii.isprint, 32 <= i <= 126) |
---|
445 | n/a | check(curses.ascii.ispunct, c in string.punctuation) |
---|
446 | n/a | check(curses.ascii.isxdigit, c in string.hexdigits) |
---|
447 | n/a | |
---|
448 | n/a | for i in (-2, -1, 256, sys.maxunicode, sys.maxunicode+1): |
---|
449 | n/a | self.assertFalse(curses.ascii.isalnum(i)) |
---|
450 | n/a | self.assertFalse(curses.ascii.isalpha(i)) |
---|
451 | n/a | self.assertFalse(curses.ascii.isdigit(i)) |
---|
452 | n/a | self.assertFalse(curses.ascii.islower(i)) |
---|
453 | n/a | self.assertFalse(curses.ascii.isspace(i)) |
---|
454 | n/a | self.assertFalse(curses.ascii.isupper(i)) |
---|
455 | n/a | |
---|
456 | n/a | self.assertFalse(curses.ascii.isascii(i)) |
---|
457 | n/a | self.assertFalse(curses.ascii.isctrl(i)) |
---|
458 | n/a | self.assertFalse(curses.ascii.iscntrl(i)) |
---|
459 | n/a | self.assertFalse(curses.ascii.isblank(i)) |
---|
460 | n/a | self.assertFalse(curses.ascii.isgraph(i)) |
---|
461 | n/a | self.assertFalse(curses.ascii.isprint(i)) |
---|
462 | n/a | self.assertFalse(curses.ascii.ispunct(i)) |
---|
463 | n/a | self.assertFalse(curses.ascii.isxdigit(i)) |
---|
464 | n/a | |
---|
465 | n/a | self.assertFalse(curses.ascii.ismeta(-1)) |
---|
466 | n/a | |
---|
467 | n/a | def test_ascii(self): |
---|
468 | n/a | ascii = curses.ascii.ascii |
---|
469 | n/a | self.assertEqual(ascii('\xc1'), 'A') |
---|
470 | n/a | self.assertEqual(ascii('A'), 'A') |
---|
471 | n/a | self.assertEqual(ascii(ord('\xc1')), ord('A')) |
---|
472 | n/a | |
---|
473 | n/a | def test_ctrl(self): |
---|
474 | n/a | ctrl = curses.ascii.ctrl |
---|
475 | n/a | self.assertEqual(ctrl('J'), '\n') |
---|
476 | n/a | self.assertEqual(ctrl('\n'), '\n') |
---|
477 | n/a | self.assertEqual(ctrl('@'), '\0') |
---|
478 | n/a | self.assertEqual(ctrl(ord('J')), ord('\n')) |
---|
479 | n/a | |
---|
480 | n/a | def test_alt(self): |
---|
481 | n/a | alt = curses.ascii.alt |
---|
482 | n/a | self.assertEqual(alt('\n'), '\x8a') |
---|
483 | n/a | self.assertEqual(alt('A'), '\xc1') |
---|
484 | n/a | self.assertEqual(alt(ord('A')), 0xc1) |
---|
485 | n/a | |
---|
486 | n/a | def test_unctrl(self): |
---|
487 | n/a | unctrl = curses.ascii.unctrl |
---|
488 | n/a | self.assertEqual(unctrl('a'), 'a') |
---|
489 | n/a | self.assertEqual(unctrl('A'), 'A') |
---|
490 | n/a | self.assertEqual(unctrl(';'), ';') |
---|
491 | n/a | self.assertEqual(unctrl(' '), ' ') |
---|
492 | n/a | self.assertEqual(unctrl('\x7f'), '^?') |
---|
493 | n/a | self.assertEqual(unctrl('\n'), '^J') |
---|
494 | n/a | self.assertEqual(unctrl('\0'), '^@') |
---|
495 | n/a | self.assertEqual(unctrl(ord('A')), 'A') |
---|
496 | n/a | self.assertEqual(unctrl(ord('\n')), '^J') |
---|
497 | n/a | # Meta-bit characters |
---|
498 | n/a | self.assertEqual(unctrl('\x8a'), '!^J') |
---|
499 | n/a | self.assertEqual(unctrl('\xc1'), '!A') |
---|
500 | n/a | self.assertEqual(unctrl(ord('\x8a')), '!^J') |
---|
501 | n/a | self.assertEqual(unctrl(ord('\xc1')), '!A') |
---|
502 | n/a | |
---|
503 | n/a | |
---|
504 | n/a | if __name__ == '__main__': |
---|
505 | n/a | unittest.main() |
---|