Skip to content

Commit ebbc767

Browse files
committed
Merge pull request #173 from gsinclair/fix_165
Fixed #165: "Bug: comparison of Symbol with 0 failed"
2 parents ecb4736 + 54e60ae commit ebbc767

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

lib/contracts/builtin_contracts.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ def self.valid? val
2929
# Check that an argument is a positive number.
3030
class Pos
3131
def self.valid? val
32-
val && val > 0
32+
val && val.is_a?(Numeric) && 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 && val < 0
39+
val && val.is_a?(Numeric) && val < 0
4040
end
4141
end
4242

4343
# Check that an argument is a natural number.
4444
class Nat
4545
def self.valid? val
46-
val && val >= 0 && val.integer?
46+
val && val.is_a?(Integer) && val >= 0
4747
end
4848
end
4949

spec/builtin_contracts_spec.rb

+22-5
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212
expect { @o.double(2.2) }.to_not raise_error
1313
end
1414

15-
it "should fail for Strings" do
16-
expect { @o.double("bad") }.to raise_error(ContractError)
15+
it "should fail for nil and other data types" do
16+
expect { @o.double(nil) }.to raise_error(ContractError)
17+
expect { @o.double(:x) }.to raise_error(ContractError)
18+
expect { @o.double("x") }.to raise_error(ContractError)
19+
expect { @o.double(/x/) }.to raise_error(ContractError)
1720
end
1821
end
1922

2023
describe "Pos:" do
2124
it "should pass for positive numbers" do
2225
expect { @o.pos_test(1) }.to_not raise_error
26+
expect { @o.pos_test(1.6) }.to_not raise_error
2327
end
2428

2529
it "should fail for 0" do
@@ -28,16 +32,21 @@
2832

2933
it "should fail for negative numbers" do
3034
expect { @o.pos_test(-1) }.to raise_error(ContractError)
35+
expect { @o.pos_test(-1.6) }.to raise_error(ContractError)
3136
end
3237

33-
it "should fail for nil" do
38+
it "should fail for nil and other data types" do
3439
expect { @o.pos_test(nil) }.to raise_error(ContractError)
40+
expect { @o.pos_test(:x) }.to raise_error(ContractError)
41+
expect { @o.pos_test("x") }.to raise_error(ContractError)
42+
expect { @o.pos_test(/x/) }.to raise_error(ContractError)
3543
end
3644
end
3745

3846
describe "Neg:" do
3947
it "should pass for negative numbers" do
4048
expect { @o.neg_test(-1) }.to_not raise_error
49+
expect { @o.neg_test(-1.6) }.to_not raise_error
4150
end
4251

4352
it "should fail for 0" do
@@ -46,10 +55,14 @@
4655

4756
it "should fail for positive numbers" do
4857
expect { @o.neg_test(1) }.to raise_error(ContractError)
58+
expect { @o.neg_test(1.6) }.to raise_error(ContractError)
4959
end
5060

51-
it "should fail for nil" do
61+
it "should fail for nil and other data types" do
5262
expect { @o.neg_test(nil) }.to raise_error(ContractError)
63+
expect { @o.neg_test(:x) }.to raise_error(ContractError)
64+
expect { @o.neg_test("x") }.to raise_error(ContractError)
65+
expect { @o.neg_test(/x/) }.to raise_error(ContractError)
5366
end
5467
end
5568

@@ -68,10 +81,14 @@
6881

6982
it "should fail for negative numbers" do
7083
expect { @o.nat_test(-1) }.to raise_error(ContractError)
84+
expect { @o.nat_test(-1.6) }.to raise_error(ContractError)
7185
end
7286

73-
it "should fail for nil" do
87+
it "should fail for nil and other data types" do
7488
expect { @o.nat_test(nil) }.to raise_error(ContractError)
89+
expect { @o.nat_test(:x) }.to raise_error(ContractError)
90+
expect { @o.nat_test("x") }.to raise_error(ContractError)
91+
expect { @o.nat_test(/x/) }.to raise_error(ContractError)
7592
end
7693
end
7794

0 commit comments

Comments
 (0)