Skip to content

Instantly share code, notes, and snippets.

@jmyers0022
Last active August 29, 2015 14:16
Show Gist options
  • Save jmyers0022/9ec23afa3b5f0ba29f52 to your computer and use it in GitHub Desktop.
Save jmyers0022/9ec23afa3b5f0ba29f52 to your computer and use it in GitHub Desktop.
BoulderRBTesting
class FizzBuzz
def print(range)
raise ArgumentError unless range.is_a? Range
raise ArgumentError unless range.min > 0
range.map do |num|
if num % 3 == 0
'Fizz'
elsif num % 5 == 0
'Buzz'
elsif num % 15 == 0
'FizzBuzz'
else
num.to_s
end
end
end
end
require_relative 'fizz_buzz'
require 'minitest/autorun'
describe 'FizzBuzz' do
before do
@fizzbuzz = FizzBuzz.new
@range = 1..20
end
# Output expected:
# [1, 2, "Fizz", 4, "Buzz", 6, 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz", ...]
describe '.print' do
it 'should return an array' do
@fizzbuzz.print(@range).must_be_instance_of Array
end
it "should print 'Fizz' for multiples of 3" do
@fizzbuzz.print(@range)[2].must_equal 'Fizz'
end
it "should print 'Buzz' for multiples of 5" do
@fizzbuzz.print(@range)[4].must_equal 'Buzz'
end
it "should print 'FizzBuzz' for multiples of 15" do
@fizzbuzz.print(@range)[14].must_equal 'FizzBuzz'
end
it 'should print the given number if not a multiple of 3, 5, or 15' do
@fizzbuzz.print(@range)[14].must_equal '14'
end
it 'should throw an argument error if an argument is not given' do
proc { @fizzbuzz.print }.must_raise ArgumentError
end
it "should raise an argument error if the argument isn't a range" do
proc { @fizzbuzz.print(4) }.must_raise ArgumentError
end
it 'should throw an argument error if a zero is in the range' do
proc { @fizzbuzz.print(0..10) }.must_raise ArgumentError
end
it 'should throw an argument error if a negative number is in the range' do
proc { @fizzbuzz.print(-1..5) }.must_raise ArgumentError
end
end
end
@jmyers0022
Copy link
Author

This is a simple FizzBuzz implementation written to demonstrate MiniTest in Ruby. Presented March 2015 to the Beginners' Track at the Boulder Ruby meetup.

@jmyers0022
Copy link
Author

The requirements are:

  • Should take a range of positive integers
  • Should print 'Fizz' for multiples of 3
  • Should print 'Buzz' for multiples of 5
  • Should print 'FizzBuzz' for multiples of 15
  • Should not affect other numbers
  • Should return an array of the results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment