diff options
author | Zeth <theology@gmail.com> | 2019-09-09 14:50:36 (GMT) |
---|---|---|
committer | Steve Dower <steve.dower@python.org> | 2019-09-09 14:50:36 (GMT) |
commit | b3b48c81f09d1472010937f1331c5a208a2a2d48 (patch) | |
tree | 59cd24fc5373649570913e04801f4057675aae10 | |
parent | f5e7f39d2916ed150e80381faed125f405a11e11 (diff) | |
download | cpython-b3b48c81f09d1472010937f1331c5a208a2a2d48.zip cpython-b3b48c81f09d1472010937f1331c5a208a2a2d48.tar.gz cpython-b3b48c81f09d1472010937f1331c5a208a2a2d48.tar.bz2 |
bpo-37876: Tests for ROT-13 codec (GH-15314)
The Rot-13 codec is for educational use but does not have unit tests,
dragging down test coverage. This adds a few very simple tests.
-rw-r--r-- | Lib/test/test_codecs.py | 37 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Tests/2019-08-16-16-15-14.bpo-37876.m3k1w3.rst | 1 |
2 files changed, 38 insertions, 0 deletions
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index ba7f484..b37525b 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3343,5 +3343,42 @@ class LocaleCodecTest(unittest.TestCase): self.assertEqual(str(cm.exception), 'unsupported error handler') +class Rot13Test(unittest.TestCase): + """Test the educational ROT-13 codec.""" + def test_encode(self): + ciphertext = codecs.encode("Caesar liked ciphers", 'rot-13') + self.assertEqual(ciphertext, 'Pnrfne yvxrq pvcuref') + + def test_decode(self): + plaintext = codecs.decode('Rg gh, Oehgr?', 'rot-13') + self.assertEqual(plaintext, 'Et tu, Brute?') + + def test_incremental_encode(self): + encoder = codecs.getincrementalencoder('rot-13')() + ciphertext = encoder.encode('ABBA nag Cheryl Baker') + self.assertEqual(ciphertext, 'NOON ant Purely Onxre') + + def test_incremental_decode(self): + decoder = codecs.getincrementaldecoder('rot-13')() + plaintext = decoder.decode('terra Ares envy tha') + self.assertEqual(plaintext, 'green Nerf rail gun') + + +class Rot13UtilTest(unittest.TestCase): + """Test the ROT-13 codec via rot13 function, + i.e. the user has done something like: + $ echo "Hello World" | python -m encodings.rot_13 + """ + def test_rot13_func(self): + infile = io.StringIO('Gb or, be abg gb or, gung vf gur dhrfgvba') + outfile = io.StringIO() + encodings.rot_13.rot13(infile, outfile) + outfile.seek(0) + plain_text = outfile.read() + self.assertEqual( + plain_text, + 'To be, or not to be, that is the question') + + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/Tests/2019-08-16-16-15-14.bpo-37876.m3k1w3.rst b/Misc/NEWS.d/next/Tests/2019-08-16-16-15-14.bpo-37876.m3k1w3.rst new file mode 100644 index 0000000..45702fc --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2019-08-16-16-15-14.bpo-37876.m3k1w3.rst @@ -0,0 +1 @@ +Add tests for ROT-13 codec.
\ No newline at end of file |