Class: RLab::Assert::Assertion

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/rlab/assert/assertion.rb

Direct Known Subclasses

Refutation

Defined Under Namespace

Classes: SubjectThunk

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#failsObject (readonly)

Returns the value of attribute fails.



8
9
10
# File 'lib/rlab/assert/assertion.rb', line 8

def fails
  @fails
end

#passesObject (readonly)

Returns the value of attribute passes.



9
10
11
# File 'lib/rlab/assert/assertion.rb', line 9

def passes
  @passes
end

#sourceObject

Returns the value of attribute source.



10
11
12
# File 'lib/rlab/assert/assertion.rb', line 10

def source
  @source
end

Instance Method Details

#assertObject



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_checksObject



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

Returns:

  • (Boolean)


36
37
38
# File 'lib/rlab/assert/assertion.rb', line 36

def failed?
  fails.any?
end

#fileObject



40
41
42
# File 'lib/rlab/assert/assertion.rb', line 40

def file
  @trace[0].path
end

#freezeObject



44
45
46
47
# File 'lib/rlab/assert/assertion.rb', line 44

def freeze
  passes.freeze
  fails.freeze
end

#lineObject



49
50
51
# File 'lib/rlab/assert/assertion.rb', line 49

def line
  @trace[0].lineno
end

#passed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rlab/assert/assertion.rb', line 53

def passed?
  not failed?
end

#perform_checksObject



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

#subjectObject



74
75
76
# File 'lib/rlab/assert/assertion.rb', line 74

def subject
  subject_thunk.call
end

#subject_thunkObject



78
79
80
# File 'lib/rlab/assert/assertion.rb', line 78

def subject_thunk
  @subject_thunk ||= SubjectThunk.new @subject_proc
end

#to_errorObject



82
83
84
# File 'lib/rlab/assert/assertion.rb', line 82

def to_error
  AssertionFailed.new self
end

#traceObject



86
87
88
# File 'lib/rlab/assert/assertion.rb', line 86

def trace
  Assert.filter_trace @trace
end