functions: tests and solutions

what is a function?: tests

The code in functions/tests/test_functions.py from what is a function?

 1def test_making_a_function_w_pass():
 2    def w_pass():
 3        pass
 4
 5    assert w_pass() is None
 6
 7
 8def test_making_a_function_w_return():
 9    def w_return():
10        return
11
12    assert w_return() is None
13
14
15def test_making_a_function_w_return_none():
16    def w_return_none():
17        return None
18
19    assert w_return_none() is None
20
21
22def test_what_happens_after_functions_return():
23    def return_leaves_the_function():
24        return None
25        return 'only one way for this line to run'
26
27    assert return_leaves_the_function() is None
28
29
30def test_constant_function():
31    def constant():
32        return 'the same thing'
33
34    assert constant() == 'the same thing'
35
36
37# Exceptions seen
38# AssertionError
39# NameError
40# TypeError

functions that take input: tests

The code in functions/tests/test_functions.py from functions that take input

  1def test_making_a_function_w_pass():
  2    def w_pass():
  3        pass
  4
  5    assert w_pass() is None
  6
  7
  8def test_making_a_function_w_return():
  9    def w_return():
 10        return
 11
 12    assert w_return() is None
 13
 14
 15def test_making_a_function_w_return_none():
 16    def w_return_none():
 17        return None
 18
 19    assert w_return_none() is None
 20
 21
 22def test_what_happens_after_functions_return():
 23    def return_leaves_the_function():
 24        return None
 25        return 'only one way for this line to run'
 26
 27    assert return_leaves_the_function() is None
 28
 29
 30def test_constant_function():
 31    def constant():
 32        return 'the same thing'
 33
 34    assert constant() == 'the same thing'
 35
 36
 37def test_identity_function():
 38    def identity(the_input):
 39        return the_input
 40
 41    assert identity(None) == None
 42    assert identity(object) == object
 43
 44
 45def test_why_use_a_function():
 46    def add_x(number):
 47        return 3 + number
 48
 49    assert add_x(0) == 3
 50    assert add_x(1) == 4
 51    assert add_x(2) == 5
 52    assert add_x(3) == 6
 53    assert add_x(4) == 7
 54    assert add_x(5) == 8
 55    assert add_x(6) == 9
 56    assert add_x(7) == 10
 57    assert add_x(8) == 11
 58    assert add_x(9) == 12
 59
 60
 61def positional_arguments(first_input, last_input):
 62    return first_input, last_input
 63
 64
 65def test_positional_arguments():
 66    first, last = 'first', 'last'
 67
 68    assert (
 69        positional_arguments(first, last)
 70     == (first, last)
 71    )
 72    assert (
 73        positional_arguments(last, first)
 74     == (last, first)
 75    )
 76    assert (
 77        positional_arguments(0, 1)
 78     == (0, 1)
 79    )
 80
 81    a_tuple = (0, 1, 2, 'n')
 82    a_list = [0, 1, 2, 'n']
 83    assert (
 84        positional_arguments(a_tuple, a_list)
 85     == (a_tuple, a_list)
 86    )
 87
 88    a_set = {0, 1, 2, 'n'}
 89    a_dictionary = {'key': 'value'}
 90    assert (
 91        keyword_arguments(
 92            a_set, a_dictionary,
 93        )
 94     == (a_set, a_dictionary)
 95    )
 96
 97
 98def keyword_arguments(first_input, last_input):
 99    return first_input, last_input
100
101def test_keyword_arguments():
102    first, last = 'first', 'last'
103
104    assert (
105        keyword_arguments(
106            first_input=first, last_input=last,
107        )
108     == (first, last)
109    )
110    assert (
111        keyword_arguments(
112            last_input=last, first_input=first,
113        )
114     == (first, last)
115    )
116    assert (
117        keyword_arguments(
118            last_input=0, first_input=1,
119        )
120     == (1, 0)
121    )
122
123    a_tuple = (0, 1, 2, 'n')
124    a_list = [0, 1, 2, 'n']
125    assert (
126        keyword_arguments(
127            first_input=a_tuple,
128            last_input=a_list,
129        )
130     == (a_tuple, a_list)
131    )
132
133    a_set = {0, 1, 2, 'n'}
134    a_dictionary = {'key': 'value'}
135    assert (
136        positional_arguments(
137            last_input=a_dictionary,
138            first_input=a_set,
139        )
140     == (a_set, a_dictionary)
141    )
142
143
144def test_args_and_kwargs():
145    def args_and_kwargs(first_input, last_input):
146        return first_input, last_input
147
148    first, last = 'first', 'last'
149
150    assert (
151        args_and_kwargs(
152            first, last_input=last,
153        )
154     == (first, last)
155    )
156
157
158def test_optional_arguments():
159    def optional_arguments(
160        first_input, last_input='doe',
161    ):
162        return first_input, last_input
163
164    first_name, last_name = 'jane', 'doe'
165
166    assert (
167        optional_arguments(
168            first_name,
169        )
170     == (first_name, last_name)
171    )
172
173    first_name, blow = 'joe', 'blow'
174    assert (
175        optional_arguments(
176            first_name, blow
177        )
178     == (first_name, blow)
179    )
180
181    first_name = 'john'
182    assert (
183        optional_arguments(
184            first_input=first_name
185        )
186     == (first_name, last_name)
187    )
188
189    last_name = 'smith'
190    assert (
191        optional_arguments(
192            last_input=last_name,
193            first_input=first_name,
194        )
195     == (first_name, last_name)
196    )
197
198
199def test_unknown_number_of_arguments():
200    def unknown_number_of_arguments(
201        *positional_arguments, **keyword_arguments
202    ):
203        return positional_arguments, keyword_arguments
204
205    a_tuple = (0, 1)
206    a_dictionary = {'a': 2, 'b': 3}
207    assert (
208        unknown_number_of_arguments(
209            *a_tuple, **a_dictionary
210        )
211     == (a_tuple, a_dictionary)
212    )
213
214    a_tuple = (0, 1)
215    a_dictionary = {'a': 2, 'b': 3, 'c': 4}
216    assert (
217        unknown_number_of_arguments(
218            *a_tuple, **a_dictionary
219        )
220     == (a_tuple, a_dictionary)
221    )
222
223    a_tuple = (0, 1, 2)
224    a_dictionary = {'a': 3, 'b': 4, 'c': 5}
225    assert (
226        unknown_number_of_arguments(
227            *a_tuple, **a_dictionary
228        )
229     == (a_tuple, a_dictionary)
230    )
231
232    a_tuple = (0, 1, 2, 'n')
233    assert (
234        unknown_number_of_arguments(*a_tuple)
235     == (a_tuple, {})
236    )
237
238    a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'}
239    assert (
240        unknown_number_of_arguments(**a_dictionary)
241     == ((), a_dictionary)
242    )
243
244    assert (
245        unknown_number_of_arguments()
246     == ((), {})
247    )
248
249
250# Exceptions seen
251# AssertionError
252# NameError
253# TypeError
254# SyntaxError

separate and equal functions: tests and solutions


separate and equal functions tests


The code in functions/tests/test_functions.py from separate and equal functions

  1import src.functions
  2
  3
  4def test_making_a_function_w_pass():
  5    assert src.functions.w_pass() is None
  6
  7
  8def test_making_a_function_w_return():
  9    assert src.functions.w_return() is None
 10
 11
 12def test_making_a_function_w_return_none():
 13    assert src.functions.w_return_none() is None
 14
 15
 16def test_what_happens_after_functions_return():
 17    assert (
 18        src.functions
 19           .return_leaves_the_function()
 20    ) is None
 21
 22
 23def test_constant_function():
 24    assert src.functions.constant() == 'the same thing'
 25
 26
 27def test_identity_function():
 28    assert src.functions.identity(None) == None
 29    assert src.functions.identity(object) == object
 30
 31
 32def test_why_use_a_function():
 33    def add_x(number):
 34        return 3 + number
 35
 36    assert add_x(0) == 3
 37    assert add_x(1) == 4
 38    assert add_x(2) == 5
 39    assert add_x(3) == 6
 40    assert add_x(4) == 7
 41    assert add_x(5) == 8
 42    assert add_x(6) == 9
 43    assert add_x(7) == 10
 44    assert add_x(8) == 11
 45    assert add_x(9) == 12
 46
 47
 48def test_positional_arguments():
 49    positional_arguments = (
 50        src.functions.positional_arguments
 51    )
 52    first, last = 'first', 'last'
 53
 54    assert (
 55        positional_arguments(first, last)
 56     == (first, last)
 57    )
 58    assert (
 59        positional_arguments(last, first)
 60     == (last, first)
 61    )
 62    assert (
 63        positional_arguments(0, 1)
 64     == (0, 1)
 65    )
 66
 67    a_tuple = (0, 1, 2, 'n')
 68    a_list = [0, 1, 2, 'n']
 69    assert (
 70        positional_arguments(a_tuple, a_list)
 71     == (a_tuple, a_list)
 72    )
 73
 74    keyword_arguments = (
 75        src.functions.keyword_arguments
 76    )
 77    a_set = {0, 1, 2, 'n'}
 78    a_dictionary = {'key': 'value'}
 79    assert (
 80        keyword_arguments(
 81            a_set, a_dictionary,
 82        )
 83     == (a_set, a_dictionary)
 84    )
 85
 86
 87def test_keyword_arguments():
 88    keyword_arguments = (
 89        src.functions.keyword_arguments
 90    )
 91    first, last = 'first', 'last'
 92
 93    assert (
 94        keyword_arguments(
 95            first_input=first, last_input=last,
 96        )
 97     == (first, last)
 98    )
 99    assert (
100        keyword_arguments(
101            last_input=last, first_input=first,
102        )
103     == (first, last)
104    )
105    assert (
106        keyword_arguments(
107            last_input=0, first_input=1,
108        )
109     == (1, 0)
110    )
111
112    a_tuple = (0, 1, 2, 'n')
113    a_list = [0, 1, 2, 'n']
114    assert (
115        keyword_arguments(
116            first_input=a_tuple,
117            last_input=a_list,
118        )
119     == (a_tuple, a_list)
120    )
121
122    positional_arguments = (
123        src.functions.positional_arguments
124    )
125    a_set = {0, 1, 2, 'n'}
126    a_dictionary = {'key': 'value'}
127    assert (
128        positional_arguments(
129            last_input=a_dictionary,
130            first_input=a_set,
131        )
132     == (a_set, a_dictionary)
133    )
134
135
136def test_args_and_kwargs():
137    first, last = 'first', 'last'
138
139    assert (
140        src.functions.args_and_kwargs(
141            first, last_input=last,
142        )
143     == (first, last)
144    )
145
146
147def test_optional_arguments():
148    optional_arguments = (
149        src.functions.optional_arguments
150    )
151
152    first_name, last_name = 'jane', 'doe'
153
154    assert (
155        optional_arguments(
156            first_name,
157        )
158     == (first_name, last_name)
159    )
160
161    first_name, blow = 'joe', 'blow'
162    assert (
163        optional_arguments(
164            first_name, blow
165        )
166     == (first_name, blow)
167    )
168
169    first_name = 'john'
170    assert (
171        optional_arguments(
172            first_input=first_name
173        )
174     == (first_name, last_name)
175    )
176
177    last_name = 'smith'
178    assert (
179        optional_arguments(
180            last_input=last_name,
181            first_input=first_name,
182        )
183     == (first_name, last_name)
184    )
185
186
187def test_unknown_number_of_arguments():
188    unknown_number_of_arguments = (
189        src.functions.unknown_number_of_arguments
190    )
191
192    a_tuple = (0, 1)
193    a_dictionary = {'a': 2, 'b': 3}
194    assert (
195        unknown_number_of_arguments(
196            *a_tuple, **a_dictionary
197        )
198     == (a_tuple, a_dictionary)
199    )
200
201    a_tuple = (0, 1)
202    a_dictionary = {'a': 2, 'b': 3, 'c': 4}
203    assert (
204        unknown_number_of_arguments(
205            *a_tuple, **a_dictionary
206        )
207     == (a_tuple, a_dictionary)
208    )
209
210    a_tuple = (0, 1, 2)
211    a_dictionary = {'a': 3, 'b': 4, 'c': 5}
212    assert (
213        unknown_number_of_arguments(
214            *a_tuple, **a_dictionary
215        )
216     == (a_tuple, a_dictionary)
217    )
218
219    a_tuple = (0, 1, 2, 'n')
220    assert (
221        unknown_number_of_arguments(*a_tuple)
222     == (a_tuple, {})
223    )
224
225    a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'}
226    assert (
227        unknown_number_of_arguments(**a_dictionary)
228     == ((), a_dictionary)
229    )
230
231    assert (
232        unknown_number_of_arguments()
233     == ((), {})
234    )
235
236
237# Exceptions seen
238# AssertionError
239# NameError
240# TypeError
241# SyntaxError
242# ModuleNotFoundError
243# AttributeError

separate and equal functions solutions


The code in functions/src/functions.py from separate and equal functions

 1def unknown_number_of_arguments(
 2    *positional, **keyword
 3):
 4    return positional, keyword
 5
 6
 7def optional_arguments(
 8    first_input, last_input='doe',
 9):
10    return first_input, last_input
11
12
13def args_and_kwargs(argument, last_input):
14    return argument, last_input
15
16
17def keyword_arguments(first_input, last_input):
18    return first_input, last_input
19
20
21def positional_arguments(first_input, last_input):
22    return first_input, last_input
23
24
25def identity(argument):
26    return argument
27
28
29def constant():
30    return 'the same thing'
31
32
33def return_leaves_the_function():
34    return None
35
36
37def w_return_none():
38    return None
39
40
41def w_return():
42    return None
43
44
45def w_pass():
46    return None

test functions with unittest: tests

The code in functions/tests/test_functions.py from test functions with unittest

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

test functions with assertIsNotNone and assertIsNone: tests

The code in functions/tests/test_functions.py from test functions with assertIsNotNone and assertIsNone