Class: RLab::Assert::Assertion
- Inherits:
-
Object
- Object
- RLab::Assert::Assertion
- Includes:
- Observable
- Defined in:
- lib/rlab/assert/assertion.rb
Direct Known Subclasses
Defined Under Namespace
Classes: SubjectThunk
Instance Attribute Summary collapse
-
#fails ⇒ Object
readonly
Returns the value of attribute fails.
-
#passes ⇒ Object
readonly
Returns the value of attribute passes.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #assert ⇒ Object
- #build_checks ⇒ Object
- #failed? ⇒ Boolean
- #file ⇒ Object
- #freeze ⇒ Object
-
#initialize(subject_proc, checks) ⇒ Assertion
constructor
A new instance of Assertion.
- #line ⇒ Object
- #passed? ⇒ Boolean
- #perform_checks ⇒ Object
- #resolve_check(check_name, argument) ⇒ Object
- #subject ⇒ Object
- #subject_thunk ⇒ Object
- #to_error ⇒ Object
- #trace ⇒ Object
Constructor Details
#initialize(subject_proc, checks) ⇒ Assertion
Returns a new instance of Assertion.
12 13 14 15 16 17 18 |
# File 'lib/rlab/assert/assertion.rb', line 12 def initialize subject_proc, checks @subject_proc = subject_proc @checks = checks @passes = [] @fails = [] @trace = caller_locations end |
Instance Attribute Details
#fails ⇒ Object (readonly)
Returns the value of attribute fails.
8 9 10 |
# File 'lib/rlab/assert/assertion.rb', line 8 def fails @fails end |
#passes ⇒ Object (readonly)
Returns the value of attribute passes.
9 10 11 |
# File 'lib/rlab/assert/assertion.rb', line 9 def passes @passes end |
#source ⇒ Object
Returns the value of attribute source.
10 11 12 |
# File 'lib/rlab/assert/assertion.rb', line 10 def source @source end |
Instance Method Details
#assert ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/rlab/assert/assertion.rb', line 20 def assert changed perform_checks freeze raise to_error if failed? subject ensure notify_observers self end |
#build_checks ⇒ Object
30 31 32 33 34 |
# File 'lib/rlab/assert/assertion.rb', line 30 def build_checks @checks.map do |check_name, argument| resolve_check check_name, argument end end |
#failed? ⇒ Boolean
36 37 38 |
# File 'lib/rlab/assert/assertion.rb', line 36 def failed? fails.any? end |
#file ⇒ Object
40 41 42 |
# File 'lib/rlab/assert/assertion.rb', line 40 def file @trace[0].path end |
#freeze ⇒ Object
44 45 46 47 |
# File 'lib/rlab/assert/assertion.rb', line 44 def freeze passes.freeze fails.freeze end |
#line ⇒ Object
49 50 51 |
# File 'lib/rlab/assert/assertion.rb', line 49 def line @trace[0].lineno end |
#passed? ⇒ Boolean
53 54 55 |
# File 'lib/rlab/assert/assertion.rb', line 53 def passed? not failed? end |
#perform_checks ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/rlab/assert/assertion.rb', line 57 def perform_checks build_checks.each do |check| check.evaluate ary = if check.passed? then passes else fails end ary.push check end end |
#resolve_check(check_name, argument) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/rlab/assert/assertion.rb', line 65 def resolve_check check_name, argument check = Checks.resolve check_name do check_name = :include if check_name == :includes argument = [check_name, *argument] Checks[:predicate] end check.new subject_thunk, argument end |
#subject ⇒ Object
74 75 76 |
# File 'lib/rlab/assert/assertion.rb', line 74 def subject subject_thunk.call end |
#subject_thunk ⇒ Object
78 79 80 |
# File 'lib/rlab/assert/assertion.rb', line 78 def subject_thunk @subject_thunk ||= SubjectThunk.new @subject_proc end |
#to_error ⇒ Object
82 83 84 |
# File 'lib/rlab/assert/assertion.rb', line 82 def to_error AssertionFailed.new self end |
#trace ⇒ Object
86 87 88 |
# File 'lib/rlab/assert/assertion.rb', line 86 def trace Assert.filter_trace @trace end |