Skip to content

Commit 3e9fc11

Browse files
bites 218
1 parent 4860d13 commit 3e9fc11

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@
3737
/231/README.md
3838
/225/README.md
3939
/254/README.md
40+
/218/README.md

218/sandwich.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from functools import wraps
2+
3+
UPPER_SLICE = "=== Upper bread slice ==="
4+
LOWER_SLICE = "=== Lower bread slice ==="
5+
6+
7+
def sandwich(func):
8+
"""Write a decorator that prints UPPER_SLICE and
9+
LOWER_SLICE before and after calling the function (func)
10+
that is passed in (@wraps is to preserve the original
11+
func's docstring)
12+
"""
13+
@wraps(func)
14+
def wrapped(*args, **kwargs):
15+
print(UPPER_SLICE)
16+
func(*args, **kwargs)
17+
print(LOWER_SLICE)
18+
return wrapped
19+

218/test_sandwich.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from sandwich import sandwich
2+
3+
SANDWICH_BACON = """=== Upper bread slice ===
4+
bacon / lettuce / tomato
5+
=== Lower bread slice ===
6+
"""
7+
SANDWICH_EGG = """=== Upper bread slice ===
8+
fried egg / tomato / cucumber
9+
=== Lower bread slice ===
10+
"""
11+
12+
13+
@sandwich
14+
def add_ingredients(ingredients):
15+
print(' / '.join(ingredients))
16+
17+
18+
def test_bacon_sandwich(capfd):
19+
ingredients = ['bacon', 'lettuce', 'tomato']
20+
add_ingredients(ingredients)
21+
actual = capfd.readouterr()[0]
22+
assert actual == SANDWICH_BACON
23+
24+
def test_fried_egg_sandwich(capfd):
25+
ingredients = ['fried egg', 'tomato', 'cucumber']
26+
add_ingredients(ingredients)
27+
actual = capfd.readouterr()[0]
28+
assert actual == SANDWICH_EGG

0 commit comments

Comments
 (0)