| Module | Aquarium::Extras::DesignByContract |
| In: |
lib/aquarium/extras/design_by_contract.rb
|
A simple Design by Contract module. Adds advice to test that the contract, which is specified with a block passes. Note that it doesn‘t attempt to handle the correct behavior under contract inheritance. A usage example is included in the Examples as part of the distribution and it is also shown on the web site. Normally, you want to disable the contracts in production runs, so you avoid the overhead. To do this effectively, call DesignByContract.disable_all before any contracts are created. That will prevent all of the aspects from being created along with their overhead. Warning: This module automatically includes Aquarium::DSL into the class with the contract and it adds the :precondition, :postcondition, and the :invariant methods to Object!
Disable creation of any subsequent contracts and disable execution of existing contracts. That is, while contracts are disabled, it no existing contracts will be executed and any attempts to define new contracts will be ignored.
# File lib/aquarium/extras/design_by_contract.rb, line 36
36: def self.disable_all
37: @@enabled = false
38: end
Enable creation and execution of contracts
# File lib/aquarium/extras/design_by_contract.rb, line 29
29: def self.enable_all
30: @@enabled = true
31: end
# File lib/aquarium/extras/design_by_contract.rb, line 52
52: def invariant *args, &contract_block
53: return unless @@enabled
54: message = handle_message_arg args
55: Aspect.new make_args(:around, *args) do |jp, obj, *params|
56: DesignByContract.test_condition "invariant failure (before invocation): #{message}", jp, obj, *params, &contract_block
57: result = jp.proceed
58: DesignByContract.test_condition "invariant failure (after invocation): #{message}", jp, obj, *params, &contract_block
59: result
60: end
61: end
# File lib/aquarium/extras/design_by_contract.rb, line 46
46: def postcondition *args, &contract_block
47: return unless @@enabled
48: message = handle_message_arg args
49: add_advice :after_returning, "postcondition", message, *args, &contract_block
50: end