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 tests/test_functions.py
from functions
import src.functions
import unittest
class TestFunctions(unittest.TestCase):
def test_make_a_function_w_pass(self):
self.assertIsNone(src.functions.w_pass())
def test_make_a_function_w_return(self):
self.assertIsNone(src.functions.w_return())
def test_make_a_function_w_return_none(self):
self.assertIsNone(src.functions.w_return_none())
def test_constant_function(self):
self.assertEqual(
src.functions.constant(),
'the same thing'
)
def test_identity_function(self):
self.assertIsNone(src.functions.identity(None))
self.assertEqual(src.functions.identity(object), object)
def test_functions_w_positional_arguments(self):
self.assertEqual(
src.functions.w_positional_arguments('first', 'second'),
('first', 'second')
)
self.assertEqual(
src.functions.w_positional_arguments('second', 'first'),
('second', 'first')
)
def test_functions_w_keyword_arguments(self):
self.assertEqual(
src.functions.w_keyword_arguments(
x='first', y='second'
),
('first', 'second')
)
self.assertEqual(
src.functions.w_keyword_arguments(
y='second', x='first'
),
('first', 'second')
)
self.assertEqual(
src.functions.w_keyword_arguments('second', 'first'),
('second', 'first')
)
def test_functions_w_positional_and_keyword_arguments(self):
self.assertEqual(
src.functions.w_positional_and_keyword_arguments(
'john', last_name='smith',
),
('john', 'smith')
)
def test_functions_w_default_arguments(self):
self.assertEqual(
src.functions.w_default_arguments(
'jane',
),
('jane', 'doe')
)
def test_functions_w_unknown_arguments(self):
self.assertEqual(
src.functions.w_unknown_arguments(
1, 2, 3, 4, a=5, b=6, c=7, d=8
),
((1, 2, 3, 4), {'a': 5, 'b': 6, 'c': 7, 'd': 8})
)
self.assertEqual(
src.functions.w_unknown_arguments(
None, bool, int, float, str, tuple, list, set, dict,
none=None, a_boolean=bool, an_integer=int, a_float=float,
a_string=str, a_list=list, a_set=set, a_dictionary=dict,
),
(
(None, bool, int, float, str, tuple, list, set, dict),
dict(
a_boolean=bool, a_dictionary=dict, a_float=float,
a_list=list, a_set=set, a_string=str, an_integer=int,
none=None,
)
)
)
# Exceptions Encountered
# AssertionError
# NameError
# AttributeError
# SyntaxError
solutions¶
the solutions in functions.py
from functions
def w_pass():
pass
def w_return():
return
def w_return_none():
return None
def constant():
return 'the same thing'
def identity(argument):
return argument
def w_positional_arguments(x, y):
return x, y
def w_keyword_arguments(x, y):
return x, y
def w_positional_and_keyword_arguments(first_name, last_name='doe'):
return first_name, last_name
def w_default_arguments(first_name, last_name='doe'):
return first_name, last_name
def w_unknown_arguments(*arguments, **keyword_arguments):
return arguments, keyword_arguments