Module: Expectation
- Defined in:
- lib/expectation.rb,
lib/expectation.rb,
lib/expectation/version.rb
Overview
–
- Author
-
radiospiel ([email protected])
- Copyright
-
Copyright © 2011, 2012 radiospiel
- License
-
Distributes under the terms of the Modified BSD License, see LICENSE.BSD for details.
Defined Under Namespace
Modules: Assertions Classes: Error
Constant Summary collapse
- VERSION =
The expectation gem’s version.
"1.0.0"
Instance Method Summary collapse
-
#expect!(*expectations, &block) ⇒ Object
Verifies a number of expectations.
-
#match!(value, expectation, key = nil) ⇒ Object
Matches a value against an expectation.
-
#match?(value, expectation) ⇒ Boolean
Does a value match an expectation?.
Instance Method Details
#expect!(*expectations, &block) ⇒ Object
Verifies a number of expectations. If one or more expectations are not met it raises an ArgumentError (on the first failing expectation).
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/expectation.rb', line 51 def expect!(*expectations, &block) expectations.each do |expectation| if expectation.is_a?(Hash) expectation.all? do |actual, exp| match! actual, exp end else match! expectation, :truish end end match! block, :__block if block rescue Error $!.reraise_with_current_backtrace! end |
#match!(value, expectation, key = nil) ⇒ Object
Matches a value against an expectation. Raises an Expectation::Error if the expectation could not be matched.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/expectation.rb', line 78 def match!(value, expectation, key=nil) match = case expectation when :truish then !!value when :fail then false when Array then expectation.any? { |e| _match?(value, e) } when Proc then expectation.arity == 0 ? expectation.call : expectation.call(value) when Regexp then value.is_a?(String) && expectation =~ value when :__block then value.call when Hash then Hash === value && expectation.each { |key, exp| match! value[key], exp, key } else expectation === value end return if match raise Error.new(value, expectation, key && "at key #{key.inspect}") end |
#match?(value, expectation) ⇒ Boolean
Does a value match an expectation?
69 70 71 72 73 74 |
# File 'lib/expectation.rb', line 69 def match?(value, expectation) match! value, expectation true rescue Error false end |