2014-04-08 00:17:34 -03:00
|
|
|
import unittest
|
2018-06-15 08:41:09 -04:00
|
|
|
import re
|
2014-04-08 00:17:34 -03:00
|
|
|
|
2018-06-15 08:41:09 -04:00
|
|
|
from simple_cipher import Cipher
|
2014-06-11 15:06:22 +02:00
|
|
|
|
2014-04-20 19:43:22 +02:00
|
|
|
|
2018-06-15 08:41:09 -04:00
|
|
|
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0
|
2015-09-02 16:14:03 +02:00
|
|
|
|
2018-06-15 08:41:09 -04:00
|
|
|
class SimpleCipherTest(unittest.TestCase):
|
2017-12-12 18:11:43 +00:00
|
|
|
# Utility functions
|
|
|
|
|
def setUp(self):
|
|
|
|
|
try:
|
2018-01-18 10:47:11 -06:00
|
|
|
self.assertRaisesRegex
|
2017-12-12 18:11:43 +00:00
|
|
|
except AttributeError:
|
2018-01-18 10:47:11 -06:00
|
|
|
self.assertRaisesRegex = self.assertRaisesRegexp
|
2017-12-12 18:11:43 +00:00
|
|
|
|
|
|
|
|
def assertRaisesWithMessage(self, exception):
|
|
|
|
|
return self.assertRaisesRegex(exception, r".+")
|
|
|
|
|
|
2014-04-08 00:17:34 -03:00
|
|
|
|
2018-06-15 08:41:09 -04:00
|
|
|
class RandomKeyCipherTest(SimpleCipherTest):
|
|
|
|
|
def test_can_encode(self):
|
|
|
|
|
cipher = Cipher()
|
|
|
|
|
self.assertEqual(cipher.encode('aaaaaaaaaa'), cipher.key[:10])
|
|
|
|
|
|
|
|
|
|
def test_can_decode(self):
|
|
|
|
|
cipher = Cipher()
|
|
|
|
|
plaintext = 'aaaaaaaaaa'
|
|
|
|
|
self.assertEqual(cipher.decode(cipher.key[:len(plaintext)]), plaintext)
|
|
|
|
|
|
|
|
|
|
def test_is_reversible(self):
|
|
|
|
|
cipher = Cipher()
|
|
|
|
|
plaintext = 'abcdefghij'
|
|
|
|
|
self.assertEqual(cipher.decode(cipher.encode(plaintext)), plaintext)
|
|
|
|
|
|
|
|
|
|
def test_key_is_only_made_of_lowercase_letters(self):
|
|
|
|
|
self.assertIsNotNone(re.match('^[a-z]+$', Cipher().key))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SubstitutionCipherTest(SimpleCipherTest):
|
|
|
|
|
def test_can_encode(self):
|
|
|
|
|
cipher = Cipher('abcdefghij')
|
|
|
|
|
self.assertEqual(cipher.encode('aaaaaaaaaa'), cipher.key)
|
|
|
|
|
|
|
|
|
|
def test_can_decode(self):
|
|
|
|
|
cipher = Cipher('abcdefghij')
|
|
|
|
|
self.assertEqual(cipher.decode(cipher.key), 'aaaaaaaaaa')
|
|
|
|
|
|
|
|
|
|
def test_is_reversible(self):
|
|
|
|
|
cipher = Cipher('abcdefghij')
|
|
|
|
|
plaintext = 'abcdefghij'
|
|
|
|
|
self.assertEqual(cipher.decode(cipher.encode(plaintext)), plaintext)
|
|
|
|
|
|
|
|
|
|
def test_can_double_shift_encode(self):
|
|
|
|
|
plaintext = 'iamapandabear'
|
|
|
|
|
cipher = Cipher(plaintext)
|
|
|
|
|
self.assertEqual(cipher.encode(plaintext), 'qayaeaagaciai')
|
|
|
|
|
|
|
|
|
|
def test_can_wrap_on_encode(self):
|
|
|
|
|
cipher = Cipher('abcdefghij')
|
|
|
|
|
self.assertEqual(cipher.encode('zzzzzzzzzz'), 'zabcdefghi')
|
|
|
|
|
|
|
|
|
|
def test_can_wrap_on_decode(self):
|
|
|
|
|
cipher = Cipher('abcdefghij')
|
|
|
|
|
self.assertEqual(cipher.decode('zabcdefghi'), 'zzzzzzzzzz')
|
|
|
|
|
|
|
|
|
|
def test_can_handle_messages_longer_than_key(self):
|
|
|
|
|
cipher = Cipher('abc')
|
|
|
|
|
self.assertEqual(cipher.encode('iamapandabear'), 'iboaqcnecbfcr')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IncorrectKeyCipher(SimpleCipherTest):
|
|
|
|
|
def test_throws_an_error_with_all_uppercase_key(self):
|
|
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
|
|
|
|
Cipher('ABCDEF')
|
|
|
|
|
|
|
|
|
|
def test_throws_an_error_with_a_numeric_key(self):
|
|
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
|
|
|
|
Cipher('12345')
|
|
|
|
|
|
|
|
|
|
def test_throws_an_error_with_empty_key(self):
|
|
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
|
|
|
|
Cipher('')
|
|
|
|
|
|
|
|
|
|
|
2014-04-08 00:17:34 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|