File tree 3 files changed +48
-0
lines changed
3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 37
37
/231 /README.md
38
38
/225 /README.md
39
39
/254 /README.md
40
+ /218 /README.md
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments