Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spec behavior of mixing default values with blocks #214

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Spec behavior of mixing default values with blocks #214

wants to merge 2 commits into from

Conversation

rfdonnelly
Copy link

Default values and blocks don't mix well. The specs added here show this.

I have added specs for both nil default values and non-nil default values because they fail differently in the should fail for lack of block when default used case.

Output from bundle exec rake spec:

  1) Contracts: blocks and non-nil default values should succeed when default used and block given                                                                                                                                                                                 [78/1951]
     Failure/Error:
       expect do
         @o.default_with_block() {}
       end.to_not raise_error

       expected no Exception, got #<ParamContractError: Contract violation for argument 1 of 1:
               Expected: Symbol,
               Actual: #<Proc:0x00000601359560@/home/rfdonnelly/repos/contracts.ruby/spec/contracts_spec.rb:430>
               Value guarded in: GenericExample::default_with_block
               With Contract: Symbol, Proc => NilClass
               At: /home/rfdonnelly/repos/contracts.ruby/spec/fixtures/fixtures.rb:199 > with backtrace:
         # ./lib/contracts.rb:48:in `block in <class:Contract>'
         # ./lib/contracts.rb:154:in `call'
         # ./lib/contracts.rb:154:in `failure_callback'
         # ./lib/contracts/method_handler.rb:143:in `rescue in block in redefine_method'
         # ./lib/contracts/method_handler.rb:136:in `block in redefine_method'
         # ./spec/contracts_spec.rb:430:in `block (5 levels) in <top (required)>'
         # ./spec/contracts_spec.rb:429:in `block (4 levels) in <top (required)>'
     # ./spec/contracts_spec.rb:429:in `block (4 levels) in <top (required)>'

  2) Contracts: blocks and non-nil default values should fail for lack of block when default used
     Failure/Error:
       expect do
         @o.default_with_block
       end.to raise_error(ContractError, /Expected: Proc/)

       expected ContractError with message matching /Expected: Proc/, got #<ParamContractError: Contract violation for argument 1 of 1:
               Expected: Symbol,
               Actual: nil
               Value guarded in: GenericExample::default_with_block
               With Contract: Symbol, Proc => NilClass
               At: /home/rfdonnelly/repos/contracts.ruby/spec/fixtures/fixtures.rb:199 > with backtrace:
         # ./lib/contracts.rb:48:in `block in <class:Contract>'
         # ./lib/contracts.rb:154:in `call'
         # ./lib/contracts.rb:154:in `failure_callback'
         # ./lib/contracts/method_handler.rb:143:in `rescue in block in redefine_method'
         # ./lib/contracts/method_handler.rb:136:in `block in redefine_method'
         # ./spec/contracts_spec.rb:418:in `block (5 levels) in <top (required)>'
         # ./spec/contracts_spec.rb:417:in `block (4 levels) in <top (required)>'
     # ./spec/contracts_spec.rb:417:in `block (4 levels) in <top (required)>'

  3) Contracts: blocks and nil default values should fail for lack of block when default used
     Failure/Error:
       expect do
         @o.default_nil_with_block
       end.to raise_error(ContractError, /Expected: Proc/)

       expected ContractError with message matching /Expected: Proc/ but nothing was raised
     # ./spec/contracts_spec.rb:391:in `block (4 levels) in <top (required)>'

  4) Contracts: blocks and nil default values should succeed when default used and block given
     Failure/Error:
       expect do
         @o.default_nil_with_block() {}
       end.to_not raise_error

       expected no Exception, got #<ParamContractError: Contract violation for argument 1 of 1:
               Expected: (Symbol or nil),
               Actual: #<Proc:0x00000600f9e380@/home/rfdonnelly/repos/contracts.ruby/spec/contracts_spec.rb:404>
               Value guarded in: GenericExample::default_nil_with_block
               With Contract: Maybe, Proc => NilClass
               At: /home/rfdonnelly/repos/contracts.ruby/spec/fixtures/fixtures.rb:195 > with backtrace:
         # ./lib/contracts.rb:48:in `block in <class:Contract>'
         # ./lib/contracts.rb:154:in `call'
         # ./lib/contracts.rb:154:in `failure_callback'
         # ./lib/contracts/method_handler.rb:143:in `rescue in block in redefine_method'
         # ./lib/contracts/method_handler.rb:136:in `block in redefine_method'
         # ./spec/contracts_spec.rb:404:in `block (5 levels) in <top (required)>'
         # ./spec/contracts_spec.rb:403:in `block (4 levels) in <top (required)>'
     # ./spec/contracts_spec.rb:403:in `block (4 levels) in <top (required)>'

@waterlink
Copy link
Collaborator

@rfdonnelly Thanks for the specs!

I will take a look into this soon.

rfdonnelly added a commit to rfdonnelly/jobrnr that referenced this pull request Feb 3, 2016
Tried [contracts.ruby](https://github.com/egonSchiele/contracts.ruby)
but does not allow mixing of default values and blocks
[contracts.ruby/214](egonSchiele/contracts.ruby#214).
Additionally, contracts.ruby gives developer friendly messages but does
not give user friendly messages.

Instead, rolled by own argument validation.  Error messages include
expected type, actual value, actual type, and source location.

Addresses part of issue #5.  Need validation on remaining DSL statements
to close.
@indigoviolet
Copy link
Contributor

@waterlink did this problem ever get fixed?

Copy link

@aussieb81 aussieb81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants