lib/caricature/isolator.rb in caricature-0.6.3 vs lib/caricature/isolator.rb in caricature-0.7.0

- old
+ new

@@ -1,5 +1,7 @@ +require 'rubygems' +require 'uuidtools' require File.dirname(__FILE__) + '/messenger' require File.dirname(__FILE__) + '/descriptor' module Caricature @@ -132,11 +134,11 @@ # Verifies whether the specified class method has been called # You can specify constraints in the block # # The most complex configuration you can make currently is one that is constrained by arguments. - # This is most likely to be extended in the future to allow for more complex verifications. + # This is likely to be extended in the future to allow for more complex verifications. # # Example: # # an_isolation.did_class_receive?(:a_method) do |method_call| # method_call.with(3, "a") @@ -148,10 +150,18 @@ # # You will probably be using this method only when you're interested in whether a method has been called # during the course of the test you're running. def did_class_receive?(method_name, &block) self.class.did_receive?(method_name, &block) + end + + # Initializes the underlying subject + # It expects the constructor parameters if they are needed. + def with_subject(*args, &b) + isolation_context.instance = self.class.superclass.new *args + yield self if block_given? + self end end # A base class for +Isolator+ objects @@ -192,11 +202,11 @@ end # Creates the new class name for the isolation def class_name(subj) nm = subj.respond_to?(:class_eval) ? subj.demodulize : subj.class.demodulize - @class_name = "#{nm}#{System::Guid.new_guid.to_string('n')}" + @class_name = "#{nm}#{UUIDTools::UUID.random_create.to_s.gsub /-/, ''}" @class_name end # Sets up the necessary instance variables for the isolation def initialize_isolation(klass, context) @@ -230,11 +240,12 @@ # implemented template method for creating Ruby isolations def initialize(context) super klass = @context.subject.respond_to?(:class_eval) ? @context.subject : @context.subject.class - inst = @context.subject.respond_to?(:class_eval) ? @context.subject.new : @context.subject + inst = @context.subject.respond_to?(:class_eval) ? nil : @context.subject + # inst = @context.subject.respond_to?(:class_eval) ? @context.subject.new : @context.subject @descriptor = RubyObjectDescriptor.new klass build_isolation klass, inst end # initializes the messaging strategy for the isolator @@ -259,22 +270,27 @@ imembers.each do |mn| mn = mn.name.to_s.to_sym define_method mn do |*args| b = nil - b = Proc.new { yield } if block_given? + b = Proc.new { yield } if block_given? isolation_context.send_message(mn, nil, *args, &b) end - end - + end + + def initialize(*args) + self + end + cmembers.each do |mn| mn = mn.name.to_s.to_sym - define_cmethod mn do |*args| + define_cmethod mn do |*args| b = nil - b = Proc.new { yield } if block_given? + b = Proc.new { yield } if block_given? isolation_context.send_class_message(mn, nil, *args, &b) end - end + end + end klass \ No newline at end of file