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