Skip to content

Instantly share code, notes, and snippets.

@snusnu
Created August 19, 2010 15:42
Show Gist options
  • Save snusnu/538187 to your computer and use it in GitHub Desktop.
Save snusnu/538187 to your computer and use it in GitHub Desktop.
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