Skip to content

Commit adb4348

Browse files
committed
Fix Pos,Neg contracts which fail incorrectly for nil values
1 parent da13531 commit adb4348

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/contracts/builtin_contracts.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ def self.valid? val
2929
# Check that an argument is a positive number.
3030
class Pos
3131
def self.valid? val
32-
val > 0
32+
val && val > 0
3333
end
3434
end
3535

3636
# Check that an argument is a negative number.
3737
class Neg
3838
def self.valid? val
39-
val < 0
39+
val && val < 0
4040
end
4141
end
4242

spec/builtin_contracts_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
it "should fail for negative numbers" do
3030
expect { @o.pos_test(-1) }.to raise_error(ContractError)
3131
end
32+
33+
it "should fail for nil" do
34+
expect { @o.pos_test(nil) }.to raise_error(ContractError)
35+
end
3236
end
3337

3438
describe "Neg:" do
@@ -43,6 +47,10 @@
4347
it "should fail for positive numbers" do
4448
expect { @o.neg_test(1) }.to raise_error(ContractError)
4549
end
50+
51+
it "should fail for nil" do
52+
expect { @o.neg_test(nil) }.to raise_error(ContractError)
53+
end
4654
end
4755

4856
describe "Nat:" do
@@ -61,6 +69,10 @@
6169
it "should fail for negative numbers" do
6270
expect { @o.nat_test(-1) }.to raise_error(ContractError)
6371
end
72+
73+
it "should fail for nil" do
74+
expect { @o.nat_test(nil) }.to raise_error(ContractError)
75+
end
6476
end
6577

6678
describe "Any:" do

0 commit comments

Comments
 (0)