Commit f08b5bd6 authored by Collin Winter's avatar Collin Winter

Make test_getopt use unittest.

parent 5d21e975
# test_getopt.py # test_getopt.py
# David Goodger <dgoodger@bigfoot.com> 2000-08-19 # David Goodger <dgoodger@bigfoot.com> 2000-08-19
from test.test_support import verbose, run_doctest, run_unittest
import unittest
import getopt import getopt
from getopt import GetoptError
from test.test_support import verify, verbose, run_doctest
import os import os
def expectException(teststr, expected, failure=AssertionError): sentinel = object()
"""Executes a statement passed in teststr, and raises an exception
(failure) if the expected exception is *not* raised.""" class GetoptTests(unittest.TestCase):
try: def setUp(self):
exec teststr self.old_posixly_correct = os.environ.get("POSIXLY_CORRECT", sentinel)
except expected: if self.old_posixly_correct is not sentinel:
pass del os.environ["POSIXLY_CORRECT"]
else:
raise failure def tearDown(self):
if self.old_posixly_correct is sentinel:
old_posixly_correct = os.environ.get("POSIXLY_CORRECT") os.environ.pop("POSIXLY_CORRECT", None)
if old_posixly_correct is not None: else:
del os.environ["POSIXLY_CORRECT"] os.environ["POSIXLY_CORRECT"] = self.old_posixly_correct
if verbose: def assertError(self, *args, **kwargs):
print 'Running tests on getopt.short_has_arg' self.assertRaises(getopt.GetoptError, *args, **kwargs)
verify(getopt.short_has_arg('a', 'a:'))
verify(not getopt.short_has_arg('a', 'a')) def test_short_has_arg(self):
expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError) self.failUnless(getopt.short_has_arg('a', 'a:'))
expectException("tmp = getopt.short_has_arg('a', '')", GetoptError) self.failIf(getopt.short_has_arg('a', 'a'))
self.assertError(getopt.short_has_arg, 'a', 'b')
if verbose:
print 'Running tests on getopt.long_has_args' def test_long_has_args(self):
has_arg, option = getopt.long_has_args('abc', ['abc=']) has_arg, option = getopt.long_has_args('abc', ['abc='])
verify(has_arg) self.failUnless(has_arg)
verify(option == 'abc') self.assertEqual(option, 'abc')
has_arg, option = getopt.long_has_args('abc', ['abc'])
verify(not has_arg) has_arg, option = getopt.long_has_args('abc', ['abc'])
verify(option == 'abc') self.failIf(has_arg)
has_arg, option = getopt.long_has_args('abc', ['abcd']) self.assertEqual(option, 'abc')
verify(not has_arg)
verify(option == 'abcd') has_arg, option = getopt.long_has_args('abc', ['abcd'])
expectException("has_arg, option = getopt.long_has_args('abc', ['def'])", self.failIf(has_arg)
GetoptError) self.assertEqual(option, 'abcd')
expectException("has_arg, option = getopt.long_has_args('abc', [])",
GetoptError) self.assertError(getopt.long_has_args, 'abc', ['def'])
expectException("has_arg, option = " + \ self.assertError(getopt.long_has_args, 'abc', [])
"getopt.long_has_args('abc', ['abcd','abcde'])", self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde'])
GetoptError)
def test_do_shorts(self):
if verbose: opts, args = getopt.do_shorts([], 'a', 'a', [])
print 'Running tests on getopt.do_shorts' self.assertEqual(opts, [('-a', '')])
opts, args = getopt.do_shorts([], 'a', 'a', []) self.assertEqual(args, [])
verify(opts == [('-a', '')])
verify(args == []) opts, args = getopt.do_shorts([], 'a1', 'a:', [])
opts, args = getopt.do_shorts([], 'a1', 'a:', []) self.assertEqual(opts, [('-a', '1')])
verify(opts == [('-a', '1')]) self.assertEqual(args, [])
verify(args == [])
#opts, args = getopt.do_shorts([], 'a=1', 'a:', []) #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
#verify(opts == [('-a', '1')]) #self.assertEqual(opts, [('-a', '1')])
#verify(args == []) #self.assertEqual(args, [])
opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
verify(opts == [('-a', '1')]) opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
verify(args == []) self.assertEqual(opts, [('-a', '1')])
opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) self.assertEqual(args, [])
verify(opts == [('-a', '1')])
verify(args == ['2']) opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])", self.assertEqual(opts, [('-a', '1')])
GetoptError) self.assertEqual(args, ['2'])
expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])",
GetoptError) self.assertError(getopt.do_shorts, [], 'a1', 'a', [])
self.assertError(getopt.do_shorts, [], 'a', 'a:', [])
if verbose:
print 'Running tests on getopt.do_longs' def test_do_longs(self):
opts, args = getopt.do_longs([], 'abc', ['abc'], []) opts, args = getopt.do_longs([], 'abc', ['abc'], [])
verify(opts == [('--abc', '')]) self.assertEqual(opts, [('--abc', '')])
verify(args == []) self.assertEqual(args, [])
opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
verify(opts == [('--abc', '1')]) opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
verify(args == []) self.assertEqual(opts, [('--abc', '1')])
opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) self.assertEqual(args, [])
verify(opts == [('--abcd', '1')])
verify(args == []) opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) self.assertEqual(opts, [('--abcd', '1')])
verify(opts == [('--abc', '')]) self.assertEqual(args, [])
verify(args == [])
# Much like the preceding, except with a non-alpha character ("-") in opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
# option name that precedes "="; failed in self.assertEqual(opts, [('--abc', '')])
# http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470 self.assertEqual(args, [])
opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
verify(opts == [('--foo', '42')]) # Much like the preceding, except with a non-alpha character ("-") in
verify(args == []) # option name that precedes "="; failed in
expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])", # http://python.org/sf/126863
GetoptError) opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])", self.assertEqual(opts, [('--foo', '42')])
GetoptError) self.assertEqual(args, [])
# note: the empty string between '-a' and '--beta' is significant: self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], [])
# it simulates an empty string option argument ('-a ""') on the command line. self.assertError(getopt.do_longs, [], 'abc', ['abc='], [])
cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '',
'--beta', 'arg1', 'arg2'] def test_getopt(self):
# note: the empty string between '-a' and '--beta' is significant:
if verbose: # it simulates an empty string option argument ('-a ""') on the
print 'Running tests on getopt.getopt' # command line.
opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta']) cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a',
verify(opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''), '', '--beta', 'arg1', 'arg2']
('-a', '3'), ('-a', ''), ('--beta', '')] )
# Note ambiguity of ('-b', '') and ('-a', '') above. This must be opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
# accounted for in the code that calls getopt(). self.assertEqual(opts, [('-a', '1'), ('-b', ''),
verify(args == ['arg1', 'arg2']) ('--alpha', '2'), ('--beta', ''),
('-a', '3'), ('-a', ''), ('--beta', '')])
expectException( # Note ambiguity of ('-b', '') and ('-a', '') above. This must be
"opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])", # accounted for in the code that calls getopt().
GetoptError) self.assertEqual(args, ['arg1', 'arg2'])
# Test handling of GNU style scanning mode. self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta'])
if verbose:
print 'Running tests on getopt.gnu_getopt' def test_gnu_getopt(self):
cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2'] # Test handling of GNU style scanning mode.
# GNU style cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
verify(opts == [('-a', ''), ('-b', '1'), ('--alpha', ''), ('--beta', '2')]) # GNU style
verify(args == ['arg1']) opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
# Posix style via + self.assertEqual(args, ['arg1'])
opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta=']) self.assertEqual(opts, [('-a', ''), ('-b', '1'),
verify(opts == [('-a', '')]) ('--alpha', ''), ('--beta', '2')])
verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2'])
# Posix style via POSIXLY_CORRECT # Posix style via +
os.environ["POSIXLY_CORRECT"] = "1" opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) self.assertEqual(opts, [('-a', '')])
verify(opts == [('-a', '')]) self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2'])
# Posix style via POSIXLY_CORRECT
os.environ["POSIXLY_CORRECT"] = "1"
if old_posixly_correct is None: opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
del os.environ["POSIXLY_CORRECT"] self.assertEqual(opts, [('-a', '')])
else: self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
os.environ["POSIXLY_CORRECT"] = old_posixly_correct
def test_libref_examples(self):
#------------------------------------------------------------------------------ s = """
Examples from the Library Reference: Doc/lib/libgetopt.tex
libreftest = """
Examples from the Library Reference: Doc/lib/libgetopt.tex An example using only Unix style options:
An example using only Unix style options:
>>> import getopt
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
>>> import getopt >>> args
>>> args = '-a -b -cfoo -d bar a1 a2'.split() ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
>>> args >>> optlist, args = getopt.getopt(args, 'abc:d:')
['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] >>> optlist
>>> optlist, args = getopt.getopt(args, 'abc:d:') [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
>>> optlist >>> args
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] ['a1', 'a2']
>>> args
['a1', 'a2'] Using long option names is equally easy:
Using long option names is equally easy:
>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
>>> args = s.split()
>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' >>> args
>>> args = s.split() ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
>>> args >>> optlist, args = getopt.getopt(args, 'x', [
['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] ... 'condition=', 'output-file=', 'testing'])
>>> optlist, args = getopt.getopt(args, 'x', [ >>> optlist
... 'condition=', 'output-file=', 'testing']) [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
>>> optlist >>> args
[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')] ['a1', 'a2']
>>> args """
['a1', 'a2']
import new
""" m = new.module("libreftest", s)
run_doctest(m, verbose)
__test__ = {'libreftest' : libreftest}
import sys def test_main():
run_doctest(sys.modules[__name__], verbose) run_unittest(GetoptTests)
#------------------------------------------------------------------------------ if __name__ == "__main__":
test_main()
if verbose:
print "Module getopt: tests completed successfully."
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment