|
| 1 | +"""Tests for the this module.""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +import this |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | + |
| 8 | +class TestThis(unittest.TestCase): |
| 9 | + """Test cases for the this module.""" |
| 10 | + |
| 11 | + def test_module_has_expected_variables(self): |
| 12 | + """Test that the module has the expected variables.""" |
| 13 | + # The module should have 's' (the ROT13 encoded string) and 'd' (the translation dict) |
| 14 | + self.assertTrue(hasattr(this, 's')) |
| 15 | + self.assertTrue(hasattr(this, 'd')) |
| 16 | + |
| 17 | + # 's' should be a string |
| 18 | + self.assertIsInstance(this.s, str) |
| 19 | + self.assertTrue(len(this.s) > 0) |
| 20 | + |
| 21 | + # 'd' should be a dictionary |
| 22 | + self.assertIsInstance(this.d, dict) |
| 23 | + self.assertTrue(len(this.d) > 0) |
| 24 | + |
| 25 | + def test_rot13_dictionary(self): |
| 26 | + """Test that the ROT13 dictionary is correctly constructed.""" |
| 27 | + # Test that it contains mappings for both uppercase and lowercase letters |
| 28 | + self.assertIn('A', this.d) |
| 29 | + self.assertIn('a', this.d) |
| 30 | + self.assertIn('Z', this.d) |
| 31 | + self.assertIn('z', this.d) |
| 32 | + |
| 33 | + # Test ROT13 properties |
| 34 | + self.assertEqual(this.d['A'], 'N') # A -> N |
| 35 | + self.assertEqual(this.d['N'], 'A') # N -> A (ROT13 is its own inverse) |
| 36 | + self.assertEqual(this.d['a'], 'n') # a -> n |
| 37 | + self.assertEqual(this.d['n'], 'a') # n -> a |
| 38 | + |
| 39 | + # Test that it covers the full alphabet |
| 40 | + for i in range(26): |
| 41 | + upper_char = chr(ord('A') + i) |
| 42 | + lower_char = chr(ord('a') + i) |
| 43 | + self.assertIn(upper_char, this.d) |
| 44 | + self.assertIn(lower_char, this.d) |
| 45 | + |
| 46 | + def test_rot13_inverse_property(self): |
| 47 | + """Test that ROT13 is its own inverse.""" |
| 48 | + # For any letter, applying ROT13 twice should give the original |
| 49 | + for char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz': |
| 50 | + if char in this.d: |
| 51 | + rotated = this.d[char] |
| 52 | + double_rotated = this.d.get(rotated, rotated) |
| 53 | + self.assertEqual(char, double_rotated) |
| 54 | + |
| 55 | + def test_string_translation(self): |
| 56 | + """Test that the string translation works correctly.""" |
| 57 | + # Test a simple ROT13 transformation |
| 58 | + test_string = "Hello" |
| 59 | + expected = "Uryyb" |
| 60 | + |
| 61 | + result = ''.join([this.d.get(c, c) for c in test_string]) |
| 62 | + self.assertEqual(result, expected) |
| 63 | + |
| 64 | + def test_zen_of_python_content(self): |
| 65 | + """Test that the encoded string contains the Zen of Python.""" |
| 66 | + # Decode the string using the dictionary |
| 67 | + decoded = ''.join([this.d.get(c, c) for c in this.s]) |
| 68 | + |
| 69 | + # Check for key phrases from the Zen of Python |
| 70 | + zen_phrases = [ |
| 71 | + "Beautiful is better than ugly", |
| 72 | + "Explicit is better than implicit", |
| 73 | + "Simple is better than complex", |
| 74 | + "Complex is better than complicated", |
| 75 | + "Readability counts", |
| 76 | + "There should be one", |
| 77 | + "Although never is often better than *right* now" |
| 78 | + ] |
| 79 | + |
| 80 | + for phrase in zen_phrases: |
| 81 | + self.assertIn(phrase, decoded) |
| 82 | + |
| 83 | + def test_print_statement_execution(self): |
| 84 | + """Test that the print statement would execute correctly.""" |
| 85 | + with patch('builtins.print') as mock_print: |
| 86 | + # Execute the print statement from the module |
| 87 | + exec('print("".join([d.get(c, c) for c in s]))', {'d': this.d, 's': this.s}) |
| 88 | + |
| 89 | + # Verify print was called |
| 90 | + mock_print.assert_called_once() |
| 91 | + |
| 92 | + # Get the printed content |
| 93 | + printed_content = mock_print.call_args[0][0] |
| 94 | + |
| 95 | + # Should contain the Zen of Python |
| 96 | + self.assertIn("Beautiful is better than ugly", printed_content) |
| 97 | + self.assertIn("The Zen of Python", printed_content) |
| 98 | + |
| 99 | + def test_module_structure(self): |
| 100 | + """Test the overall structure of the module.""" |
| 101 | + # The module should be importable |
| 102 | + import this |
| 103 | + |
| 104 | + # Should have the expected attributes |
| 105 | + expected_attrs = ['s', 'd'] |
| 106 | + for attr in expected_attrs: |
| 107 | + self.assertTrue(hasattr(this, attr)) |
| 108 | + |
| 109 | + def test_rot13_edge_cases(self): |
| 110 | + """Test ROT13 with edge cases.""" |
| 111 | + # Test with non-alphabetic characters |
| 112 | + test_chars = "123!@#$%^&*()_+-=[]{}|;':\",./<>?" |
| 113 | + for char in test_chars: |
| 114 | + # Non-alphabetic characters should not be in the dictionary |
| 115 | + # and should remain unchanged |
| 116 | + if char not in this.d: |
| 117 | + result = this.d.get(char, char) |
| 118 | + self.assertEqual(result, char) |
| 119 | + |
| 120 | + def test_dictionary_completeness(self): |
| 121 | + """Test that the ROT13 dictionary is complete.""" |
| 122 | + # Should have exactly 52 entries (26 uppercase + 26 lowercase) |
| 123 | + self.assertEqual(len(this.d), 52) |
| 124 | + |
| 125 | + # All entries should be single character strings |
| 126 | + for key, value in this.d.items(): |
| 127 | + self.assertIsInstance(key, str) |
| 128 | + self.assertIsInstance(value, str) |
| 129 | + self.assertEqual(len(key), 1) |
| 130 | + self.assertEqual(len(value), 1) |
| 131 | + |
| 132 | + def test_string_length(self): |
| 133 | + """Test that the encoded string has reasonable length.""" |
| 134 | + # The Zen of Python is substantial, so the encoded string should be long |
| 135 | + self.assertGreater(len(this.s), 100) |
| 136 | + |
| 137 | + # Should contain newlines and spaces |
| 138 | + self.assertIn('\n', this.s) |
| 139 | + self.assertIn(' ', this.s) |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == '__main__': |
| 143 | + unittest.main() |
0 commit comments