From e097304ea34ef691056c49e29b3aca89644ad769 Mon Sep 17 00:00:00 2001 From: Geoff Riley Date: Fri, 1 Oct 2021 11:23:54 +0100 Subject: [PATCH] PCC45 GeoffRiley --- 45/GeoffRiley/fizzbuzz.py | 33 ++++++++++++++++++++++++++ 45/GeoffRiley/fizzbuzz_spec.py | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 45/GeoffRiley/fizzbuzz.py create mode 100644 45/GeoffRiley/fizzbuzz_spec.py diff --git a/45/GeoffRiley/fizzbuzz.py b/45/GeoffRiley/fizzbuzz.py new file mode 100644 index 000000000..d75f01c32 --- /dev/null +++ b/45/GeoffRiley/fizzbuzz.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +from numbers import Number +from typing import Union + + +def fizzbuzz(val: Number) -> Union[str, Number]: + if not isinstance(val, Number) or val % 1 != 0: + raise ValueError('Only whole integer values accepted') + if val % 15 == 0: + return "FizzBuzz" + if val % 5 == 0: + return "Buzz" + if val % 3 == 0: + return "Fizz" + return val + + # Playing FizzBuzz + # when passed the value 3 + # ✓ it says "Fizz" + # when passed a multiple of 3 + # ✓ it says "Fizz" + # when passed the value 5 + # ✓ it says "Buzz" + # when passed a multiple of 5 + # ✓ it says "Buzz" + # when passed a multiple of 3 and 5 + # ✓ it says "FizzBuzz" + # when passed a value divisible by neither 3 nor 5 + # ✓ it says the value back unchanged + # when passed a none whole integer value + # ✓ it screams and raises a ValueError + # when pass a none numeric value + # ✓ it screams and raises a ValueError diff --git a/45/GeoffRiley/fizzbuzz_spec.py b/45/GeoffRiley/fizzbuzz_spec.py new file mode 100644 index 000000000..482e0b026 --- /dev/null +++ b/45/GeoffRiley/fizzbuzz_spec.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- + +from expects import expect, equal, raise_error +from mamba import description, context, it + +from fizzbuzz import fizzbuzz + +with description('Playing FizzBuzz') as self: + with context('when passed the value 3'): + with it('says "Fizz"'): + expect(fizzbuzz(3)).to(equal('Fizz')) + + with context('when passed a multiple of 3'): + with it('says "Fizz"'): + expect(fizzbuzz(9)).to(equal('Fizz')) + + with context('when passed the value 5'): + with it('says "Buzz"'): + expect(fizzbuzz(5)).to(equal('Buzz')) + + with context('when passed a multiple of 5'): + with it('says "Buzz"'): + expect(fizzbuzz(20)).to(equal('Buzz')) + + with context('when passed a multiple of 3 and 5'): + with it('says "FizzBuzz"'): + expect(fizzbuzz(15)).to(equal('FizzBuzz')) + + with context('when passed a value divisible by neither 3 nor 5'): + with it('says the value back unchanged'): + expect(fizzbuzz(1)).to(equal(1)) + expect(fizzbuzz(2)).to(equal(2)) + + with context('when passed a none whole integer value'): + with it('screams and raises a ValueError'): + # Use lambda to overcome deficiency in expects library + expect(lambda: fizzbuzz(3.14)).to(raise_error(ValueError)) + + with context('when pass a none numeric value'): + with it('screams and raises a ValueError'): + # Use lambda to overcome deficiency in expects library + expect(lambda: fizzbuzz('fred')).to(raise_error(ValueError))