Module: CodeAssertions
- Defined in:
- lib/code-assertions.rb
Defined Under Namespace
Classes: CodeAssertionFailed
Constant Summary
collapse
- @@assertions_check =
true
Instance Method Summary
collapse
Instance Method Details
#assert(message = nil, &block) ⇒ Object
13
14
15
16
17
18
19
20
21
|
# File 'lib/code-assertions.rb', line 13
def assert(message=nil, &block)
return unless @@assertions_check
unless block
warn "Empty assertion passed."
return
end
message = "Assertion failed: " + block.to_source[6...-1].strip unless message
raise CodeAssertionFailed, message unless block.call
end
|
#assert_not_nil(object) ⇒ Object
Also known as:
assert_nn
23
24
25
|
# File 'lib/code-assertions.rb', line 23
def assert_not_nil(object)
assert("The result cannot be `nil` or `false`. #{object} given") { object }
end
|
#assert_type(type, object) ⇒ Object
28
29
30
31
|
# File 'lib/code-assertions.rb', line 28
def assert_type(type, object)
assert("`type` should be an instance of `Class`") { type.is_a?(Class) }
assert("Wrong type: expected #{type.name}, given #{object.class.name}") { object.is_a?(type) }
end
|
#assertions_off ⇒ Object
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
33
34
35
36
37
38
39
40
41
|
# File 'lib/code-assertions.rb', line 33
def soft_assert(message=nil, &block)
return unless @@assertions_check
unless block
warn "Empty assertion passed."
return
end
message = "Assertion failed: " + block.to_source[6...-1].strip unless message
warn message unless block.call
end
|