Skip to content

Instantly share code, notes, and snippets.

@Riggs333
Created June 20, 2022 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Riggs333/2b2b13482422598fbed2b21d15b8009e to your computer and use it in GitHub Desktop.
Save Riggs333/2b2b13482422598fbed2b21d15b8009e to your computer and use it in GitHub Desktop.
import pytest
class Hat:
def __init__(self, color: str) -> None:
self.hat_color = color
def suits(self, style: str):
return self.hat_color == style
class Weather:
def __init__(self, rainy: bool) -> None:
self.rainy = rainy
self.sunny = not rainy
def is_raining(self):
return self.rainy
def should_i_wear_this_hat(hat, weather):
res = False
if isinstance(hat, Hat):
current_fashion = _get_fashion()
weather_outside = weather
is_stylish = evaluate_style(hat, current_fashion)
if weather_outside.is_raining():
res = True
if res is True:
print("Damn!")
return res
else:
res = is_stylish
print("Nice!")
return res
else:
return res
def _get_fashion():
return ["green", "blue", "purple", "yellow"]
def evaluate_style(hat, current_fashion):
if len(current_fashion) > 0:
for f in current_fashion:
if hat.suits(f):
return True
return False
else:
return False # User Error
def test_cannot_wear_anything_other_than_a_hat():
no_hat = "foo"
assert should_i_wear_this_hat(no_hat, Weather(False)) is False
def test_should_wear_any_hat_if_weather_is_rainy():
assert should_i_wear_this_hat(Hat("black"), Weather(True)) is True
class TestSunnyWeather:
@pytest.fixture
def sunny_weather(self):
return Weather(False)
def test_should_not_wear_hat_if_it_is_not_stylish(self, sunny_weather):
assert should_i_wear_this_hat(Hat("black"), sunny_weather) is False
@pytest.mark.parametrize("stylish_color", [
"green",
"blue",
"purple",
"yellow"
])
def test_should_wear_hat_if_it_has_stylish_color(self, stylish_color, sunny_weather):
assert should_i_wear_this_hat(Hat(stylish_color), sunny_weather) is True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment