Skip to content

Commit

Permalink
More syntax enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
casualjim committed Jan 24, 2010
1 parent d636e07 commit 5104025
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
9 changes: 8 additions & 1 deletion lib/caricature/core_ext/class.rb
Expand Up @@ -15,7 +15,14 @@ def clr_type?
def isolate(name=nil, recorder = Caricature::MethodCallRecorder.new, expectations = Caricature::Expectations.new, &block) def isolate(name=nil, recorder = Caricature::MethodCallRecorder.new, expectations = Caricature::Expectations.new, &block)
iso = Caricature::Isolation.for(self, recorder, expectations) iso = Caricature::Isolation.for(self, recorder, expectations)
return iso unless name return iso unless name
iso.when_class_receives(name, &block) if block
if block.arity > 0
@expectation = iso.when_class_receives(name, &block)
else
@expectation = iso.when_class_receives(name)
instance_eval &block
end
end
iso iso
end end
alias_method :when_receiving, :isolate alias_method :when_receiving, :isolate
Expand Down
56 changes: 52 additions & 4 deletions lib/caricature/core_ext/object.rb
Expand Up @@ -13,17 +13,65 @@ def to_clr_type


# defines a class method on an object # defines a class method on an object
def define_cmethod(name, &blk) def define_cmethod(name, &blk)
(class << self; self; end).instance_eval { define_method name, &blk } (
class << self;
self;
end).instance_eval { define_method name, &blk }
end end


public def isolate(name=nil, recorder = Caricature::MethodCallRecorder.new, expectations = Caricature::Expectations.new, &block)
def isolate(name=nil, recorder = Caricature::MethodCallRecorder.new, expectations = Caricature::Expectations.new, &block)
iso = Caricature::Isolation.for(self, recorder, expectations) iso = Caricature::Isolation.for(self, recorder, expectations)
return iso unless name return iso unless name
iso.when_receiving(name, &block) if block
if block.arity > 0
@expectation = iso.when_receiving(name, &block)
else
@expectation = iso.when_receiving(name)
instance_eval &block
end
end
iso iso
end end
alias_method :when_receiving, :isolate alias_method :when_receiving, :isolate
alias_method :mock, :isolate alias_method :mock, :isolate
alias_method :stub, :isolate alias_method :stub, :isolate

# tell the expectation which arguments it needs to respond to
# there is a magic argument here +any+ which configures
# the expectation to respond to any arguments
def with(*ags, &b)
@expectation.with(*ags, &b)
self
end

# tell the expectation it needs to return this value or the value returned by the block
# you provide to this method.
def returns(value=nil, &b)
@expectation.return(value, &b)
self
end

# Sets up arguments for the block that is being passed into the isolated method call
def pass_block(*ags, &b)
@expectation.pass_block(*ags, &b)
self
end

# tell the expectation it needs to raise an error with the specified arguments
def raise_error(*args)
@expectation.raise(*args)
self
end

# tell the expectation it needs to call the super before the expectation exectution
def super_before(&b)
@expectation.super_before(&b)
self
end

# tell the expectation it needs to call the super after the expecation execution
def super_after(&b)
@expectation.super_after &b
self
end
end end
1 change: 1 addition & 0 deletions lib/caricature/expectation.rb
Expand Up @@ -53,6 +53,7 @@ def return(value=nil, &b)
collected[:return_callback] = b if b collected[:return_callback] = b if b
self self
end end
alias_method :returns, :return


# Sets up arguments for the block that is being passed into the isolated method call # Sets up arguments for the block that is being passed into the isolated method call
def pass_block(*ags, &b) def pass_block(*ags, &b)
Expand Down
4 changes: 2 additions & 2 deletions lib/caricature/isolation.rb
Expand Up @@ -122,8 +122,8 @@ def for(subject, recorder = MethodCallRecorder.new, expectations = Expectations.


def internal_create_override(method_name, mode=:instance, &block) def internal_create_override(method_name, mode=:instance, &block)
builder = ExpectationBuilder.new method_name builder = ExpectationBuilder.new method_name
block.call builder unless block.nil? block.call builder if block
exp = builder.build exp = builder.build
expectations.add_expectation exp, mode expectations.add_expectation exp, mode
exp exp
end end
Expand Down
14 changes: 12 additions & 2 deletions spec/bacon/integration/syntax_spec.rb
Expand Up @@ -9,11 +9,16 @@
soldier.should.not.be.nil soldier.should.not.be.nil
end end


it "should allow setting an expectation" do it "should allow setting an expectation with a block parameter" do
soldier = SoldierWithClassMembers.isolate(:class_name){ |exp| exp.return("overridden") } soldier = SoldierWithClassMembers.isolate(:class_name){ |exp| exp.return("overridden") }
soldier.class.class_name.should == "overridden" soldier.class.class_name.should == "overridden"
end end


it "should allow setting an expectation without a block parameter" do
soldier = SoldierWithClassMembers.isolate(:class_name){ returns("overridden") }
soldier.class.class_name.should == "overridden"
end

end end




Expand All @@ -24,10 +29,15 @@
soldier.should.not.be.nil soldier.should.not.be.nil
end end


it "should allow setting an expectation" do it "should allow setting an expectation with a block parameter" do
soldier = Soldier.new.isolate(:name){ |exp| exp.return("overridden") } soldier = Soldier.new.isolate(:name){ |exp| exp.return("overridden") }
soldier.name.should == "overridden" soldier.name.should == "overridden"
end end


it "should allow setting an expectation without a block parameter" do
soldier = Soldier.new.isolate(:name){ returns("overridden") }
soldier.name.should == "overridden"
end

end end
end end

0 comments on commit 5104025

Please sign in to comment.