Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. class Car:
  2. def __init__(self, make="", model="", fuel_consumption=0, fuel_capacity=0):
  3. self.__make = make
  4. self.__model = model
  5. self.__fuel_consumption = fuel_capacity
  6. self.__fuel_capacity = fuel_capacity
  7. self.__fuel_amount = 0
  8.  
  9. @property
  10. def make(self):
  11. return self.__make
  12.  
  13. @make.setter
  14. def make(self, new_value):
  15. if not new_value:
  16. raise Exception("Make cannot be null or empty!")
  17. self.__make = new_value
  18.  
  19. @property
  20. def model(self):
  21. return self.__model
  22.  
  23. @model.setter
  24. def model(self, new_value):
  25. if not new_value:
  26. raise Exception("Model cannot be null or empty!")
  27. self.__model = new_value
  28.  
  29. @property
  30. def fuel_consumption(self):
  31. return self.__fuel_consumption
  32.  
  33. @fuel_consumption.setter
  34. def fuel_consumption(self, new_value):
  35. if new_value <= 0:
  36. raise Exception("Fuel consumption cannot be zero or negative!")
  37. self.__fuel_consumption = new_value
  38.  
  39. @property
  40. def fuel_capacity(self):
  41. return self.__fuel_capacity
  42.  
  43. @fuel_capacity.setter
  44. def fuel_capacity(self, new_value):
  45. if new_value <= 0:
  46. raise Exception("Fuel capacity cannot be zero or negative!")
  47. self.__fuel_capacity = new_value
  48.  
  49. @property
  50. def fuel_amount(self):
  51. return self.__fuel_amount
  52.  
  53. @fuel_amount.setter
  54. def fuel_amount(self, new_value):
  55. if new_value < 0:
  56. raise Exception("Fuel amount cannot be negative!")
  57. self.__fuel_amount = new_value
  58.  
  59. def refuel(self, fuel):
  60. if fuel <= 0:
  61. raise Exception("Fuel amount cannot be zero or negative!")
  62. self.__fuel_amount += fuel
  63. if self.__fuel_amount > self.__fuel_capacity:
  64. self.__fuel_amount = self.__fuel_capacity
  65.  
  66. def drive(self, distance):
  67. needed = (distance / 100) * self.__fuel_consumption
  68.  
  69. if needed > self.__fuel_amount:
  70. raise Exception("You don't have enough fuel to drive!")
  71.  
  72. self.__fuel_amount -= needed
  73.  
  74. import unittest
  75.  
  76.  
  77. class Tests(unittest.TestCase):
  78. def test_constr(self):
  79. car = Car('', "Audi", 69, 420)
  80. self.assertEqual(car.make, '')
  81. self.assertEqual(car.model, 'Audi')
  82. self.assertEqual(car.fuel_consumption, 69)
  83. self.assertEqual(car.fuel_capacity, 420)
  84.  
  85. def test_constr_should_break_on_make(self):
  86. car = Car()
  87. with self.assertRaises(Exception):
  88. car.make = None
  89.  
  90. def test_constr_should_break_on_model(self):
  91. car = Car()
  92. with self.assertRaises(Exception):
  93. car.model = None
  94.  
  95. def test_constr_should_break_on_cons_negative(self):
  96. car = Car()
  97. with self.assertRaises(Exception):
  98. car.fuel_consumption = -2
  99.  
  100. def test_constr_should_break_on_cap_negative(self):
  101. car = Car()
  102. with self.assertRaises(Exception):
  103. car.fuel_capacity = -2
  104.  
  105. def test_constr_should_break_on_cons_zero(self):
  106. car = Car()
  107. with self.assertRaises(Exception):
  108. car.fuel_consumption = 0
  109.  
  110. def test_constr_should_break_on_cap_zero(self):
  111. car = Car()
  112. with self.assertRaises(Exception):
  113. car.fuel_capacity = 0
  114.  
  115. def test_constr_should_break_on_amount_negative(self):
  116. car = Car()
  117. with self.assertRaises(Exception):
  118. car.fuel_amount = -2
  119.  
  120. def test_refuel_working(self):
  121. car = Car('', "Audi", 69, 420)
  122. car.refuel(2)
  123.  
  124. def test_refuel_fuel_zero(self):
  125. car = Car('', "Audi", 69, 420)
  126. with self.assertRaises(Exception):
  127. car.refuel(0)
  128.  
  129. def test_refuel_fuel_negative(self):
  130. car = Car('', "Audi", 69, 420)
  131. with self.assertRaises(Exception):
  132. car.refuel(-2)
  133.  
  134. def test_drive_working(self):
  135. car = Car('', "Audi", 69, 420)
  136. car.fuel_amount = 100
  137. car.drive(100)
  138. self.assertEqual(car.fuel_amount, 31)
  139.  
  140. def test_drive_error(self):
  141. car = Car('', "Audi", 69, 420)
  142. with self.assertRaises(Exception):
  143. car.drive(50)
  144.  
  145. if __name__ == '__main__':
  146. unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement