forked from UWPCE-PythonCert/ProgrammingInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_kwargs_lab.py
More file actions
412 lines (313 loc) · 11.1 KB
/
Copy pathtest_kwargs_lab.py
File metadata and controls
412 lines (313 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python3
"""
Test code for the args-kwargs lab
This kind of experimental code isn't all the suited to testing, but it's
not a bad way to run code as you develop it anyway...
And we want to encourage test code -- so we'll use it everywhere possible
Note: I decided that instead of having my function print somthing it would
return a string -- that way I could test that the returned string was correct.
"""
import pytest # used for testing exceptions
from kwargs_lab import colors, call_colors, colors_manual
# Calling "colors" in various ways.
def test_all_positional():
result = colors('red', 'blue', 'yellow', 'chartreuse')
# these aren't exhaustive by any means
# but mostly I want to make the code run without error
print(result)
assert 'red' in result
assert 'blue' in result
assert 'chartreuse' in result
# you can force a test failure if you want to see the output
# assert False
def test_one_keyword():
result = colors(link_color='purple')
# these aren't exhaustive by any means
# but mostly I want to make the code runs without error
print(result)
assert 'link: purple' in result
# you can force a test failure if you want to see the output
# assert False
def test_pos_and_keyword():
result = colors('yellow', 'gray', link_color='purple')
# these aren't exhaustive by any means
print(result)
assert 'foreground: yellow' in result
assert 'background: gray' in result
assert 'link: purple' in result
# you can force a test failure if you want to see the output
# assert False
def test_duplicate():
"""
It's an error to have a keyword argument that duplicates a
positional one
"""
# this is the nifty pytest way to check for Exceptions
with pytest.raises(TypeError):
result = colors('yellow', fore_color='purple')
print(result)
def test_duplicate2():
"""
It's an error to have a keyword argument that duplicates a
positional one
"""
# this is a way to do it by hand:
try:
result = colors('yellow', fore_color='purple')
print(result)
assert False
except TypeError as err:
# you can also check if the error message is what you expect
assert "multiple values for argument" in err.args[0]
def test_call_tuple():
t = ('red', 'blue', 'yellow', 'chartreuse')
result = colors(*t)
# these aren't exhaustive by any means
# but mostly I want to make the code runs without error
print(result)
assert 'red' in result
assert 'blue' in result
assert 'chartreuse' in result
# you can force a test failure if you want to see the output
# assert False
def test_call_dict():
d = {'fore_color': 'red',
'visited_color': 'cyan',
'link_color': 'green',
'back_color': 'blue',
}
result = colors(**d)
print(result)
assert 'foreground: red' in result
assert 'background: blue' in result
assert 'visited: cyan' in result
assert 'link: green' in result
# you can force a test failure if you want to see the output
# assert False
def test_call_tuple_dict():
t = ('red', 'blue')
d = {'visited_color': 'cyan',
'link_color': 'green',
}
result = colors(*t, **d)
print(result)
assert 'foreground: red' in result
assert 'background: blue' in result
assert 'visited: cyan' in result
assert 'link: green' in result
# you can force a test failure if you want to see the output
# assert False
def test_call_everything():
"""
this one uses:
- a positional argument
- *tuple
- a keyword argument
- **dict
"""
t = ('red',) # note the extra comma to amke it a tuple!
d = {'visited_color': 'cyan'}
result = colors('blue', *t, link_color='orange', **d)
print(result)
assert 'foreground: blue' in result
assert 'background: red' in result
assert 'visited: cyan' in result
assert 'link: orange' in result
# you can force a test failure if you want to see the output
# assert False
def test_call_undefined_kwarg():
# should get an error passing in non-existand keyword
with pytest.raises(TypeError):
result = colors(weird_color='grey')
# ###########################
# now lets try calling call_colors in all the above ways, and see if we get what we expect.
#
# Note that these tests are really testing the Python machinery
# -- which you don't need to do! Python is already well tested.
#
# But writing theese tests tests your undestanding of how things work
# if a test fails, it's because you ( I ;- ) didn't understand the
# calling convertions.
# ############################
def test_call_all_positional():
args, kwargs = call_colors('red', 'blue', 'yellow', 'chartreuse')
# no kwrags, they should all be in the args tuple:
assert not kwargs # kwargs is empty
assert args == ('red', 'blue', 'yellow', 'chartreuse')
def test_call_one_keyword():
args, kwargs = call_colors(link_color='purple')
assert not args # args should be an empty tuple
assert kwargs['link_color'] == 'purple'
assert len(kwargs) == 1 # only one entry in the kwargs dict
def test_call_pos_and_keyword():
args, kwargs = call_colors('yellow', 'gray', link_color='purple')
assert args == ('yellow', 'gray')
assert kwargs == {'link_color': 'purple'}
def test_call_duplicate():
"""
This was an error above -- but is not here -- no keyword arguments
to duplicate!
"""
args, kwargs = call_colors('yellow', fore_color='purple')
assert args == ('yellow',)
assert kwargs == {'fore_color': 'purple'}
def test_call_call_tuple():
t = ('red', 'blue', 'yellow', 'chartreuse')
args, kwargs = call_colors(*t)
# This is straighforward -- the args tuple should be the one passed in!
assert args == t
# But it is NOT the SAME tuple!
assert args is not t
# and an empty kwargs dict
assert kwargs == {} # multiple ways to test for an empty dict.
def test_call_call_dict():
d = {'fore_color': 'red',
'visited_color': 'cyan',
'link_color': 'green',
'back_color': 'blue',
}
args, kwargs = call_colors(**d)
# also easy -- should be the dict passed in
assert kwargs == d
assert args == tuple()
def test_call_call_tuple_dict():
t = ('red', 'blue')
d = {'visited_color': 'cyan',
'link_color': 'green',
}
args, kwargs = call_colors(*t, **d)
# this should be just what's passed in.
assert args == t
assert kwargs == d
def test_call_call_everything():
"""
this one uses:
- a positional argument
- *tuple
- a keyword argument
- **dict
"""
t = ('red',) # note the extra comma to amke it a tuple!
d = {'visited_color': 'cyan'}
args, kwargs = call_colors('blue', *t, link_color='orange', **d)
# This one is more interesting:
assert args == ('blue',) + t
assert args == ('blue', 'red') # a different way to spell the same thing
assert kwargs == {'visited_color': 'cyan', 'link_color': 'orange'}
# or:
d['link_color'] = 'orange'
assert kwargs == d
# ##################
# Now to test the manual upacking of args, kwargs
# All the same tests as above should pass
# ##################
# Calling "colors" in various ways.
def test_manual_all_positional():
result = colors('red', 'blue', 'yellow', 'chartreuse')
# these aren't exhaustive by any means
# but mostly I want to make the code runs without error
print(result)
assert 'red' in result
assert 'blue' in result
assert 'chartreuse' in result
# you can force a test failure if you want to see the output
# assert False
def test_manual_one_keyword():
result = colors_manual(link_color='purple')
# these aren't exhaustive by any means
# but mostly I want to make the code runs without error
print(result)
assert 'link: purple' in result
# you can force a test failure if you want to see the output
# assert False
def test_manual_pos_and_keyword():
result = colors_manual('yellow', 'gray', link_color='purple')
# these aren't exhaustive by any means
print(result)
assert 'foreground: yellow' in result
assert 'background: gray' in result
assert 'link: purple' in result
# you can force a test failure if you want to see the output
# assert False
def test_manual_duplicate():
"""
It's an error to have a keyword argument that duplicates a
positional one
"""
# this is the nifty pytest way to check for Exceptions
with pytest.raises(TypeError):
result = colors_manual('yellow', fore_color='purple')
print(result)
def test_manual_duplicate2():
"""
It's an error to have a keyword argument that duplicates a
positional one
"""
# this is a way to do it by hand:
try:
result = colors_manual('yellow', fore_color='purple')
print(result)
assert False
except TypeError as err:
# you can also check if the error message is what you expect
assert "multiple values for argument" in err.args[0]
def test_manual_call_tuple():
t = ('red', 'blue', 'yellow', 'chartreuse')
result = colors_manual(*t)
# these aren't exhaustive by any means
# but mostly I want to make the code runs without error
print(result)
assert 'red' in result
assert 'blue' in result
assert 'chartreuse' in result
# you can force a test failure if you want to see the output
# assert False
def test_manual_call_dict():
d = {'fore_color': 'red',
'visited_color': 'cyan',
'link_color': 'green',
'back_color': 'blue',
}
result = colors_manual(**d)
print(result)
assert 'foreground: red' in result
assert 'background: blue' in result
assert 'visited: cyan' in result
assert 'link: green' in result
# you can force a test failure if you want to see the output
# assert False
def test_manual_call_tuple_dict():
t = ('red', 'blue')
d = {'visited_color': 'cyan',
'link_color': 'green',
}
result = colors_manual(*t, **d)
print(result)
assert 'foreground: red' in result
assert 'background: blue' in result
assert 'visited: cyan' in result
assert 'link: green' in result
# you can force a test failure if you want to see the output
# assert False
def test_manual_call_everything():
"""
this one uses:
- a positional argument
- *tuple
- a keyword argument
- **dict
"""
t = ('red',) # note the extra comma to amke it a tuple!
d = {'visited_color': 'cyan'}
result = colors_manual('blue', *t, link_color='orange', **d)
print(result)
assert 'foreground: blue' in result
assert 'background: red' in result
assert 'visited: cyan' in result
assert 'link: orange' in result
# you can force a test failure if you want to see the output
# assert False
def test_manual_call_undefined_kwarg():
# should get an error passing in non-existand keyword
with pytest.raises(TypeError):
result = colors_manual(weird_color='grey')