Files
python/exercises/simple-cipher/simple_cipher_test.py

79 lines
2.7 KiB
Python
Raw Normal View History

2014-04-08 00:17:34 -03:00
import unittest
from simple_cipher import Caesar, Cipher
2014-04-08 00:17:34 -03:00
class CipherTest(unittest.TestCase):
def test_caesar_encode1(self):
self.assertEqual(Caesar().encode('itisawesomeprogramminginpython'),
'lwlvdzhvrphsurjudpplqjlqsbwkrq')
2014-04-08 00:17:34 -03:00
def test_caesar_encode2(self):
self.assertEqual(Caesar().encode('venividivici'), 'yhqlylglylfl')
2014-04-08 00:17:34 -03:00
def test_caesar_encode3(self):
self.assertEqual(Caesar().encode('\'Twas the night before Christmas'),
'wzdvwkhqljkwehiruhfkulvwpdv')
2014-04-08 00:17:34 -03:00
def test_caesar_encode_with_numbers(self):
self.assertEqual(Caesar().encode('1, 2, 3, Go!'), 'jr')
2014-04-08 00:17:34 -03:00
def test_caesar_decode(self):
self.assertEqual(Caesar().decode('yhqlylglylfl'), 'venividivici')
2014-04-08 00:17:34 -03:00
def test_cipher_encode1(self):
c = Cipher('a')
self.assertEqual(
c.encode('itisawesomeprogramminginpython'),
'itisawesomeprogramminginpython')
2014-04-08 00:17:34 -03:00
def test_cipher_encode2(self):
c = Cipher('aaaaaaaaaaaaaaaaaaaaaa')
self.assertEqual(
c.encode('itisawesomeprogramminginpython'),
'itisawesomeprogramminginpython')
2014-04-08 00:17:34 -03:00
def test_cipher_encode3(self):
c = Cipher('dddddddddddddddddddddd')
self.assertEqual(c.encode('venividivici'), 'yhqlylglylfl')
2014-04-08 00:17:34 -03:00
def test_cipher_encode4(self):
2015-11-09 11:10:22 +01:00
key = ('duxrceqyaimciuucnelkeoxjhdyduucpmrxmaivacmybmsdrzwqxvbxsy'
'gzsabdjmdjabeorttiwinfrpmpogvabiofqexnohrqu')
2014-04-08 00:17:34 -03:00
c = Cipher(key)
self.assertEqual(c.encode('diffiehellman'), 'gccwkixcltycv')
2014-04-08 00:17:34 -03:00
def test_cipher_encode_short_key(self):
c = Cipher('abcd')
self.assertEqual(c.encode('aaaaaaaa'), 'abcdabcd')
2014-04-08 00:17:34 -03:00
def test_cipher_compositiion1(self):
2015-11-09 11:10:22 +01:00
key = ('duxrceqyaimciuucnelkeoxjhdyduucpmrxmaivacmybmsdrzwqxvbxsy'
'gzsabdjmdjabeorttiwinfrpmpogvabiofqexnohrqu')
2014-04-08 00:17:34 -03:00
plaintext = 'adaywithoutlaughterisadaywasted'
c = Cipher(key)
self.assertEqual(c.decode(c.encode(plaintext)), plaintext)
2014-04-08 00:17:34 -03:00
def test_cipher_compositiion2(self):
plaintext = 'adaywithoutlaughterisadaywasted'
c = Cipher()
self.assertEqual(c.decode(c.encode(plaintext)), plaintext)
2014-04-08 00:17:34 -03:00
def test_cipher_random_key(self):
c = Cipher()
self.assertTrue(
len(c.key) >= 100,
'A random key must be generated when no key is given!')
self.assertTrue(c.key.islower() and c.key.isalpha(),
2015-11-09 11:10:22 +01:00
'All items in the key must be chars and lowercase!')
def test_cipher_wrong_key(self):
with self.assertRaises(ValueError):
Cipher('a1cde')
with self.assertRaises(ValueError):
Cipher('aBcde')
2014-04-08 00:17:34 -03:00
if __name__ == '__main__':
unittest.main()