Guest User

Untitled

a guest
Apr 25th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. class Assertion < RunnableBlock
  2. def self.pass() [:pass]; end
  3. def self.fail(message) [:fail, message]; end
  4. def self.error(e) [:error, e]; end
  5. def run(situation)
  6. #apply expectation block
  7. @expectings << situation.evaluate(&@expectation_block) if @expectation_block
  8. action = @@assertions[@action]
  9. process_macro(situation,action[0]) { |actual| action[1].call(actual,*@expectings) }
  10. end
  11.  
  12. @@assertions = Hash.new([false,Proc.new { |actual| actual ? Assertion.pass : Assertion.fail("failed") }])
  13. def self.assertion(name, expect_exception=false, &assertion_block)
  14. @@assertions[name] = [expect_exception,assertion_block]
  15. class_eval <<-END_RUBY
  16. def #{name}(*expectings,&expectation_block)
  17. @action, @expectings, @expectation_block = :#{name}, expectings, expectation_block
  18. self
  19. end
  20. END_RUBY
  21. end
  22.  
  23. private
  24. def process_macro(situation, expect_exception)
  25. actual = situation.evaluate(&@definition)
  26. yield(expect_exception ? nil : actual)
  27. rescue Exception => e
  28. expect_exception ? yield(e) : Assertion.error(e)
  29. end
  30. end # Assertion
Add Comment
Please, Sign In to add comment