home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 July / maximum-cd-2011-07.iso / DiscContents / LibO_3.3.2_Win_x86_install_multi.exe / libreoffice1.cab / test_getopt.py < prev    next >
Encoding:
Python Source  |  2011-03-15  |  6.4 KB  |  180 lines

  1. # test_getopt.py
  2. # David Goodger <dgoodger@bigfoot.com> 2000-08-19
  3.  
  4. from test.test_support import verbose, run_doctest, run_unittest
  5. import unittest
  6.  
  7. import getopt
  8. import os
  9.  
  10. sentinel = object()
  11.  
  12. class GetoptTests(unittest.TestCase):
  13.     def setUp(self):
  14.         self.old_posixly_correct = os.environ.get("POSIXLY_CORRECT", sentinel)
  15.         if self.old_posixly_correct is not sentinel:
  16.             del os.environ["POSIXLY_CORRECT"]
  17.  
  18.     def tearDown(self):
  19.         if self.old_posixly_correct is sentinel:
  20.             os.environ.pop("POSIXLY_CORRECT", None)
  21.         else:
  22.             os.environ["POSIXLY_CORRECT"] = self.old_posixly_correct
  23.  
  24.     def assertError(self, *args, **kwargs):
  25.         self.assertRaises(getopt.GetoptError, *args, **kwargs)
  26.  
  27.     def test_short_has_arg(self):
  28.         self.failUnless(getopt.short_has_arg('a', 'a:'))
  29.         self.failIf(getopt.short_has_arg('a', 'a'))
  30.         self.assertError(getopt.short_has_arg, 'a', 'b')
  31.  
  32.     def test_long_has_args(self):
  33.         has_arg, option = getopt.long_has_args('abc', ['abc='])
  34.         self.failUnless(has_arg)
  35.         self.assertEqual(option, 'abc')
  36.  
  37.         has_arg, option = getopt.long_has_args('abc', ['abc'])
  38.         self.failIf(has_arg)
  39.         self.assertEqual(option, 'abc')
  40.  
  41.         has_arg, option = getopt.long_has_args('abc', ['abcd'])
  42.         self.failIf(has_arg)
  43.         self.assertEqual(option, 'abcd')
  44.  
  45.         self.assertError(getopt.long_has_args, 'abc', ['def'])
  46.         self.assertError(getopt.long_has_args, 'abc', [])
  47.         self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde'])
  48.  
  49.     def test_do_shorts(self):
  50.         opts, args = getopt.do_shorts([], 'a', 'a', [])
  51.         self.assertEqual(opts, [('-a', '')])
  52.         self.assertEqual(args, [])
  53.  
  54.         opts, args = getopt.do_shorts([], 'a1', 'a:', [])
  55.         self.assertEqual(opts, [('-a', '1')])
  56.         self.assertEqual(args, [])
  57.  
  58.         #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
  59.         #self.assertEqual(opts, [('-a', '1')])
  60.         #self.assertEqual(args, [])
  61.  
  62.         opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
  63.         self.assertEqual(opts, [('-a', '1')])
  64.         self.assertEqual(args, [])
  65.  
  66.         opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
  67.         self.assertEqual(opts, [('-a', '1')])
  68.         self.assertEqual(args, ['2'])
  69.  
  70.         self.assertError(getopt.do_shorts, [], 'a1', 'a', [])
  71.         self.assertError(getopt.do_shorts, [], 'a', 'a:', [])
  72.  
  73.     def test_do_longs(self):
  74.         opts, args = getopt.do_longs([], 'abc', ['abc'], [])
  75.         self.assertEqual(opts, [('--abc', '')])
  76.         self.assertEqual(args, [])
  77.  
  78.         opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
  79.         self.assertEqual(opts, [('--abc', '1')])
  80.         self.assertEqual(args, [])
  81.  
  82.         opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
  83.         self.assertEqual(opts, [('--abcd', '1')])
  84.         self.assertEqual(args, [])
  85.  
  86.         opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
  87.         self.assertEqual(opts, [('--abc', '')])
  88.         self.assertEqual(args, [])
  89.  
  90.         # Much like the preceding, except with a non-alpha character ("-") in
  91.         # option name that precedes "="; failed in
  92.         # http://python.org/sf/126863
  93.         opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
  94.         self.assertEqual(opts, [('--foo', '42')])
  95.         self.assertEqual(args, [])
  96.  
  97.         self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], [])
  98.         self.assertError(getopt.do_longs, [], 'abc', ['abc='], [])
  99.  
  100.     def test_getopt(self):
  101.         # note: the empty string between '-a' and '--beta' is significant:
  102.         # it simulates an empty string option argument ('-a ""') on the
  103.         # command line.
  104.         cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a',
  105.                    '', '--beta', 'arg1', 'arg2']
  106.  
  107.         opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
  108.         self.assertEqual(opts, [('-a', '1'), ('-b', ''),
  109.                                 ('--alpha', '2'), ('--beta', ''),
  110.                                 ('-a', '3'), ('-a', ''), ('--beta', '')])
  111.         # Note ambiguity of ('-b', '') and ('-a', '') above. This must be
  112.         # accounted for in the code that calls getopt().
  113.         self.assertEqual(args, ['arg1', 'arg2'])
  114.  
  115.         self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta'])
  116.  
  117.     def test_gnu_getopt(self):
  118.         # Test handling of GNU style scanning mode.
  119.         cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
  120.  
  121.         # GNU style
  122.         opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
  123.         self.assertEqual(args, ['arg1'])
  124.         self.assertEqual(opts, [('-a', ''), ('-b', '1'),
  125.                                 ('--alpha', ''), ('--beta', '2')])
  126.  
  127.         # Posix style via +
  128.         opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
  129.         self.assertEqual(opts, [('-a', '')])
  130.         self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
  131.  
  132.         # Posix style via POSIXLY_CORRECT
  133.         os.environ["POSIXLY_CORRECT"] = "1"
  134.         opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
  135.         self.assertEqual(opts, [('-a', '')])
  136.         self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
  137.  
  138.     def test_libref_examples(self):
  139.         s = """
  140.         Examples from the Library Reference:  Doc/lib/libgetopt.tex
  141.  
  142.         An example using only Unix style options:
  143.  
  144.  
  145.         >>> import getopt
  146.         >>> args = '-a -b -cfoo -d bar a1 a2'.split()
  147.         >>> args
  148.         ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
  149.         >>> optlist, args = getopt.getopt(args, 'abc:d:')
  150.         >>> optlist
  151.         [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
  152.         >>> args
  153.         ['a1', 'a2']
  154.  
  155.         Using long option names is equally easy:
  156.  
  157.  
  158.         >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
  159.         >>> args = s.split()
  160.         >>> args
  161.         ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
  162.         >>> optlist, args = getopt.getopt(args, 'x', [
  163.         ...     'condition=', 'output-file=', 'testing'])
  164.         >>> optlist
  165.         [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
  166.         >>> args
  167.         ['a1', 'a2']
  168.         """
  169.  
  170.         import types
  171.         m = types.ModuleType("libreftest", s)
  172.         run_doctest(m, verbose)
  173.  
  174.  
  175. def test_main():
  176.     run_unittest(GetoptTests)
  177.  
  178. if __name__ == "__main__":
  179.     test_main()
  180.