truth table: Nullary and Unary Operations¶
requirements¶
I open a terminal to run makePythonTdd.sh with
truth_tableas the name of the project./makePythonTdd.sh truth_tableon Windows without Windows Subsystem for Linux use makePythonTdd.ps1 instead of makePythonTdd.sh
./makePythonTdd.ps1 truth_table
it makes the folders and files that are needed, installs packages, runs the first test, and the terminal shows AssertionError
E AssertionError: True is not false tests/test_truth_table.py:7: AssertionError
I hold
ctrl(Windows/Linux) oroption or command(MacOS) on the keyboard and use the mouse to click ontests/test_truth_table.py:7to open it in the editorthen I change True to False to make the test pass
7 self.assertFalse(False)
Nullary Operations¶
There are 2 Nullary operations, they do not take input and always return the same value
test_logical_true¶
RED: make it fail¶
I change the text in test_truth_table.py
1import unittest
2import src.truth_table
3
4
5class TestNullaryOperations(unittest.TestCase):
6
7 def test_logical_true(self):
8 self.assertTrue(src.truth_table.logical_true())
9
10
11# Exceptions Encountered
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_true'
GREEN: make it pass¶
I add it to the list of Exceptions encountered in
test_truth_table.py11# Exceptions Encountered 12# AssertionError 13# AttributeError
I click on
truth_table.pyin thesrcfolder to open it in the editor, then I add a function1def logical_true(): 2 return None
the terminal shows AssertionError
AssertionError: None is not true
I change False to True in the return statement
1def logical_true(): 2 return True
the test passes
test_logical_false¶
RED: make it fail¶
I add another test to test_truth_table.py
7 def test_logical_true(self):
8 self.assertTrue(src.truth_table.logical_true())
9
10 def test_logical_false(self):
11 self.assertFalse(src.truth_table.logical_false())
12
13
14# Exceptions Encountered
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_false'. Did you mean: 'logical_true'?
GREEN: make it pass¶
I add a function definition to
truth_table.py1def logical_true(): 2 return True 3 4 5def logical_false(): 6 return True
the terminal shows AssertionError
AssertionError: True is not false
I change True to False in the return statement
5def logical_false(): 6 return False
the test passes
Unary Operations¶
There are 2 unary operations, they each take one input
test_logical_identity¶
RED: make it fail¶
I add a new TestCase and a test to test_truth_table.py
10 def test_logical_false(self):
11 self.assertFalse(src.truth_table.logical_false())
12
13
14class TestUnaryOperations(unittest.TestCase):
15
16 def test_logical_identity(self):
17 self.assertTrue(src.truth_table.logical_identity(True))
18
19
20# Exceptions Encountered
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_identity'
GREEN: make it pass¶
I add the function to
truth_table.py5def logical_false(): 6 return False 7 8 9def logical_identity(): 10 return False
TypeError: logical_identity() takes 0 positional arguments but 1 was given
I add the error to the list of Exceptions encountered in
test_truth_table.py20# Exceptions Encountered 21# AssertionError 22# AttributeError 23# TypeError
I add a name in parentheses for
logical_identityto take input intruth_table.py9def logical_identity(the_input): 10 return False
the terminal shows AssertionError
AssertionError: False is not true
I change the return statement
9def logical_identity(the_input): 10 return True
the test passes
REFACTOR: make it better¶
I add another line to
test_logical_identityintest_truth_table.py16 def test_logical_identity(self): 17 self.assertTrue(src.truth_table.logical_identity(True)) 18 self.assertFalse(src.truth_table.logical_identity(False))
the terminal shows AssertionError
AssertionError: True is not false
I change the return statement of
logical_identityintruth_table.py9def logical_identity(the_input): 10 return False
the terminal shows AssertionError
AssertionError: False is not true
there is a failure for the test that passed before. The expectation of the test is that when True is given, the result is True and when False is given, the result is False
I change the return statement of the function
9def logical_identity(the_input): 10 return the_input
the test passes.
logical_identityreturns its input as output.
test_logical_negation¶
RED: make it fail¶
I add a new test to test_truth_table.py
16 def test_logical_identity(self):
17 self.assertTrue(src.truth_table.logical_identity(True))
18 self.assertFalse(src.truth_table.logical_identity(False))
19
20 def test_logical_negation(self):
21 self.assertFalse(src.truth_table.logical_negation(True))
22
23
24# Exceptions Encountered
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_negation'
GREEN: make it pass¶
I add a definition for the function in truth_table.py
9def logical_identity(the_input):
10 return the_input
11
12
13def logical_negation(the_input):
14 return the_input
the terminal shows AssertionError
AssertionError: True is not false
I change the return statement
13def logical_negation(the_input):
14 return False
the test passes
REFACTOR: make it better¶
I add another line in
test_logical_negationintest_truth_table.py20 def test_logical_negation(self): 21 self.assertFalse(src.truth_table.logical_negation(True)) 22 self.assertTrue(src.truth_table.logical_negation(False))
the terminal shows AssertionError
AssertionError: False is not true
I change the return statement of the
logical_negationfunction intruth_table.py13def logical_negation(the_input): 14 return True
the terminal shows AssertionError
AssertionError: True is not false
the test fails for the line that passed before
I make the function return its input
13def logical_negation(the_input): 14 return the_input
the terminal shows AssertionError
AssertionError: True is not false
the expectation of the test is that when True is given, the result is False and when False is given, the result is True, I can make that happen with the “not” keyword. I add it to the return statement
13def logical_negation(the_input): 14 return not the_input
the test passes.
logical_negationreturns the opposite of its inputI change the name of the test
14class TestUnaryOperations(unittest.TestCase): 15 16 def test_logical_identity(self): 17 self.assertTrue(src.truth_table.logical_identity(True)) 18 self.assertFalse(src.truth_table.logical_identity(False)) 19 20 def test_logical_negation_aka_not(self): 21 self.assertFalse(src.truth_table.logical_negation(True)) 22 self.assertTrue(src.truth_table.logical_negation(False)) 23 24 25# Exceptions Encountered
review¶
I ran the following tests for Nullary
and
for Unary operations
Would you like to test binary operations?
truth table: tests and solutions