Danger

DANGER WILL ROBINSON! This chapter is still UNDER CONSTRUCTION. It is DEFINITELY full of mistakes and may be completely different when I am done editing it though most of the code should work

functions: tests and solutions

tests

the code in functions/tests/test_functions.py from functions

 1import src.functions
 2import unittest
 3
 4
 5class TestFunctions(unittest.TestCase):
 6
 7    def test_making_a_function_w_pass(self):
 8        self.assertIsNone(src.functions.w_pass())
 9
10    def test_making_a_function_w_return(self):
11        self.assertIsNone(src.functions.w_return())
12
13    def test_making_a_function_w_return_none(self):
14        self.assertIsNone(src.functions.w_return_none())
15
16    def test_constant_function(self):
17        self.assertEqual(
18            src.functions.constant(),
19            'the same thing'
20        )
21
22    def test_identity_function(self):
23        self.assertIsNone(src.functions.identity(None))
24        self.assertEqual(src.functions.identity(object), object)
25
26    def test_functions_w_positional_arguments(self):
27        self.assertEqual(
28            src.functions.w_positional_arguments('first', 'last'),
29            ('first', 'last')
30        )
31        self.assertEqual(
32            src.functions.w_positional_arguments('last', 'first'),
33            ('last', 'first')
34        )
35
36    def test_functions_w_keyword_arguments(self):
37        self.assertEqual(
38            src.functions.w_keyword_arguments(
39                first='first', last='last',
40            ),
41            ('first', 'last')
42        )
43        self.assertEqual(
44            src.functions.w_keyword_arguments(
45                last='last', first='first',
46            ),
47            ('first', 'last')
48        )
49        self.assertEqual(
50            src.functions.w_keyword_arguments('last', 'first'),
51            ('last', 'first')
52        )
53
54    def test_functions_w_positional_and_keyword_arguments(self):
55        self.assertEqual(
56            src.functions.w_positional_and_keyword_arguments(
57                'first', last='last',
58            ),
59            ('first', 'last')
60        )
61
62    def test_functions_w_default_arguments(self):
63        self.assertEqual(
64            src.functions.w_default_arguments('jane'),
65            ('jane', 'doe')
66        )
67        self.assertEqual(
68            src.functions.w_default_arguments('joe', 'blow'),
69            ('joe', 'blow')
70        )
71
72    def test_functions_w_unknown_arguments(self):
73        self.assertEqual(
74            src.functions.w_unknown_arguments(
75                0, 1, 2, 3, a=4, b=5, c=6, d=7,
76            ),
77            ((0, 1, 2, 3), {'a': 4, 'b': 5, 'c': 6, 'd': 7})
78        )
79        self.assertEqual(
80            src.functions.w_unknown_arguments(0, 1, 2, 3),
81            ((0, 1, 2, 3), {})
82        )
83        self.assertEqual(
84            src.functions.w_unknown_arguments(a=4, b=5, c=6, d=7),
85            ((), dict(a=4, b=5, c=6, d=7))
86        )
87        self.assertEqual(
88            src.functions.w_unknown_arguments(),
89            ((), {})
90        )
91
92
93# Exceptions Encountered
94# AssertionError
95# NameError
96# IndentationError
97# SyntaxError

solutions

the solutions in functions/src/functions.py from functions

 1def w_pass():
 2    pass
 3
 4
 5def w_return():
 6    return
 7
 8
 9def w_return_none():
10    return None
11
12
13def constant():
14    return 'the same thing'
15
16
17def identity(the_input):
18    return the_input
19
20
21def w_positional_arguments(first_input, last_input):
22    return first_input, last_input
23
24
25def w_keyword_arguments(first_input, last_input):
26    return first_input, last_input
27
28
29def w_positional_and_keyword_arguments(first_input, last_input):
30    return first_input, last_input
31
32
33def w_default_arguments(first_name, last_name='doe'):
34    return first_name, last_name
35
36
37def w_unknown_arguments(*arguments, **keyword_arguments):
38    return arguments, keyword_arguments