truth table: test_truth_table_tests


requirements

how to make a python test driven development environment with truth_table as the name of the project


I want to write a program that makes the tests in test_truth_table.py pass without looking at them

red: make it fail

  • I close test_truth_table.py

  • then delete all the test in truth_table.py, which gives me a list of AttributeErrors I start with the last one

    AttributeError: module 'src.truth_table' has no attribute 'tautology'
    

green: make it pass

  • I add the name

    tautology
    

    the terminal shows NameError

    NameError: name 'tautology' is not defined
    

    I point it to None

    tautology = None
    

    which gives me TypeError

    TypeError: 'NoneType' object is not callable
    

    I make it a function

    def tautology():
        return None
    

    the terminal shows TypeError

    TypeError: tautology() takes 0 positional arguments but 2 were given
    

    I add input arguments in the function definition

    def tautology(x, y):
        return None
    

    the terminal shows AssertionError

    AssertionError: None is not true
    

    I change None to True in the return statement

    def tautology(x, y):
        return True
    
  • I have another AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'project_second'
    

    I add a function

    def project_second(x, y):
        return True
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    I change the return statement

    def project_second(x, y):
        return False
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    this test expects False sometimes and True other times. I return the inputs instead

    def project_second(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I change the return statement to return the second input because it is the same as the expectation

    def project_second(x, y):
        return y
    
  • the terminal shows AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'project_first'
    

    I add the function

    def project_first(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    the first input matches the expectation, I remove the second input from the return statement

    def project_first(x, y):
        return x
    

    the name is a clue

  • there is another AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'negate_second'
    

    I add the function

    def negate_second(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I do not know how the input relates to the output so I add an if statement

    def negate_second(x, y):
        if (x, y) == (True, True): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add another if statement

    def negate_second(x, y):
        if (x, y) == (True, True): return False
        if (x, y) == (False, True): return False
        return x, y
    
  • I get AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'negate_first'
    

    I add a definition for it

    def negate_first(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, True)
    

    I add an if statement

    def negate_first(x, y):
        if (x, y) == (True, True): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I add an if statement for it

    def negate_first(x, y):
        if (x, y) == (True, True): return False
        if (x, y) == (True, False): return False
        return x, y
    
  • the terminal shows AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'material_non_implication'
    

    I add the function

    def material_non_implication(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I add an if statement

    def material_non_implication(x, y):
        if (x, y) == (True, True): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add another if statement

    def material_non_implication(x, y):
        if (x, y) == (True, True): return False
        if (x, y) == (False, True): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    

    another if statement

    def material_non_implication(x, y):
        if (x, y) == (True, True): return False
        if (x, y) == (False, True): return False
        if (x, y) == (False, False): return False
        return x, y
    
  • the terminal shows AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'material_implication'
    

    I add the function

    def material_implication(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I add an if statement

    def material_implication(x, y):
        if (x, y) == (True, False): return False
        return x, y
    
  • I get AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'logical_nor'
    

    I add a definition for it

    def logical_nor(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I add an if statement

    def logical_nor(x, y):
        if (x, y) == (True, True): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    another if statement

    def logical_nor(x, y):
        if (x, y) == (True, True): return False
        if (x, y) == (True, False): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add another if statement

    def logical_nor(x, y):
        if (x, y) == (True, True): return False
        if (x, y) == (True, False): return False
        if (x, y) == (False, True): return False
        return x, y
    
  • I get AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'logical_nand'. Did you mean: 'logical_nor'?
    

    I add the function

    def logical_nand(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I add an if statement

    def logical_nand(x, y):
        if (x, y) == (True, True): return False
        return x, y
    
  • on to the next AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'logical_equality'
    

    I add the function

    def logical_equality(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I add an if statement

    def logical_equality(x, y):
        if (x, y) == (True, False): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add another if statement

    def logical_equality(x, y):
        if (x, y) == (True, False): return False
        if (x, y) == (False, True): return False
        return x, y
    
  • another AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'logical_disjunction'
    

    I add the function

    def logical_disjunction(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    

    I add an if statement

    def logical_disjunction(x, y):
        if (x, y) == (False, False): return False
        return x, y
    
  • the next AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'logical_conjunction'. Did you mean: 'logical_disjunction'?
    

    I add it

    def logical_conjunction(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I add an if statement

    def logical_conjunction(x, y):
        if (x, y) == (True, False): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add another if statement

    def logical_conjunction(x, y):
        if (x, y) == (True, False): return False
        if (x, y) == (False, True): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    

    I add an if statement

    def logical_conjunction(x, y):
        if (x, y) == (True, False): return False
        if (x, y) == (False, True): return False
        if (x, y) == (False, False): return False
        return x, y
    
  • I get another AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'exclusive_disjunction
    

    I add a function for it

    def exclusive_disjunction(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I add an if statement

    def exclusive_disjunction(x, y):
        if (x, y) == (True, True): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    

    I add another if statement

    def exclusive_disjunction(x, y):
        if (x, y) == (True, True): return False
        if (x, y) == (False, False): return False
        return x, y
    
  • next AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'converse_non_implication'. Did you mean: 'material_non_implication'?
    

    I add the function

    def converse_non_implication(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I add an if statement

    def converse_non_implication(x, y):
        if (x, y) == (True, True): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I add another if statement

    def converse_non_implication(x, y):
        if (x, y) == (True, True): return False
        if (x, y) == (True, False): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    

    I add an if statement

    def converse_non_implication(x, y):
        if (x, y) == (True, True): return False
        if (x, y) == (True, False): return False
        if (x, y) == (False, False): return False
        return x, y
    
  • next AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'converse_implication'. Did you mean: 'converse_non_implication'?
    

    I add the function

    def converse_implication(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add an if statement

    def converse_implication(x, y):
        if (x, y) == (False, True): return False
        return x, y
    
  • I get AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'contradiction'
    

    I add the function

    def contradiction(x, y):
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I add an if statement

    def contradiction(x, y):
        if (x, y) == (True, True): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I add another if statement

    def contradiction(x, y):
        if (x, y) == (True, True): return False
        if (x, y) == (True, False): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add another if statement

    def contradiction(x, y):
        if (x, y) == (True, True): return False
        if (x, y) == (True, False): return False
        if (x, y) == (False, True): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    

    I add an if statement

    def contradiction(x, y):
        if (x, y) == (True, True): return False
        if (x, y) == (True, False): return False
        if (x, y) == (False, True): return False
        if (x, y) == (False, False): return False
        return x, y
    
  • the terminal shows AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'logical_negation'. Did you mean: 'logical_conjunction'?
    

    I add the function

    def logical_negation(x, y):
        return x, y
    

    the terminal shows TypeError

    TypeError: logical_negation() missing 1 required positional argument: 'x'
    

    I change the signature

    def logical_negation(x):
        return x
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    ah, it expects the opposite. I add not to the return statement

    def logical_negation(x):
        return not x
    
  • the terminal shows AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'logical_identity'. Did you mean: 'logical_equality'?
    

    I add a function

    def logical_identity(x):
        return x
    
  • the terminal shows AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'logical_true'. Did you mean: 'logical_nand'?
    

    I add the function

    def logical_true(x):
        return x
    

    the terminal shows TypeError

    TypeError: logical_true() missing 1 required positional argument: 'x'
    

    I remove the input parameter

    def logical_true():
        return None
    

    the terminal shows AssertionError

    AssertionError: None is not true
    

    I change None to True in the return statement

    def logical_true():
        return True
    
  • another AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'logical_false'. Did you mean: 'logical_nand'?
    

    I add the function

    def logical_false():
        return False
    

    the terminal shows green!

refactor: make it better

Since all the tests are passing, I can refactor the functions to make them simpler

  • contradiction returns False in every case, I can use a simple return statement

    def contradiction(x, y):
        return False
        if (x, y) == (True, True): return False
        if (x, y) == (True, False): return False
        if (x, y) == (False, True): return False
        if (x, y) == (False, False): return False
        return x, y
    

    the test is still green. I remove the other statements

    def contradiction(x, y):
        return False
    
  • I use a one line return statement for converse_implication to return the opposite of the if statement

    def converse_implication(x, y):
        return (x, y) != (False, True)
        if (x, y) == (False, True): return False
        return x, y
    

    still green. I remove the other statements

    def converse_implication(x, y):
        return (x, y) != (False, True)
    
  • converse_non_implication has only one case that returns True, I use that

    def converse_non_implication(x, y):
        return (x, y) == (False, True)
        if (x, y) == (True, True): return False
        if (x, y) == (True, False): return False
        if (x, y) == (False, False): return False
        return x, y
    

    the terminal still shows green and I remove the other statements

    def converse_non_implication(x, y):
        return (x, y) == (False, True)
    
  • exclusive_disjunction has two if statements I try to combine them

    def exclusive_disjunction(x, y):
        return (x, y) != (True, True) or (x, y) != (False, False)
        if (x, y) == (True, True): return False
        if (x, y) == (False, False): return False
        return x, y
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    Oops! It looks like I cannot do that since the statements are the opposite of the if statements, I change or to and

    def exclusive_disjunction(x, y):
        return (x, y) != (True, True) and (x, y) != (False, False)
        if (x, y) == (True, True): return False
        if (x, y) == (False, False): return False
        return x, y
    

    the test is green again, I remove the other statements

    def exclusive_disjunction(x, y):
        return (x, y) != (True, True) and (x, y) != (False, False)
    
  • logical_conjunction only has one case that returns True

    def logical_conjunction(x, y):
        return (x, y) == (True, True)
        if (x, y) == (True, False): return False
        if (x, y) == (False, True): return False
        if (x, y) == (False, False): return False
        return x, y
    

    still green, I remove the other statements

    def logical_conjunction(x, y):
        return (x, y) == (True, True)
    
  • logical_disjunction only has one case that returns False

    def logical_disjunction(x, y):
        return (x, y) != (False, False)
    
  • logical_equality has two if statements, I put their opposites together

    def logical_equality(x, y):
        return (x, y) != (True, False) and (x, y) != (False, True)
    

    the terminal still shows green

  • I use the opposite of condition in the if statement for logical_nand

    def logical_nand(x, y):
        return (x, y) != (True, True)
    

    still green

  • logical_nor only has one case that returns True

    def logical_nor(x, y):
        return (x, y) == (False, False)
    
  • material_non_implication and material_implication

    def material_non_implication(x, y):
        return (x, y) == (True, False)
    
    
    def material_implication(x, y):
        return (x, y) != (True, False)
    
  • x is True in both cases of negate_first that return False

    def negate_first(x, y):
        return x != True
    
  • y is True in both cases of negate_second that return False

    def negate_second(x, y):
        return y != True
    

    I change the return statement

    def negate_second(x, y):
        return not y == True
        return y != True
    

    since not y is equal to True I can remove the duplication

    def negate_second(x, y):
        return not y
    

    the test is still green

  • I do the same thing with negate_first

    def negate_first(x, y):
        return not x
    

    still green

  • I use this with material_non_implication

    def material_non_implication(x, y):
        return x and not y
    

    the terminal shows all tests are still passing

  • I try it with material_implication

    def material_implication(x, y):
        return not x and y
        return (x, y) != (True, False)
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    I made a mistake, != is not and, I change it to or

    def material_implication(x, y):
        return not x or y
    

    the test is green again

  • I use this with logical_nor

    def logical_nor(x, y):
        return not x and not y
    

    which factors to

    def logical_nor(x, y):
        return not (x or y)
    
  • logical_nand

    def logical_nand(x, y):
        return not x or not y
    

    still green and it factors to

    def logical_nand(x, y):
        return not (x and y)
    
  • I do it with logical_equality

    def logical_equality(x, y):
        return (not x or y) and (x or not y)
        return (x, y) != (True, False) and (x, y) != (False, True)
    

    the tests are all still passing, I remove the other statement

  • logical_disjunction and logical_conjunction

    def logical_disjunction(x, y):
        return x or y
    
    
    def logical_conjunction(x, y):
        return x and y
    
  • exclusive_disjunction has 2 conditions

    def exclusive_disjunction(x, y):
        return (not x or not y) and (x or y)
        return (x, y) != (True, True) and (x, y) != (False, False)
    

    still green, I can factor out not from the first part of the statement

    def exclusive_disjunction(x, y):
        return not (x and y) and (x or y)
        return (not x or not y) and (x or y)
    

    the test is still green. I remove the other statement

  • converse_non_implication and converse_implication

    def converse_non_implication(x, y):
        return not x and y
    
    
    def converse_implication(x, y):
        return x or not y
    

all the tests are still passing

review

I ran tests for the operations of the Truth Table involving booleans which can either be True or False

Would you like to test lists?


truth table: tests and solutions