how to pass values: tests and solution¶
tests¶
the code in tests/test_telephone.py from how to pass values
1import src.telephone
2import unittest
3
4
5class TestTelephone(unittest.TestCase):
6
7 def test_passing_a_string(self):
8 self.assertEqual(
9 src.telephone.text("hello"),
10 "I received: hello"
11 )
12 self.assertEqual(
13 src.telephone.text("yes"),
14 "I received: yes"
15 )
16
17 def test_passing_a_class(self):
18 self.assertEqual(
19 src.telephone.text(object),
20 "I received: <class 'object'>"
21 )
22
23 def test_passing_none(self):
24 self.assertEqual(
25 src.telephone.text(None),
26 "I received: None"
27 )
28
29 def test_passing_a_boolean(self):
30 self.assertEqual(
31 src.telephone.text(True),
32 "I received: True"
33 )
34 self.assertEqual(
35 src.telephone.text(False),
36 "I received: False"
37 )
38
39 def test_passing_an_integer(self):
40 self.assertEqual(
41 src.telephone.text(1234),
42 "I received: 1234"
43 )
44
45 def test_passing_a_float(self):
46 self.assertEqual(
47 src.telephone.text(1.234),
48 "I received: 1.234"
49 )
50
51 def test_passing_a_tuple(self):
52 self.assertEqual(
53 src.telephone.text((1, 2, 3, "n")),
54 "I received: (1, 2, 3, 'n')"
55 )
56
57 def test_passing_a_list(self):
58 self.assertEqual(
59 src.telephone.text([1, 2, 3, "n"]),
60 "I received: [1, 2, 3, 'n']"
61 )
62
63 def test_passing_a_dictionary(self):
64 self.assertEqual(
65 src.telephone.text({
66 'key1': 'value1',
67 'keyN': [1, 2, 3, 'n'],
68 }),
69 "I received: {'key1': 'value1', 'keyN': [0, 1, 2, 'n']}"
70 )
71
72
73# Exceptions Encountered
74# AssertionError
75# NameError
76# AttributeError
77# TypeError
solution¶
the solution in in telephone.py from how to pass values
1def text(value):
2 return f'I received: {value}'