Module: Yadriggy::Assert

Defined in:
lib/yadriggy/assert.rb

Overview

Power assert by Yadriggy

Defined Under Namespace

Classes: AssertFailure, Reason

Class Method Summary collapse

Class Method Details

.assert(&block) ⇒ Object

Checks the given assertion is correct and prints the result if the assertion fails.

Parameters:

  • block (Proc)

    the assertion.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/yadriggy/assert.rb', line 13

def self.assert(&block)
  reason = Reason.new
  begin
    res = assertion(reason, block)
    puts_reason(reason) unless res
    return res
  rescue AssertFailure => evar
    puts_reason(evar.reason, evar)
    raise evar.cause
  end
end

.assertion(reason, block) ⇒ Object

Checks the given assertion is correct.

Parameters:

  • reason (Reason)

    the object where the reason that the assertion fails will be stored.

  • block (Proc)

    the assertion.

Returns:

  • (Object)

    the result of executing the given block.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/yadriggy/assert.rb', line 41

def self.assertion(reason, block)
  return if block.nil?
  ast = Yadriggy::reify(block)
  begin
    results = {}
    reason.setup(ast.tree.body, results)
    run_ast(ast.tree.body, block.binding, results)[1]
  rescue => evar
    raise AssertFailure.new(reason, evar.message, evar)
  end
end