home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / TEST_GETOPT.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  3.6 KB  |  102 lines

  1. # test_getopt.py
  2. # David Goodger <dgoodger@bigfoot.com> 2000-08-19
  3.  
  4. import getopt
  5. from getopt import GetoptError
  6. from test_support import verbose
  7.  
  8. def expectException(teststr, expected, failure=AssertionError):
  9.     """Executes a statement passed in teststr, and raises an exception
  10.        (failure) if the expected exception is *not* raised."""
  11.     try:
  12.         exec teststr
  13.     except expected:
  14.         pass
  15.     else:
  16.         raise failure
  17.  
  18. if verbose:
  19.     print 'Running tests on getopt.short_has_arg'
  20. assert getopt.short_has_arg('a', 'a:')
  21. assert not getopt.short_has_arg('a', 'a')
  22. expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError)
  23. expectException("tmp = getopt.short_has_arg('a', '')", GetoptError)
  24.  
  25. if verbose:
  26.     print 'Running tests on getopt.long_has_args'
  27. has_arg, option = getopt.long_has_args('abc', ['abc='])
  28. assert has_arg
  29. assert option == 'abc'
  30. has_arg, option = getopt.long_has_args('abc', ['abc'])
  31. assert not has_arg
  32. assert option == 'abc'
  33. has_arg, option = getopt.long_has_args('abc', ['abcd'])
  34. assert not has_arg
  35. assert option == 'abcd'
  36. expectException("has_arg, option = getopt.long_has_args('abc', ['def'])",
  37.                 GetoptError)
  38. expectException("has_arg, option = getopt.long_has_args('abc', [])",
  39.                 GetoptError)
  40. expectException("has_arg, option = " + \
  41.                      "getopt.long_has_args('abc', ['abcd','abcde'])",
  42.                 GetoptError)
  43.  
  44. if verbose:
  45.     print 'Running tests on getopt.do_shorts'
  46. opts, args = getopt.do_shorts([], 'a', 'a', [])
  47. assert opts == [('-a', '')]
  48. assert args == []
  49. opts, args = getopt.do_shorts([], 'a1', 'a:', [])
  50. assert opts == [('-a', '1')]
  51. assert args == []
  52. #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
  53. #assert opts == [('-a', '1')]
  54. #assert args == []
  55. opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
  56. assert opts == [('-a', '1')]
  57. assert args == []
  58. opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
  59. assert opts == [('-a', '1')]
  60. assert args == ['2']
  61. expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])",
  62.                 GetoptError)
  63. expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])",
  64.                 GetoptError)
  65.  
  66. if verbose:
  67.     print 'Running tests on getopt.do_longs'
  68. opts, args = getopt.do_longs([], 'abc', ['abc'], [])
  69. assert opts == [('--abc', '')]
  70. assert args == []
  71. opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
  72. assert opts == [('--abc', '1')]
  73. assert args == []
  74. opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
  75. assert opts == [('--abcd', '1')]
  76. assert args == []
  77. expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])",
  78.                 GetoptError)
  79. expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])",
  80.                 GetoptError)
  81.  
  82. # note: the empty string between '-a' and '--beta' is significant:
  83. # it simulates an empty string option argument ('-a ""') on the command line.
  84. cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '',
  85.            '--beta', 'arg1', 'arg2']
  86.  
  87. if verbose:
  88.     print 'Running tests on getopt.getopt'
  89. opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
  90. assert opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''),
  91.                 ('-a', '3'), ('-a', ''), ('--beta', '')]
  92. # Note ambiguity of ('-b', '') and ('-a', '') above. This must be
  93. # accounted for in the code that calls getopt().
  94. assert args == ['arg1', 'arg2']
  95.  
  96. expectException(
  97.     "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])",
  98.     GetoptError)
  99.  
  100. if verbose:
  101.     print "Module getopt: tests completed successfully."
  102.