From 254a1fc7b2f13c7f9db43136f7ea486ca0791ae9 Mon Sep 17 00:00:00 2001 From: Anuj092 Date: Sun, 21 Sep 2025 23:31:19 +0530 Subject: [PATCH] Add comprehensive unit tests for antigravity and this modules - Add test_antigravity.py with 9 test cases covering: * Basic geohash functionality and edge cases * Hash consistency and algorithm verification * Output format validation and function signature * Module import verification - Add test_this.py with 10 test cases covering: * ROT13 dictionary correctness and completeness * Zen of Python content verification * String translation functionality * Edge case handling for non-alphabetic characters These tests provide comprehensive coverage for previously untested modules in the standard library, improving code quality and preventing regressions. --- Lib/test/test_antigravity.py | 124 ++++++++++++++++++++++++++++++ Lib/test/test_this.py | 143 +++++++++++++++++++++++++++++++++++ 2 files changed, 267 insertions(+) create mode 100644 Lib/test/test_antigravity.py create mode 100644 Lib/test/test_this.py diff --git a/Lib/test/test_antigravity.py b/Lib/test/test_antigravity.py new file mode 100644 index 00000000000000..3ab288c15f3036 --- /dev/null +++ b/Lib/test/test_antigravity.py @@ -0,0 +1,124 @@ +"""Tests for the antigravity module.""" + +import unittest +import antigravity +import hashlib +from unittest.mock import patch, MagicMock + + +class TestAntigravity(unittest.TestCase): + """Test cases for the antigravity module.""" + + def test_geohash_basic_functionality(self): + """Test basic geohash functionality with known inputs.""" + # Test with the example from the docstring + with patch('builtins.print') as mock_print: + antigravity.geohash(37.421542, -122.085589, b'2005-05-26-10458.68') + mock_print.assert_called_once() + # The exact output depends on the MD5 hash, but we can verify it was called + args = mock_print.call_args[0][0] + self.assertIsInstance(args, str) + # Should contain the latitude and longitude parts + self.assertIn('37.', args) + self.assertIn('-122.', args) + + def test_geohash_different_inputs(self): + """Test geohash with different input values.""" + with patch('builtins.print') as mock_print: + # Test with different coordinates + antigravity.geohash(0.0, 0.0, b'test-date') + mock_print.assert_called_once() + args = mock_print.call_args[0][0] + self.assertIsInstance(args, str) + self.assertIn('0.', args) + + def test_geohash_hash_consistency(self): + """Test that geohash produces consistent results for same inputs.""" + with patch('builtins.print') as mock_print: + # Call geohash twice with same inputs + antigravity.geohash(10.0, 20.0, b'same-date') + first_call = mock_print.call_args[0][0] + + mock_print.reset_mock() + antigravity.geohash(10.0, 20.0, b'same-date') + second_call = mock_print.call_args[0][0] + + # Results should be identical + self.assertEqual(first_call, second_call) + + def test_geohash_different_dates(self): + """Test that different dates produce different results.""" + with patch('builtins.print') as mock_print: + # Call geohash with different dates + antigravity.geohash(10.0, 20.0, b'date1') + first_call = mock_print.call_args[0][0] + + mock_print.reset_mock() + antigravity.geohash(10.0, 20.0, b'date2') + second_call = mock_print.call_args[0][0] + + # Results should be different + self.assertNotEqual(first_call, second_call) + + def test_geohash_hash_algorithm(self): + """Test that geohash uses MD5 as expected.""" + # Test the internal hash generation + test_input = b'2005-05-26-10458.68' + expected_hash = hashlib.md5(test_input, usedforsecurity=False).hexdigest() + + # We can't directly test the internal hash, but we can verify + # that the function doesn't crash with various inputs + with patch('builtins.print'): + antigravity.geohash(0.0, 0.0, test_input) + # If we get here without exception, the hash generation worked + + def test_geohash_edge_cases(self): + """Test geohash with edge case inputs.""" + with patch('builtins.print'): + # Test with extreme coordinates + antigravity.geohash(90.0, 180.0, b'edge-case') + antigravity.geohash(-90.0, -180.0, b'edge-case') + + # Test with zero coordinates + antigravity.geohash(0.0, 0.0, b'zero-coords') + + # Test with empty date + antigravity.geohash(0.0, 0.0, b'') + + def test_geohash_output_format(self): + """Test that geohash output has expected format.""" + with patch('builtins.print') as mock_print: + antigravity.geohash(37.421542, -122.085589, b'test') + output = mock_print.call_args[0][0] + + # Output should be a string with two parts separated by space + parts = output.split() + self.assertEqual(len(parts), 2) + + # Each part should contain a number and additional characters + for part in parts: + self.assertIsInstance(part, str) + self.assertTrue(len(part) > 0) + + def test_module_imports(self): + """Test that the module imports correctly.""" + # Test that webbrowser and hashlib are available + self.assertTrue(hasattr(antigravity, 'webbrowser')) + self.assertTrue(hasattr(antigravity, 'hashlib')) + self.assertTrue(hasattr(antigravity, 'geohash')) + + def test_geohash_function_signature(self): + """Test that geohash function has expected signature.""" + import inspect + sig = inspect.signature(antigravity.geohash) + params = list(sig.parameters.keys()) + + # Should have three parameters: latitude, longitude, datedow + self.assertEqual(len(params), 3) + self.assertIn('latitude', params) + self.assertIn('longitude', params) + self.assertIn('datedow', params) + + +if __name__ == '__main__': + unittest.main() diff --git a/Lib/test/test_this.py b/Lib/test/test_this.py new file mode 100644 index 00000000000000..1524f3aa560345 --- /dev/null +++ b/Lib/test/test_this.py @@ -0,0 +1,143 @@ +"""Tests for the this module.""" + +import unittest +import this +from unittest.mock import patch + + +class TestThis(unittest.TestCase): + """Test cases for the this module.""" + + def test_module_has_expected_variables(self): + """Test that the module has the expected variables.""" + # The module should have 's' (the ROT13 encoded string) and 'd' (the translation dict) + self.assertTrue(hasattr(this, 's')) + self.assertTrue(hasattr(this, 'd')) + + # 's' should be a string + self.assertIsInstance(this.s, str) + self.assertTrue(len(this.s) > 0) + + # 'd' should be a dictionary + self.assertIsInstance(this.d, dict) + self.assertTrue(len(this.d) > 0) + + def test_rot13_dictionary(self): + """Test that the ROT13 dictionary is correctly constructed.""" + # Test that it contains mappings for both uppercase and lowercase letters + self.assertIn('A', this.d) + self.assertIn('a', this.d) + self.assertIn('Z', this.d) + self.assertIn('z', this.d) + + # Test ROT13 properties + self.assertEqual(this.d['A'], 'N') # A -> N + self.assertEqual(this.d['N'], 'A') # N -> A (ROT13 is its own inverse) + self.assertEqual(this.d['a'], 'n') # a -> n + self.assertEqual(this.d['n'], 'a') # n -> a + + # Test that it covers the full alphabet + for i in range(26): + upper_char = chr(ord('A') + i) + lower_char = chr(ord('a') + i) + self.assertIn(upper_char, this.d) + self.assertIn(lower_char, this.d) + + def test_rot13_inverse_property(self): + """Test that ROT13 is its own inverse.""" + # For any letter, applying ROT13 twice should give the original + for char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz': + if char in this.d: + rotated = this.d[char] + double_rotated = this.d.get(rotated, rotated) + self.assertEqual(char, double_rotated) + + def test_string_translation(self): + """Test that the string translation works correctly.""" + # Test a simple ROT13 transformation + test_string = "Hello" + expected = "Uryyb" + + result = ''.join([this.d.get(c, c) for c in test_string]) + self.assertEqual(result, expected) + + def test_zen_of_python_content(self): + """Test that the encoded string contains the Zen of Python.""" + # Decode the string using the dictionary + decoded = ''.join([this.d.get(c, c) for c in this.s]) + + # Check for key phrases from the Zen of Python + zen_phrases = [ + "Beautiful is better than ugly", + "Explicit is better than implicit", + "Simple is better than complex", + "Complex is better than complicated", + "Readability counts", + "There should be one", + "Although never is often better than *right* now" + ] + + for phrase in zen_phrases: + self.assertIn(phrase, decoded) + + def test_print_statement_execution(self): + """Test that the print statement would execute correctly.""" + with patch('builtins.print') as mock_print: + # Execute the print statement from the module + exec('print("".join([d.get(c, c) for c in s]))', {'d': this.d, 's': this.s}) + + # Verify print was called + mock_print.assert_called_once() + + # Get the printed content + printed_content = mock_print.call_args[0][0] + + # Should contain the Zen of Python + self.assertIn("Beautiful is better than ugly", printed_content) + self.assertIn("The Zen of Python", printed_content) + + def test_module_structure(self): + """Test the overall structure of the module.""" + # The module should be importable + import this + + # Should have the expected attributes + expected_attrs = ['s', 'd'] + for attr in expected_attrs: + self.assertTrue(hasattr(this, attr)) + + def test_rot13_edge_cases(self): + """Test ROT13 with edge cases.""" + # Test with non-alphabetic characters + test_chars = "123!@#$%^&*()_+-=[]{}|;':\",./<>?" + for char in test_chars: + # Non-alphabetic characters should not be in the dictionary + # and should remain unchanged + if char not in this.d: + result = this.d.get(char, char) + self.assertEqual(result, char) + + def test_dictionary_completeness(self): + """Test that the ROT13 dictionary is complete.""" + # Should have exactly 52 entries (26 uppercase + 26 lowercase) + self.assertEqual(len(this.d), 52) + + # All entries should be single character strings + for key, value in this.d.items(): + self.assertIsInstance(key, str) + self.assertIsInstance(value, str) + self.assertEqual(len(key), 1) + self.assertEqual(len(value), 1) + + def test_string_length(self): + """Test that the encoded string has reasonable length.""" + # The Zen of Python is substantial, so the encoded string should be long + self.assertGreater(len(this.s), 100) + + # Should contain newlines and spaces + self.assertIn('\n', this.s) + self.assertIn(' ', this.s) + + +if __name__ == '__main__': + unittest.main()