Spaces:
Sleeping
Sleeping
| import unittest | |
| from app import ( | |
| to_leet, from_leet, | |
| to_morse, from_morse, | |
| to_binary, from_binary, | |
| to_base64, from_base64, | |
| to_hex, from_hex, | |
| to_markdown_html, | |
| to_url_encoded, from_url_encoded | |
| ) | |
| class TestTextTransformer(unittest.TestCase): | |
| # --- Leet Tests --- | |
| def test_leet(self): | |
| self.assertEqual(to_leet("Leet"), "1337") | |
| self.assertEqual(from_leet("1337"), "LEET") | |
| self.assertEqual(to_leet(""), "Input cannot be empty.") | |
| # --- Morse Tests --- | |
| def test_morse(self): | |
| self.assertEqual(to_morse("SOS"), "... --- ...") | |
| self.assertEqual(from_morse("... --- ..."), "SOS") | |
| self.assertEqual(to_morse("Hello World"), ".... . .-.. .-.. --- / .-- --- .-. .-.. -..") | |
| # --- Binary Tests --- | |
| def test_binary(self): | |
| self.assertEqual(to_binary("A"), "01000001") | |
| self.assertEqual(from_binary("01000001"), "A") | |
| self.assertTrue("Error" in from_binary("02000001")) # Invalid binary | |
| # --- Base64 Tests --- | |
| def test_base64(self): | |
| self.assertEqual(to_base64("Hello"), "SGVsbG8=") | |
| self.assertEqual(from_base64("SGVsbG8="), "Hello") | |
| self.assertTrue("Error" in from_base64("Invalid...")) | |
| # --- Hex Tests --- | |
| def test_hex(self): | |
| self.assertEqual(to_hex("A"), "41") | |
| self.assertEqual(from_hex("41"), "A") | |
| self.assertTrue("Error" in from_hex("GG")) # Invalid hex | |
| # --- Markdown Tests --- | |
| def test_markdown(self): | |
| self.assertEqual(to_markdown_html("# Hello"), "<h1>Hello</h1>") | |
| self.assertEqual(to_markdown_html("**Bold**"), "<p><strong>Bold</strong></p>") | |
| # --- URL Tests --- | |
| def test_url(self): | |
| self.assertEqual(to_url_encoded("hello world"), "hello%20world") | |
| self.assertEqual(from_url_encoded("hello%20world"), "hello world") | |
| # --- Validation Tests --- | |
| def test_empty_input(self): | |
| msg = "Input cannot be empty." | |
| self.assertIn(msg, to_leet("")) | |
| self.assertIn(msg, to_morse("")) | |
| self.assertIn(msg, to_binary("")) | |
| self.assertIn(msg, to_base64("")) | |
| self.assertIn(msg, to_hex("")) | |
| self.assertIn(msg, to_markdown_html("")) | |
| self.assertIn(msg, to_url_encoded("")) | |
| if __name__ == '__main__': | |
| unittest.main() | |