truth table: test binary operations with generators

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

truth_table/tests/test_binary.py
 1import src.truth_table
 2import unittest
 3
 4
 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, False),
13                    (
14                        src.truth_table.logical_conjunction,
15                        (first, second) == (True, True)
16                    ),
17                    (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, not first),
23                    (
24                        src.truth_table.logical_nand,
25                        (first, second) != (True, True)
26                    ),
27                    (src.truth_table.tautology, True),
28                    (
29                        src.truth_table.logical_disjunction,
30                        (first, second) != (False, False)
31                    ),
32                    (
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, first),
41                    (
42                        src.truth_table.converse_implication,
43                        (first, second) != (False, True)
44                    ),
45                    (src.truth_table.negate_second, not second),
46                    (
47                        src.truth_table.logical_nor,
48                        (first, second) == (False, False)
49                    ),
50                    (
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                    ):
64                        self.assertEqual(
65                            operation(first, second),
66                            expectation
67                        )
68
69
70# Exceptions seen
71# AttributeError
72# TypeError
73# AssertionError
74# Syntaxerror

open the project


test_binary_operations with generators

RED: make it fail


I change the the nested for loops in test_binary_operations to a generator expression

truth_table/tests/test_binary.py
 7    def test_binary_operations(self):
 8        booleans = (False, True)
 9        for first, second, operation, expectation in (
10            (first, second, operation, expectation)
11            for first in booleans
12            for second in booleans
13            for operation, expectation in (
14                (src.truth_table.contradiction, False),
15                (
16                    src.truth_table.logical_conjunction,
17                    (first, second) == (True, True)
18                ),
19                (src.truth_table.project_second, second),
20                (
21                    src.truth_table.converse_non_implication,
22                    (first, second) == (False, True)
23                ),
24                (src.truth_table.negate_first, not first),
truth_table/tests/test_binary.py
25                (
26                    src.truth_table.logical_nand,
27                    (first, second) != (True, True)
28                ),
29                (src.truth_table.tautology, True),
30                (
31                    src.truth_table.logical_disjunction,
32                    (first, second) != (False, False)
33                ),
34                (
35                    src.truth_table.exclusive_disjunction,
36                    first != second
37                ),
38                (
39                    src.truth_table.material_non_implication,
40                    (first, second) == (True, False)
41                ),
42                (src.truth_table.project_first, first),
truth_table/tests/test_binary.py
43                (
44                    src.truth_table.converse_implication,
45                    (first, second) != (False, True)
46                ),
47                (src.truth_table.negate_second, not second),
48                (
49                    src.truth_table.logical_nor,
50                    (first, second) == (False, False)
51                ),
52                (
53                    src.truth_table.logical_equality,
54                    first == second
55                ),
truth_table/tests/test_binary.py
56                (
57                    src.truth_table.material_implication,
58                    (first, second) != (True, False)
59                ),
60            )
61        ):
62            with self.subTest(
63                operation.__name__,
64                first=first,
65                second=second
66            ):
67                raise Exception
68                self.assertEqual(
69                    operation(first, second),
70                    expectation
71                )
72
73
74# Exceptions seen

the terminal is my friend, and shows Exception for sixty four sub tests.


GREEN: make it pass


I remove the Exception

truth_table/tests/test_binary.py
62            with self.subTest(
63                operation.__name__,
64                first=first,
65                second=second
66            ):
67                self.assertEqual(
68                    operation(first, second),
69                    expectation
70                )
71
72
73# Exceptions seen

the test is green again.


REFACTOR: make it better


  • I use itertools.product to make the statement simpler

    truth_table/tests/test_binary.py
     7    def test_binary_operations(self):
     8        # booleans = (False, True)
     9        # for first, second, operation, expectation in (
    10        #     (first, second, operation, expectation)
    11        #     for first in booleans
    12        #     for second in booleans
    13        for first, second in itertools.product(
    14            (False, True), repeat=2
    15        ):
    16            for operation, expectation in (
    17                (src.truth_table.contradiction, False),
    18                (
    19                    src.truth_table.logical_conjunction,
    20                    (first, second) == (True, True)
    21                ),
    
    truth_table/tests/test_binary.py
    59                (
    60                    src.truth_table.material_implication,
    61                    (first, second) != (True, False)
    62                ),
    63            ):
    64                with self.subTest(
    65                    operation.__name__,
    66                    first=first,
    67                    second=second
    68                ):
    69                    self.assertEqual(
    70                        operation(first, second),
    71                        expectation
    72                    )
    73
    74
    75# Exceptions seen
    

    the terminal is my friend, and shows NameError

    terminal
     NameError: name 'itertools' is not defined.
                Did you forget to import 'itertools'?
    
  • I add an import statement for the itertools library

    truth_table/tests/test_binary.py
    1import itertools
    2import src.truth_table
    3import unittest
    

    the test is green again.

  • I remove the commented lines

  • I add a git commit message

    terminal
    git commit -am 'test_binary_operations with generators'
    

use getattribute


close the project

  • I close tests/test_binary.py

  • I click in the terminal where the tests are running

  • I use q on the keyboard to leave the tests. The terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


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

Do you want to see all the CODE I typed in this chapter?


what is next?

Would you like to test lists?


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.