Skip to content

Instantly share code, notes, and snippets.

@geronimod
Last active December 28, 2015 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geronimod/7568130 to your computer and use it in GitHub Desktop.
Save geronimod/7568130 to your computer and use it in GitHub Desktop.
Kata Fizz Buzz
require 'minitest/autorun'
class FizzBuzz
class FizzBuzz::NonNatural < StandardError; end
def self.check(input)
raise NonNatural if !input.is_a?(Integer) || input < 1
if input % 15 == 0
'fizzbuzz'
elsif input % 3 == 0
'fizz'
elsif input % 5 == 0
'buzz'
else
input
end
end
end
describe FizzBuzz do
describe "non divisible" do
it "must to return the same input" do
FizzBuzz.check(1).must_equal 1
FizzBuzz.check(2).must_equal 2
FizzBuzz.check(4).must_equal 4
end
it "must to raise a FizzBuzz::NonNatural execption when the input is not a natural number" do
proc {
FizzBuzz.check(-1)
}.must_raise FizzBuzz::NonNatural
proc {
FizzBuzz.check(1.5)
}.must_raise FizzBuzz::NonNatural
proc {
FizzBuzz.check('1.5')
}.must_raise FizzBuzz::NonNatural
end
end
describe "fizz" do
it "must to return 'fizz' when the input is dividable by 3" do
FizzBuzz.check(3).must_equal "fizz"
FizzBuzz.check(6).must_equal "fizz"
end
end
describe "buzz" do
it "must to return 'buzz' when the input is dividable by 5" do
FizzBuzz.check(5).must_equal "buzz"
FizzBuzz.check(10).must_equal "buzz"
end
end
describe "fizzbuzz" do
it "must to return 'fizzbuzz' when the input is dividable by 15" do
FizzBuzz.check(15).must_equal "fizzbuzz"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment