Created
March 21, 2011 15:43
-
-
Save morygonzalez/879647 to your computer and use it in GitHub Desktop.
TDD Boot Camp 福岡
This file contains hidden or 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
#-*- coding: utf-8 -*- | |
class FizzBuzz | |
def fizzbuzz(num) | |
return "FizzBuzz" if num % 15 == 0 | |
return "Buzz" if num % 5 == 0 | |
return "Fizz" if num % 3 == 0 | |
num.to_s | |
end | |
def fizzbuzz_range(range) | |
range.map do |num| | |
fizzbuzz(num) | |
end | |
end | |
end | |
if $0 == __FILE__ | |
fizzbuzz = FizzBuzz.new | |
100.times do |i| | |
puts "#{i}: #{fizzbuzz.fizzbuzz(i)}" | |
end | |
end |
This file contains hidden or 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
#-*- coding: utf-8 -*- | |
require "rubygems" | |
require "rspec" | |
require "./fizzbuzz" | |
describe FizzBuzz, "fizzbuzz" do | |
before :each do | |
@fizzbuzz = FizzBuzz.new | |
end | |
it "3の倍数のときにFizzを返すこと" do | |
@fizzbuzz.fizzbuzz(3).should == "Fizz" | |
end | |
it "5の倍数のときにBuzzを返すこと" do | |
@fizzbuzz.fizzbuzz(5).should == "Buzz" | |
end | |
it "15の倍数ときにBuzzを返すこと" do | |
@fizzbuzz.fizzbuzz(15).should == "FizzBuzz" | |
@fizzbuzz.fizzbuzz(45).should == "FizzBuzz" | |
end | |
it "0が与えられたときにFizzBuzzを返すこと" do | |
@fizzbuzz.fizzbuzz(0).should == "FizzBuzz" | |
end | |
it "2が与えられたとき2を返すこと" do | |
@fizzbuzz.fizzbuzz(2).should == "2" | |
end | |
end | |
describe FizzBuzz, "fizzbuzz_range" do | |
before :each do | |
@fizzbuzz = FizzBuzz.new | |
end | |
it "1から2までを与えたとき、1, 2が文字列で返ってくること" do | |
@fizzbuzz.fizzbuzz_range((1..2)).should == ["1", "2"] | |
end | |
it "1から5までを与えたとき、[1,2,Fizz,4,Buzz]が文字列で返ってくること" do | |
[1..5].class.should == Array | |
@fizzbuzz.fizzbuzz_range((1..5)).should == ["1", "2", "Fizz", "4", "Buzz"] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment