python-2.5.2/win32/Lib/test/test_curses.py
changeset 0 ae805ac0140d
equal deleted inserted replaced
-1:000000000000 0:ae805ac0140d
       
     1 #
       
     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 # init_color()
       
     9 # Only called, not tested: getmouse(), ungetmouse()
       
    10 #
       
    11 
       
    12 import curses, sys, tempfile, os
       
    13 import curses.panel
       
    14 
       
    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 
       
    19 from test.test_support import requires, TestSkipped
       
    20 requires('curses')
       
    21 
       
    22 # XXX: if newterm was supported we could use it instead of initscr and not exit
       
    23 term = os.environ.get('TERM')
       
    24 if not term or term == 'unknown':
       
    25     raise TestSkipped, "$TERM=%r, calling initscr() may cause exit" % term
       
    26 
       
    27 if sys.platform == "cygwin":
       
    28     raise TestSkipped("cygwin's curses mostly just hangs")
       
    29 
       
    30 def window_funcs(stdscr):
       
    31     "Test the methods of windows"
       
    32     win = curses.newwin(10,10)
       
    33     win = curses.newwin(5,5, 5,5)
       
    34     win2 = curses.newwin(15,15, 5,5)
       
    35 
       
    36     for meth in [stdscr.addch, stdscr.addstr]:
       
    37         for args in [('a'), ('a', curses.A_BOLD),
       
    38                      (4,4, 'a'), (5,5, 'a', curses.A_BOLD)]:
       
    39             meth(*args)
       
    40 
       
    41     for meth in [stdscr.box, stdscr.clear, stdscr.clrtobot,
       
    42                  stdscr.clrtoeol, stdscr.cursyncup, stdscr.delch,
       
    43                  stdscr.deleteln, stdscr.erase, stdscr.getbegyx,
       
    44                  stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx,
       
    45                  stdscr.getparyx, stdscr.getyx, stdscr.inch,
       
    46                  stdscr.insertln, stdscr.instr, stdscr.is_wintouched,
       
    47                  win.noutrefresh, stdscr.redrawwin, stdscr.refresh,
       
    48                  stdscr.standout, stdscr.standend, stdscr.syncdown,
       
    49                  stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]:
       
    50         meth()
       
    51 
       
    52     stdscr.addnstr('1234', 3)
       
    53     stdscr.addnstr('1234', 3, curses.A_BOLD)
       
    54     stdscr.addnstr(4,4, '1234', 3)
       
    55     stdscr.addnstr(5,5, '1234', 3, curses.A_BOLD)
       
    56 
       
    57     stdscr.attron(curses.A_BOLD)
       
    58     stdscr.attroff(curses.A_BOLD)
       
    59     stdscr.attrset(curses.A_BOLD)
       
    60     stdscr.bkgd(' ')
       
    61     stdscr.bkgd(' ', curses.A_REVERSE)
       
    62     stdscr.bkgdset(' ')
       
    63     stdscr.bkgdset(' ', curses.A_REVERSE)
       
    64 
       
    65     win.border(65, 66, 67, 68,
       
    66                69, 70, 71, 72)
       
    67     win.border('|', '!', '-', '_',
       
    68                '+', '\\', '#', '/')
       
    69     try:
       
    70         win.border(65, 66, 67, 68,
       
    71                    69, [], 71, 72)
       
    72     except TypeError:
       
    73         pass
       
    74     else:
       
    75         raise RuntimeError, "Expected win.border() to raise TypeError"
       
    76 
       
    77     stdscr.clearok(1)
       
    78 
       
    79     win4 = stdscr.derwin(2,2)
       
    80     win4 = stdscr.derwin(1,1, 5,5)
       
    81     win4.mvderwin(9,9)
       
    82 
       
    83     stdscr.echochar('a')
       
    84     stdscr.echochar('a', curses.A_BOLD)
       
    85     stdscr.hline('-', 5)
       
    86     stdscr.hline('-', 5, curses.A_BOLD)
       
    87     stdscr.hline(1,1,'-', 5)
       
    88     stdscr.hline(1,1,'-', 5, curses.A_BOLD)
       
    89 
       
    90     stdscr.idcok(1)
       
    91     stdscr.idlok(1)
       
    92     stdscr.immedok(1)
       
    93     stdscr.insch('c')
       
    94     stdscr.insdelln(1)
       
    95     stdscr.insnstr('abc', 3)
       
    96     stdscr.insnstr('abc', 3, curses.A_BOLD)
       
    97     stdscr.insnstr(5, 5, 'abc', 3)
       
    98     stdscr.insnstr(5, 5, 'abc', 3, curses.A_BOLD)
       
    99 
       
   100     stdscr.insstr('def')
       
   101     stdscr.insstr('def', curses.A_BOLD)
       
   102     stdscr.insstr(5, 5, 'def')
       
   103     stdscr.insstr(5, 5, 'def', curses.A_BOLD)
       
   104     stdscr.is_linetouched(0)
       
   105     stdscr.keypad(1)
       
   106     stdscr.leaveok(1)
       
   107     stdscr.move(3,3)
       
   108     win.mvwin(2,2)
       
   109     stdscr.nodelay(1)
       
   110     stdscr.notimeout(1)
       
   111     win2.overlay(win)
       
   112     win2.overwrite(win)
       
   113     win2.overlay(win, 1, 2, 3, 3, 2, 1)
       
   114     win2.overwrite(win, 1, 2, 3, 3, 2, 1)
       
   115     stdscr.redrawln(1,2)
       
   116 
       
   117     stdscr.scrollok(1)
       
   118     stdscr.scroll()
       
   119     stdscr.scroll(2)
       
   120     stdscr.scroll(-3)
       
   121 
       
   122     stdscr.move(12, 2)
       
   123     stdscr.setscrreg(10,15)
       
   124     win3 = stdscr.subwin(10,10)
       
   125     win3 = stdscr.subwin(10,10, 5,5)
       
   126     stdscr.syncok(1)
       
   127     stdscr.timeout(5)
       
   128     stdscr.touchline(5,5)
       
   129     stdscr.touchline(5,5,0)
       
   130     stdscr.vline('a', 3)
       
   131     stdscr.vline('a', 3, curses.A_STANDOUT)
       
   132     stdscr.vline(1,1, 'a', 3)
       
   133     stdscr.vline(1,1, 'a', 3, curses.A_STANDOUT)
       
   134 
       
   135     if hasattr(curses, 'resize'):
       
   136         stdscr.resize()
       
   137     if hasattr(curses, 'enclose'):
       
   138         stdscr.enclose()
       
   139 
       
   140 
       
   141 def module_funcs(stdscr):
       
   142     "Test module-level functions"
       
   143 
       
   144     for func in [curses.baudrate, curses.beep, curses.can_change_color,
       
   145                  curses.cbreak, curses.def_prog_mode, curses.doupdate,
       
   146                  curses.filter, curses.flash, curses.flushinp,
       
   147                  curses.has_colors, curses.has_ic, curses.has_il,
       
   148                  curses.isendwin, curses.killchar, curses.longname,
       
   149                  curses.nocbreak, curses.noecho, curses.nonl,
       
   150                  curses.noqiflush, curses.noraw,
       
   151                  curses.reset_prog_mode, curses.termattrs,
       
   152                  curses.termname, curses.erasechar, curses.getsyx]:
       
   153         func()
       
   154 
       
   155     # Functions that actually need arguments
       
   156     if curses.tigetstr("cnorm"):
       
   157         curses.curs_set(1)
       
   158     curses.delay_output(1)
       
   159     curses.echo() ; curses.echo(1)
       
   160 
       
   161     f = tempfile.TemporaryFile()
       
   162     stdscr.putwin(f)
       
   163     f.seek(0)
       
   164     curses.getwin(f)
       
   165     f.close()
       
   166 
       
   167     curses.halfdelay(1)
       
   168     curses.intrflush(1)
       
   169     curses.meta(1)
       
   170     curses.napms(100)
       
   171     curses.newpad(50,50)
       
   172     win = curses.newwin(5,5)
       
   173     win = curses.newwin(5,5, 1,1)
       
   174     curses.nl() ; curses.nl(1)
       
   175     curses.putp('abc')
       
   176     curses.qiflush()
       
   177     curses.raw() ; curses.raw(1)
       
   178     curses.setsyx(5,5)
       
   179     curses.tigetflag('hc')
       
   180     curses.tigetnum('co')
       
   181     curses.tigetstr('cr')
       
   182     curses.tparm('cr')
       
   183     curses.typeahead(sys.__stdin__.fileno())
       
   184     curses.unctrl('a')
       
   185     curses.ungetch('a')
       
   186     curses.use_env(1)
       
   187 
       
   188     # Functions only available on a few platforms
       
   189     if curses.has_colors():
       
   190         curses.start_color()
       
   191         curses.init_pair(2, 1,1)
       
   192         curses.color_content(1)
       
   193         curses.color_pair(2)
       
   194         curses.pair_content(curses.COLOR_PAIRS - 1)
       
   195         curses.pair_number(0)
       
   196 
       
   197         if hasattr(curses, 'use_default_colors'):
       
   198             curses.use_default_colors()
       
   199 
       
   200     if hasattr(curses, 'keyname'):
       
   201         curses.keyname(13)
       
   202 
       
   203     if hasattr(curses, 'has_key'):
       
   204         curses.has_key(13)
       
   205 
       
   206     if hasattr(curses, 'getmouse'):
       
   207         (availmask, oldmask) = curses.mousemask(curses.BUTTON1_PRESSED)
       
   208         # availmask indicates that mouse stuff not available.
       
   209         if availmask != 0:
       
   210             curses.mouseinterval(10)
       
   211             # just verify these don't cause errors
       
   212             m = curses.getmouse()
       
   213             curses.ungetmouse(*m)
       
   214 
       
   215     if hasattr(curses, 'is_term_resized'):
       
   216         curses.is_term_resized(*stdscr.getmaxyx())
       
   217     if hasattr(curses, 'resizeterm'):
       
   218         curses.resizeterm(*stdscr.getmaxyx())
       
   219     if hasattr(curses, 'resize_term'):
       
   220         curses.resize_term(*stdscr.getmaxyx())
       
   221 
       
   222 def unit_tests():
       
   223     from curses import ascii
       
   224     for ch, expected in [('a', 'a'), ('A', 'A'),
       
   225                          (';', ';'), (' ', ' '),
       
   226                          ('\x7f', '^?'), ('\n', '^J'), ('\0', '^@'),
       
   227                          # Meta-bit characters
       
   228                          ('\x8a', '!^J'), ('\xc1', '!A'),
       
   229                          ]:
       
   230         if ascii.unctrl(ch) != expected:
       
   231             print 'curses.unctrl fails on character', repr(ch)
       
   232 
       
   233 
       
   234 def test_userptr_without_set(stdscr):
       
   235     w = curses.newwin(10, 10)
       
   236     p = curses.panel.new_panel(w)
       
   237     # try to access userptr() before calling set_userptr() -- segfaults
       
   238     try:
       
   239         p.userptr()
       
   240         raise RuntimeError, 'userptr should fail since not set'
       
   241     except curses.panel.error:
       
   242         pass
       
   243 
       
   244 def test_resize_term(stdscr):
       
   245     if hasattr(curses, 'resizeterm'):
       
   246         lines, cols = curses.LINES, curses.COLS
       
   247         curses.resizeterm(lines - 1, cols + 1)
       
   248 
       
   249         if curses.LINES != lines - 1 or curses.COLS != cols + 1:
       
   250             raise RuntimeError, "Expected resizeterm to update LINES and COLS"
       
   251 
       
   252 def main(stdscr):
       
   253     curses.savetty()
       
   254     try:
       
   255         module_funcs(stdscr)
       
   256         window_funcs(stdscr)
       
   257         test_userptr_without_set(stdscr)
       
   258         test_resize_term(stdscr)
       
   259     finally:
       
   260         curses.resetty()
       
   261 
       
   262 if __name__ == '__main__':
       
   263     curses.wrapper(main)
       
   264     unit_tests()
       
   265 else:
       
   266     try:
       
   267         # testing setupterm() inside initscr/endwin
       
   268         # causes terminal breakage
       
   269         curses.setupterm(fd=sys.__stdout__.fileno())
       
   270         stdscr = curses.initscr()
       
   271         main(stdscr)
       
   272     finally:
       
   273         curses.endwin()
       
   274 
       
   275     unit_tests()