Created
November 7, 2014 18:22
-
-
Save dliberalesso/c12254ae331f7df37a1d to your computer and use it in GitHub Desktop.
FizzBuzz Ruby TDD
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
#!/usr/local/bin/ruby -w | |
require 'test/unit' | |
class FizzBuzz | |
def self.fizzbuzz(number) | |
result = "" | |
result << "Fizz" if is_divisible_by_three?(number) | |
result << "Buzz" if is_divisible_by_five?(number) | |
return result if result != "" | |
number | |
end | |
def self.is_divisible_by_three?(number) | |
is_divisible_by?(number, 3) | |
end | |
def self.is_divisible_by_five?(number) | |
is_divisible_by?(number, 5) | |
end | |
def self.is_divisible_by?(number, divisor) | |
number % divisor == 0 | |
end | |
end | |
class TestFizzBuzz < Test::Unit::TestCase | |
def test_divisible_by_three | |
# Should return true when multiples of 3 are informed | |
assert_equal(true, FizzBuzz.is_divisible_by_three?(3), "Should return true") | |
assert_equal(true, FizzBuzz.is_divisible_by_three?(6), "Should return true") | |
assert_equal(false, FizzBuzz.is_divisible_by_three?(5), "Should return false") | |
end | |
def test_divisible_by_five | |
# Should return true when multiples of 5 are informed | |
assert_equal(true, FizzBuzz.is_divisible_by_five?(5), "Should return true") | |
assert_equal(true, FizzBuzz.is_divisible_by_five?(10), "Should return true") | |
assert_equal(false, FizzBuzz.is_divisible_by_five?(3), "Should return false") | |
end | |
def test_fizzbuzz_number | |
# Should return only the number | |
assert_equal(1, FizzBuzz.fizzbuzz(1)) | |
assert_equal(2, FizzBuzz.fizzbuzz(2)) | |
end | |
def test_fizz | |
# Should return "Fizz" for multiples of 3 | |
assert_equal("Fizz", FizzBuzz.fizzbuzz(3)) | |
assert_equal("Fizz", FizzBuzz.fizzbuzz(6)) | |
end | |
def test_buzz | |
# Should return "Buzz" for multiples of 5 | |
assert_equal("Buzz", FizzBuzz.fizzbuzz(5)) | |
assert_equal("Buzz", FizzBuzz.fizzbuzz(10)) | |
end | |
def test_fizzbuzz | |
# Should return "FizzBuzz" for multiples of 15 | |
assert_equal("FizzBuzz", FizzBuzz.fizzbuzz(15)) | |
assert_equal("FizzBuzz", FizzBuzz.fizzbuzz(30)) | |
end | |
end | |
1..101.times do |number| | |
puts FizzBuzz.fizzbuzz(number) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment