2023-07-15 15:27:31 -07:00
|
|
|
# These tests are auto-generated with test data from:
|
|
|
|
|
# https://github.com/exercism/problem-specifications/tree/main/exercises/simple-cipher/canonical-data.json
|
2023-07-21 16:54:40 -07:00
|
|
|
# File last updated on 2023-07-20
|
2023-07-15 15:27:31 -07:00
|
|
|
|
2023-07-21 16:54:40 -07:00
|
|
|
import re
|
2019-11-11 16:34:39 -03:00
|
|
|
import unittest
|
2014-04-08 00:17:34 -03:00
|
|
|
|
2021-01-31 16:49:12 -05:00
|
|
|
from simple_cipher import (
|
|
|
|
|
Cipher,
|
|
|
|
|
)
|
2014-06-11 15:06:22 +02:00
|
|
|
|
2014-04-08 00:17:34 -03:00
|
|
|
|
2019-11-11 16:34:39 -03:00
|
|
|
class RandomKeyCipherTest(unittest.TestCase):
|
2018-06-15 08:41:09 -04:00
|
|
|
def test_can_encode(self):
|
|
|
|
|
cipher = Cipher()
|
2019-11-11 16:34:39 -03:00
|
|
|
plaintext = "aaaaaaaaaa"
|
|
|
|
|
self.assertEqual(cipher.encode(plaintext), cipher.key[0 : len(plaintext)])
|
2018-06-15 08:41:09 -04:00
|
|
|
|
|
|
|
|
def test_can_decode(self):
|
|
|
|
|
cipher = Cipher()
|
2019-11-11 16:34:39 -03:00
|
|
|
self.assertEqual(cipher.decode(cipher.key[0 : len("aaaaaaaaaa")]), "aaaaaaaaaa")
|
2018-06-15 08:41:09 -04:00
|
|
|
|
|
|
|
|
def test_is_reversible(self):
|
|
|
|
|
cipher = Cipher()
|
2019-11-11 16:34:39 -03:00
|
|
|
plaintext = "abcdefghij"
|
2018-06-15 08:41:09 -04:00
|
|
|
self.assertEqual(cipher.decode(cipher.encode(plaintext)), plaintext)
|
|
|
|
|
|
2019-11-11 16:34:39 -03:00
|
|
|
def test_key_is_made_only_of_lowercase_letters(self):
|
|
|
|
|
self.assertIsNotNone(re.match("^[a-z]+$", Cipher().key))
|
2018-06-15 08:41:09 -04:00
|
|
|
|
|
|
|
|
|
2019-11-11 16:34:39 -03:00
|
|
|
class SubstitutionCipherTest(unittest.TestCase):
|
2018-06-15 08:41:09 -04:00
|
|
|
def test_can_encode(self):
|
2019-11-11 16:34:39 -03:00
|
|
|
cipher = Cipher("abcdefghij")
|
|
|
|
|
plaintext = "aaaaaaaaaa"
|
|
|
|
|
self.assertEqual(cipher.encode(plaintext), cipher.key)
|
2018-06-15 08:41:09 -04:00
|
|
|
|
|
|
|
|
def test_can_decode(self):
|
2019-11-11 16:34:39 -03:00
|
|
|
cipher = Cipher("abcdefghij")
|
|
|
|
|
self.assertEqual(cipher.decode(cipher.key), "aaaaaaaaaa")
|
2018-06-15 08:41:09 -04:00
|
|
|
|
|
|
|
|
def test_is_reversible(self):
|
2019-11-11 16:34:39 -03:00
|
|
|
cipher = Cipher("abcdefghij")
|
|
|
|
|
plaintext = "abcdefghij"
|
2018-06-15 08:41:09 -04:00
|
|
|
self.assertEqual(cipher.decode(cipher.encode(plaintext)), plaintext)
|
|
|
|
|
|
|
|
|
|
def test_can_double_shift_encode(self):
|
2019-11-11 16:34:39 -03:00
|
|
|
cipher = Cipher("iamapandabear")
|
|
|
|
|
plaintext = "iamapandabear"
|
|
|
|
|
self.assertEqual(cipher.encode(plaintext), "qayaeaagaciai")
|
2018-06-15 08:41:09 -04:00
|
|
|
|
|
|
|
|
def test_can_wrap_on_encode(self):
|
2019-11-11 16:34:39 -03:00
|
|
|
cipher = Cipher("abcdefghij")
|
|
|
|
|
plaintext = "zzzzzzzzzz"
|
|
|
|
|
self.assertEqual(cipher.encode(plaintext), "zabcdefghi")
|
2018-06-15 08:41:09 -04:00
|
|
|
|
|
|
|
|
def test_can_wrap_on_decode(self):
|
2019-11-11 16:34:39 -03:00
|
|
|
cipher = Cipher("abcdefghij")
|
|
|
|
|
self.assertEqual(cipher.decode("zabcdefghi"), "zzzzzzzzzz")
|
2018-06-15 08:41:09 -04:00
|
|
|
|
2019-11-11 16:34:39 -03:00
|
|
|
def test_can_encode_messages_longer_than_the_key(self):
|
|
|
|
|
cipher = Cipher("abc")
|
|
|
|
|
plaintext = "iamapandabear"
|
|
|
|
|
self.assertEqual(cipher.encode(plaintext), "iboaqcnecbfcr")
|
2018-06-15 08:41:09 -04:00
|
|
|
|
2019-11-11 16:34:39 -03:00
|
|
|
def test_can_decode_messages_longer_than_the_key(self):
|
|
|
|
|
cipher = Cipher("abc")
|
|
|
|
|
self.assertEqual(cipher.decode("iboaqcnecbfcr"), "iamapandabear")
|