-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes issue #242 - contract's return value is now enforced with block…
…s properly (#251) * fixes issue #242 - contract's return value is now enforced with blocks properly * more robust if condition * handle block with keyword arguments and change Gemfile syntax to work with travis properly
- Loading branch information
1 parent
3d09338
commit 13e56bd
Showing
3 changed files
with
60 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
RSpec.describe "Contracts:" do | ||
describe "method called with blocks" do | ||
module FuncTest | ||
include Contracts::Core | ||
include Contracts::Builtin | ||
|
||
Contract Func[Num=>Num] => nil | ||
def foo(&blk) | ||
_ = blk.call(2) | ||
nil | ||
end | ||
|
||
Contract Num, Func[Num=>Num] => nil | ||
def foo2(a, &blk) | ||
_ = blk.call(2) | ||
nil | ||
end | ||
|
||
Contract Func[Num=>Num] => nil | ||
def bar(blk) | ||
_ = blk.call(2) | ||
nil | ||
end | ||
|
||
Contract Num, Func[Num=>Num] => nil | ||
def bar2(a, blk) | ||
_ = blk.call(2) | ||
nil | ||
end | ||
end | ||
|
||
def obj | ||
Object.new.tap do |o| | ||
o.extend(FuncTest) | ||
end | ||
end | ||
|
||
it "should enforce return value inside block with no other parameter" do | ||
expect { obj.foo(&:to_s) }.to raise_error | ||
end | ||
|
||
it "should enforce return value inside block with other parameter" do | ||
expect { obj.foo2(2) { |x| x.to_s } }.to raise_error | ||
end | ||
|
||
it "should enforce return value inside lambda with no other parameter" do | ||
expect { obj.bar lambda { |x| x.to_s } }.to raise_error | ||
end | ||
|
||
it "should enforce return value inside lambda with other parameter" do | ||
expect { obj.bar2(2, lambda { |x| x.to_s }) }.to raise_error | ||
end | ||
end | ||
end |