Last active
May 12, 2020 18:36
-
-
Save chobeat/c9f04592cac999fb6e11749be05157b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Implementare una classe Spritz che modelli un bicchiere di Spritz. | |
Uno spritz può essere normale (200 ml) o grande (350 ml). | |
Implementare un metodo "sip()" che riduca la quantità nel bicchiere di spritz di 30ml e ritorni la quantità bevuta. Se il bicchiere è vuoto, solleva un'eccezione. | |
Implementare un metodo "boia_se_l_era_bon(size)" che ritorni una nuova istanza della classe Spritz. Anche in questo caso lo spritz può essere normale o grande. | |
Implementare un metodo "can_i_drive(kg)" che ritorni un valore booleano. La funzione ritorna True se la quantità di alcool ingerita ad inizio serata è inferiore ai limiti di legge. La tabella è riportata in versione semplificata qui di seguito: se kg<65 allora il limite è di 350ml, se 65<kg<75 il limite è di 450ml, se kg>75 il limite è di 550ml. (NdA: i conti li ho fatti a cazzo di cane, non usate questo esercizio come riferimento per decidere se mettervi alla guida dopo un aperitivo). | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmptyGlassException(Exception): | |
pass | |
class Spritz(): | |
def __init__(self, is_big=False): | |
self._is_big = is_big | |
self._counter = 0 | |
def is_big(self): | |
return self._is_big | |
def sip(self): | |
self._counter += 1 | |
if self._counter==8: | |
raise EmptyGlassException() | |
else: | |
return 30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aperikata.spritz import EmptyGlassException | |
from aperikata.spritz import Spritz | |
import pytest | |
@pytest.fixture | |
def spritz(): | |
return Spritz() | |
def test_init(spritz): | |
assert isinstance(spritz,Spritz) | |
def test_size_default(spritz): | |
assert not spritz.is_big() | |
def test_size_big(): | |
spritz = Spritz(is_big=True) | |
assert spritz.is_big() | |
def test_sip_fail(spritz): | |
for _ in range(7): | |
spritz.sip() | |
with pytest.raises(EmptyGlassException): | |
spritz.sip() | |
def test_sip(spritz): | |
quantity = spritz.sip() | |
assert quantity == 30 | |
def test_last_sip(spritz): | |
for _ in range(6): | |
spritz.sip() | |
quantity = spritz.sip() | |
assert quantity == 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment