Generator Public

Code #2518

Python
'''Self-contained demonstration: a simple target_function and its unit tests. This module is designed to be self-contained and runnable as a script. It includes a concise docstring, a straightforward implementation of target_function, and a unittest-based test suite. Running it executes the tests.'''\n\nimport unittest\n\ndef target_function(value):\n    '''\n    A simple multipurpose function used for illustrating unit tests.\n\n    Behavior:\n    - If value is an int, returns value squared.\n    - If value is a str, returns the string reversed.\n    - Otherwise, raises TypeError.\n    '''\n    if isinstance(value, int):\n        return value * value\n    if isinstance(value, str):\n        return value[::-1]\n    raise TypeError('Unsupported type: {}'.format(type(value).__name__))\n\nclass TestTargetFunction(unittest.TestCase):\n    # Unit tests for target_function.\n\n    def test_int_input(self):\n        self.assertEqual(target_function(3), 9)\n        self.assertEqual(target_function(-4), 16)\n\n    def test_str_input(self):\n        self.assertEqual(target_function('abc'), 'cba')\n        self.assertEqual(target_function(''), '')\n\n    def test_unsupported_type(self):\n        with self.assertRaises(TypeError):\n            target_function(3.14)\n        with self.assertRaises(TypeError):\n            target_function([1, 2, 3])\n\nif __name__ == '__main__':\n    unittest.main()
Prompt: unspecified