Skip to content

Instantly share code, notes, and snippets.

@brianc
Created December 2, 2009 21:25
Show Gist options
  • Save brianc/247605 to your computer and use it in GitHub Desktop.
Save brianc/247605 to your computer and use it in GitHub Desktop.
class Assertion < RunnableBlock
def self.pass() [:pass]; end
def self.fail(message) [:fail, message]; end
def self.error(e) [:error, e]; end
def run(situation)
#apply expectation block
@expectings << situation.evaluate(&@expectation_block) if @expectation_block
action = @@assertions[@action]
process_macro(situation,action[0]) { |actual| action[1].call(actual,*@expectings) }
end
@@assertions = Hash.new([false,Proc.new { |actual| actual ? Assertion.pass : Assertion.fail("failed") }])
def self.assertion(name, expect_exception=false, &assertion_block)
@@assertions[name] = [expect_exception,assertion_block]
class_eval <<-END_RUBY
def #{name}(*expectings,&expectation_block)
@action, @expectings, @expectation_block = :#{name}, expectings, expectation_block
self
end
END_RUBY
end
private
def process_macro(situation, expect_exception)
actual = situation.evaluate(&@definition)
yield(expect_exception ? nil : actual)
rescue Exception => e
expect_exception ? yield(e) : Assertion.error(e)
end
end # Assertion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment