Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

honor Func contract on returns (fixes issue #143) #145

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
honor Func contract on returns (fixes issue #143)
  • Loading branch information
Rob Rosenbaum committed May 7, 2015
commit c8a8067789093b1ea0198cd42fc488f427a16e10
4 changes: 4 additions & 0 deletions lib/contracts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ def call_with(this, *args, &blk)

this.verify_invariants!(method) if this.respond_to?(:verify_invariants!)

if ret_contract.is_a?(Contracts::Func)
result = Contract.new(klass, result, *ret_contract.contracts)
end

result
end

Expand Down
12 changes: 12 additions & 0 deletions spec/contracts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,18 @@ def self.greeting(name)
it "should fail for a function that doesn't pass the contract with weak other args" do
expect { @o.map_plain(["hello", "joe"], lambda { |_| nil }) }.to raise_error(ContractError)
end

it "should fail for a returned function that doesn't pass the contract" do
expect { @o.lambda_with_wrong_return().("hello") }.to raise_error(ContractError)
end

it "should fail for a returned function that receives the wrong argument type" do
expect { @o.lambda_with_correct_return().(12345) }.to raise_error(ContractError)
end

it "should not fail for a returned function that passes the contract" do
expect { @o.lambda_with_correct_return().("hello") }.to_not raise_error
end
end

describe "default args to functions" do
Expand Down
10 changes: 10 additions & 0 deletions spec/fixtures/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ def map_plain(arr, func)
end
end

Contract None => Func[String => Num]
def lambda_with_wrong_return
lambda { |x| x }
end

Contract None => Func[String => Num]
def lambda_with_correct_return
lambda { |x| x.length }
end

Contract Num => Num
def default_args(x = 1)
2
Expand Down