diff --git a/lib/contracts/builtin_contracts.rb b/lib/contracts/builtin_contracts.rb index 0164b22..9c60a18 100644 --- a/lib/contracts/builtin_contracts.rb +++ b/lib/contracts/builtin_contracts.rb @@ -29,14 +29,14 @@ def self.valid? val # Check that an argument is a positive number. class Pos def self.valid? val - val > 0 + val && val > 0 end end # Check that an argument is a negative number. class Neg def self.valid? val - val < 0 + val && val < 0 end end diff --git a/spec/builtin_contracts_spec.rb b/spec/builtin_contracts_spec.rb index 9987506..01d9379 100644 --- a/spec/builtin_contracts_spec.rb +++ b/spec/builtin_contracts_spec.rb @@ -29,6 +29,10 @@ it "should fail for negative numbers" do expect { @o.pos_test(-1) }.to raise_error(ContractError) end + + it "should fail for nil" do + expect { @o.pos_test(nil) }.to raise_error(ContractError) + end end describe "Neg:" do @@ -43,6 +47,10 @@ it "should fail for positive numbers" do expect { @o.neg_test(1) }.to raise_error(ContractError) end + + it "should fail for nil" do + expect { @o.neg_test(nil) }.to raise_error(ContractError) + end end describe "Nat:" do @@ -61,6 +69,10 @@ it "should fail for negative numbers" do expect { @o.nat_test(-1) }.to raise_error(ContractError) end + + it "should fail for nil" do + expect { @o.nat_test(nil) }.to raise_error(ContractError) + end end describe "Any:" do