Skip to content

Make KeywordArgs fail if unexpected keys are passed in #187

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

Merged
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
1 change: 1 addition & 0 deletions lib/contracts/builtin_contracts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def initialize(options)
end

def valid?(hash)
return false unless hash.keys - options.keys == []
options.all? do |key, contract|
Optional._valid?(hash, key, contract)
end
Expand Down
18 changes: 18 additions & 0 deletions spec/builtin_contracts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,24 @@
end
end

describe "KeywordArgs:" do
it "should pass for exact correct input" do
expect { @o.person_keywordargs(:name => "calvin", :age => 10) }.to_not raise_error
end

it "should fail if some keys don't have contracts" do
expect { @o.person_keywordargs(:name => "calvin", :age => 10, :foo => "bar") }.to raise_error(ContractError)
end

it "should fail if a key with a contract on it isn't provided" do
expect { @o.person_keywordargs(:name => "calvin") }.to raise_error(ContractError)
end

it "should fail for incorrect input" do
expect { @o.person_keywordargs(:name => 50, :age => 10) }.to raise_error(ContractError)
end
end

describe "Optional:" do
it "can't be used outside of KeywordArgs" do
expect do
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def hash_complex_contracts(data)
def nested_hash_complex_contracts(data)
end

Contract KeywordArgs[:name => String, :age => Fixnum] => nil
def person_keywordargs(data)
end

Contract [Or[TrueClass, FalseClass]] => nil
def array_complex_contracts(data)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/ruby_version_specific/contracts_spec_2.1.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class GenericExample
Contract String, Bool, Args[Symbol], Float, KeywordArgs[e: Range, f: Optional[Num]], Proc =>
Contract String, Bool, Args[Symbol], Float, KeywordArgs[e: Range, f: Optional[Num], g: Symbol], Proc =>
[Proc, Hash, Maybe[Num], Range, Float, ArrayOf[Symbol], Bool, String]
def complicated(a, b = true, *c, d, e:, f:2, **g, &h)
h.call [h, g, f, e, d, c, b, a]
Expand All @@ -15,7 +15,7 @@ def complicated(a, b = true, *c, d, e:, f:2, **g, &h)
describe "really complicated method signature" do
it "should work with default named args used" do
expect do
@o.complicated("a", false, :b, 2.0, e: (1..5)) { |x| x }
@o.complicated("a", false, :b, 2.0, e: (1..5), g: :d) { |x| x }
end.to_not raise_error
end

Expand Down Expand Up @@ -56,7 +56,7 @@ def complicated(a, b = true, *c, d, e:, f:2, **g, &h)
@o.complicated("a", true, :b, :c, 2.0, e: (1..5), f: nil, g: :d) do |x|
x
end
end.to raise_error(ContractError, /Expected: \(KeywordArgs\[{:e=>Range, :f=>Optional\[Num\]}\]\)/)
end.to raise_error(ContractError, /Expected: \(KeywordArgs\[{:e=>Range, :f=>Optional\[Num\], :g=>Symbol}\]\)/)
end
end
end
Expand Down