Skip to content

Instantly share code, notes, and snippets.

@bertocq
Created June 22, 2015 14:56
Show Gist options
  • Save bertocq/2364c0edadc03a48bd9d to your computer and use it in GitHub Desktop.
Save bertocq/2364c0edadc03a48bd9d to your computer and use it in GitHub Desktop.
#fizzbuzz_test.rb
# "Write a program that prints the numbers from 1 to 100.
# But for multiples of three print “Fizz” instead of the
# number and for the multiples of five print “Buzz”.
# For numbers which are multiples of both three and five
# print “FizzBuzz”."
class FizzBuzz
def run(max=100)
result = ''
(1..max).each do |num|
result << fizzbuzz(num)
end
result
end
def fizzbuzz(num)
result = ''
if num % 3 == 0
result << 'Fizz'
end
if num % 5 == 0
result << 'Buzz'
end
if result == ''
result = "#{num}"
end
return result
end
end
#fizzbuzz.rb
require_relative "fizzbuzz"
require "test/unit"
class TestFizzBuzz < Test::Unit::TestCase
def test_number
assert_equal('4', FizzBuzz.new.fizzbuzz(4) )
assert_equal('1', FizzBuzz.new.fizzbuzz(1) )
end
def test_Fizz
assert_equal('Fizz', FizzBuzz.new.fizzbuzz(3) )
assert_equal('Fizz', FizzBuzz.new.fizzbuzz(9) )
end
def test_Buzz
assert_equal('Buzz', FizzBuzz.new.fizzbuzz(5) )
assert_equal('Buzz', FizzBuzz.new.fizzbuzz(25) )
end
def test_FizzBuzz
assert_equal('FizzBuzz', FizzBuzz.new.fizzbuzz(15) )
assert_equal('FizzBuzz', FizzBuzz.new.fizzbuzz(30) )
end
def test_CompleteFizzBuzz
assert_equal('12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617Fizz', FizzBuzz.new.run(18) )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment