spec/contracts_spec.rb in contracts-lite-0.14.0 vs spec/contracts_spec.rb in contracts-lite-0.15.0
- old
+ new
@@ -5,16 +5,27 @@
describe "basic" do
it "should fail for insufficient arguments" do
expect do
@o.hello
- end.to raise_error
+ end.to raise_error(ArgumentError)
end
it "should fail for insufficient contracts" do
expect { @o.bad_double(2) }.to raise_error(ContractError)
end
+
+ it "requires last argument to be a hash for more than one contracts" do
+ expect{
+ Class.new(GenericExample) do
+ Contract C::Num, C::Num
+ def no_args_bad_contract(num)
+ 1
+ end
+ end
+ }.to raise_error(RuntimeError, Regexp.new("A contract should be written as"))
+ end
end
describe "contracts for functions with no arguments" do
it "should work for functions with no args" do
expect { @o.no_args }.to_not raise_error
@@ -30,11 +41,11 @@
Contract Num, Num
def no_args_bad_contract
1
end
end
- end.to raise_error
+ end.to raise_error(NameError)
end
end
describe "pattern matching" do
let(:string_with_hello) { "Hello, world" }
@@ -460,13 +471,21 @@
expect do
@o.with_partial_sums(1, 2, 3)
end.to raise_error(ContractError, /Actual: nil/)
- expect do
- @o.with_partial_sums(1, 2, 3, lambda { |x| x })
- end.to raise_error(ContractError, /Actual: nil/)
+ to_call = lambda {
+ expect do
+ # because lambda is not a valid &block, it is considered to belong to the splat argument, and raises for num contract!
+ @o.with_partial_sums(1, 2, 3, lambda { |x| x })
+ end
+ }
+ to_call.call.to raise_error(ContractError, /Contract violation for argument 4 of 5/)
+ to_call.call.to raise_error(ContractError, /Actual: #<Proc/)
+ to_call.call.to raise_error(ContractError, Regexp.new("Expected: \\(SplatArgs\\[Contracts::Builtin::Num\\]\\)"))
+
+
end
context "when block has Func contract" do
it "should fail for incorrect input" do
expect do
@@ -694,10 +713,10 @@
describe "inherited methods" do
it "should apply the contract to an inherited method" do
c = Child.new
expect { c.double(2) }.to_not raise_error
- expect { c.double("asd") }.to raise_error
+ expect { c.double("asd") }.to raise_error(ParamContractError)
end
end
describe "classes with extended modules" do
let(:klass) do