truth table: projects

You have seen the Truth Table from Mathematics and I used the assertFalse and assertTrue methods from AssertionError, None and booleans to go through each operation.

Time to write programs that use the truth table


preview

These are the tests I have at the end of the chapters

  • Traffic Light Tests

      1import src.traffic_light
      2import unittest
      3
      4
      5RED, YELLOW, GREEN = 'RED', 'YELLOW', 'GREEN'
      6NO_WALK = 'NO WALK'
      7WALK = (RED, 'WALK')
      8YELLOW_NO_WALK = (YELLOW, NO_WALK)
      9GREEN_NO_WALK = (GREEN, NO_WALK)
     10
     11
     12class TestTrafficLight(unittest.TestCase):
     13
     14    def test_red_traffic_light_w_walk(self):
     15        self.assertEqual(
     16            src.traffic_light.show(
     17                current_light=RED,
     18                timer_done=True,
     19                walk_button=True,
     20            ),
     21            WALK
     22        )
     23
     24        self.assertEqual(
     25            src.traffic_light.show(
     26                current_light=RED,
     27                timer_done=True,
     28                walk_button=False,
     29            ),
     30            GREEN_NO_WALK
     31        )
     32
     33        self.assertEqual(
     34            src.traffic_light.show(
     35                current_light=RED,
     36                timer_done=False,
     37                walk_button=True,
     38            ),
     39            WALK
     40        )
     41
     42        self.assertEqual(
     43            src.traffic_light.show(
     44                current_light=RED,
     45                timer_done=False,
     46                walk_button=False,
     47            ),
     48            WALK
     49        )
     50
     51    def test_yellow_traffic_light_w_walk(self):
     52        self.assertEqual(
     53            src.traffic_light.show(
     54                current_light=YELLOW,
     55                timer_done=True,
     56                walk_button=True,
     57            ),
     58            WALK
     59        )
     60
     61        self.assertEqual(
     62            src.traffic_light.show(
     63                current_light=YELLOW,
     64                timer_done=True,
     65                walk_button=False,
     66            ),
     67            WALK
     68        )
     69
     70        self.assertEqual(
     71            src.traffic_light.show(
     72                current_light=YELLOW,
     73                timer_done=False,
     74                walk_button=True,
     75            ),
     76            YELLOW_NO_WALK
     77        )
     78
     79        self.assertEqual(
     80            src.traffic_light.show(
     81                current_light=YELLOW,
     82                timer_done=False,
     83                walk_button=False,
     84            ),
     85            YELLOW_NO_WALK
     86        )
     87
     88    def test_green_traffic_light_w_walk(self):
     89        self.assertEqual(
     90            src.traffic_light.show(
     91                current_light=GREEN,
     92                timer_done=True,
     93                walk_button=True,
     94            ),
     95            YELLOW_NO_WALK
     96        )
     97
     98        self.assertEqual(
     99            src.traffic_light.show(
    100                current_light=GREEN,
    101                timer_done=True,
    102                walk_button=False,
    103            ),
    104            YELLOW_NO_WALK
    105        )
    106
    107        self.assertEqual(
    108            src.traffic_light.show(
    109                current_light=GREEN,
    110                timer_done=False,
    111                walk_button=True,
    112            ),
    113            GREEN_NO_WALK
    114        )
    115
    116        self.assertEqual(
    117            src.traffic_light.show(
    118                current_light=GREEN,
    119                timer_done=False,
    120                walk_button=False,
    121            ),
    122            GREEN_NO_WALK
    123        )
    124
    125
    126# Exceptions seen
    127# AssertionError
    128# NameError
    129# AttributeError
    130# TypeError
    131# SyntaxError
    
  • Automated Teller Machine Tests

      1import src.atm
      2import unittest
      3
      4
      5DENIED = 'DENIED'
      6
      7
      8class TestATM(unittest.TestCase):
      9
     10    def test_right_pin_enough_money_w_card(self):
     11        self.assertEqual(
     12            src.atm.withdraw(
     13                right_pin=True,
     14                enough_money=True,
     15                above_daily_limit=True,
     16                card_expired=True,
     17            ),
     18            DENIED
     19        )
     20
     21        self.assertEqual(
     22            src.atm.withdraw(
     23                right_pin=True,
     24                enough_money=True,
     25                above_daily_limit=True,
     26                card_expired=False,
     27            ),
     28            DENIED
     29        )
     30
     31        self.assertEqual(
     32            src.atm.withdraw(
     33                right_pin=True,
     34                enough_money=True,
     35                above_daily_limit=False,
     36                card_expired=True,
     37            ),
     38            DENIED
     39        )
     40
     41        self.assertEqual(
     42            src.atm.withdraw(
     43                right_pin=True,
     44                enough_money=True,
     45                above_daily_limit=False,
     46                card_expired=False,
     47            ),
     48            'CASH'
     49        )
     50
     51    def test_right_pin_not_enough_money_w_card(self):
     52        self.assertEqual(
     53            src.atm.withdraw(
     54                right_pin=True,
     55                enough_money=False,
     56                above_daily_limit=True,
     57                card_expired=True,
     58            ),
     59            DENIED
     60        )
     61
     62        self.assertEqual(
     63            src.atm.withdraw(
     64                right_pin=True,
     65                enough_money=False,
     66                above_daily_limit=True,
     67                card_expired=False,
     68            ),
     69            DENIED
     70        )
     71
     72        self.assertEqual(
     73            src.atm.withdraw(
     74                right_pin=True,
     75                enough_money=False,
     76                above_daily_limit=False,
     77                card_expired=True,
     78            ),
     79            DENIED
     80        )
     81
     82        self.assertEqual(
     83            src.atm.withdraw(
     84                right_pin=True,
     85                enough_money=False,
     86                above_daily_limit=False,
     87                card_expired=True,
     88            ),
     89            DENIED
     90        )
     91
     92    def test_wrong_pin_enough_money_w_card(self):
     93        self.assertEqual(
     94            src.atm.withdraw(
     95                right_pin=False,
     96                enough_money=True,
     97                above_daily_limit=True,
     98                card_expired=True,
     99            ),
    100            DENIED
    101        )
    102
    103        self.assertEqual(
    104            src.atm.withdraw(
    105                right_pin=False,
    106                enough_money=True,
    107                above_daily_limit=True,
    108                card_expired=False,
    109            ),
    110            DENIED
    111        )
    112
    113        self.assertEqual(
    114            src.atm.withdraw(
    115                right_pin=False,
    116                enough_money=True,
    117                above_daily_limit=False,
    118                card_expired=True,
    119            ),
    120            DENIED
    121        )
    122
    123        self.assertEqual(
    124            src.atm.withdraw(
    125                right_pin=False,
    126                enough_money=True,
    127                above_daily_limit=False,
    128                card_expired=False,
    129            ),
    130            DENIED
    131        )
    132
    133    def test_wrong_pin_not_enough_money_w_card(self):
    134        self.assertEqual(
    135            src.atm.withdraw(
    136                right_pin=False,
    137                enough_money=False,
    138                above_daily_limit=True,
    139                card_expired=True,
    140            ),
    141            DENIED
    142        )
    143
    144        self.assertEqual(
    145            src.atm.withdraw(
    146                right_pin=False,
    147                enough_money=False,
    148                above_daily_limit=True,
    149                card_expired=False,
    150            ),
    151            DENIED
    152        )
    153
    154        self.assertEqual(
    155            src.atm.withdraw(
    156                right_pin=False,
    157                enough_money=False,
    158                above_daily_limit=False,
    159                card_expired=True,
    160            ),
    161            DENIED
    162        )
    163
    164        self.assertEqual(
    165            src.atm.withdraw(
    166                right_pin=False,
    167                enough_money=False,
    168                above_daily_limit=False,
    169                card_expired=False,
    170            ),
    171            DENIED
    172        )
    173
    174
    175# Exceptions seen
    176# AssertionError
    177# NameError
    178# AttributeError
    179# TypeError
    180# SyntaxError
    
  • Microwave Tests

      1import src.microwave
      2import unittest
      3
      4
      5OFF = 'OFF'
      6
      7
      8class TestMicrowave(unittest.TestCase):
      9
     10    def test_too_hot_open_door_timer_set(self):
     11        self.assertEqual(
     12            src.microwave.microwave(
     13                door_is_open=True,
     14                timer_is_set=True,
     15                start_is_pushed=True,
     16                too_hot=True,
     17            ),
     18            OFF
     19        )
     20
     21        self.assertEqual(
     22            src.microwave.microwave(
     23                door_is_open=True,
     24                timer_is_set=True,
     25                start_is_pushed=True,
     26                too_hot=False,
     27            ),
     28            OFF
     29        )
     30
     31        self.assertEqual(
     32            src.microwave.microwave(
     33                door_is_open=True,
     34                timer_is_set=True,
     35                start_is_pushed=False,
     36                too_hot=True,
     37            ),
     38            OFF
     39        )
     40
     41        self.assertEqual(
     42            src.microwave.microwave(
     43                door_is_open=True,
     44                timer_is_set=True,
     45                start_is_pushed=False,
     46                too_hot=False,
     47            ),
     48            OFF
     49        )
     50
     51    def test_too_hot_open_door_timer_not_set(self):
     52        self.assertEqual(
     53            src.microwave.microwave(
     54                door_is_open=True,
     55                timer_is_set=False,
     56                start_is_pushed=True,
     57                too_hot=True,
     58            ),
     59            OFF
     60        )
     61
     62        self.assertEqual(
     63            src.microwave.microwave(
     64                door_is_open=True,
     65                timer_is_set=False,
     66                start_is_pushed=True,
     67                too_hot=False,
     68            ),
     69            OFF
     70        )
     71
     72        self.assertEqual(
     73            src.microwave.microwave(
     74                door_is_open=True,
     75                timer_is_set=False,
     76                start_is_pushed=False,
     77                too_hot=True,
     78            ),
     79            OFF
     80        )
     81
     82        self.assertEqual(
     83            src.microwave.microwave(
     84                door_is_open=True,
     85                timer_is_set=False,
     86                start_is_pushed=False,
     87                too_hot=False,
     88            ),
     89            OFF
     90        )
     91
     92    def test_too_hot_closed_door_timer_set(self):
     93        self.assertEqual(
     94            src.microwave.microwave(
     95                door_is_open=False,
     96                timer_is_set=True,
     97                start_is_pushed=True,
     98                too_hot=True,
     99            ),
    100            OFF
    101        )
    102
    103        self.assertEqual(
    104            src.microwave.microwave(
    105                door_is_open=False,
    106                timer_is_set=True,
    107                start_is_pushed=True,
    108                too_hot=False,
    109            ),
    110            'HEATING'
    111        )
    112
    113        self.assertEqual(
    114            src.microwave.microwave(
    115                door_is_open=False,
    116                timer_is_set=True,
    117                start_is_pushed=False,
    118                too_hot=True,
    119            ),
    120            OFF
    121        )
    122
    123        self.assertEqual(
    124            src.microwave.microwave(
    125                door_is_open=False,
    126                timer_is_set=True,
    127                start_is_pushed=False,
    128                too_hot=False,
    129            ),
    130            OFF
    131        )
    132
    133    def test_too_hot_closed_door_timer_not_set(self):
    134        self.assertEqual(
    135            src.microwave.microwave(
    136                door_is_open=False,
    137                timer_is_set=False,
    138                start_is_pushed=True,
    139                too_hot=True,
    140            ),
    141            OFF
    142        )
    143
    144        self.assertEqual(
    145            src.microwave.microwave(
    146                door_is_open=False,
    147                timer_is_set=False,
    148                start_is_pushed=True,
    149                too_hot=False,
    150            ),
    151            OFF
    152        )
    153
    154        self.assertEqual(
    155            src.microwave.microwave(
    156                door_is_open=False,
    157                timer_is_set=False,
    158                start_is_pushed=False,
    159                too_hot=True,
    160            ),
    161            OFF
    162        )
    163
    164        self.assertEqual(
    165            src.microwave.microwave(
    166                door_is_open=False,
    167                timer_is_set=False,
    168                start_is_pushed=False,
    169                too_hot=False,
    170            ),
    171            OFF
    172        )
    173
    174
    175# Exceptions seen
    176# AssertionError
    177# NameError
    178# AttributeError
    179# TypeError
    180# SyntaxError
    

the projects


what is next?

Traffic Light