Created
February 24, 2015 16:56
-
-
Save nestorsalceda/a4497649e7af0674644d to your computer and use it in GitHub Desktop.
TDD Fizzbuzz
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
# multiplo de 3 => Fizz | |
# multiplo de 5 => Buzz | |
# multiplo de 5 y de 3 => FizzBuzz | |
# si no, el mismo número | |
require 'test/unit' | |
# Red -> Green -> Refactor ... | |
# | |
def fizzbuzz(number) | |
return 'FizzBuzz' if is_fizz_buzz(number) | |
return 'Buzz' if is_buzz(number) | |
return 'Fizz' if is_fizz(number) | |
number | |
end | |
def is_fizz_buzz(number) | |
is_fizz(number) and is_buzz(number) | |
end | |
def is_fizz(number) | |
number % 3 == 0 | |
end | |
def is_buzz(number) | |
number % 5 == 0 | |
end | |
class FizzBuzzTest < Test::Unit::TestCase | |
def test_returns_fizz_when_number_is_fizz | |
assert_equal(fizzbuzz(6), 'Fizz') | |
end | |
def test_returns_buzz_when_number_is_buzz | |
assert_equal(fizzbuzz(20), 'Buzz') | |
end | |
def test_returns_same_number_when_number_is_not_fizz_nor_buzz | |
assert_equal(fizzbuzz(2), 2) | |
end | |
def test_returns_fizzbuzz_when_number_is_fizz_and_buzz | |
assert_equal(fizzbuzz(15), 'FizzBuzz') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment