Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Car:
- def __init__(self, make="", model="", fuel_consumption=0, fuel_capacity=0):
- self.__make = make
- self.__model = model
- self.__fuel_consumption = fuel_capacity
- self.__fuel_capacity = fuel_capacity
- self.__fuel_amount = 0
- @property
- def make(self):
- return self.__make
- @make.setter
- def make(self, new_value):
- if not new_value:
- raise Exception("Make cannot be null or empty!")
- self.__make = new_value
- @property
- def model(self):
- return self.__model
- @model.setter
- def model(self, new_value):
- if not new_value:
- raise Exception("Model cannot be null or empty!")
- self.__model = new_value
- @property
- def fuel_consumption(self):
- return self.__fuel_consumption
- @fuel_consumption.setter
- def fuel_consumption(self, new_value):
- if new_value <= 0:
- raise Exception("Fuel consumption cannot be zero or negative!")
- self.__fuel_consumption = new_value
- @property
- def fuel_capacity(self):
- return self.__fuel_capacity
- @fuel_capacity.setter
- def fuel_capacity(self, new_value):
- if new_value <= 0:
- raise Exception("Fuel capacity cannot be zero or negative!")
- self.__fuel_capacity = new_value
- @property
- def fuel_amount(self):
- return self.__fuel_amount
- @fuel_amount.setter
- def fuel_amount(self, new_value):
- if new_value < 0:
- raise Exception("Fuel amount cannot be negative!")
- self.__fuel_amount = new_value
- def refuel(self, fuel):
- if fuel <= 0:
- raise Exception("Fuel amount cannot be zero or negative!")
- self.__fuel_amount += fuel
- if self.__fuel_amount > self.__fuel_capacity:
- self.__fuel_amount = self.__fuel_capacity
- def drive(self, distance):
- needed = (distance / 100) * self.__fuel_consumption
- if needed > self.__fuel_amount:
- raise Exception("You don't have enough fuel to drive!")
- self.__fuel_amount -= needed
- import unittest
- class Tests(unittest.TestCase):
- def test_constr(self):
- car = Car('', "Audi", 69, 420)
- self.assertEqual(car.make, '')
- self.assertEqual(car.model, 'Audi')
- self.assertEqual(car.fuel_consumption, 69)
- self.assertEqual(car.fuel_capacity, 420)
- def test_constr_should_break_on_make(self):
- car = Car()
- with self.assertRaises(Exception):
- car.make = None
- def test_constr_should_break_on_model(self):
- car = Car()
- with self.assertRaises(Exception):
- car.model = None
- def test_constr_should_break_on_cons_negative(self):
- car = Car()
- with self.assertRaises(Exception):
- car.fuel_consumption = -2
- def test_constr_should_break_on_cap_negative(self):
- car = Car()
- with self.assertRaises(Exception):
- car.fuel_capacity = -2
- def test_constr_should_break_on_cons_zero(self):
- car = Car()
- with self.assertRaises(Exception):
- car.fuel_consumption = 0
- def test_constr_should_break_on_cap_zero(self):
- car = Car()
- with self.assertRaises(Exception):
- car.fuel_capacity = 0
- def test_constr_should_break_on_amount_negative(self):
- car = Car()
- with self.assertRaises(Exception):
- car.fuel_amount = -2
- def test_refuel_working(self):
- car = Car('', "Audi", 69, 420)
- car.refuel(2)
- def test_refuel_fuel_zero(self):
- car = Car('', "Audi", 69, 420)
- with self.assertRaises(Exception):
- car.refuel(0)
- def test_refuel_fuel_negative(self):
- car = Car('', "Audi", 69, 420)
- with self.assertRaises(Exception):
- car.refuel(-2)
- def test_drive_working(self):
- car = Car('', "Audi", 69, 420)
- car.fuel_amount = 100
- car.drive(100)
- self.assertEqual(car.fuel_amount, 31)
- def test_drive_error(self):
- car = Car('', "Audi", 69, 420)
- with self.assertRaises(Exception):
- car.drive(50)
- if __name__ == '__main__':
- unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement