Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
82c36715
Commit
82c36715
authored
Apr 04, 2007
by
Collin Winter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make test_getopt use unittest.
parent
3bf573f9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
173 additions
and
174 deletions
+173
-174
Lib/test/test_getopt.py
Lib/test/test_getopt.py
+173
-174
No files found.
Lib/test/test_getopt.py
View file @
82c36715
# test_getopt.py
# David Goodger <dgoodger@bigfoot.com> 2000-08-19
from
test.test_support
import
verbose
,
run_doctest
,
run_unittest
import
unittest
import
getopt
from
getopt
import
GetoptError
from
test.test_support
import
verify
,
verbose
,
run_doctest
import
os
def
expectException
(
teststr
,
expected
,
failure
=
AssertionError
):
"""Executes a statement passed in teststr, and raises an exception
(failure) if the expected exception is *not* raised."""
try
:
exec
teststr
except
expected
:
pass
else
:
raise
failure
old_posixly_correct
=
os
.
environ
.
get
(
"POSIXLY_CORRECT"
)
if
old_posixly_correct
is
not
None
:
del
os
.
environ
[
"POSIXLY_CORRECT"
]
if
verbose
:
print
'Running tests on getopt.short_has_arg'
verify
(
getopt
.
short_has_arg
(
'a'
,
'a:'
))
verify
(
not
getopt
.
short_has_arg
(
'a'
,
'a'
))
expectException
(
"tmp = getopt.short_has_arg('a', 'b')"
,
GetoptError
)
expectException
(
"tmp = getopt.short_has_arg('a', '')"
,
GetoptError
)
if
verbose
:
print
'Running tests on getopt.long_has_args'
has_arg
,
option
=
getopt
.
long_has_args
(
'abc'
,
[
'abc='
])
verify
(
has_arg
)
verify
(
option
==
'abc'
)
has_arg
,
option
=
getopt
.
long_has_args
(
'abc'
,
[
'abc'
])
verify
(
not
has_arg
)
verify
(
option
==
'abc'
)
has_arg
,
option
=
getopt
.
long_has_args
(
'abc'
,
[
'abcd'
])
verify
(
not
has_arg
)
verify
(
option
==
'abcd'
)
expectException
(
"has_arg, option = getopt.long_has_args('abc', ['def'])"
,
GetoptError
)
expectException
(
"has_arg, option = getopt.long_has_args('abc', [])"
,
GetoptError
)
expectException
(
"has_arg, option = "
+
\
"getopt.long_has_args('abc', ['abcd','abcde'])"
,
GetoptError
)
if
verbose
:
print
'Running tests on getopt.do_shorts'
opts
,
args
=
getopt
.
do_shorts
([],
'a'
,
'a'
,
[])
verify
(
opts
==
[(
'-a'
,
''
)])
verify
(
args
==
[])
opts
,
args
=
getopt
.
do_shorts
([],
'a1'
,
'a:'
,
[])
verify
(
opts
==
[(
'-a'
,
'1'
)])
verify
(
args
==
[])
#opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
#verify(opts == [('-a', '1')])
#verify(args == [])
opts
,
args
=
getopt
.
do_shorts
([],
'a'
,
'a:'
,
[
'1'
])
verify
(
opts
==
[(
'-a'
,
'1'
)])
verify
(
args
==
[])
opts
,
args
=
getopt
.
do_shorts
([],
'a'
,
'a:'
,
[
'1'
,
'2'
])
verify
(
opts
==
[(
'-a'
,
'1'
)])
verify
(
args
==
[
'2'
])
expectException
(
"opts, args = getopt.do_shorts([], 'a1', 'a', [])"
,
GetoptError
)
expectException
(
"opts, args = getopt.do_shorts([], 'a', 'a:', [])"
,
GetoptError
)
if
verbose
:
print
'Running tests on getopt.do_longs'
opts
,
args
=
getopt
.
do_longs
([],
'abc'
,
[
'abc'
],
[])
verify
(
opts
==
[(
'--abc'
,
''
)])
verify
(
args
==
[])
opts
,
args
=
getopt
.
do_longs
([],
'abc=1'
,
[
'abc='
],
[])
verify
(
opts
==
[(
'--abc'
,
'1'
)])
verify
(
args
==
[])
opts
,
args
=
getopt
.
do_longs
([],
'abc=1'
,
[
'abcd='
],
[])
verify
(
opts
==
[(
'--abcd'
,
'1'
)])
verify
(
args
==
[])
opts
,
args
=
getopt
.
do_longs
([],
'abc'
,
[
'ab'
,
'abc'
,
'abcd'
],
[])
verify
(
opts
==
[(
'--abc'
,
''
)])
verify
(
args
==
[])
# Much like the preceding, except with a non-alpha character ("-") in
# option name that precedes "="; failed in
# http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470
opts
,
args
=
getopt
.
do_longs
([],
'foo=42'
,
[
'foo-bar'
,
'foo='
,],
[])
verify
(
opts
==
[(
'--foo'
,
'42'
)])
verify
(
args
==
[])
expectException
(
"opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])"
,
GetoptError
)
expectException
(
"opts, args = getopt.do_longs([], 'abc', ['abc='], [])"
,
GetoptError
)
# note: the empty string between '-a' and '--beta' is significant:
# it simulates an empty string option argument ('-a ""') on the command line.
cmdline
=
[
'-a'
,
'1'
,
'-b'
,
'--alpha=2'
,
'--beta'
,
'-a'
,
'3'
,
'-a'
,
''
,
'--beta'
,
'arg1'
,
'arg2'
]
if
verbose
:
print
'Running tests on getopt.getopt'
opts
,
args
=
getopt
.
getopt
(
cmdline
,
'a:b'
,
[
'alpha='
,
'beta'
])
verify
(
opts
==
[(
'-a'
,
'1'
),
(
'-b'
,
''
),
(
'--alpha'
,
'2'
),
(
'--beta'
,
''
),
(
'-a'
,
'3'
),
(
'-a'
,
''
),
(
'--beta'
,
''
)]
)
# Note ambiguity of ('-b', '') and ('-a', '') above. This must be
# accounted for in the code that calls getopt().
verify
(
args
==
[
'arg1'
,
'arg2'
])
expectException
(
"opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])"
,
GetoptError
)
# Test handling of GNU style scanning mode.
if
verbose
:
print
'Running tests on getopt.gnu_getopt'
cmdline
=
[
'-a'
,
'arg1'
,
'-b'
,
'1'
,
'--alpha'
,
'--beta=2'
]
# GNU style
opts
,
args
=
getopt
.
gnu_getopt
(
cmdline
,
'ab:'
,
[
'alpha'
,
'beta='
])
verify
(
opts
==
[(
'-a'
,
''
),
(
'-b'
,
'1'
),
(
'--alpha'
,
''
),
(
'--beta'
,
'2'
)])
verify
(
args
==
[
'arg1'
])
# Posix style via +
opts
,
args
=
getopt
.
gnu_getopt
(
cmdline
,
'+ab:'
,
[
'alpha'
,
'beta='
])
verify
(
opts
==
[(
'-a'
,
''
)])
verify
(
args
==
[
'arg1'
,
'-b'
,
'1'
,
'--alpha'
,
'--beta=2'
])
# Posix style via POSIXLY_CORRECT
os
.
environ
[
"POSIXLY_CORRECT"
]
=
"1"
opts
,
args
=
getopt
.
gnu_getopt
(
cmdline
,
'ab:'
,
[
'alpha'
,
'beta='
])
verify
(
opts
==
[(
'-a'
,
''
)])
verify
(
args
==
[
'arg1'
,
'-b'
,
'1'
,
'--alpha'
,
'--beta=2'
])
if
old_posixly_correct
is
None
:
del
os
.
environ
[
"POSIXLY_CORRECT"
]
else
:
os
.
environ
[
"POSIXLY_CORRECT"
]
=
old_posixly_correct
#------------------------------------------------------------------------------
libreftest
=
"""
Examples from the Library Reference: Doc/lib/libgetopt.tex
An example using only Unix style options:
>>> import getopt
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
>>> args
['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'abc:d:')
>>> optlist
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
>>> args
['a1', 'a2']
Using long option names is equally easy:
>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
>>> args = s.split()
>>> args
['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'x', [
... 'condition=', 'output-file=', 'testing'])
>>> optlist
[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
>>> args
['a1', 'a2']
"""
__test__
=
{
'libreftest'
:
libreftest
}
import
sys
run_doctest
(
sys
.
modules
[
__name__
],
verbose
)
#------------------------------------------------------------------------------
if
verbose
:
print
"Module getopt: tests completed successfully."
sentinel
=
object
()
class
GetoptTests
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
old_posixly_correct
=
os
.
environ
.
get
(
"POSIXLY_CORRECT"
,
sentinel
)
if
self
.
old_posixly_correct
is
not
sentinel
:
del
os
.
environ
[
"POSIXLY_CORRECT"
]
def
tearDown
(
self
):
if
self
.
old_posixly_correct
is
sentinel
:
os
.
environ
.
pop
(
"POSIXLY_CORRECT"
,
None
)
else
:
os
.
environ
[
"POSIXLY_CORRECT"
]
=
self
.
old_posixly_correct
def
assertError
(
self
,
*
args
,
**
kwargs
):
self
.
assertRaises
(
getopt
.
GetoptError
,
*
args
,
**
kwargs
)
def
test_short_has_arg
(
self
):
self
.
failUnless
(
getopt
.
short_has_arg
(
'a'
,
'a:'
))
self
.
failIf
(
getopt
.
short_has_arg
(
'a'
,
'a'
))
self
.
assertError
(
getopt
.
short_has_arg
,
'a'
,
'b'
)
def
test_long_has_args
(
self
):
has_arg
,
option
=
getopt
.
long_has_args
(
'abc'
,
[
'abc='
])
self
.
failUnless
(
has_arg
)
self
.
assertEqual
(
option
,
'abc'
)
has_arg
,
option
=
getopt
.
long_has_args
(
'abc'
,
[
'abc'
])
self
.
failIf
(
has_arg
)
self
.
assertEqual
(
option
,
'abc'
)
has_arg
,
option
=
getopt
.
long_has_args
(
'abc'
,
[
'abcd'
])
self
.
failIf
(
has_arg
)
self
.
assertEqual
(
option
,
'abcd'
)
self
.
assertError
(
getopt
.
long_has_args
,
'abc'
,
[
'def'
])
self
.
assertError
(
getopt
.
long_has_args
,
'abc'
,
[])
self
.
assertError
(
getopt
.
long_has_args
,
'abc'
,
[
'abcd'
,
'abcde'
])
def
test_do_shorts
(
self
):
opts
,
args
=
getopt
.
do_shorts
([],
'a'
,
'a'
,
[])
self
.
assertEqual
(
opts
,
[(
'-a'
,
''
)])
self
.
assertEqual
(
args
,
[])
opts
,
args
=
getopt
.
do_shorts
([],
'a1'
,
'a:'
,
[])
self
.
assertEqual
(
opts
,
[(
'-a'
,
'1'
)])
self
.
assertEqual
(
args
,
[])
#opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
#self.assertEqual(opts, [('-a', '1')])
#self.assertEqual(args, [])
opts
,
args
=
getopt
.
do_shorts
([],
'a'
,
'a:'
,
[
'1'
])
self
.
assertEqual
(
opts
,
[(
'-a'
,
'1'
)])
self
.
assertEqual
(
args
,
[])
opts
,
args
=
getopt
.
do_shorts
([],
'a'
,
'a:'
,
[
'1'
,
'2'
])
self
.
assertEqual
(
opts
,
[(
'-a'
,
'1'
)])
self
.
assertEqual
(
args
,
[
'2'
])
self
.
assertError
(
getopt
.
do_shorts
,
[],
'a1'
,
'a'
,
[])
self
.
assertError
(
getopt
.
do_shorts
,
[],
'a'
,
'a:'
,
[])
def
test_do_longs
(
self
):
opts
,
args
=
getopt
.
do_longs
([],
'abc'
,
[
'abc'
],
[])
self
.
assertEqual
(
opts
,
[(
'--abc'
,
''
)])
self
.
assertEqual
(
args
,
[])
opts
,
args
=
getopt
.
do_longs
([],
'abc=1'
,
[
'abc='
],
[])
self
.
assertEqual
(
opts
,
[(
'--abc'
,
'1'
)])
self
.
assertEqual
(
args
,
[])
opts
,
args
=
getopt
.
do_longs
([],
'abc=1'
,
[
'abcd='
],
[])
self
.
assertEqual
(
opts
,
[(
'--abcd'
,
'1'
)])
self
.
assertEqual
(
args
,
[])
opts
,
args
=
getopt
.
do_longs
([],
'abc'
,
[
'ab'
,
'abc'
,
'abcd'
],
[])
self
.
assertEqual
(
opts
,
[(
'--abc'
,
''
)])
self
.
assertEqual
(
args
,
[])
# Much like the preceding, except with a non-alpha character ("-") in
# option name that precedes "="; failed in
# http://python.org/sf/126863
opts
,
args
=
getopt
.
do_longs
([],
'foo=42'
,
[
'foo-bar'
,
'foo='
,],
[])
self
.
assertEqual
(
opts
,
[(
'--foo'
,
'42'
)])
self
.
assertEqual
(
args
,
[])
self
.
assertError
(
getopt
.
do_longs
,
[],
'abc=1'
,
[
'abc'
],
[])
self
.
assertError
(
getopt
.
do_longs
,
[],
'abc'
,
[
'abc='
],
[])
def
test_getopt
(
self
):
# note: the empty string between '-a' and '--beta' is significant:
# it simulates an empty string option argument ('-a ""') on the
# command line.
cmdline
=
[
'-a'
,
'1'
,
'-b'
,
'--alpha=2'
,
'--beta'
,
'-a'
,
'3'
,
'-a'
,
''
,
'--beta'
,
'arg1'
,
'arg2'
]
opts
,
args
=
getopt
.
getopt
(
cmdline
,
'a:b'
,
[
'alpha='
,
'beta'
])
self
.
assertEqual
(
opts
,
[(
'-a'
,
'1'
),
(
'-b'
,
''
),
(
'--alpha'
,
'2'
),
(
'--beta'
,
''
),
(
'-a'
,
'3'
),
(
'-a'
,
''
),
(
'--beta'
,
''
)])
# Note ambiguity of ('-b', '') and ('-a', '') above. This must be
# accounted for in the code that calls getopt().
self
.
assertEqual
(
args
,
[
'arg1'
,
'arg2'
])
self
.
assertError
(
getopt
.
getopt
,
cmdline
,
'a:b'
,
[
'alpha'
,
'beta'
])
def
test_gnu_getopt
(
self
):
# Test handling of GNU style scanning mode.
cmdline
=
[
'-a'
,
'arg1'
,
'-b'
,
'1'
,
'--alpha'
,
'--beta=2'
]
# GNU style
opts
,
args
=
getopt
.
gnu_getopt
(
cmdline
,
'ab:'
,
[
'alpha'
,
'beta='
])
self
.
assertEqual
(
args
,
[
'arg1'
])
self
.
assertEqual
(
opts
,
[(
'-a'
,
''
),
(
'-b'
,
'1'
),
(
'--alpha'
,
''
),
(
'--beta'
,
'2'
)])
# Posix style via +
opts
,
args
=
getopt
.
gnu_getopt
(
cmdline
,
'+ab:'
,
[
'alpha'
,
'beta='
])
self
.
assertEqual
(
opts
,
[(
'-a'
,
''
)])
self
.
assertEqual
(
args
,
[
'arg1'
,
'-b'
,
'1'
,
'--alpha'
,
'--beta=2'
])
# Posix style via POSIXLY_CORRECT
os
.
environ
[
"POSIXLY_CORRECT"
]
=
"1"
opts
,
args
=
getopt
.
gnu_getopt
(
cmdline
,
'ab:'
,
[
'alpha'
,
'beta='
])
self
.
assertEqual
(
opts
,
[(
'-a'
,
''
)])
self
.
assertEqual
(
args
,
[
'arg1'
,
'-b'
,
'1'
,
'--alpha'
,
'--beta=2'
])
def
test_libref_examples
(
self
):
s
=
"""
Examples from the Library Reference: Doc/lib/libgetopt.tex
An example using only Unix style options:
>>> import getopt
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
>>> args
['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'abc:d:')
>>> optlist
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
>>> args
['a1', 'a2']
Using long option names is equally easy:
>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
>>> args = s.split()
>>> args
['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'x', [
... 'condition=', 'output-file=', 'testing'])
>>> optlist
[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
>>> args
['a1', 'a2']
"""
import
new
m
=
new
.
module
(
"libreftest"
,
s
)
run_doctest
(
m
,
verbose
)
def
test_main
():
run_unittest
(
GetoptTests
)
if
__name__
==
"__main__"
:
test_main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment