spec/contracts_spec.rb in contracts-0.9 vs spec/contracts_spec.rb in contracts-0.10

- old
+ new

@@ -248,11 +248,10 @@ describe "anonymous modules" do let(:mod) do Module.new do include Contracts - include Contracts::Modules Contract String => String def greeting(name) "hello, #{name}" end @@ -402,10 +401,30 @@ it "should handle properly lack of block when there are other arguments" do expect do @o.double_with_proc(4) end.to raise_error(ContractError, /Actual: nil/) end + + it "should succeed for maybe proc with no proc" do + expect do + @o.maybe_call(5) + end.to_not raise_error + end + + it "should succeed for maybe proc with proc" do + expect do + @o.maybe_call(5) do + 2 + 2 + end + end.to_not raise_error + end + + it "should fail for maybe proc with invalid input" do + expect do + @o.maybe_call("bad") + end.to raise_error(ContractError) + end end describe "varargs" do it "should pass for correct input" do expect do @@ -487,10 +506,22 @@ end it "should fail for a function that doesn't pass the contract with weak other args" do expect { @o.map_plain(["hello", "joe"], lambda { |_| nil }) }.to raise_error(ContractError) end + + it "should fail for a returned function that doesn't pass the contract" do + expect { @o.lambda_with_wrong_return.call("hello") }.to raise_error(ContractError) + end + + it "should fail for a returned function that receives the wrong argument type" do + expect { @o.lambda_with_correct_return.call(123) }.to raise_error(ContractError) + end + + it "should not fail for a returned function that passes the contract" do + expect { @o.lambda_with_correct_return.call("hello") }.to_not raise_error + end end describe "default args to functions" do it "should work for a function call that relies on default args" do expect { @o.default_args }.to_not raise_error @@ -531,9 +562,41 @@ it "calls a function for which the contract fails" do res = @o.double("bad") expect(res).to eq("badbad") end + end + end + + describe "module contracts" do + it "passes for instance of class including module" do + expect( + ModuleContractExample.hello(ModuleContractExample::AClassWithModule.new) + ).to eq(:world) + end + + it "passes for instance of class including inherited module" do + expect( + ModuleContractExample.hello(ModuleContractExample::AClassWithInheritedModule.new) + ).to eq(:world) + end + + it "does not pass for instance of class not including module" do + expect do + ModuleContractExample.hello(ModuleContractExample::AClassWithoutModule.new) + end.to raise_error(ContractError, /Expected: ModuleContractExample::AModule/) + end + + it "does not pass for instance of class including another module" do + expect do + ModuleContractExample.hello(ModuleContractExample::AClassWithAnotherModule.new) + end.to raise_error(ContractError, /Expected: ModuleContractExample::AModule/) + end + + it "passes for instance of class including both modules" do + expect( + ModuleContractExample.hello(ModuleContractExample::AClassWithBothModules.new) + ).to eq(:world) end end describe "Contracts to_s formatting in expected" do def not_s(match)