-
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
boraxpr
committed
Oct 22, 2019
1 parent
ec7515a
commit ae0a34e
Showing
3 changed files
with
93 additions
and
2 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,28 @@ | ||
VALID_COLORS = ['blue', 'yellow', 'red'] | ||
|
||
|
||
def print_colors(): | ||
"""In the while loop ask the user to enter a color, | ||
lowercase it and store it in a variable. Next check: | ||
- if 'quit' was entered for color, print 'bye' and break. | ||
- if the color is not in VALID_COLORS, print 'Not a valid color' and continue. | ||
- otherwise print the color in lower case.""" | ||
while True: | ||
print("A Color: ") | ||
inp = input() | ||
input_lowered = inp.lower() | ||
valid = 0 | ||
if input_lowered == "quit": | ||
print("bye") | ||
break | ||
for color in VALID_COLORS: | ||
if color == input_lowered: | ||
valid = 1 | ||
if valid == 1: | ||
print(input_lowered) | ||
else: | ||
print("Not a valid color") | ||
continue | ||
pass | ||
|
||
print_colors() |
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,63 @@ | ||
from unittest.mock import patch | ||
|
||
from colors import print_colors | ||
|
||
NOT_VALID = 'Not a valid color' | ||
|
||
|
||
def call_print_colors(): | ||
# some people prefer sys.exit instead of break | ||
try: | ||
print_colors() | ||
except SystemExit: | ||
pass | ||
|
||
|
||
@patch("builtins.input", side_effect=['quit']) | ||
def test_straight_quit(input_mock, capsys): | ||
# user only enter quit, program prints bye and breaks loop | ||
call_print_colors() | ||
actual = capsys.readouterr()[0].strip() | ||
expected = 'bye' | ||
assert actual == expected | ||
|
||
|
||
@patch("builtins.input", side_effect=['blue', 'quit']) | ||
def test_one_valid_color_then_quit(input_mock, capsys): | ||
# user enters blue = valid color so print it | ||
# then user enters quit so break out of loop = end program | ||
call_print_colors() | ||
actual = capsys.readouterr()[0].strip() | ||
expected = 'blue\nbye' | ||
assert actual == expected | ||
|
||
|
||
@patch("builtins.input", side_effect=['green', 'quit']) | ||
def test_one_invalid_color_then_quit(input_mock, capsys): | ||
# user enters green which is not in VALID_COLORS so continue the loop, | ||
# user then enters quit so loop breaks (end function / program) | ||
call_print_colors() | ||
actual = capsys.readouterr()[0].strip() | ||
expected = f'{NOT_VALID}\nbye' | ||
assert actual == expected | ||
|
||
|
||
@patch("builtins.input", side_effect=['white', 'red', 'quit']) | ||
def test_invalid_then_valid_color_then_quit(nput_mock, capsys): | ||
# white is not a valid color so continue the loop, | ||
# then user enters red which is valid so print it, then quit | ||
call_print_colors() | ||
actual = capsys.readouterr()[0].strip() | ||
expected = f'{NOT_VALID}\nred\nbye' | ||
assert actual == expected | ||
|
||
|
||
@patch("builtins.input", side_effect=['yellow', 'orange', 'quit']) | ||
def test_valid_then_invalid_color_then_quit(input_mock, capsys): | ||
# yellow is a valid color so print it, user then enters orange | ||
# which is not a valid color so continue loop, lastly user | ||
# enters quit so exit loop = reaching end function / program | ||
call_print_colors() | ||
actual = capsys.readouterr()[0].strip() | ||
expected = f'yellow\n{NOT_VALID}\nbye' | ||
assert actual == expected |