Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6ef8f2

Browse files
miss-islingtonzeth
andauthored
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. (cherry picked from commit b3b48c8) Co-authored-by: Zeth <[email protected]>
1 parent 0d4396c commit b6ef8f2

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Lib/test/test_codecs.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3343,5 +3343,42 @@ def test_decode_unsupported_error_handler(self):
33433343
self.assertEqual(str(cm.exception), 'unsupported error handler')
33443344

33453345

3346+
class Rot13Test(unittest.TestCase):
3347+
"""Test the educational ROT-13 codec."""
3348+
def test_encode(self):
3349+
ciphertext = codecs.encode("Caesar liked ciphers", 'rot-13')
3350+
self.assertEqual(ciphertext, 'Pnrfne yvxrq pvcuref')
3351+
3352+
def test_decode(self):
3353+
plaintext = codecs.decode('Rg gh, Oehgr?', 'rot-13')
3354+
self.assertEqual(plaintext, 'Et tu, Brute?')
3355+
3356+
def test_incremental_encode(self):
3357+
encoder = codecs.getincrementalencoder('rot-13')()
3358+
ciphertext = encoder.encode('ABBA nag Cheryl Baker')
3359+
self.assertEqual(ciphertext, 'NOON ant Purely Onxre')
3360+
3361+
def test_incremental_decode(self):
3362+
decoder = codecs.getincrementaldecoder('rot-13')()
3363+
plaintext = decoder.decode('terra Ares envy tha')
3364+
self.assertEqual(plaintext, 'green Nerf rail gun')
3365+
3366+
3367+
class Rot13UtilTest(unittest.TestCase):
3368+
"""Test the ROT-13 codec via rot13 function,
3369+
i.e. the user has done something like:
3370+
$ echo "Hello World" | python -m encodings.rot_13
3371+
"""
3372+
def test_rot13_func(self):
3373+
infile = io.StringIO('Gb or, be abg gb or, gung vf gur dhrfgvba')
3374+
outfile = io.StringIO()
3375+
encodings.rot_13.rot13(infile, outfile)
3376+
outfile.seek(0)
3377+
plain_text = outfile.read()
3378+
self.assertEqual(
3379+
plain_text,
3380+
'To be, or not to be, that is the question')
3381+
3382+
33463383
if __name__ == "__main__":
33473384
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add tests for ROT-13 codec.

0 commit comments

Comments
 (0)