Skip to content

Instantly share code, notes, and snippets.

@al-the-x
Created May 8, 2011 03:34
Show Gist options
  • Save al-the-x/961073 to your computer and use it in GitHub Desktop.
Save al-the-x/961073 to your computer and use it in GitHub Desktop.
OrlandoDojo -- May 7, 2011
class Car ( object ):
is_locked = True
capacity = 4
occupancy = 0
engineIsOn = False
def unlock(self):
self.is_locked = False
return self
def lock(self):
self.is_locked = True
return self
def open_door(self):
return not self.is_locked
def acceptPassenger(self):
if self.occupancy >= self.capacity:
raise Exception('Car occupancy %d' % self.occupancy)
self.occupancy += 1
return self
def turnOn(self):
self.engineIsOn = True
return True
def isRunning(self):
return self.engineIsOn
from main import *
import unittest
class Test ( unittest.TestCase ):
def setUp(self):
self.car = Car()
def test_Car_can_be_unlocked (self):
self.assertEqual(self.car, self.car.unlock(), 'This car cannot be unlocked')
self.assertFalse(self.car.is_locked, 'The car was not unlocked')
def test_Car_can_be_locked(self):
self.assertEqual(self.car, self.car.lock(), 'This car cannot be locked')
self.assertTrue(self.car.is_locked, 'The car was not locked')
def test_Driver_cannot_open_locked_door(self):
self.assertTrue(self.car.lock(), 'Could not lock the car')
self.assertFalse(self.car.open_door(), 'The door opened sesame!')
def test_driver_can_open_unlocked_door(self):
self.assertTrue(self.car.unlock(), 'Could not unlock the car')
self.assertTrue(self.car.open_door(), 'Why is not the door open?')
def test_passengers_can_get_into_car_below_capacity(self):
self.car.open_door()
self.assertEquals(4, self.car.capacity)
for i in range(self.car.capacity - 1):
self.assertEquals(self.car, self.car.acceptPassenger())
def test_passengers_cannot_get_into_car_over_capacity(self):
self.test_passengers_can_get_into_car_below_capacity()
self.assertEquals(self.car, self.car.acceptPassenger())
self.assertRaises(Exception, self.car.acceptPassenger)
self.assertEquals(self.car.capacity, self.car.occupancy,
'even after we add too many people, the occupancy should not exceed capacity')
def test_Car_initially_is_off(self):
self.assertFalse(self.car.isRunning(), 'Car should be off but its on')
def test_Car_can_turn_on(self):
self.assertTrue(self.car.turnOn())
self.assertTrue(self.car.isRunning())
def test_Key_is_Present(self):
self.car.acceptPassenger()
self.assertTrue(self.car.occupancy)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment