Skip to content

Commit

Permalink
Fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
manicmaniac committed Aug 19, 2022
1 parent 4027ad1 commit 1f81c86
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion test_arc4.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ def decorated(self, *args, **kwargs):
return decorated


def raises_deprecation_warning(f):
@functools.wraps(f)
def decorated(self, *args, **kwargs):
with self.assertWarns(DeprecationWarning):
return f(self, *args, **kwargs)
return decorated


def raises_deprecation_warning_if(condition):
if condition:
return raises_deprecation_warning
return lambda x: x


def expected_failure_if(condition):
if condition:
return unittest.expectedFailure
Expand Down Expand Up @@ -103,6 +117,7 @@ def test_init_with_bytes_returns_instance(self):
self.assertIsInstance(arc4.ARC4(b'spam'), arc4.ARC4)

@raises_unicode_encode_error_on_python_2
@raises_deprecation_warning_if(sys.version_info.major >= 3)
def test_init_with_unicode_returns_instance(self):
self.assertIsInstance(arc4.ARC4(u'スパム'), arc4.ARC4)

Expand All @@ -112,12 +127,14 @@ def test_init_with_unicode_returns_instance(self):
def test_init_with_buffer_returns_instance(self):
self.assertIsInstance(arc4.ARC4(builtins.buffer('spam')), arc4.ARC4)

@raises_deprecation_warning_if(platform.python_implementation() == 'PyPy')
def test_init_with_bytearray_raises_type_error(self):
with self.assertRaisesRegex(
TypeError,
r'argument 1 must be .*, not bytearray'):
arc4.ARC4(bytearray([0x66, 0x6f, 0x6f]))

@raises_deprecation_warning_if(platform.python_implementation() == 'PyPy')
def test_init_with_memoryview_raises_type_error(self):
if (platform.python_implementation() == 'PyPy' and
sys.version_info.major <= 2):
Expand All @@ -140,11 +157,12 @@ def test_encrypt_multiple_times_returns_encrypted_bytes(self):
encrypted = b''
for c in LOREM:
if isinstance(c, int):
c = chr(c)
c = chr(c).encode('utf-8')
encrypted += cipher.encrypt(c)
self.assertEqual(LOREM_ARC4, encrypted)

@raises_unicode_encode_error_on_python_2
@raises_deprecation_warning_if(sys.version_info.major >= 3)
def test_encrypt_with_unicode_returns_encrypted_bytes(self):
cipher = arc4.ARC4(b'spam')
self.assertEqual(b'Q\xcd\xb1!\xecg', cipher.encrypt(u'ハム'))
Expand Down

0 comments on commit 1f81c86

Please sign in to comment.