Module: AE::Expect
- Included in:
- Object
- Defined in:
- lib/ae/expect.rb
Overview
Expect
"When love and skill work together, expect a masterpiece."
--John Ruskin (1819 - 1900)
Instance Method Summary collapse
-
#expect(*args, &block) ⇒ Object
The #expect method is a convenient tool for defining certain sets of expectations in your specifications.
-
#expect!(exp = NoArgument, &block) ⇒ Object
(also: #forbid)
Designate a negated expectation.
-
#expected(*args, &block) ⇒ Object
Like #expect but uses the reciever as the object of expectation.
Instance Method Details
#expect(*args, &block) ⇒ Object
The #expect method is a convenient tool for defining certain sets of expectations in your specifications.
Expect is used to expect a result from a block of code. If the argument to expect is a subclass of Exception or instance thereof, then the block is monitored for the raising of such an exception.
expect StandardError do
raise ArgumentError
end
All other expectations are compared using case equality (#===). This allows one to verify matching Regexp.
expect /x/ do
"x"
end
As well as checking that an object is an instance of a given Class.
expect String do
"x"
end
Like #assert it can be used to designate an expectation via a functor.
4.expect == 3
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ae/expect.rb', line 43 def expect(*args, &block) return Assertor.new(self, :backtrace=>caller) if args.empty? && !block block = args.shift if !block_given? && Proc === args.first if block exp = args.empty? ? self : args.shift if Exception === exp || (Class===exp && exp.ancestors.include?(Exception)) begin block.call pass = false msg = "#{exp} not raised" rescue exp => error pass = true rescue Exception => error pass = false msg = "#{exp} expected but #{error.class} was raised" end else res = block.call pass = (exp === res) msg = "#{exp} === #{res}" end else pass = (exp === self) msg = "#{exp} === #{self}" end flunk(msg, caller) unless pass end |
#expect!(exp = NoArgument, &block) ⇒ Object Also known as: forbid
Designate a negated expectation. Read this as “expect not”.
See #expect.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ae/expect.rb', line 75 def expect!(exp=NoArgument, &block) return Assertor.new(self, :backtrace=>caller) if args.empty? && !block block = args.shift if !block_given? && Proc === args.first if block exp = args.empty? ? self : args.shift if Exception === exp || (Class===exp && exp.is?(Exception)) begin block.call pass = true rescue exp => error pass = false msg = "#{exp} raised" rescue Exception => error pass = true #msg = "#{exp} expected but #{error.class} was raised" end else res = block.call pass = !(exp === res) msg = "not #{exp} === #{res}" end else pass = !(exp === self) msg = "not #{exp} === #{self}" end flunk(msg, caller) unless pass end |
#expected(*args, &block) ⇒ Object
Like #expect but uses the reciever as the object of expectation.
/x/.expected do
"oooxooo"
end
113 114 115 |
# File 'lib/ae/expect.rb', line 113 def expected(*args, &block) expect(self, *args, &block) end |