functions: tests and solutions

functions: tests

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

  1import src.functions
  2import unittest
  3
  4
  5def add_x(number):
  6    # return 2 + number
  7    return 3 + number
  8
  9
 10class TestFunctions(unittest.TestCase):
 11
 12    def test_why_use_a_function(self):
 13        # reality = 1 + 0
 14        # reality = 2 + 0
 15        reality = add_x(0)
 16        # my_expectation = 0
 17        # my_expectation = 1
 18        # my_expectation = 2
 19        my_expectation = 3
 20        self.assertEqual(reality, my_expectation)
 21
 22        # reality = 1 + 1
 23        # reality = 2 + 1
 24        reality = add_x(1)
 25        # my_expectation = 1
 26        # my_expectation = 2
 27        # my_expectation = 3
 28        my_expectation = 4
 29        self.assertEqual(reality, my_expectation)
 30
 31        # reality = 1 + 2
 32        # reality = 2 + 2
 33        reality = add_x(2)
 34        # my_expectation = 2
 35        # my_expectation = 3
 36        # my_expectation = 4
 37        my_expectation = 5
 38        self.assertEqual(reality, my_expectation)
 39
 40        # reality = 1 + 3
 41        # reality = 2 + 3
 42        reality = add_x(3)
 43        # my_expectation = 3
 44        # my_expectation = 4
 45        # my_expectation = 5
 46        my_expectation = 6
 47        self.assertEqual(reality, my_expectation)
 48
 49        # reality = 1 + 4
 50        # reality = 2 + 4
 51        reality = add_x(4)
 52        # my_expectation = 4
 53        # my_expectation = 5
 54        # my_expectation = 6
 55        my_expectation = 7
 56        self.assertEqual(reality, my_expectation)
 57
 58        # reality = 1 + 5
 59        # reality = 2 + 5
 60        reality = add_x(5)
 61        # my_expectation = 5
 62        # my_expectation = 6
 63        # my_expectation = 7
 64        my_expectation = 8
 65        self.assertEqual(reality, my_expectation)
 66
 67        # reality = 1 + 6
 68        # reality = 2 + 6
 69        reality = add_x(6)
 70        # my_expectation = 6
 71        # my_expectation = 7
 72        # my_expectation = 8
 73        my_expectation = 9
 74        self.assertEqual(reality, my_expectation)
 75
 76        # reality = 1 + 7
 77        # reality = 2 + 7
 78        reality = add_x(7)
 79        # my_expectation = 7
 80        # my_expectation = 8
 81        # my_expectation = 9
 82        my_expectation = 10
 83        self.assertEqual(reality, my_expectation)
 84
 85        # reality = 1 + 8
 86        # reality = 2 + 8
 87        reality = add_x(8)
 88        # my_expectation = 8
 89        # my_expectation = 9
 90        # my_expectation = 10
 91        my_expectation = 11
 92        self.assertEqual(reality, my_expectation)
 93
 94        # reality = 1 + 9
 95        # reality = 2 + 9
 96        reality = add_x(9)
 97        # my_expectation = 9
 98        # my_expectation = 10
 99        # my_expectation = 11
100        my_expectation = 12
101        self.assertEqual(reality, my_expectation)
102
103    def test_making_a_function_w_pass(self):
104        self.assertIs(src.functions.w_pass(), None)
105
106    def test_making_a_function_w_return(self):
107        self.assertIs(src.functions.w_return(), None)
108
109    def test_making_a_function_w_return_none(self):
110        self.assertIs(
111            src.functions.w_return_none(), None
112        )
113
114    def test_what_happens_after_a_function_returns(self):
115        self.assertIs(
116            src.functions.return_is_last(), None
117        )
118
119    def test_constant_function(self):
120        reality = src.functions.constant()
121        my_expectation = 'the same thing'
122        self.assertEqual(reality, my_expectation)
123
124    def test_identity_function(self):
125        reality = src.functions.identity(None)
126        my_expectation = None
127        self.assertEqual(reality, my_expectation)
128
129        reality = src.functions.identity(object)
130        my_expectation = object
131        self.assertEqual(reality, my_expectation)
132
133    def test_w_positional_arguments(self):
134        first, last = 'first', 'last'
135
136        reality = src.functions.w_positional_arguments(
137            first, last,
138        )
139        my_expectation = (first, last)
140        self.assertEqual(reality, my_expectation)
141
142        reality = src.functions.w_positional_arguments(
143            last, first,
144        )
145        my_expectation = (last, first)
146        self.assertEqual(reality, my_expectation)
147
148        first_number, second_number = 0, 1
149        reality = src.functions.w_positional_arguments(
150            first_number, second_number,
151        )
152        my_expectation = (first_number, second_number)
153        self.assertEqual(reality, my_expectation)
154
155        a_tuple = (1, 2, 3, 'n')
156        a_list = [1, 2, 3, 'n']
157        reality = src.functions.w_positional_arguments(
158            a_tuple, a_list,
159        )
160        my_expectation = (a_tuple, a_list)
161        self.assertEqual(reality, my_expectation)
162
163    def test_w_keyword_arguments(self):
164        first, last = 'first', 'last'
165
166        reality = src.functions.w_keyword_arguments(
167            first_input=first, last_input=last,
168        )
169        my_expectation = (first, last)
170        self.assertEqual(reality, my_expectation)
171
172        reality = src.functions.w_keyword_arguments(
173            last_input=last, first_input=first,
174        )
175        my_expectation = (first, last)
176        self.assertEqual(reality, my_expectation)
177
178        reality = src.functions.w_keyword_arguments(
179            last, first,
180        )
181        my_expectation = (last, first)
182        self.assertEqual(reality, my_expectation)
183
184        zero, one = 0, 1
185        reality = src.functions.w_keyword_arguments(
186            last_input=zero, first_input=one,
187        )
188        my_expectation = (one, zero)
189        self.assertEqual(reality, my_expectation)
190
191        a_set = {1, 2, 3, 'n'}
192        a_dictionary = {'key': 'value'}
193        reality = src.functions.w_keyword_arguments(
194            first_input=a_set,
195            last_input=a_dictionary,
196        )
197        my_expectation = (a_set, a_dictionary)
198        self.assertEqual(reality, my_expectation)
199
200        a_tuple = (1, 2, 3, 'n')
201        a_list = [1, 2, 3, 'n']
202        reality = src.functions.w_positional_arguments(
203            first_input=a_list,
204            last_input=a_tuple,
205        )
206        my_expectation = (a_list, a_tuple)
207        self.assertEqual(reality, my_expectation)
208
209    def test_w_args_and_kwargs(self):
210        first, last = 'first', 'last'
211        reality = (
212            src.functions.w_args_and_kwargs(
213                first, last_input=last,
214            )
215        )
216        my_expectation = (first, last)
217        self.assertEqual(reality, my_expectation)
218
219    def test_w_optional_arguments(self):
220        first_name, last_name = 'jane', 'doe'
221        reality = src.functions.w_optional_arguments(
222            first_name,
223        )
224        my_expectation = (first_name, last_name)
225        self.assertEqual(reality, my_expectation)
226
227        first_name, blow = 'joe', 'blow'
228        reality = src.functions.w_optional_arguments(
229            first_name, blow,
230        )
231        my_expectation = (first_name, blow)
232        self.assertEqual(reality, my_expectation)
233
234        first_name = 'john'
235        reality = src.functions.w_optional_arguments(
236            first_input=first_name,
237        )
238        my_expectation = (first_name, last_name)
239        self.assertEqual(reality, my_expectation)
240
241        last_name = 'smith'
242        reality = src.functions.w_optional_arguments(
243            last_input=last_name,
244            first_input=first_name,
245        )
246        my_expectation = (first_name, last_name)
247        self.assertEqual(reality, my_expectation)
248
249    def test_w_unknown_arguments(self):
250        a_tuple = (0, 1)
251        a_dictionary = {'a': 2, 'b': 3}
252        reality = src.functions.w_unknown_arguments(
253            *a_tuple, **a_dictionary
254        )
255        my_expectation = (a_tuple, a_dictionary)
256        self.assertEqual(reality, my_expectation)
257
258        a_tuple = (0, 1)
259        a_dictionary = {'a': 2, 'b': 3, 'c': 4}
260        reality = src.functions.w_unknown_arguments(
261            *a_tuple, **a_dictionary
262        )
263        my_expectation = (
264            a_tuple, a_dictionary
265        )
266        self.assertEqual(reality, my_expectation)
267
268        a_tuple = (0, 1, 2)
269        a_dictionary = {'a': 3, 'b': 4, 'c': 5}
270        reality = src.functions.w_unknown_arguments(
271            *a_tuple, **a_dictionary,
272        )
273        my_expectation = (
274            a_tuple, a_dictionary
275        )
276        self.assertEqual(reality, my_expectation)
277
278        a_tuple = (0, 1, 2, 3)
279        reality = src.functions.w_unknown_arguments(
280            *a_tuple
281        )
282        my_expectation = (a_tuple, {})
283        self.assertEqual(reality, my_expectation)
284
285        a_dictionary = {'a': 4, 'b': 5, 'c': 6, 'd': 7}
286        reality = src.functions.w_unknown_arguments(
287            **a_dictionary
288        )
289        my_expectation = ((), a_dictionary)
290        self.assertEqual(reality, my_expectation)
291
292        reality = src.functions.w_unknown_arguments()
293        my_expectation = ((), {})
294        self.assertEqual(reality, my_expectation)
295
296
297# Exceptions seen
298# AssertionError
299# NameError
300# AttributeError
301# TypeError
302# SyntaxError

functions: 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 return_is_last():
14    return None
15    return 'will NEVER run'
16
17
18def constant():
19    return 'the same thing'
20
21
22def identity(the_input):
23    return the_input
24
25
26def w_positional_arguments(first_input, last_input):
27    return first_input, last_input
28
29
30def w_keyword_arguments(first_input, last_input):
31    return first_input, last_input
32
33
34def w_args_and_kwargs(first_input, last_input):
35    return first_input, last_input
36
37
38def w_optional_arguments(first_input, last_input='doe'):
39    return first_input, last_input
40
41
42def w_unknown_arguments(
43        *positional_arguments, **keyword_arguments
44    ):
45    return positional_arguments, keyword_arguments

functions 2: tests

the code from functions/tests/test_functions.py (after refactor) from functions 2: use class attributes

  1import src.functions
  2import unittest
  3
  4
  5def add_x(number):
  6    # return 2 + number
  7    return 3 + number
  8
  9
 10class TestFunctions(unittest.TestCase):
 11
 12    first = 'first'
 13    last = 'last'
 14    a_tuple = (1, 2, 3, 'n')
 15    a_list = [1, 2, 3, 'n']
 16    first_number = 0
 17    second_number = 1
 18
 19    def test_why_use_a_function(self):
 20        # reality = 1 + 0
 21        # reality = 2 + 0
 22        reality = add_x(0)
 23        # my_expectation = 0
 24        # my_expectation = 1
 25        # my_expectation = 2
 26        my_expectation = 3
 27        self.assertEqual(reality, my_expectation)
 28
 29        # reality = 1 + 1
 30        # reality = 2 + 1
 31        reality = add_x(1)
 32        # my_expectation = 1
 33        # my_expectation = 2
 34        # my_expectation = 3
 35        my_expectation = 4
 36        self.assertEqual(reality, my_expectation)
 37
 38        # reality = 1 + 2
 39        # reality = 2 + 2
 40        reality = add_x(2)
 41        # my_expectation = 2
 42        # my_expectation = 3
 43        # my_expectation = 4
 44        my_expectation = 5
 45        self.assertEqual(reality, my_expectation)
 46
 47        # reality = 1 + 3
 48        # reality = 2 + 3
 49        reality = add_x(3)
 50        # my_expectation = 3
 51        # my_expectation = 4
 52        # my_expectation = 5
 53        my_expectation = 6
 54        self.assertEqual(reality, my_expectation)
 55
 56        # reality = 1 + 4
 57        # reality = 2 + 4
 58        reality = add_x(4)
 59        # my_expectation = 4
 60        # my_expectation = 5
 61        # my_expectation = 6
 62        my_expectation = 7
 63        self.assertEqual(reality, my_expectation)
 64
 65        # reality = 1 + 5
 66        # reality = 2 + 5
 67        reality = add_x(5)
 68        # my_expectation = 5
 69        # my_expectation = 6
 70        # my_expectation = 7
 71        my_expectation = 8
 72        self.assertEqual(reality, my_expectation)
 73
 74        # reality = 1 + 6
 75        # reality = 2 + 6
 76        reality = add_x(6)
 77        # my_expectation = 6
 78        # my_expectation = 7
 79        # my_expectation = 8
 80        my_expectation = 9
 81        self.assertEqual(reality, my_expectation)
 82
 83        # reality = 1 + 7
 84        # reality = 2 + 7
 85        reality = add_x(7)
 86        # my_expectation = 7
 87        # my_expectation = 8
 88        # my_expectation = 9
 89        my_expectation = 10
 90        self.assertEqual(reality, my_expectation)
 91
 92        # reality = 1 + 8
 93        # reality = 2 + 8
 94        reality = add_x(8)
 95        # my_expectation = 8
 96        # my_expectation = 9
 97        # my_expectation = 10
 98        my_expectation = 11
 99        self.assertEqual(reality, my_expectation)
100
101        # reality = 1 + 9
102        # reality = 2 + 9
103        reality = add_x(9)
104        # my_expectation = 9
105        # my_expectation = 10
106        # my_expectation = 11
107        my_expectation = 12
108        self.assertEqual(reality, my_expectation)
109
110    def test_making_a_function_w_pass(self):
111        self.assertIs(src.functions.w_pass(), None)
112
113    def test_making_a_function_w_return(self):
114        self.assertIs(src.functions.w_return(), None)
115
116    def test_making_a_function_w_return_none(self):
117        self.assertIs(
118            src.functions.w_return_none(), None
119        )
120
121    def test_what_happens_after_a_function_returns(self):
122        self.assertIs(
123            src.functions.return_is_last(), None
124        )
125
126    def test_constant_function(self):
127        reality = src.functions.constant()
128        my_expectation = 'the same thing'
129        self.assertEqual(reality, my_expectation)
130
131    def test_identity_function(self):
132        reality = src.functions.identity(None)
133        my_expectation = None
134        self.assertEqual(reality, my_expectation)
135
136        reality = src.functions.identity(object)
137        my_expectation = object
138        self.assertEqual(reality, my_expectation)
139
140    def test_w_positional_arguments(self):
141        reality = src.functions.w_positional_arguments(
142            self.first, self.last,
143        )
144        my_expectation = (self.first, self.last)
145        self.assertEqual(reality, my_expectation)
146
147        reality = src.functions.w_positional_arguments(
148            self.last, self.first,
149        )
150        my_expectation = (self.last, self.first)
151        self.assertEqual(reality, my_expectation)
152
153        reality = src.functions.w_positional_arguments(
154            self.first_number, self.second_number,
155        )
156        my_expectation = (
157            self.first_number, self.second_number
158        )
159        self.assertEqual(reality, my_expectation)
160
161        reality = src.functions.w_positional_arguments(
162            self.a_tuple, self.a_list,
163        )
164        my_expectation = (self.a_tuple, self.a_list)
165        self.assertEqual(reality, my_expectation)
166
167    def test_w_keyword_arguments(self):
168        reality = src.functions.w_keyword_arguments(
169            first_input=self.first,
170            last_input=self.last,
171        )
172        my_expectation = (self.first, self.last)
173        self.assertEqual(reality, my_expectation)
174
175        reality = src.functions.w_keyword_arguments(
176            last_input=self.last,
177            first_input=self.first,
178        )
179        my_expectation = (self.first, self.last)
180        self.assertEqual(reality, my_expectation)
181
182        reality = src.functions.w_keyword_arguments(
183            self.last, self.first,
184        )
185        my_expectation = (self.last, self.first)
186        self.assertEqual(reality, my_expectation)
187
188        reality = src.functions.w_keyword_arguments(
189            last_input=self.first_number,
190            first_input=self.second_number,
191        )
192        my_expectation = (
193            self.second_number, self.first_number
194        )
195        self.assertEqual(reality, my_expectation)
196
197        a_set = {1, 2, 3, 'n'}
198        a_dictionary = {'key': 'value'}
199        reality = src.functions.w_keyword_arguments(
200            first_input=a_set,
201            last_input=a_dictionary,
202        )
203        my_expectation = (a_set, a_dictionary)
204        self.assertEqual(reality, my_expectation)
205
206        reality = src.functions.w_positional_arguments(
207            first_input=self.a_list,
208            last_input=self.a_tuple,
209        )
210        my_expectation = (self.a_list, self.a_tuple)
211        self.assertEqual(reality, my_expectation)
212
213    def test_w_args_and_kwargs(self):
214        reality = (
215            src.functions.w_args_and_kwargs(
216                self.first, last_input=self.last,
217            )
218        )
219        my_expectation = (self.first, self.last)
220        self.assertEqual(reality, my_expectation)
221
222    def test_w_optional_arguments(self):
223        first_name, last_name = 'jane', 'doe'
224        reality = src.functions.w_optional_arguments(
225            first_name,
226        )
227        my_expectation = (first_name, last_name)
228        self.assertEqual(reality, my_expectation)
229
230        first_name, blow = 'joe', 'blow'
231        reality = src.functions.w_optional_arguments(
232            first_name, blow,
233        )
234        my_expectation = (first_name, blow)
235        self.assertEqual(reality, my_expectation)
236
237        first_name = 'john'
238        reality = src.functions.w_optional_arguments(
239            first_input=first_name,
240        )
241        my_expectation = (first_name, last_name)
242        self.assertEqual(reality, my_expectation)
243
244        last_name = 'smith'
245        reality = src.functions.w_optional_arguments(
246            last_input=last_name,
247            first_input=first_name,
248        )
249        my_expectation = (first_name, last_name)
250        self.assertEqual(reality, my_expectation)
251
252    def test_w_unknown_arguments(self):
253        a_tuple = (0, 1)
254        a_dictionary = {'a': 2, 'b': 3}
255        reality = src.functions.w_unknown_arguments(
256            *a_tuple, **a_dictionary
257        )
258        my_expectation = (a_tuple, a_dictionary)
259        self.assertEqual(reality, my_expectation)
260
261        a_tuple = (0, 1)
262        a_dictionary = {'a': 2, 'b': 3, 'c': 4}
263        reality = src.functions.w_unknown_arguments(
264            *a_tuple, **a_dictionary
265        )
266        my_expectation = (
267            a_tuple, a_dictionary
268        )
269        self.assertEqual(reality, my_expectation)
270
271        a_tuple = (0, 1, 2)
272        a_dictionary = {'a': 3, 'b': 4, 'c': 5}
273        reality = src.functions.w_unknown_arguments(
274            *a_tuple, **a_dictionary,
275        )
276        my_expectation = (
277            a_tuple, a_dictionary
278        )
279        self.assertEqual(reality, my_expectation)
280
281        a_tuple = (0, 1, 2, 3)
282        reality = src.functions.w_unknown_arguments(
283            *a_tuple
284        )
285        my_expectation = (a_tuple, {})
286        self.assertEqual(reality, my_expectation)
287
288        a_dictionary = {'a': 4, 'b': 5, 'c': 6, 'd': 7}
289        reality = src.functions.w_unknown_arguments(
290            **a_dictionary
291        )
292        my_expectation = ((), a_dictionary)
293        self.assertEqual(reality, my_expectation)
294
295        reality = src.functions.w_unknown_arguments()
296        my_expectation = ((), {})
297        self.assertEqual(reality, my_expectation)
298
299
300# Exceptions seen
301# AssertionError
302# NameError
303# AttributeError
304# TypeError
305# SyntaxError

functions 3: solutions

the solutions in functions/tests/test_functions.py from functions 3

  1import src.functions
  2import unittest
  3
  4
  5class TestFunctions(unittest.TestCase):
  6
  7    def test_why_use_a_function(self):
  8        def add(x, y):
  9            return x + y
 10
 11        numbers = range(-10, 10)
 12        for y in numbers:
 13            for x in numbers:
 14                with self.subTest(x=x, y=y):
 15                    self.assertEqual(add(x, y), x+y)
 16
 17        for x, y in ((x, y) for x in numbers for y in numbers):
 18            with self.subTest(x=x, y=y):
 19                self.assertEqual(add(x, y), x+y)
 20
 21        import itertools
 22        for x, y in itertools.product(numbers, repeat=2):
 23            with self.subTest(x=x, y=y):
 24                self.assertEqual(add(x, y), x+y)
 25
 26    def test_making_a_function_w_pass(self):
 27        self.assertIsNone(src.functions.w_pass())
 28
 29    def test_making_a_function_w_return(self):
 30        self.assertIsNone(src.functions.w_return())
 31
 32    def test_making_a_function_w_return_none(self):
 33        self.assertIsNone(src.functions.w_return_none())
 34
 35    def test_what_happens_after_functions_return(self):
 36        self.assertIsNone(src.functions.return_is_last())
 37
 38    def test_constant_function(self):
 39        self.assertEqual(
 40            src.functions.constant(),
 41            'the same thing'
 42        )
 43
 44    def test_identity_function(self):
 45        self.assertIsNone(src.functions.identity(None))
 46        self.assertEqual(src.functions.identity(object), object)
 47
 48    def test_w_positional_arguments(self):
 49        self.assertEqual(
 50            src.functions.w_positional_arguments('first', 'last'),
 51            ('first', 'last')
 52        )
 53        self.assertEqual(
 54            src.functions.w_positional_arguments('last', 'first'),
 55            ('last', 'first')
 56        )
 57
 58    def test_w_keyword_arguments(self):
 59        self.assertEqual(
 60            src.functions.w_keyword_arguments(
 61                first_input='first', last_input='last',
 62            ),
 63            ('first', 'last')
 64        )
 65        self.assertEqual(
 66            src.functions.w_keyword_arguments(
 67                last_input='last', first_input='first',
 68            ),
 69            ('first', 'last')
 70        )
 71        self.assertEqual(
 72            src.functions.w_keyword_arguments('last', 'first'),
 73            ('last', 'first')
 74        )
 75
 76    def test_w_args_and_kwargs(self):
 77        self.assertEqual(
 78            src.functions.w_args_and_kwargs(
 79                'first', last_input='last',
 80            ),
 81            ('first', 'last')
 82        )
 83
 84    def test_w_optional_arguments(self):
 85        self.assertEqual(
 86            src.functions.w_optional_arguments('jane'),
 87            ('jane', 'doe')
 88        )
 89        self.assertEqual(
 90            src.functions.w_optional_arguments('joe', 'blow'),
 91            ('joe', 'blow')
 92        )
 93
 94    def test_w_unknown_arguments(self):
 95        self.assertEqual(
 96            src.functions.w_unknown_arguments(
 97                0, 1, 2, 3, a=4, b=5, c=6, d=7,
 98            ),
 99            ((0, 1, 2, 3, ), {'a': 4, 'b': 5, 'c': 6, 'd': 7})
100        )
101        self.assertEqual(
102            src.functions.w_unknown_arguments(0, 1, 2, 3),
103            ((0, 1, 2, 3), {})
104        )
105        self.assertEqual(
106            src.functions.w_unknown_arguments(a=4, b=5, c=6, d=7),
107            ((), dict(a=4, b=5, c=6, d=7))
108        )
109        self.assertEqual(
110            src.functions.w_unknown_arguments(),
111            ((), {})
112        )
113
114
115# Exceptions seen
116# AssertionError
117# NameError
118# AttributeError
119# TypeError