Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions solutions/car.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
class Car:
def __init__(self, capacity: int, gas_per_km: int, gas=0):
self.__capacity = capacity
self.__gas_per_km = gas_per_km
self.__gas = gas

def __init__(self, capacity: int, gas_per_km: int, gas_to_fill=0):
self.capacity = capacity
self.gas_per_km = gas_per_km
self.gas = 0
self.fill(gas_to_fill)
self.milage = 0

def fill(self, liters: int) -> None:

if liters > self.__capacity - self.__gas:
extra_liters = liters - (self.__capacity - self.__gas)
self.__gas = self.__capacity
if liters > self.capacity - self.gas:
extra_liters = liters - (self.capacity - self.gas)
self.gas = self.capacity
print(f'Tank is full. You have {extra_liters} extra liters')
else:
self.gas += liters

self.__gas += liters

def ride(self, milage: int) -> None:
max_distance = int(self.__gas / self.__gas_per_km)
if milage > max_distance:
self.__gas = 0
def ride(self, distance: int) -> None:
max_distance = int(self.gas / self.gas_per_km)
if distance > max_distance:
self.gas = 0
self.milage += max_distance

self.__gas -= self.__gas_per_km * milage
self.milage += milage
print(f'Out of gas. {distance - max_distance}km left to go.')
else:
self.milage += distance
self.gas -= distance * self.gas_per_km
62 changes: 39 additions & 23 deletions tests/test_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,42 @@
from solutions.car import Car


@pytest.fixture
def create_car():
return Car(500, 25)


def test_car_creation(create_car):
car = create_car
assert isinstance(car, Car)


def test_car_fill(create_car):
car = create_car
assert car.fill(5) == 'Has 5 liters out of 500'
assert car.fill(500) == 'Tank is full. You have 5 extra liters'


def test_car_ride(create_car):
car = create_car
car.fill(500)
assert car.ride(10) == 'Trip finished'
assert car.milage == 10
assert car.ride(100) == 'Out of gas. 90km left to go.'
assert car.milage == 20
@pytest.mark.parametrize(
'car,gas_in_tank',
[
(Car(400, 25), 0),
(Car(600, 25, gas_to_fill=10), 10),
(Car(500, 25, gas_to_fill=600), 500)
]
)
def test_car_creation(car, gas_in_tank):
assert car.gas == gas_in_tank


@pytest.mark.parametrize(
'car,gas_to_fill,result',
[
(Car(400, 25), 0, 0),
(Car(600, 25, gas_to_fill=10), 10, 20),
(Car(500, 25, gas_to_fill=600), 500, 500),
(Car(200, 20, gas_to_fill=100), 150, 200)
]
)
def test_car_fill(car, gas_to_fill, result):
car.fill(gas_to_fill)
assert car.gas == result


@pytest.mark.parametrize(
'car,distance,result_milage,result_gas',
[
(Car(400, 25), 0, 0, 0),
(Car(600, 25, gas_to_fill=600), 10, 10, 350),
(Car(500, 1, gas_to_fill=600), 500, 500, 0),
(Car(200, 20, gas_to_fill=100), 10, 5, 0)
]
)
def test_car_ride(car, distance, result_milage, result_gas):
car.ride(distance)
assert car.milage == result_milage
assert car.gas == result_gas