telephone: tests and solution

telephone tests

The code in telephone/tests/test_telephone.py from telephone

telephone/tests/test_telephone.py
 1def assert_equal(a, b):
 2    assert a == b
 3
 4
 5def text(the_input):
 6    return f'I got: {the_input}'
 7
 8
 9def test_passing_none():
10    assert_equal(text(None), 'I got: None')
telephone/tests/test_telephone.py
13def test_passing_booleans():
14    assert_equal(text(False), 'I got: False')
15    assert_equal(text(True), 'I got: True')
16
17
18def test_passing_an_integer():
19    an_integer = 1234
20    assert_equal(text(an_integer), f'I got: {an_integer}')
21
22
23def test_passing_a_float():
24    a_float = 5.678
25    assert_equal(text(a_float), f'I got: {a_float}')
telephone/tests/test_telephone.py
28def test_passing_a_string():
29    a_string = 'hello'
30    assert_equal(text(a_string), f'I got: {a_string}')
31
32
33def test_passing_a_tuple():
34    a_tuple = (0, 1, 2, 'n')
35    assert_equal(text(a_tuple), f"I got: {a_tuple}")
36
37
38def test_passing_a_list():
39    a_list = [0, 1, 2, 'n']
40    assert_equal(text(a_list), f'I got: {a_list}')
telephone/tests/test_telephone.py
43def test_passing_a_set():
44    a_set = {0, 1, 2, 'n'}
45    assert_equal(text(a_set), f'I got: {a_set}')
46
47
48def test_passing_a_dictionary():
49    a_dictionary = {
50        'key0': 'value0',
51        'keyN': [0, 1, 2, 'n'],
52    }
53    reality = text(a_dictionary)
54    my_expectation = f'I got: {a_dictionary}'
55    assert_equal(reality, my_expectation)
telephone/tests/test_telephone.py
58def test_passing_a_class():
59    assert_equal(
60        text(object), "I got: <class 'object'>"
61    )
62    assert_equal(text(bool), "I got: <class 'bool'>")
63    assert_equal(text(int), "I got: <class 'int'>")
64    assert_equal(text(float), "I got: <class 'float'>")
65    assert_equal(text(str), "I got: <class 'str'>")
66    assert_equal(text(tuple), "I got: <class 'tuple'>")
67    assert_equal(text(list), "I got: <class 'list'>")
68    assert_equal(text(set), "I got: <class 'set'>")
69    assert_equal(text(dict), "I got: <class 'dict'>")
70
71
72# Exceptions seen
73# AssertionError
74# NameError
75# TypeError

separate and equal telephone: tests and solution

separate and equal telephone: tests


The code in telephone/tests/test_telephone.py from separate and equal telephone

telephone/tests/test_telephone.py
 1import src.telephone
 2
 3
 4text = src.telephone.text
 5
 6
 7def assert_equal(a, b):
 8    assert a == b
 9
10
11def test_passing_none():
12    assert_equal(text(None), 'I got: None')
telephone/tests/test_telephone.py
15def test_passing_booleans():
16    assert_equal(text(False), 'I got: False')
17    assert_equal(text(True), 'I got: True')
18
19
20def test_passing_an_integer():
21    an_integer = 1234
22    assert_equal(text(an_integer), f'I got: {an_integer}')
23
24
25def test_passing_a_float():
26    a_float = 5.678
27    assert_equal(text(a_float), f'I got: {a_float}')
telephone/tests/test_telephone.py
30def test_passing_a_string():
31    a_string = 'hello'
32    assert_equal(text('hello'), f'I got: {a_string}')
33
34
35def test_passing_a_tuple():
36    a_tuple = (0, 1, 2, 'n')
37    assert_equal(text(a_tuple), f"I got: {a_tuple}")
38
39
40def test_passing_a_list():
41    a_list = [0, 1, 2, 'n']
42    assert_equal(text(a_list), f'I got: {a_list}')
telephone/tests/test_telephone.py
45def test_passing_a_set():
46    a_set = {0, 1, 2, 'n'}
47    assert_equal(text(a_set), f'I got: {a_set}')
48
49
50def test_passing_a_dictionary():
51    a_dictionary = {
52        'key0': 'value0',
53        'keyN': [0, 1, 2, 'n'],
54    }
55    reality = text(a_dictionary)
56    my_expectation = f'I got: {a_dictionary}'
57    assert_equal(reality, my_expectation)
telephone/tests/test_telephone.py
60def test_passing_a_class():
61    assert_equal(
62        text(object), "I got: <class 'object'>"
63    )
64    assert_equal(text(bool), "I got: <class 'bool'>")
65    assert_equal(text(int), "I got: <class 'int'>")
66    assert_equal(text(float), "I got: <class 'float'>")
67    assert_equal(text(str), "I got: <class 'str'>")
68    assert_equal(text(tuple), "I got: <class 'tuple'>")
69    assert_equal(text(list), "I got: <class 'list'>")
70    assert_equal(text(set), "I got: <class 'set'>")
71    assert_equal(text(dict), "I got: <class 'dict'>")
72
73
74# Exceptions seen
75# AssertionError
76# NameError
77# TypeError
78# AttributeError

separate and equal telephone: solution


The code in telephone/src/telephone/__init__.py from separate and equal telephone

telephone/src/telephone/__init__.py
1def text(value):
2    return f'I got: {value}'

test telephone with unittest: tests

The code in telephone/tests/test_telephone.py from test telephone with unittest

telephone/tests/test_telephone.py
 1import src.telephone
 2import unittest
 3
 4
 5text = src.telephone.text
 6
 7
 8class TestTelephone(unittest.TestCase):
 9
10    def test_passing_none(self):
11        self.assertEqual(text(None), 'I got: None')
12
13    def test_passing_booleans(self):
14        self.assertEqual(text(False), 'I got: False')
15        self.assertEqual(text(True), 'I got: True')
telephone/tests/test_telephone.py
17    def test_passing_an_integer(self):
18        an_integer = 1234
19        self.assertEqual(
20            text(an_integer), f'I got: {an_integer}'
21        )
22
23    def test_passing_a_float(self):
24        a_float = 5.678
25        self.assertEqual(
26            text(a_float), f'I got: {a_float}'
27        )
telephone/tests/test_telephone.py
29    def test_passing_a_string(self):
30        a_string = 'hello'
31        self.assertEqual(
32            text('hello'), f'I got: {a_string}'
33        )
34
35    def test_passing_a_tuple(self):
36        a_tuple = (0, 1, 2, 'n')
37        self.assertEqual(
38            text(a_tuple), f"I got: {a_tuple}"
39        )
telephone/tests/test_telephone.py
41    def test_passing_a_list(self):
42        a_list = [0, 1, 2, 'n']
43        self.assertEqual(
44            text(a_list), f'I got: {a_list}'
45        )
46
47    def test_passing_a_set(self):
48        a_set = {0, 1, 2, 'n'}
49        self.assertEqual(text(a_set), f'I got: {a_set}')
telephone/tests/test_telephone.py
51    def test_passing_a_dictionary(self):
52        a_dictionary = {
53            'key0': 'value0',
54            'keyN': [0, 1, 2, 'n'],
55        }
56        reality = text(a_dictionary)
57        my_expectation = f'I got: {a_dictionary}'
58        self.assertEqual(reality, my_expectation)
telephone/tests/test_telephone.py
60    def test_passing_a_class(self):
61        self.assertEqual(
62            text(object), "I got: <class 'object'>"
63        )
64        self.assertEqual(
65            text(bool), "I got: <class 'bool'>"
66        )
67        self.assertEqual(text(int), "I got: <class 'int'>")
68        self.assertEqual(
69            text(float), "I got: <class 'float'>"
70        )
71        self.assertEqual(text(str), "I got: <class 'str'>")
72        self.assertEqual(
73            text(tuple), "I got: <class 'tuple'>"
74        )
75        self.assertEqual(
76            text(list), "I got: <class 'list'>"
77        )
78        self.assertEqual(text(set), "I got: <class 'set'>")
79        self.assertEqual(
80            text(dict), "I got: <class 'dict'>"
81        )
82
83
84# Exceptions seen
85# AssertionError
86# NameError
87# TypeError
88# AttributeError