Created
August 19, 2010 15:42
-
-
Save snusnu/538187 to your computer and use it in GitHub Desktop.
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
require 'rubygems' | |
require 'spec' | |
module Transformable | |
private | |
def transform(&block) | |
copy = self.clone | |
copy.instance_eval(&block) | |
copy.freeze | |
end | |
end | |
class Immutable | |
include Transformable | |
attr_reader :state, :delta | |
def initialize(state) | |
@state = state | |
@delta = nil | |
end | |
def modify(delta) | |
transform { @delta = delta } | |
end | |
end | |
describe 'Transformable#transform' do | |
subject { instance.modify(delta) } | |
let(:instance) { Immutable.new(state) } | |
let(:state) { 'foo' } | |
let(:delta) { 'baz' } | |
it { should_not equal(instance) } | |
it { should be_instance_of(Immutable) } | |
it { should be_frozen } | |
its(:state) { should == state } | |
its(:delta) { should == delta } | |
end | |
__END__ | |
ree-1.8.7-2010.02 mungo:playground snusnu$ spec transform_spec.rb | |
Profiling enabled. | |
..... | |
Finished in 0.003661 seconds | |
5 examples, 0 failures | |
ree-1.8.7-2010.02 mungo:playground snusnu$ spec transform_spec.rb --heckle="Immutable#modify" | |
********************************************************************** | |
*** Immutable#modify loaded with 2 possible mutations | |
********************************************************************** | |
2 mutations remaining... | |
1 mutations remaining... | |
No mutants survived. Cool! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment