Hashes: simplify test suites

- Use hasattr instead of assigning the import availability to a variable
- Use a simple if statement instead of redefining skipIf
This commit is contained in:
James Lu 2017-11-09 09:04:21 -08:00
parent 41830a46aa
commit 287610b776

View File

@ -28,29 +28,11 @@
###
import re
import hashlib
from supybot.test import *
import supybot.utils as utils
try:
from unittest import skipIf
except ImportError: # Python 2.6
def skipIf(cond, reason):
if cond:
print('Skipped: %s' % reason)
def decorator(f):
return None
else:
def decorator(f):
return f
return decorator
try:
from hashlib import algorithms_available
has_algos = True
except ImportError:
has_algos = False
brown_fox = 'The quick brown fox jumped over the lazy dog'
class HashesTestCase(PluginTestCase):
@ -61,8 +43,12 @@ class HashesTestCase(PluginTestCase):
self.assertResponse('sha %s' % brown_fox, 'f6513640f3045e9768b239785625caa6a2588842')
self.assertResponse('sha256 %s' % brown_fox, '7d38b5cd25a2baf85ad3bb5b9311383e671a8a142eb302b324d4a5fba8748c69')
self.assertResponse('sha512 %s' % brown_fox, 'db25330cfa5d14eaadf11a6263371cfa0e70fcd7a63a433b91f2300ca25d45b66a7b50d2f6747995c8fa0ff365b28974792e7acd5624e1ddd0d66731f346f0e7')
@skipIf(not has_algos, "'hashlib.algorithms_available' is not available")
def testHashNew(self):
self.assertResponse('mkhash md5 %s' % brown_fox, '08a008a01d498c404b0c30852b39d3b8')
self.assertError('mkhash NonExistant %s' % brown_fox)
self.assertNotError('algorithms')
if hasattr(hashlib, 'algorithms_available'):
def testMkhash(self):
self.assertResponse('mkhash md5 %s' % brown_fox, '08a008a01d498c404b0c30852b39d3b8')
self.assertError('mkhash NonExistant %s' % brown_fox)
self.assertNotError('algorithms')
else:
print("Hashes: Skipping algorithms/mkhash tests as 'hashlib.algorithms_available' is not available.")