Skip to content

Commit 59b6558

Browse files
dseptempybites
authored andcommittedFeb 10, 2018
first (and hopefully) final commit of TDDFizz (#155)
1 parent 78c7a90 commit 59b6558

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed
 

Diff for: ‎45/dseptem/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# TDDFizz
2+
3+
Implementation of FizzBuzz with TDD and pytest.
4+
5+
### Prerequisites
6+
7+
The one and only, **pytest**!
8+
9+
## Running the tests
10+
11+
Simply get into the project directory and type:
12+
13+
```
14+
pytest
15+
```
16+
17+
## Author
18+
19+
* **Dante Septem** - *The one who did everything...! Except pytest and Python of course! Oh and the english language and PyCharm, I didn't make those.*
20+
21+
## Acknowledgments
22+
23+
* pytest for being so awesome and better than the alternatives!
24+
* clamytoe from PyBites slack, for his feedback, thanks pal!
25+
* The PyBites folks, for the motivation and their awesome blog!

Diff for: ‎45/dseptem/fizzbuzz.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
def fizzbuzz(num):
2+
"""Evaluates if a number is a multiple of 3, of 5 or both.
3+
4+
Args:
5+
num: The number to evaluate for "FizzBuzz". Must be an integer, or an integer as a string.
6+
7+
Returns:
8+
str: "Fizz" for multiples of 3, "Buzz" for mults of 5, "FizzBuzz" for mults of 3 and 5, otherwise num.
9+
"""
10+
if isinstance(num, float):
11+
raise ValueError
12+
try:
13+
num = int(num)
14+
except ValueError as e:
15+
raise e
16+
17+
if num % 3 == 0 and num % 5 == 0:
18+
return "FizzBuzz"
19+
elif num % 3 == 0:
20+
return "Fizz"
21+
elif num % 5 == 0:
22+
return "Buzz"
23+
return num
24+
25+
26+
def display_fizzbuzz(num):
27+
"""Prints the "FizzBuzz" of a number.
28+
29+
Args:
30+
num: The integer to be printed after going through the fizzbuzz function.
31+
32+
Returns:
33+
None: Only prints to stdout.
34+
"""
35+
if isinstance(num, float):
36+
raise ValueError
37+
try:
38+
num = int(num)
39+
except ValueError as e:
40+
raise e
41+
42+
print(fizzbuzz(num))
43+
44+
45+
def main():
46+
"""Prints the return of fizzbuzz for numbers from 1 to 100."""
47+
for i in range(1,101):
48+
display_fizzbuzz(i)
49+
50+
51+
if __name__ == '__main__':
52+
main()

Diff for: ‎45/dseptem/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==3.3.1

Diff for: ‎45/dseptem/test_fizzbuzz.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import pytest
2+
from fizzbuzz import fizzbuzz, display_fizzbuzz
3+
4+
5+
def test_invalid_input_fizzbuzz():
6+
"""Tests fizzbuzz handling of invalid input"""
7+
with pytest.raises(ValueError):
8+
fizzbuzz('FAIL')
9+
with pytest.raises(ValueError):
10+
fizzbuzz(3.5)
11+
12+
13+
def test_invalid_input_display_fizzbuzz():
14+
"""Tests display_fizzbuzz handling of invalid input"""
15+
with pytest.raises(ValueError):
16+
display_fizzbuzz('FAIL')
17+
with pytest.raises(ValueError):
18+
display_fizzbuzz(3.5)
19+
20+
21+
def test_fizz():
22+
"""Tests if fizzbuzz returns Fizz for multiples of 3"""
23+
assert fizzbuzz(3) == 'Fizz'
24+
assert fizzbuzz(9) == 'Fizz'
25+
assert fizzbuzz(63) == 'Fizz'
26+
27+
28+
def test_buzz():
29+
"""Tests if fizzbuzz returns Buzz for multiples of 5"""
30+
assert fizzbuzz(5) == 'Buzz'
31+
assert fizzbuzz(10) == 'Buzz'
32+
assert fizzbuzz(55) == 'Buzz'
33+
34+
35+
def test_fizzbuzz():
36+
"""Tests if fizzbuzz returns FizzBuzz for multiples of 3 and 5"""
37+
assert fizzbuzz(15) == 'FizzBuzz'
38+
assert fizzbuzz(30) == 'FizzBuzz'
39+
assert fizzbuzz(90) == 'FizzBuzz'
40+
41+
42+
def test_nofizznobuzz():
43+
"""Tests if fizzbuzz returns a number for non-multiples of 3 and/or 5"""
44+
assert fizzbuzz(1) == 1
45+
assert fizzbuzz(7) == 7
46+
assert fizzbuzz(92) == 92
47+
48+
49+
def test_print_fizz(capsys):
50+
"""Tests if display_fizzbuzz prints Fizz correctly to stdout"""
51+
display_fizzbuzz(3)
52+
out, err = capsys.readouterr()
53+
assert out == "Fizz\n"
54+
display_fizzbuzz(9)
55+
out, err = capsys.readouterr()
56+
assert out == "Fizz\n"
57+
display_fizzbuzz(33)
58+
out, err = capsys.readouterr()
59+
assert out == "Fizz\n"
60+
61+
62+
def test_print_buzz(capsys):
63+
"""Tests if display_fizzbuzz prints Buzz correctly to stdout"""
64+
display_fizzbuzz(5)
65+
out, err = capsys.readouterr()
66+
assert out == "Buzz\n"
67+
display_fizzbuzz(10)
68+
out, err = capsys.readouterr()
69+
assert out == "Buzz\n"
70+
display_fizzbuzz(55)
71+
out, err = capsys.readouterr()
72+
assert out == "Buzz\n"
73+
74+
75+
def test_print_fizzbuzz(capsys):
76+
"""Tests if display_fizzbuzz prints FizzBuzz correctly to stdout"""
77+
display_fizzbuzz(15)
78+
out, err = capsys.readouterr()
79+
assert out == "FizzBuzz\n"
80+
display_fizzbuzz(30)
81+
out, err = capsys.readouterr()
82+
assert out == "FizzBuzz\n"
83+
display_fizzbuzz(60)
84+
out, err = capsys.readouterr()
85+
assert out == "FizzBuzz\n"
86+
87+
88+
def test_print_nofizznobuzz(capsys):
89+
"""Tests if display_fizzbuzz prints non-multiples correctly to stdout"""
90+
display_fizzbuzz(4)
91+
out, err = capsys.readouterr()
92+
assert out == "4\n"
93+
display_fizzbuzz(11)
94+
out, err = capsys.readouterr()
95+
assert out == "11\n"
96+
display_fizzbuzz(98)
97+
out, err = capsys.readouterr()
98+
assert out == "98\n"

0 commit comments

Comments
 (0)
Please sign in to comment.