Module: RSpec::EnrichedJson::HandlerWrapperShared

Included in:
NegativeHandlerWrapper, PositiveHandlerWrapper
Defined in:
lib/rspec/enriched_json/expectation_helper_wrapper.rb

Instance Method Summary collapse

Instance Method Details

#capture_test_values(actual, initial_matcher, negated: false) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rspec/enriched_json/expectation_helper_wrapper.rb', line 139

def capture_test_values(actual, initial_matcher, negated: false)
  return unless initial_matcher && RSpec.current_example

  begin
    if initial_matcher.is_a?(RSpec::Matchers::BuiltIn::BePredicate) || initial_matcher.is_a?(RSpec::Matchers::BuiltIn::Has)
      expected_value = !negated

      if negated && initial_matcher.respond_to?(:does_not_match?)
        initial_matcher.does_not_match?(actual)
      else
        initial_matcher.matches?(actual)
      end

      actual_value = if initial_matcher.instance_variable_defined?(:@predicate_result)
        initial_matcher.instance_variable_get(:@predicate_result)
      end
    else
      expected_value = initial_matcher.respond_to?(:expected) ? initial_matcher.expected : nil
      actual_value = actual
    end

    key = RSpec.current_example.id
    RSpec::EnrichedJson.all_test_values[key] = {
      expected: ExpectationHelperWrapper::Serializer.serialize_value(expected_value),
      actual: ExpectationHelperWrapper::Serializer.serialize_value(actual_value),
      matcher_name: initial_matcher.class.name,
      passed: nil
    }

    RSpec::EnrichedJson.all_test_values[key][:negated] = true if negated
  rescue => e
    if defined?(RSpec.configuration) && RSpec.configuration.reporter
      RSpec.configuration.reporter.message("Warning: Error capturing test values: #{e.message}")
    elsif ENV["DEBUG"]
      puts "Error capturing test values: #{e.message}"
    end
  end
end

#mark_as_passed(initial_matcher) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/rspec/enriched_json/expectation_helper_wrapper.rb', line 178

def mark_as_passed(initial_matcher)
  return unless initial_matcher && RSpec.current_example

  key = RSpec.current_example.id
  if RSpec::EnrichedJson.all_test_values[key]
    RSpec::EnrichedJson.all_test_values[key][:passed] = true
  end
end