Permalink
Browse files

Add instance variable assignment

  • Loading branch information...
1 parent 6d0cbc6 commit ee0c7c8302a6f086c11b5522cd8484a4e2cfc67e @txus committed Feb 27, 2012
View
@@ -114,6 +114,19 @@ def literal_class
end
end
+ class InstanceVariableAssignment < BaseLiteral
+ def swap
+ @node.value = literal_class.new(@node.value.clone).swap
+ @node
+ end
+
+ private
+
+ def literal_class
+ Module.nesting[1].literal_class(@node.value)
+ end
+ end
+
class If < BaseLiteral
def swap
@node.body, @node.else = @node.else, @node.body
@@ -0,0 +1,62 @@
+require 'spec_helper'
+
+describe 'Mutating instance variable assignments' do
+ context 'for an instance method' do
+ context 'that contains @a = 1' do
+ before do
+ write_file 'thing.rb', """
+ class Thing
+ attr_reader :a
+ def set_a
+ @a = 1
+ end
+ end
+ """
+ end
+
+ context 'with an expectation that a will be set to 1' do
+ before do
+ write_file 'spec/thing_spec.rb', """
+ $: << '.'
+ require 'thing'
+
+ describe 'Thing#set_a' do
+ specify do
+ thing = Thing.new
+ thing.set_a
+ thing.a.should eq(1)
+ end
+ end
+ """
+ mutate 'Thing#set_a spec/thing_spec.rb'
+ end
+
+ specify 'the mutation passes' do
+ all_output.should include('passed')
+ end
+ end
+
+ context 'with an expectation that a will bet set to some number' do
+ before do
+ write_file 'spec/thing_spec.rb', """
+ $: << '.'
+ require 'thing'
+
+ describe 'Thing#set_a' do
+ specify do
+ thing = Thing.new
+ thing.set_a
+ thing.a.should be_kind_of(Fixnum)
+ end
+ end
+ """
+ mutate 'Thing#set_a spec/thing_spec.rb'
+ end
+
+ specify 'the mutation fails' do
+ all_output.should include('failed')
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,60 @@
+require 'spec_helper'
+
+describe 'Mutating instance variable assignments' do
+ context 'for an instance method' do
+ context 'that contains @a = 1' do
+ before do
+ write_file 'thing.rb', """
+ class Thing
+ class << self; attr_reader :a; end
+ def self.set_a
+ @a = 1
+ end
+ end
+ """
+ end
+
+ context 'with an expectation that a will be set to 1' do
+ before do
+ write_file 'spec/thing_spec.rb', """
+ $: << '.'
+ require 'thing'
+
+ describe 'Thing.set_a' do
+ specify do
+ Thing.set_a
+ Thing.a.should eq(1)
+ end
+ end
+ """
+ mutate 'Thing.set_a spec/thing_spec.rb'
+ end
+
+ specify 'the mutation passes' do
+ all_output.should include('passed')
+ end
+ end
+
+ context 'with an expectation that a will bet set to some number' do
+ before do
+ write_file 'spec/thing_spec.rb', """
+ $: << '.'
+ require 'thing'
+
+ describe 'Thing.set_a' do
+ specify do
+ Thing.set_a
+ Thing.a.should be_kind_of(Fixnum)
+ end
+ end
+ """
+ mutate 'Thing.set_a spec/thing_spec.rb'
+ end
+
+ specify 'the mutation fails' do
+ all_output.should include('failed')
+ end
+ end
+ end
+ end
+end

0 comments on commit ee0c7c8

Please sign in to comment.