truth table: test binary operations with lists
I want to use for loops in the truth table project for binary operations
preview
This is the test I have at the end of the chapter
open the project
I open a terminal
I change directory to the truth_table folder in the
pumping_pythonfolderI open
test_binary.pyfrom thetestsfolderI use pytest-watcher to run the tests automatically
test_binary_operations
RED: make it fail
I add a test with a for loop for all the binary operations in tests/test_binary.py
11class TestBinaryOperations(unittest.TestCase):
12
13 def test_binary_operations(self):
14 for operation, tt_out, tf_out, ft_out, ff_out in (
15 (src.truth_table.contradiction, False, False, False, False),
16 (src.truth_table.logical_conjunction, True, False, False, False),
17 (src.truth_table.project_second, True, False, True, False),
18 (src.truth_table.converse_non_implication, False, False, True, False),
19 (src.truth_table.negate_first, False, False, True, True),
20 (src.truth_table.logical_nand, False, True, True, True),
21 (src.truth_table.tautology, True, True, True, True),
22 (src.truth_table.logical_disjunction, True, True, True, False),
23 (src.truth_table.exclusive_disjunction, False, True, True, False),
24 (src.truth_table.material_non_implication, False, True, False, False),
25 (src.truth_table.project_first, True, True, False, False),
26 (src.truth_table.converse_implication, True, True, False, True),
27 (src.truth_table.negate_second, False, True, False, True),
28 (src.truth_table.logical_nor, False, False, False, True),
29 (src.truth_table.logical_equality, True, False, False, True),
30 (src.truth_table.material_implication, True, False, True, True),
31 ):
32 with self.subTest(operation=operation.__name__):
33 self.assertEqual(operation(*FF), 'BOOM!!!')
34 self.assertEqual(operation(*FT), 'BAP!!!')
35 self.assertEqual(operation(*TF), 'POW!!!')
36 self.assertEqual(operation(*TT), 'TING!!!')
37
38 def test_contradiction(self):
the terminal is my friend, and shows AssertionError
SUBFAILED(operation='contradiction') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='logical_conjunction') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='project_second') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='converse_non_implication') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='negate_first') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='logical_nand') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='tautology') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='logical_disjunction') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='exclusive_disjunction') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='material_non_implication') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='project_first') ...AssertionError: False != 'BOOM!!!'
SUBFAILED(operation='converse_implication') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='negate_second') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='logical_nor') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='logical_equality') ...AssertionError: True != 'BOOM!!!'
SUBFAILED(operation='material_implication') ...AssertionError: True != 'BOOM!!!'
GREEN: make it pass
I change the expectation of
self.assertEqual(operation(*FF), 'BOOM!!!')32 with self.subTest(operation=operation.__name__): 33 self.assertEqual(operation(*FF), ff_out) 34 self.assertEqual(operation(*FT), 'BAP!!!')the terminal is my friend, and shows AssertionError
SUBFAILED(operation='contradiction') ...AssertionError: False != 'BAP!!!' SUBFAILED(operation='logical_conjunction') ...AssertionError: False != 'BAP!!!' SUBFAILED(operation='project_second') ...AssertionError: True != 'BAP!!!' SUBFAILED(operation='converse_non_implication') ...AssertionError: True != 'BAP!!!' SUBFAILED(operation='negate_first') ...AssertionError: True != 'BAP!!!' SUBFAILED(operation='logical_nand') ...AssertionError: True != 'BAP!!!' SUBFAILED(operation='tautology') ...AssertionError: True != 'BAP!!!' SUBFAILED(operation='logical_disjunction') ...AssertionError: True != 'BAP!!!' SUBFAILED(operation='exclusive_disjunction') ...AssertionError: True != 'BAP!!!' SUBFAILED(operation='material_non_implication') ...AssertionError: False != 'BAP!!!' SUBFAILED(operation='project_first') ...AssertionError: False != 'BAP!!!' SUBFAILED(operation='converse_implication') ...AssertionError: False != 'BAP!!!' SUBFAILED(operation='negate_second') ...AssertionError: False != 'BAP!!!' SUBFAILED(operation='logical_nor') ...AssertionError: False != 'BAP!!!' SUBFAILED(operation='logical_equality') ...AssertionError: False != 'BAP!!!' SUBFAILED(operation='material_implication') ...AssertionError: True != 'BAP!!!'I change the expectation of
self.assertEqual(operation(*FT), 'BAP!!!')32 with self.subTest(operation=operation.__name__): 33 self.assertEqual(operation(*FF), ff_out) 34 self.assertEqual(operation(*FT), ft_out) 35 self.assertEqual(operation(*TF), 'POW!!!')the terminal is my friend, and shows AssertionError
SUBFAILED(operation='contradiction') ...AssertionError: False != 'POW!!!' SUBFAILED(operation='logical_conjunction') ...AssertionError: False != 'POW!!!' SUBFAILED(operation='project_second') ...AssertionError: False != 'POW!!!' SUBFAILED(operation='converse_non_implication') ...AssertionError: False != 'POW!!!' SUBFAILED(operation='negate_first') ...AssertionError: False != 'POW!!!' SUBFAILED(operation='logical_nand') ...AssertionError: True != 'POW!!!' SUBFAILED(operation='tautology') ...AssertionError: True != 'POW!!!' SUBFAILED(operation='logical_disjunction') ...AssertionError: True != 'POW!!!' SUBFAILED(operation='exclusive_disjunction') ...AssertionError: True != 'POW!!!' SUBFAILED(operation='material_non_implication') ...AssertionError: True != 'POW!!!' SUBFAILED(operation='project_first') ...AssertionError: True != 'POW!!!' SUBFAILED(operation='converse_implication') ...AssertionError: True != 'POW!!!' SUBFAILED(operation='negate_second') ...AssertionError: True != 'POW!!!' SUBFAILED(operation='logical_nor') ...AssertionError: False != 'POW!!!' SUBFAILED(operation='logical_equality') ...AssertionError: False != 'POW!!!' SUBFAILED(operation='material_implication') ...AssertionError: False != 'POW!!!'I change the expectation of
self.assertEqual(operation(*TF), 'POW!!!')32 with self.subTest(operation=operation.__name__): 33 self.assertEqual(operation(*FF), ff_out) 34 self.assertEqual(operation(*FT), ft_out) 35 self.assertEqual(operation(*TF), tf_out) 36 self.assertEqual(operation(*TT), 'TING!!!')the terminal is my friend, and shows AssertionError
SUBFAILED(operation='contradiction') ...AssertionError: False != 'TING!!!' SUBFAILED(operation='logical_conjunction') ...AssertionError: True != 'TING!!!' SUBFAILED(operation='project_second') ...AssertionError: True != 'TING!!!' SUBFAILED(operation='converse_non_implication') ...AssertionError: False != 'TING!!!' SUBFAILED(operation='negate_first') ...AssertionError: False != 'TING!!!' SUBFAILED(operation='logical_nand') ...AssertionError: False != 'TING!!!' SUBFAILED(operation='tautology') ...AssertionError: True != 'TING!!!' SUBFAILED(operation='logical_disjunction') ...AssertionError: True != 'TING!!!' SUBFAILED(operation='exclusive_disjunction') ...AssertionError: False != 'TING!!!' SUBFAILED(operation='material_non_implication') ...AssertionError: False != 'TING!!!' SUBFAILED(operation='project_first') ...AssertionError: True != 'TING!!!' SUBFAILED(operation='converse_implication') ...AssertionError: True != 'TING!!!' SUBFAILED(operation='negate_second') ...AssertionError: False != 'TING!!!' SUBFAILED(operation='logical_nor') ...AssertionError: False != 'TING!!!' SUBFAILED(operation='logical_equality') ...AssertionError: True != 'TING!!!' SUBFAILED(operation='material_implication') ...AssertionError: True != 'TING!!!'I change the expectation of
self.assertEqual(operation(*TT), 'TING!!!')32 with self.subTest(operation=operation.__name__): 33 self.assertEqual(operation(*FF), ff_out) 34 self.assertEqual(operation(*FT), ft_out) 35 self.assertEqual(operation(*TF), tf_out) 36 self.assertEqual(operation(*TT), tt_out) 37 38 def test_contradiction(self):the test passes.
REFACTOR: make it better
I remove the other tests because they are now covered by test_binary_operations
32 with self.subTest(operation=operation.__name__): 33 self.assertEqual(operation(*FF), ff_out) 34 self.assertEqual(operation(*FT), ft_out) 35 self.assertEqual(operation(*TF), tf_out) 36 self.assertEqual(operation(*TT), tt_out) 37 38 39# Exceptions seen 40# AttributeError 41# TypeError 42# AssertionError 43# SyntaxErrorI change the assertions in test_binary_operations with another for loop
13 def test_binary_operations(self): 14 for operation, tt_out, tf_out, ft_out, ff_out in ( 15 (src.truth_table.contradiction, False, False, False, False), 16 (src.truth_table.logical_conjunction, True, False, False, False), 17 (src.truth_table.project_second, True, False, True, False), 18 (src.truth_table.converse_non_implication, False, False, True, False), 19 (src.truth_table.negate_first, False, False, True, True), 20 (src.truth_table.logical_nand, False, True, True, True), 21 (src.truth_table.tautology, True, True, True, True), 22 (src.truth_table.logical_disjunction, True, True, True, False), 23 (src.truth_table.exclusive_disjunction, False, True, True, False), 24 (src.truth_table.material_non_implication, False, True, False, False), 25 (src.truth_table.project_first, True, True, False, False), 26 (src.truth_table.converse_implication, True, True, False, True), 27 (src.truth_table.negate_second, False, True, False, True), 28 (src.truth_table.logical_nor, False, False, False, True), 29 (src.truth_table.logical_equality, True, False, False, True), 30 (src.truth_table.material_implication, True, False, True, True), 31 ):32 for inputs, result in ( 33 (FF, ff_out), 34 (FT, ft_out), 35 (TF, tf_out), 36 (TT, tt_out) 37 ): 38 with self.subTest( 39 operation=operation.__name__, 40 inputs=inputs, result=result 41 ): 42 self.assertEqual(operation(*inputs), 'BOOM!!!') 43 44 45# Exceptions seenthe terminal shows AssertionError for sixty four sub tests.
I change the expectation of
self.assertEqual(operation(*inputs), 'BOOM!!!')38 with self.subTest( 39 operation=operation.__name__, 40 inputs=inputs, result=result 41 ): 42 self.assertEqual(operation(*inputs), result) 43 44 45# Exceptions seenthe test is green again.
I no longer need the TT, TF, FT and FF global variables because they are only used in one place. I write out their values
32 for inputs, result in ( 33 ((False, False), ff_out), 34 ((False, True), ft_out), 35 ((True, False), tf_out), 36 ((True, True), tt_out) 37 ): 38 with self.subTest( 39 operation=operation.__name__, 40 inputs=inputs, result=result 41 ): 42 self.assertEqual(operation(*inputs), result) 43 44 45# Exceptions seenI remove the TT, TF, FT and FF global variables because they are no longer used
I add a git commit message
refactor test_binary_operations
RED: make it fail
I rewrite the test a different way without listing four booleans for each operation
5class TestBinaryOperations(unittest.TestCase):
6
7 def test_binary_operations(self):
8 booleans = (False, True)
9 for first in booleans:
10 for second in booleans:
11 for operation, expectation in (
12 (src.truth_table.contradiction, 'BOOM!!!'),
13 (src.truth_table.logical_conjunction, 'BAP!!!'),
14 (src.truth_table.project_second, 'POW!!!'),
15 (src.truth_table.converse_non_implication, 'BOOM!!!'),
16 (src.truth_table.negate_first, 'BAP!!!'),
17 (src.truth_table.logical_nand, 'POW!!!'),
18 (src.truth_table.tautology, 'BOOM!!!'),
19 (src.truth_table.logical_disjunction, 'BAP!!!'),
20 (src.truth_table.exclusive_disjunction, 'POW!!!'),
21 (src.truth_table.material_non_implication, 'BOOM!!!'),
22 (src.truth_table.project_first, 'BAP!!!'),
23 (src.truth_table.converse_implication, 'POW!!!'),
24 (src.truth_table.negate_second, 'BOOM!!!'),
25 (src.truth_table.logical_nor, 'BOOM!!!'),
26 (src.truth_table.logical_equality, 'BAP!!!'),
27 (src.truth_table.material_implication, 'BOOM!!!'),
28 ):
29 with self.subTest(
30 operation.__name__,
31 first=first,
32 second=second
33 ):
34 self.assertEqual(
35 operation(first, second),
36 expectation
37 )
38
39
40# Exceptions seen
the terminal shows AssertionError for sixty four sub tests. Can you make the tests pass without looking at how I solve it below? You can come back to compare solutions when you are done or if you get stuck.
GREEN: make it pass
I change the value of
expectationfor contradiction11 for operation, expectation in ( 12 (src.truth_table.contradiction, False), 13 (src.truth_table.logical_conjunction, 'BAP!!!'),the terminal shows AssertionError for sixty sub tests.
I change the value of
expectationfor logical_conjunction11 for operation, expectation in ( 12 (src.truth_table.contradiction, False), 13 ( 14 src.truth_table.logical_conjunction, 15 (first, second) == (True, True) 16 ), 17 (src.truth_table.project_second, 'POW!!!'),the terminal shows AssertionError for fifty six sub tests.
I change the value of
expectationfor project_second13 ( 14 src.truth_table.logical_conjunction, 15 (first, second) == (True, True) 16 ), 17 (src.truth_table.project_second, second), 18 (src.truth_table.converse_non_implication, 'BOOM!!!'),the terminal shows AssertionError for fifty two sub tests.
I change the value of
expectationfor converse_non_implication17 (src.truth_table.project_second, second), 18 ( 19 src.truth_table.converse_non_implication, 20 (first, second) == (False, True) 21 ), 22 (src.truth_table.negate_first, 'BAP!!!'),the terminal shows AssertionError for forty eight sub tests.
I change the value of
expectationfor negate_first18 ( 19 src.truth_table.converse_non_implication, 20 (first, second) == (False, True) 21 ), 22 (src.truth_table.negate_first, not first), 23 (src.truth_table.logical_nand, 'POW!!!'),the terminal shows AssertionError for forty four sub tests.
I change the value of
expectationfor logical_nand22 (src.truth_table.negate_first, not first), 23 ( 24 src.truth_table.logical_nand, 25 (first, second) != (True, True) 26 ), 27 (src.truth_table.tautology, 'BOOM!!!'),the terminal shows AssertionError for forty sub tests.
I change the value of
expectationfor tautology23 ( 24 src.truth_table.logical_nand, 25 (first, second) != (True, True) 26 ), 27 (src.truth_table.tautology, True), 28 (src.truth_table.logical_disjunction, 'BAP!!!'),the terminal shows AssertionError for thirty six sub tests.
I change the value of
expectationfor logical_disjunction27 (src.truth_table.tautology, True), 28 ( 29 src.truth_table.logical_disjunction, 30 (first, second) != (False, False) 31 ), 32 (src.truth_table.exclusive_disjunction, 'POW!!!'),the terminal shows AssertionError for thirty two sub tests.
I change the value of
expectationfor exclusive_disjunction28 ( 29 src.truth_table.logical_disjunction, 30 (first, second) != (False, False) 31 ), 32 ( 33 src.truth_table.exclusive_disjunction, 34 first != second 35 ), 36 (src.truth_table.material_non_implication, 'BOOM!!!'),the terminal shows AssertionError for twenty eight sub tests.
I change the value of
expectationfor material_non_implication32 ( 33 src.truth_table.exclusive_disjunction, 34 first != second 35 ), 36 ( 37 src.truth_table.material_non_implication, 38 (first, second) == (True, False) 39 ), 40 (src.truth_table.project_first, 'BAP!!!'),the terminal shows AssertionError for twenty four sub tests.
I change the value of
expectationfor project_first36 ( 37 src.truth_table.material_non_implication, 38 (first, second) == (True, False) 39 ), 40 (src.truth_table.project_first, first), 41 (src.truth_table.converse_implication, 'POW!!!'),the terminal shows AssertionError for twenty sub tests.
I change the value of
expectationfor converse_implication40 (src.truth_table.project_first, first), 41 ( 42 src.truth_table.converse_implication, 43 (first, second) != (False, True) 44 ), 45 (src.truth_table.negate_second, 'BOOM!!!'),the terminal shows AssertionError for sixteen sub tests.
I change the value of
expectationfor negate_second41 ( 42 src.truth_table.converse_implication, 43 (first, second) != (False, True) 44 ), 45 (src.truth_table.negate_second, not second), 46 (src.truth_table.logical_nor, 'BOOM!!!'),the terminal shows AssertionError for twelve sub tests.
I change the value of
expectationfor logical_nor45 (src.truth_table.negate_second, not second), 46 ( 47 src.truth_table.logical_nor, 48 (first, second) == (False, False) 49 ), 50 (src.truth_table.logical_equality, 'BAP!!!'),the terminal shows AssertionError for eight sub tests.
I change the value of
expectationfor logical_equality46 ( 47 src.truth_table.logical_nor, 48 (first, second) == (False, False) 49 ), 50 ( 51 src.truth_table.logical_equality, 52 first == second 53 ), 54 (src.truth_table.material_implication, 'BOOM!!!'),the terminal shows AssertionError for four sub tests.
I change the value of
expectationfor material_implication50 ( 51 src.truth_table.logical_equality, 52 first == second 53 ), 54 ( 55 src.truth_table.material_implication, 56 (first, second) != (True, False) 57 ), 58 ): 59 with self.subTest( 60 operation.__name__, 61 first=first, 62 second=second 63 ):the test passes.
I add a git commit message
close the project
review
I used for loops to turn all the tests into one which is simpler and at the same time more complex.
code from the chapter
what is next?
rate pumping python
If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.