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

- old
+ new

@@ -13,10 +13,31 @@ it "should fail for insufficient contracts" do expect { @o.bad_double(2) }.to raise_error(ContractError) 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 + end + + it "should still work for old-style contracts for functions with no args" do + expect { @o.old_style_no_args }.to_not raise_error + end + + it "should not work for a function with a bad contract" do + expect do + Class.new(GenericExample) do + Contract Num, Num + def no_args_bad_contract + 1 + end + end + end.to raise_error + end + end + describe "pattern matching" do let(:string_with_hello) { "Hello, world" } let(:string_without_hello) { "Hi, world" } let(:expected_decorated_string) { "Hello, world!" } subject { PatternMatchingExample.new } @@ -52,10 +73,32 @@ expect( subject.do_stuff(3, "def") ).to eq("foo") end + it "if the return contract for a pattern match fails, it should fail instead of trying the next pattern match" do + expect do + subject.double(1) + end.to raise_error(ContractError) + end + + it "should fail if multiple methods are defined with the same contract (for pattern-matching)" do + expect do + Class.new(GenericExample) do + Contract Contracts::Num => Contracts::Num + def same_param_contract x + x + 2 + end + + Contract Contracts::Num => String + def same_param_contract x + "sdf" + end + end + end.to raise_error(ContractError) + end + context "when failure_callback was overriden" do before do ::Contract.override_failure_callback do |_data| fail "contract violation" end @@ -71,10 +114,16 @@ expect( subject.process_request(PatternMatchingExample::Failure.new) ).to be_a(PatternMatchingExample::Failure) end + it "if the return contract for a pattern match fails, it should fail instead of trying the next pattern match, even with the failure callback" do + expect do + subject.double(1) + end.to raise_error(ContractError) + end + it "uses overriden failure_callback when pattern matching fails" do expect do subject.process_request("hello") end.to raise_error(RuntimeError, /contract violation/) end @@ -269,13 +318,9 @@ end it "should fail for incorrect input" do expect { GenericExample.a_class_method("bad") }.to raise_error(ContractError) end - end - - it "should work for functions with no args" do - expect { @o.no_args }.to_not raise_error end describe "classes" do it "should pass for correct input" do expect { @o.hello("calvin") }.to_not raise_error