From c9e5ae96d134dbf40cfec69a9ab0363e0e769688 Mon Sep 17 00:00:00 2001 From: Rob Donnelly Date: Sun, 20 Dec 2015 15:38:18 -0800 Subject: [PATCH 1/2] add spec for default value with block The specs where default is used fail. --- spec/contracts_spec.rb | 24 ++++++++++++++++++++++++ spec/fixtures/fixtures.rb | 4 ++++ 2 files changed, 28 insertions(+) diff --git a/spec/contracts_spec.rb b/spec/contracts_spec.rb index 525588f..e8fcb79 100644 --- a/spec/contracts_spec.rb +++ b/spec/contracts_spec.rb @@ -386,6 +386,30 @@ def self.greeting(name) end.to raise_error(ContractError, /Actual: nil/) end + it "should fail for lack of block when default used" do + expect do + @o.default_with_block + end.to raise_error(ContractError, /Actual: nil/) + end + + it "should fail for lack of block when default overidden" do + expect do + @o.default_with_block(:arg) + end.to raise_error(ContractError, /Actual: nil/) + end + + it "should succeed when default used and block given" do + expect do + @o.default_with_block() {} + end.to_not raise_error + end + + it "should succeed when default overidden and block given" do + expect do + @o.default_with_block(:arg) {} + end.to_not raise_error + end + it "should succeed for maybe proc with no proc" do expect do @o.maybe_call(5) diff --git a/spec/fixtures/fixtures.rb b/spec/fixtures/fixtures.rb index 063c3ed..ee908b7 100644 --- a/spec/fixtures/fixtures.rb +++ b/spec/fixtures/fixtures.rb @@ -191,6 +191,10 @@ def double_with_proc(x, &blk) nil end + Contract C::Maybe[Symbol], Proc => nil + def default_with_block(param = nil, &block) + end + Contract C::Pos => nil def pos_test(x) end From f240bb047cf4aca26d03e184345b04e9280536b6 Mon Sep 17 00:00:00 2001 From: Rob Donnelly Date: Sun, 20 Dec 2015 16:03:51 -0800 Subject: [PATCH 2/2] add non-nil default value specs --- spec/contracts_spec.rb | 67 ++++++++++++++++++++++++++++----------- spec/fixtures/fixtures.rb | 6 +++- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/spec/contracts_spec.rb b/spec/contracts_spec.rb index e8fcb79..1fc7647 100644 --- a/spec/contracts_spec.rb +++ b/spec/contracts_spec.rb @@ -383,33 +383,62 @@ def self.greeting(name) 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.to raise_error(ContractError, /Expected: Proc/) end - it "should fail for lack of block when default used" do - expect do - @o.default_with_block - end.to raise_error(ContractError, /Actual: nil/) - end + describe "and nil default values" do + it "should fail for lack of block when default used" do + expect do + @o.default_nil_with_block + end.to raise_error(ContractError, /Expected: Proc/) + end - it "should fail for lack of block when default overidden" do - expect do - @o.default_with_block(:arg) - end.to raise_error(ContractError, /Actual: nil/) - end + it "should fail for lack of block when default overidden" do + expect do + @o.default_nil_with_block(:arg) + end.to raise_error(ContractError, /Expected: Proc/) + end - it "should succeed when default used and block given" do - expect do - @o.default_with_block() {} - end.to_not raise_error + it "should succeed when default used and block given" do + expect do + @o.default_nil_with_block() {} + end.to_not raise_error + end + + it "should succeed when default overidden and block given" do + expect do + @o.default_nil_with_block(:arg) {} + end.to_not raise_error + end end - it "should succeed when default overidden and block given" do - expect do - @o.default_with_block(:arg) {} - end.to_not raise_error + describe "and non-nil default values" do + it "should fail for lack of block when default used" do + expect do + @o.default_with_block + end.to raise_error(ContractError, /Expected: Proc/) + end + + it "should fail for lack of block when default overidden" do + expect do + @o.default_with_block(:arg) + end.to raise_error(ContractError, /Actual: nil/) + end + + it "should succeed when default used and block given" do + expect do + @o.default_with_block() {} + end.to_not raise_error + end + + it "should succeed when default overidden and block given" do + expect do + @o.default_with_block(:arg) {} + end.to_not raise_error + end end + it "should succeed for maybe proc with no proc" do expect do @o.maybe_call(5) diff --git a/spec/fixtures/fixtures.rb b/spec/fixtures/fixtures.rb index ee908b7..1036dae 100644 --- a/spec/fixtures/fixtures.rb +++ b/spec/fixtures/fixtures.rb @@ -192,7 +192,11 @@ def double_with_proc(x, &blk) end Contract C::Maybe[Symbol], Proc => nil - def default_with_block(param = nil, &block) + def default_nil_with_block(param = nil, &block) + end + + Contract Symbol, Proc => nil + def default_with_block(param = :default, &block) end Contract C::Pos => nil