spec/isolator_spec.rb in casualjim-caricature-0.3.1 vs spec/isolator_spec.rb in casualjim-caricature-0.5.0

- old
+ new

@@ -1,13 +1,36 @@ require File.dirname(__FILE__) + "/bacon_helper" +class TestIsolation + attr_accessor :instance, :expectations + def initialize(instance, expectations) + @instance, @expectations = instance, expectations + end + def send_message(method_name, return_type, *args, &b) + exp = expectations.find(method_name, *args) + if exp + res = instance.__send__(method_name, *args, &b) if exp.super_before? + res = exp.execute(*args) + res = instance.__send__(method_name, *args, &b) if !exp.super_before? and exp.call_super? + res + else + rt = nil + is_value_type = return_type && return_type != System::Void.to_clr_type && return_type.is_value_type + rt = System::Activator.create_instance(return_type) if is_value_type + rt + end + end +end + describe "Caricature::RubyIsolator" do before do @subj = Soldier.new - @recorder = Caricature::MethodCallRecorder.new - @proxy = Caricature::RubyIsolator.for(@subj, @recorder) + res = Caricature::RubyIsolator.for Caricature::IsolatorContext.new(@subj) + iso = TestIsolation.new res.subject, Caricature::Expectations.new + @proxy = res.isolation + @proxy.class.instance_variable_set("@___context___", iso) end describe "when invoking a method" do @@ -17,39 +40,48 @@ it "should return nil" do @proxy.name.should.be.nil end - it "should record a call" do - @recorder.size.should.equal 1 + end + + describe "when isolating a class with class members" do + + before do + res = Caricature::RubyIsolator.for Caricature::IsolatorContext.new(DaggerWithClassMembers) + iso = TestIsolation.new res.subject, Caricature::Expectations.new + @proxy = res.isolation + @proxy.class.instance_variable_set("@___context___", iso) end - it "should record the correct call" do - mc = @recorder[:name] - mc.method_name.should.equal :name - mc.args.should.equal [Caricature::ArgumentRecording.new] - mc.block.should.equal nil + it "should return nil for the class method" do + + @proxy.class.class_name.should.be.nil + end end end describe "Caricature::RecordingClrProxy" do describe "for an instance of a CLR class" do before do - @samurai = ClrModels::Samurai.new - @samurai.name = "Nakiro" - @recorder = Caricature::MethodCallRecorder.new - @proxy = Caricature::ClrIsolator.for(@samurai, @recorder) + @ninja = ClrModels::Ninja.new + @ninja.name = "Nakiro" + context = Caricature::IsolatorContext.new(@ninja) + res = Caricature::ClrIsolator.for context + iso = TestIsolation.new res.subject, Caricature::Expectations.new + @proxy = res.isolation + @proxy.class.instance_variable_set("@___context___", iso) end it "should create a proxy" do - @proxy.___super___.name.should.equal @samurai.name - @proxy.___super___.id.should.equal 0 + @proxy.___super___.name.should.equal @ninja.name + @proxy.___super___.id.should.equal 1 end describe "when invoking a method" do before do @@ -58,64 +90,56 @@ it "should return nil" do @proxy.name.should.be.nil end - it "should record a call" do - @recorder.size.should.equal 1 + end + + describe "when isolating a class with class members" do + + before do + res = Caricature::ClrIsolator.for Caricature::IsolatorContext.new(ClrModels::SwordWithStatics) + iso = TestIsolation.new res.subject, Caricature::Expectations.new + @proxy = res.isolation + @proxy.class.instance_variable_set("@___context___", iso) end - it "should record the correct call" do - mc = @recorder[:name] - mc.method_name.should.equal :name - mc.args.should.equal [Caricature::ArgumentRecording.new] - mc.block.should.equal nil + it "should return nil for the class method" do + + @proxy.class.class_naming.should.be.nil + end end end describe "for a CLR class" do before do @recorder = Caricature::MethodCallRecorder.new - @proxy = Caricature::ClrIsolator.for(ClrModels::Ninja, @recorder) + context = Caricature::IsolatorContext.new(ClrModels::Ninja, @recorder) + res = Caricature::ClrIsolator.for context + iso = TestIsolation.new res.subject, Caricature::Expectations.new + @proxy = res.isolation + @proxy.class.instance_variable_set("@___context___", iso) end it "should create a proxy" do - @proxy.___super___.class.superclass.should.equal ClrModels::Ninja + @proxy.class.superclass.should.equal ClrModels::Ninja end - describe "when invoking a method" do - - before do - @proxy.name - end - - it "should record a call" do - @recorder.size.should.equal 1 - end - - it "should record the correct call" do - mc = @recorder[:name] - mc.method_name.should.equal :name - mc.args.should.equal [Caricature::ArgumentRecording.new] - mc.block.should.equal nil - end - - end end describe "for a CLR interface" do before do - @recorder = Caricature::MethodCallRecorder.new - @proxy = Caricature::ClrInterfaceIsolator.for(ClrModels::IWarrior, @recorder) + res = Caricature::ClrInterfaceIsolator.for Caricature::IsolatorContext.new(ClrModels::IWarrior) + @proxy = res.isolation end it "should create a proxy" do @proxy.class.to_s.should.match /^IWarrior/ end @@ -130,36 +154,17 @@ it "should create a setter for a writable property on the proxy" do @proxy.should.respond_to?(:name=) end - describe "when invoking a method" do - - before do - @proxy.name - end - - it "should record a call" do - @recorder.size.should.equal 1 - end - - it "should record the correct call" do - mc = @recorder[:name] - mc.method_name.should.equal :name - mc.args.should.equal [Caricature::ArgumentRecording.new] - mc.block.should.equal nil - end - - end - end describe "for a CLR Interface with an event" do before do - @recorder = Caricature::MethodCallRecorder.new - @proxy = Caricature::ClrInterfaceIsolator.for(ClrModels::IExposing, @recorder) + res = Caricature::ClrInterfaceIsolator.for Caricature::IsolatorContext.new(ClrModels::IExposing) + @proxy = res.isolation end it "should create an add method for the event" do @proxy.should.respond_to?(:add_is_exposed_changed) end @@ -171,10 +176,11 @@ end describe "for CLR interface recursion" do before do - @proxy = Caricature::ClrInterfaceIsolator.for(ClrModels::IExposingWarrior, Caricature::MethodCallRecorder.new) + res = Caricature::ClrInterfaceIsolator.for Caricature::IsolatorContext.new(ClrModels::IExposingWarrior) + @proxy = res.isolation end it "should create a method defined on the CLR interface" do @proxy.should.respond_to?(:own_method) end \ No newline at end of file