Created
June 3, 2015 00:38
-
-
Save saxbophone/40e198f909eefc86ac08 to your computer and use it in GitHub Desktop.
Sample Initial test for my idea for a type safety function decorator for Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import unittest | |
| from type_safety import typed | |
| class TypeSafetyTestCase(unittest.TestCase): | |
| def test_positional_typing(self): | |
| """ | |
| When the @typed() decorator is used with a list of types | |
| it should force these types on the corresponding positional | |
| arguments of the function. | |
| """ | |
| # decorate a sample function | |
| @typed('basestring', 'int', 'basestring') | |
| def greet(name, age, country) | |
| return ('Hello, my name is {0}. I am {1} years old ' | |
| 'and I live in {2}').format(name, age, country) | |
| # test once with correctly typed arguments | |
| self.assertEqual(greet('Hamisch', 12, 'Germany'), | |
| ('Hello, my name is Hamisch. I am 12 years old ' | |
| 'and I live in Germany')) | |
| with self.assertRaises(TypeError): | |
| greet(34, 23, 'Switzerland') | |
| with self.assertRaises(TypeError): | |
| greet('Paul', 23, 34924.4234) | |
| with self.assertRaises(TypeError): | |
| greet('Paul', '45', 'Australasia') | |
| def test_keyword_typing(self): | |
| """ | |
| When the @typed() decorator is used with a list of types | |
| by keyword it should force these types on the corresponding | |
| keyword arguments of the function. | |
| """ | |
| # decorate a sample function | |
| @typed(me='basestring', problem='basestring', reason='basestring') | |
| def apologise(me, problem, reason): | |
| return 'I, {0} am sorry for the {1} casued by {2}'.format(me, problem, reason) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment