Module: CodeAssertions

Defined in:
lib/code-assertions.rb

Constant Summary collapse

CodeAssertionFailed =
Class.new(Exception)
@@assertions_check =
true

Instance Method Summary collapse

Instance Method Details

#assert(message = nil, &block) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/code-assertions.rb', line 13

def assert(message=nil, &block)
    return unless @@assertions_check
    unless block
        warn "Empty assertion passed."
        return
    end
    
    begin
        message = "Assertion failed: " + block.to_source[6...-1].strip unless message
    rescue Exception
        message = "Assertion failed: <no message provided>"
    end
    raise CodeAssertionFailed, message unless block.call
end

#assertions_offObject Also known as: do_not_assert



7
8
9
# File 'lib/code-assertions.rb', line 7

def assertions_off
    @@assertions_check = false
end

#soft_assert(message = nil, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/code-assertions.rb', line 28

def soft_assert(message=nil, &block)
    return unless @@assertions_check
    unless block
        warn "Empty assertion passed."
        return
    end
    
    begin
        message = "Assertion failed: " + block.to_source[6...-1].strip unless message
    rescue Exception
        message = "Assertion failed: <no message provided>"
    end
    warn message unless block.call
end