Class: Spectre::Assertion::Evaluation

Inherits:
Object
  • Object
show all
Defined in:
lib/spectre/assertion.rb

Constant Summary collapse

@@location_cache =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(call_location, actual, expected, method, predicate, negate: false) ⇒ Evaluation

Returns a new instance of Evaluation.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/spectre/assertion.rb', line 66

def initialize call_location, actual, expected, method, predicate, negate: false
  @call_location = call_location
  @actual = actual
  @expected = ValueWrapper.wrap(expected)
  @predicate = predicate
  @negate = negate
  @failure = nil

  # Maybe not the most elegant way, but it works for now
  # as long as the `.to` call is on the same line as the variable
  location = call_location
    .find { |x| x.label.include? 'Spectre::Engine#load_files' or x.base_label == '<top (required)>' }

  path = location.path

  if @@location_cache.key?(path)
    file_content = @@location_cache[path]
  else
    file_content = File.read(path)
    @@location_cache[path] = file_content
  end

  @var_name = file_content
    .lines[location.lineno - 1]
    .strip
    .match(/[\s(]([^\s]+|\[.*\]|{.*})\.(to|not_to)[\s(]/)
    .captures
    .first
    .strip

  @repr = @var_name
  @repr += ' not' if @negate
  @repr += " to #{method}"
  @repr += expected.nil? ? ' empty' : " #{@expected}"

  success = @expected.evaluate?(@predicate, @actual, @negate)

  return if success

  @failure = if @negate
               'it does not'
             else
               "got #{@actual.inspect}"
             end
end

Instance Attribute Details

#actualObject (readonly)

Returns the value of attribute actual.



64
65
66
# File 'lib/spectre/assertion.rb', line 64

def actual
  @actual
end

#call_locationObject (readonly)

Returns the value of attribute call_location.



64
65
66
# File 'lib/spectre/assertion.rb', line 64

def call_location
  @call_location
end

#descObject (readonly)

Returns the value of attribute desc.



64
65
66
# File 'lib/spectre/assertion.rb', line 64

def desc
  @desc
end

#expectedObject (readonly)

Returns the value of attribute expected.



64
65
66
# File 'lib/spectre/assertion.rb', line 64

def expected
  @expected
end

#failureObject (readonly)

Returns the value of attribute failure.



64
65
66
# File 'lib/spectre/assertion.rb', line 64

def failure
  @failure
end

#methodObject (readonly)

Returns the value of attribute method.



64
65
66
# File 'lib/spectre/assertion.rb', line 64

def method
  @method
end

#negateObject (readonly)

Returns the value of attribute negate.



64
65
66
# File 'lib/spectre/assertion.rb', line 64

def negate
  @negate
end

Instance Method Details

#to_sObject

:nodoc:



113
114
115
# File 'lib/spectre/assertion.rb', line 113

def to_s
  @repr
end