Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/contracts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def initialize(klass, method, *contracts)
maybe_a_proc = last_contract.is_a?(Contracts::Maybe) && last_contract.include_proc?

@has_proc_contract = is_a_proc || maybe_a_proc || last_contract.is_a?(Contracts::Func)

# ====

# == @has_options_contract
Expand Down Expand Up @@ -235,20 +236,24 @@ def call(*args, &blk)

# a better way to handle this might be to take this into account
# before throwing a "mismatched # of args" error.
# returns true if it appended nil
def maybe_append_block! args, blk
return unless @has_proc_contract && !blk &&
return false unless @has_proc_contract && !blk &&
(@args_contract_index || args.size < args_contracts.size)
args << nil
true
end

# Same thing for when we have named params but didn't pass any in.
# returns true if it appended nil
def maybe_append_options! args, blk
return unless @has_options_contract
return false unless @has_options_contract
if @has_proc_contract && args_contracts[-2].is_a?(Hash) && !args[-2].is_a?(Hash)
args.insert(-2, {})
elsif args_contracts[-1].is_a?(Hash) && !args[-1].is_a?(Hash)
args << {}
end
true
end

# Used to determine type of failure exception this contract should raise in case of failure
Expand Down
5 changes: 3 additions & 2 deletions lib/contracts/call_with.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def call_with(this, *args, &blk)
args << blk if blk

# Explicitly append blk=nil if nil != Proc contract violation anticipated
maybe_append_block!(args, blk)
nil_block_appended = maybe_append_block!(args, blk)

# Explicitly append options={} if Hash contract is present
maybe_append_options!(args, blk)
Expand Down Expand Up @@ -66,7 +66,8 @@ def call_with(this, *args, &blk)
end

# If we put the block into args for validating, restore the args
args.slice!(-1) if blk
# OR if we added a fake nil at the end because a block wasn't passed in.
args.slice!(-1) if blk || nil_block_appended
result = if method.respond_to?(:call)
# proc, block, lambda, etc
method.call(*args, &blk)
Expand Down
10 changes: 10 additions & 0 deletions spec/contracts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,16 @@ def self.greeting(name)
@o.maybe_call("bad")
end.to raise_error(ContractError)
end

describe "varargs are given with a maybe block" do
it "when a block is passed in, varargs should be correct" do
expect(@o.maybe_call(1, 2, 3) { 1 + 1 }).to eq([1, 2, 3])
end

it "when a block is NOT passed in, varargs should still be correct" do
expect(@o.maybe_call(1, 2, 3)).to eq([1, 2, 3])
end
end
end

describe "varargs" do
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def do_call(&block)
Contract Args[Num], Maybe[Proc] => Any
def maybe_call(*vals, &block)
block.call if block
vals
end

Contract Args[Num] => Num
Expand Down