Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
82c36715
Kaydet (Commit)
82c36715
authored
Nis 04, 2007
tarafından
Collin Winter
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Make test_getopt use unittest.
üst
3bf573f9
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
170 additions
and
171 deletions
+170
-171
test_getopt.py
Lib/test/test_getopt.py
+170
-171
No files found.
Lib/test/test_getopt.py
Dosyayı görüntüle @
82c36715
# 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."""
try
:
exec
teststr
except
expected
:
pass
else
:
raise
failure
old_posixly_correct
=
os
.
environ
.
get
(
"POSIXLY_CORRECT"
)
class
GetoptTests
(
unittest
.
TestCase
):
if
old_posixly_correct
is
not
None
:
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"
]
del
os
.
environ
[
"POSIXLY_CORRECT"
]
if
verbose
:
def
tearDown
(
self
):
print
'Running tests on getopt.short_has_arg'
if
self
.
old_posixly_correct
is
sentinel
:
verify
(
getopt
.
short_has_arg
(
'a'
,
'a:'
))
os
.
environ
.
pop
(
"POSIXLY_CORRECT"
,
None
)
verify
(
not
getopt
.
short_has_arg
(
'a'
,
'a'
))
else
:
expectException
(
"tmp = getopt.short_has_arg('a', 'b')"
,
GetoptError
)
os
.
environ
[
"POSIXLY_CORRECT"
]
=
self
.
old_posixly_correct
expectException
(
"tmp = getopt.short_has_arg('a', '')"
,
GetoptError
)
def
assertError
(
self
,
*
args
,
**
kwargs
):
if
verbose
:
self
.
assertRaises
(
getopt
.
GetoptError
,
*
args
,
**
kwargs
)
print
'Running tests on getopt.long_has_args'
has_arg
,
option
=
getopt
.
long_has_args
(
'abc'
,
[
'abc='
])
def
test_short_has_arg
(
self
):
verify
(
has_arg
)
self
.
failUnless
(
getopt
.
short_has_arg
(
'a'
,
'a:'
))
verify
(
option
==
'abc'
)
self
.
failIf
(
getopt
.
short_has_arg
(
'a'
,
'a'
))
has_arg
,
option
=
getopt
.
long_has_args
(
'abc'
,
[
'abc'
])
self
.
assertError
(
getopt
.
short_has_arg
,
'a'
,
'b'
)
verify
(
not
has_arg
)
verify
(
option
==
'abc'
)
def
test_long_has_args
(
self
):
has_arg
,
option
=
getopt
.
long_has_args
(
'abc'
,
[
'abcd'
])
has_arg
,
option
=
getopt
.
long_has_args
(
'abc'
,
[
'abc='
])
verify
(
not
has_arg
)
self
.
failUnless
(
has_arg
)
verify
(
option
==
'abcd'
)
self
.
assertEqual
(
option
,
'abc'
)
expectException
(
"has_arg, option = getopt.long_has_args('abc', ['def'])"
,
GetoptError
)
has_arg
,
option
=
getopt
.
long_has_args
(
'abc'
,
[
'abc'
])
expectException
(
"has_arg, option = getopt.long_has_args('abc', [])"
,
self
.
failIf
(
has_arg
)
GetoptError
)
self
.
assertEqual
(
option
,
'abc'
)
expectException
(
"has_arg, option = "
+
\
"getopt.long_has_args('abc', ['abcd','abcde'])"
,
has_arg
,
option
=
getopt
.
long_has_args
(
'abc'
,
[
'abcd'
])
GetoptError
)
self
.
failIf
(
has_arg
)
self
.
assertEqual
(
option
,
'abcd'
)
if
verbose
:
print
'Running tests on getopt.do_shorts'
self
.
assertError
(
getopt
.
long_has_args
,
'abc'
,
[
'def'
])
opts
,
args
=
getopt
.
do_shorts
([],
'a'
,
'a'
,
[])
self
.
assertError
(
getopt
.
long_has_args
,
'abc'
,
[])
verify
(
opts
==
[(
'-a'
,
''
)])
self
.
assertError
(
getopt
.
long_has_args
,
'abc'
,
[
'abcd'
,
'abcde'
])
verify
(
args
==
[])
opts
,
args
=
getopt
.
do_shorts
([],
'a1'
,
'a:'
,
[])
def
test_do_shorts
(
self
):
verify
(
opts
==
[(
'-a'
,
'1'
)])
opts
,
args
=
getopt
.
do_shorts
([],
'a'
,
'a'
,
[])
verify
(
args
==
[])
self
.
assertEqual
(
opts
,
[(
'-a'
,
''
)])
#opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
self
.
assertEqual
(
args
,
[])
#verify(opts == [('-a', '1')])
#verify(args == [])
opts
,
args
=
getopt
.
do_shorts
([],
'a1'
,
'a:'
,
[])
opts
,
args
=
getopt
.
do_shorts
([],
'a'
,
'a:'
,
[
'1'
])
self
.
assertEqual
(
opts
,
[(
'-a'
,
'1'
)])
verify
(
opts
==
[(
'-a'
,
'1'
)])
self
.
assertEqual
(
args
,
[])
verify
(
args
==
[])
opts
,
args
=
getopt
.
do_shorts
([],
'a'
,
'a:'
,
[
'1'
,
'2'
])
#opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
verify
(
opts
==
[(
'-a'
,
'1'
)])
#self.assertEqual(opts, [('-a', '1')])
verify
(
args
==
[
'2'
])
#self.assertEqual(args, [])
expectException
(
"opts, args = getopt.do_shorts([], 'a1', 'a', [])"
,
GetoptError
)
opts
,
args
=
getopt
.
do_shorts
([],
'a'
,
'a:'
,
[
'1'
])
expectException
(
"opts, args = getopt.do_shorts([], 'a', 'a:', [])"
,
self
.
assertEqual
(
opts
,
[(
'-a'
,
'1'
)])
GetoptError
)
self
.
assertEqual
(
args
,
[])
if
verbose
:
opts
,
args
=
getopt
.
do_shorts
([],
'a'
,
'a:'
,
[
'1'
,
'2'
])
print
'Running tests on getopt.do_longs'
self
.
assertEqual
(
opts
,
[(
'-a'
,
'1'
)])
opts
,
args
=
getopt
.
do_longs
([],
'abc'
,
[
'abc'
],
[])
self
.
assertEqual
(
args
,
[
'2'
])
verify
(
opts
==
[(
'--abc'
,
''
)])
verify
(
args
==
[])
self
.
assertError
(
getopt
.
do_shorts
,
[],
'a1'
,
'a'
,
[])
opts
,
args
=
getopt
.
do_longs
([],
'abc=1'
,
[
'abc='
],
[])
self
.
assertError
(
getopt
.
do_shorts
,
[],
'a'
,
'a:'
,
[])
verify
(
opts
==
[(
'--abc'
,
'1'
)])
verify
(
args
==
[])
def
test_do_longs
(
self
):
opts
,
args
=
getopt
.
do_longs
([],
'abc=1'
,
[
'abcd='
],
[])
opts
,
args
=
getopt
.
do_longs
([],
'abc'
,
[
'abc'
],
[])
verify
(
opts
==
[(
'--abcd'
,
'1'
)])
self
.
assertEqual
(
opts
,
[(
'--abc'
,
''
)])
verify
(
args
==
[])
self
.
assertEqual
(
args
,
[])
opts
,
args
=
getopt
.
do_longs
([],
'abc'
,
[
'ab'
,
'abc'
,
'abcd'
],
[])
verify
(
opts
==
[(
'--abc'
,
''
)])
opts
,
args
=
getopt
.
do_longs
([],
'abc=1'
,
[
'abc='
],
[])
verify
(
args
==
[])
self
.
assertEqual
(
opts
,
[(
'--abc'
,
'1'
)])
# Much like the preceding, except with a non-alpha character ("-") in
self
.
assertEqual
(
args
,
[])
# option name that precedes "="; failed in
# http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470
opts
,
args
=
getopt
.
do_longs
([],
'abc=1'
,
[
'abcd='
],
[])
opts
,
args
=
getopt
.
do_longs
([],
'foo=42'
,
[
'foo-bar'
,
'foo='
,],
[])
self
.
assertEqual
(
opts
,
[(
'--abcd'
,
'1'
)])
verify
(
opts
==
[(
'--foo'
,
'42'
)])
self
.
assertEqual
(
args
,
[])
verify
(
args
==
[])
expectException
(
"opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])"
,
opts
,
args
=
getopt
.
do_longs
([],
'abc'
,
[
'ab'
,
'abc'
,
'abcd'
],
[])
GetoptError
)
self
.
assertEqual
(
opts
,
[(
'--abc'
,
''
)])
expectException
(
"opts, args = getopt.do_longs([], 'abc', ['abc='], [])"
,
self
.
assertEqual
(
args
,
[])
GetoptError
)
# Much like the preceding, except with a non-alpha character ("-") in
# note: the empty string between '-a' and '--beta' is significant:
# option name that precedes "="; failed in
# it simulates an empty string option argument ('-a ""') on the command line.
# http://python.org/sf/126863
cmdline
=
[
'-a'
,
'1'
,
'-b'
,
'--alpha=2'
,
'--beta'
,
'-a'
,
'3'
,
'-a'
,
''
,
opts
,
args
=
getopt
.
do_longs
([],
'foo=42'
,
[
'foo-bar'
,
'foo='
,],
[])
'--beta'
,
'arg1'
,
'arg2'
]
self
.
assertEqual
(
opts
,
[(
'--foo'
,
'42'
)])
self
.
assertEqual
(
args
,
[])
if
verbose
:
print
'Running tests on getopt.getopt'
self
.
assertError
(
getopt
.
do_longs
,
[],
'abc=1'
,
[
'abc'
],
[])
opts
,
args
=
getopt
.
getopt
(
cmdline
,
'a:b'
,
[
'alpha='
,
'beta'
])
self
.
assertError
(
getopt
.
do_longs
,
[],
'abc'
,
[
'abc='
],
[])
verify
(
opts
==
[(
'-a'
,
'1'
),
(
'-b'
,
''
),
(
'--alpha'
,
'2'
),
(
'--beta'
,
''
),
(
'-a'
,
'3'
),
(
'-a'
,
''
),
(
'--beta'
,
''
)]
)
def
test_getopt
(
self
):
# Note ambiguity of ('-b', '') and ('-a', '') above. This must be
# note: the empty string between '-a' and '--beta' is significant:
# accounted for in the code that calls getopt().
# it simulates an empty string option argument ('-a ""') on the
verify
(
args
==
[
'arg1'
,
'arg2'
])
# command line.
cmdline
=
[
'-a'
,
'1'
,
'-b'
,
'--alpha=2'
,
'--beta'
,
'-a'
,
'3'
,
'-a'
,
expectException
(
''
,
'--beta'
,
'arg1'
,
'arg2'
]
"opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])"
,
GetoptError
)
opts
,
args
=
getopt
.
getopt
(
cmdline
,
'a:b'
,
[
'alpha='
,
'beta'
])
self
.
assertEqual
(
opts
,
[(
'-a'
,
'1'
),
(
'-b'
,
''
),
# Test handling of GNU style scanning mode.
(
'--alpha'
,
'2'
),
(
'--beta'
,
''
),
if
verbose
:
(
'-a'
,
'3'
),
(
'-a'
,
''
),
(
'--beta'
,
''
)])
print
'Running tests on getopt.gnu_getopt'
# Note ambiguity of ('-b', '') and ('-a', '') above. This must be
cmdline
=
[
'-a'
,
'arg1'
,
'-b'
,
'1'
,
'--alpha'
,
'--beta=2'
]
# accounted for in the code that calls getopt().
# GNU style
self
.
assertEqual
(
args
,
[
'arg1'
,
'arg2'
])
opts
,
args
=
getopt
.
gnu_getopt
(
cmdline
,
'ab:'
,
[
'alpha'
,
'beta='
])
verify
(
opts
==
[(
'-a'
,
''
),
(
'-b'
,
'1'
),
(
'--alpha'
,
''
),
(
'--beta'
,
'2'
)])
self
.
assertError
(
getopt
.
getopt
,
cmdline
,
'a:b'
,
[
'alpha'
,
'beta'
])
verify
(
args
==
[
'arg1'
])
# Posix style via +
def
test_gnu_getopt
(
self
):
opts
,
args
=
getopt
.
gnu_getopt
(
cmdline
,
'+ab:'
,
[
'alpha'
,
'beta='
])
# Test handling of GNU style scanning mode.
verify
(
opts
==
[(
'-a'
,
''
)])
cmdline
=
[
'-a'
,
'arg1'
,
'-b'
,
'1'
,
'--alpha'
,
'--beta=2'
]
verify
(
args
==
[
'arg1'
,
'-b'
,
'1'
,
'--alpha'
,
'--beta=2'
])
# Posix style via POSIXLY_CORRECT
# GNU style
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
(
args
,
[
'arg1'
])
verify
(
opts
==
[(
'-a'
,
''
)])
self
.
assertEqual
(
opts
,
[(
'-a'
,
''
),
(
'-b'
,
'1'
),
verify
(
args
==
[
'arg1'
,
'-b'
,
'1'
,
'--alpha'
,
'--beta=2'
])
(
'--alpha'
,
''
),
(
'--beta'
,
'2'
)])
# Posix style via +
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
# Posix style via POSIXLY_CORRECT
#------------------------------------------------------------------------------
os
.
environ
[
"POSIXLY_CORRECT"
]
=
"1"
opts
,
args
=
getopt
.
gnu_getopt
(
cmdline
,
'ab:'
,
[
'alpha'
,
'beta='
])
libreftest
=
"""
self
.
assertEqual
(
opts
,
[(
'-a'
,
''
)])
Examples from the Library Reference: Doc/lib/libgetopt.tex
self
.
assertEqual
(
args
,
[
'arg1'
,
'-b'
,
'1'
,
'--alpha'
,
'--beta=2'
])
An example using only Unix style options:
def
test_libref_examples
(
self
):
s
=
"""
Examples from the Library Reference: Doc/lib/libgetopt.tex
>>> import getopt
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
An example using only Unix style options:
>>> args
['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'abc:d:')
>>> import getopt
>>> optlist
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
>>> args
>>> args
['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
['a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'abc:d:')
>>> optlist
Using long option names is equally easy:
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
>>> args
['a1', 'a2']
>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
>>> args = s.split()
Using long option names is equally easy:
>>> args
['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'x', [
>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
... 'condition=', 'output-file=', 'testing'])
>>> args = s.split()
>>> optlist
>>> args
[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
>>> args
>>> optlist, args = getopt.getopt(args, 'x', [
['a1', 'a2']
... 'condition=', 'output-file=', 'testing'])
>>> optlist
"""
[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
>>> args
__test__
=
{
'libreftest'
:
libreftest
}
['a1', 'a2']
"""
import
sys
run_doctest
(
sys
.
modules
[
__name__
],
verbose
)
import
new
m
=
new
.
module
(
"libreftest"
,
s
)
#------------------------------------------------------------------------------
run_doctest
(
m
,
verbose
)
if
verbose
:
print
"Module getopt: tests completed successfully."
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