Module: Quarry::Grammar::Expect
- Defined in:
- lib/quarry/grammar/expect.rb
Overview
Expect Nomenclature
Provides expect nomenclature. This is Quarry’s “standard” nomenclature.
Instance Method Summary collapse
-
#expect(exp = Expectation, &block) ⇒ Object
The
expect
method is the primary means to define expectations in specifications. -
#expect!(exp = Expectation, &block) ⇒ Object
(also: #expect_not)
Designate a negated expectation.
Instance Method Details
#expect(exp = Expectation, &block) ⇒ Object
The expect
method is the primary means to define expectations in specifications.
Designate an expectation via a functor.
4.expect == 3 #=> Expectation Error
Or in block form.
expect(4){ 3 } #=> Expectation Error
The later compares the expected value and the actual value with broad equality. This is similar to case equality (#===) but also checks other forms of equality. See the #equate method.
Exception classes also check to see if the block raises the error.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/quarry/grammar/expect.rb', line 33 def expect(exp=Expectation, &block) if exp == Expectation Expectation.new(self, :backtrace=>caller) elsif Exception >= exp begin act = block.call test = exp.equate?(act) msg = "#{exp}.equate? #{act}" rescue exp => error test = true #msg = "#{exp} expected to be raised" rescue Exception => error test = false msg = "#{exp} expected but #{error.class} was raised" end raise Assertion.new(msg, caller) unless test else act = block.call test = exp.equate?(act) msg = "#{exp}.equate? #{act}" raise Assertion.new(msg, caller) unless test end end |
#expect!(exp = Expectation, &block) ⇒ Object Also known as: expect_not
Designate a negated expectation. Read this as “expect not”.
4.expect! == 4 #=> Expectation Error
Note that this method would not be necessary if Ruby would allow != to be defined as a method, or perhaps ! as a unary method.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/quarry/grammar/expect.rb', line 66 def expect!(exp=Expectation, &block) if exp == Expectation Expectation.new(self, :negate=>true, :backtrace=>caller) elsif Exception >= exp begin act = block.call test = !exp.equate?(act) msg = "! #{exp}.equate? #{act}" rescue exp => error test = false msg = "#{exp} raised" rescue Exception => error test = true #msg = "#{exp} expected but was #{error.class}" end raise Assertion.new(msg, caller) unless test else act = block.call test = !exp.equate?(act) msg = "! #{exp}.equate? #{act}" raise Assertion.new(msg, caller) unless test end end |