-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""Use a list to create a shopping list and change an item in the list.""" | ||
|
||
|
||
def change_shopping_list(index: int, item: str) -> None: | ||
""" | ||
Demo of how to change an item in a shopping list. | ||
""" | ||
shopping_list = ["Double cream", "Single cream", "Eggs", "Oreos"] | ||
shopping_list[index] = item | ||
|
||
summary = "\nSHOPPING LIST\n" | ||
summary = summary + "\n" + "\n\n".join(shopping_list) | ||
print(summary) | ||
|
||
|
||
if __name__ == "__main__": | ||
change_shopping_list(3, "Healthier option") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""Module to test the function change_shopping_list() in the file shopping_list.py.""" | ||
|
||
from unittest.mock import patch | ||
from shopping_list import change_shopping_list # type: ignore | ||
|
||
|
||
def test_change_shopping_list() -> None: | ||
""" | ||
Test that the function change_shopping_list() prints the correct summary. | ||
""" | ||
with patch("builtins.print") as mocked_print: | ||
change_shopping_list(3, "Healthier option") | ||
|
||
# Combine the print outputs into a single string | ||
expected_output = ( | ||
"\nSHOPPING LIST\n" | ||
"\nDouble cream\n" | ||
"\nSingle cream\n" | ||
"\nEggs\n" | ||
"\nHealthier option" | ||
) | ||
|
||
# Flatten the actual print calls into a single string | ||
actual_output = "".join(call[0][0] for call in mocked_print.call_args_list) | ||
|
||
assert actual_output == expected_output |