Created
May 22, 2011 04:09
-
-
Save mrkn/985176 to your computer and use it in GitHub Desktop.
CLR/H #58 で作ったコード達
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
# lib/fizz_buzz_counter.rb | |
class FizzBuzzCounter | |
def self.filter(n) | |
s = [ ['Fizz'][n % 3], ['Buzz'][n % 5] ] * '' | |
s[0] ? s : n | |
end | |
def initialize | |
@state = 0 | |
end | |
def next | |
@state += 1 | |
FizzBuzzCounter.filter(@state) | |
end | |
end |
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
# spec/fizz_buzz_counter_spec.rb | |
require 'spec_helper' | |
describe FizzBuzzCounter do | |
describe ".filter" do | |
[ [ 1, 1 ], | |
[ 2, 2 ], | |
[ 3, 'Fizz' ], | |
[ 4, 4 ], | |
[ 5, 'Buzz' ], | |
[ 6, 'Fizz' ], | |
[ 15, 'FizzBuzz' ], | |
].each do |arg, expectation| | |
context "with #{arg}" do | |
it "returns #{expectation}" do | |
FizzBuzzCounter.filter(arg).should be == expectation | |
end | |
end | |
end | |
end | |
describe "#next" do | |
context "just created" do | |
subject { FizzBuzzCounter.new } | |
it "calls FizzBuzzCounter.filter" do | |
FizzBuzzCounter.should_receive(:filter) | |
subject.next | |
end | |
it "calls FizzBuzzCounter.filter with 1" do | |
FizzBuzzCounter.should_receive(:filter).with(1) | |
subject.next | |
end | |
it "returns 1" do | |
subject.next.should be == 1 | |
end | |
end | |
context "after called once" do | |
it "return 2" do | |
subject.next | |
subject.next.should be == 2 | |
end | |
end | |
end | |
end |
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
# observer_pattern_spec.rb | |
module Observable | |
def attach_observer(observer) | |
observers << observer unless observers.include?(observer) | |
end | |
def notify | |
observers.each {|ob| ob.update() } | |
end | |
private | |
def observers | |
@observers ||= [] | |
end | |
end | |
class ConcreteObservable | |
include Observable | |
def change | |
notify | |
end | |
end | |
describe ConcreteObservable do | |
context "with an Observer" do | |
subject { ConcreteObservable.new } | |
let(:observer) { double("an observer").as_null_object } | |
before do | |
subject.attach_observer(observer) | |
end | |
it "calls its notify()" do | |
subject.should_receive(:notify).with() | |
subject.change | |
end | |
it "calls observer's update()" do | |
observer.should_receive(:update).with() | |
subject.change | |
end | |
end | |
end |
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
# spec/spec_helper.rb | |
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) | |
require 'fizz_buzz_counter' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment