Commit 6eeaddc3 authored by Brett Cannon's avatar Brett Cannon

Convert test_strftime, test_getargs, and test_pep247 to use unittest.

parent 887290d2
"""Test the internal getargs.c implementation """
Test the internal getargs.c implementation
PyArg_ParseTuple() is defined here. PyArg_ParseTuple() is defined here.
...@@ -11,14 +12,23 @@ single case that failed between 2.1 and 2.2a2. ...@@ -11,14 +12,23 @@ single case that failed between 2.1 and 2.2a2.
# verify that the error is propagated properly from the C code back to # verify that the error is propagated properly from the C code back to
# Python. # Python.
# XXX If the encoding succeeds using the current default encoding,
# this test will fail because it does not test the right part of the
# PyArg_ParseTuple() implementation.
from test.test_support import have_unicode
import marshal import marshal
import unittest
from test import test_support
class GetArgsTest(unittest.TestCase):
# If the encoding succeeds using the current default encoding,
# this test will fail because it does not test the right part of the
# PyArg_ParseTuple() implementation.
def test_with_marshal(self):
if not test_support.have_unicode:
return
arg = unicode(r'\222', 'unicode-escape')
self.assertRaises(UnicodeError, marshal.loads, arg)
def test_main():
test_support.run_unittest(GetArgsTest)
if have_unicode: if __name__ == '__main__':
try: test_main()
marshal.loads(unicode(r"\222", 'unicode-escape'))
except UnicodeError:
pass
# """
# Test suite to check compliance with PEP 247, the standard API for Test suite to check compilance with PEP 247, the standard API
# hashing algorithms. for hashing algorithms
# """
import warnings import warnings
warnings.filterwarnings("ignore", "the md5 module is deprecated.*", warnings.filterwarnings('ignore', 'the md5 module is deprecated.*',
DeprecationWarning) DeprecationWarning)
warnings.filterwarnings("ignore", "the sha module is deprecated.*", warnings.filterwarnings('ignore', 'the sha module is deprecated.*',
DeprecationWarning) DeprecationWarning)
import md5, sha, hmac import hmac
from test.test_support import verbose import md5
import sha
import unittest
from test import test_support
def check_hash_module(module, key=None): class Pep247Test(unittest.TestCase):
assert hasattr(module, 'digest_size'), "Must have digest_size"
assert (module.digest_size is None or
module.digest_size > 0), "digest_size must be None or positive"
if key is not None: def check_module(self, module, key=None):
obj1 = module.new(key) self.assert_(hasattr(module, 'digest_size'))
obj2 = module.new(key, "string") self.assert_(module.digest_size is None or module.digest_size > 0)
h1 = module.new(key, "string").digest() if not key is None:
obj3 = module.new(key) ; obj3.update("string") ; h2 = obj3.digest() obj1 = module.new(key)
assert h1 == h2, "Hashes must match" obj2 = module.new(key, 'string')
else: h1 = module.new(key, 'string').digest()
obj1 = module.new() obj3 = module.new(key)
obj2 = module.new("string") obj3.update('string')
h2 = obj3.digest()
else:
obj1 = module.new()
obj2 = module.new('string')
h1 = module.new("string").digest() h1 = module.new('string').digest()
obj3 = module.new() ; obj3.update("string") ; h2 = obj3.digest() obj3 = module.new()
assert h1 == h2, "Hashes must match" obj3.update('string')
h2 = obj3.digest()
assert hasattr(obj1, 'digest_size'), "Objects must have digest_size attr" self.assertEquals(h1, h2)
if module.digest_size is not None:
assert obj1.digest_size == module.digest_size, "digest_size must match"
assert obj1.digest_size == len(h1), "digest_size must match actual size"
obj1.update("string")
obj_copy = obj1.copy()
assert obj1.digest() == obj_copy.digest(), "Copied objects must match"
assert obj1.hexdigest() == obj_copy.hexdigest(), \
"Copied objects must match"
digest, hexdigest = obj1.digest(), obj1.hexdigest()
hd2 = ""
for byte in digest:
hd2 += "%02x" % ord(byte)
assert hd2 == hexdigest, "hexdigest doesn't appear correct"
if verbose: self.assert_(hasattr(obj1, 'digest_size'))
print 'Module', module.__name__, 'seems to comply with PEP 247'
if not module.digest_size is None:
self.assertEquals(obj1.digest_size, module.digest_size)
def test_main(): self.assertEquals(obj1.digest_size, len(h1))
check_hash_module(md5) obj1.update('string')
check_hash_module(sha) obj_copy = obj1.copy()
check_hash_module(hmac, key='abc') self.assertEquals(obj1.digest(), obj_copy.digest())
self.assertEquals(obj1.hexdigest(), obj_copy.hexdigest())
digest, hexdigest = obj1.digest(), obj1.hexdigest()
hd2 = ""
for byte in digest:
hd2 += '%02x' % ord(byte)
self.assertEquals(hd2, hexdigest)
def test_md5(self):
self.check_module(md5)
def test_sha(self):
self.check_module(sha)
def test_hmac(self):
self.check_module(hmac, key='abc')
def test_main():
test_support.run_unittest(Pep247Test)
if __name__ == '__main__': if __name__ == '__main__':
test_main() test_main()
This diff is collapsed.
...@@ -75,6 +75,8 @@ Library ...@@ -75,6 +75,8 @@ Library
Tests Tests
----- -----
- GHOP 293: Convert test_strftime, test_getargs, and test_pep247 to unittest.
- Issue #2055: Convert test_fcntl to unittest. - Issue #2055: Convert test_fcntl to unittest.
- Issue 1960: Convert test_gdbm to unittest. - Issue 1960: Convert test_gdbm to unittest.
......
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