Class: RSpec::PathMatchers::Options::ParsedContentBase

Inherits:
Base
  • Object
show all
Defined in:
lib/rspec/path_matchers/options/parsed_content_base.rb

Overview

Base class for options that parse file content like JSON, and YAML

Direct Known Subclasses

JsonContent, YamlContent

Class Method Summary collapse

Methods inherited from Base

key, match, validate_expected

Class Method Details

.description(expected) ⇒ Object

Provides a human-readable description for the option

Returns a special message for the json_content: true case.



48
49
50
# File 'lib/rspec/path_matchers/options/parsed_content_base.rb', line 48

def self.description(expected)
  expected == true ? "be #{content_type.downcase} content" : super
end

.fetch_actual(path, failures) ⇒ Object, FETCH_ERROR

Reads and parses the file content

This method will rescue any parsing errors (e.g., JSON::ParserError) and add a descriptive failure instead of crashing.

Parameters:

Returns:

  • (Object, FETCH_ERROR)

    The parsed content (e.g., a Hash) or FETCH_ERROR.



21
22
23
24
25
26
27
# File 'lib/rspec/path_matchers/options/parsed_content_base.rb', line 21

def self.fetch_actual(path, failures)
  parse(File.read(path))
rescue parsing_error => e
  message = "expected valid #{content_type} content, but got error: #{e.message}"
  add_failure(message, failures)
  FETCH_ERROR
end

.match_literal(_actual, _expected, _failures) ⇒ Object

This is the xxxx_content: true case. A successful fetch_actual is sufficient



30
# File 'lib/rspec/path_matchers/options/parsed_content_base.rb', line 30

def self.match_literal(_actual, _expected, _failures); end

.matcher_failure_message(actual, expected)

This method returns an undefined value.

Failure message for when a matcher is used (e.g., json_content: include(...))

Parameters:

  • actual (Object)

    the actual value fetched from the file system

  • expected (RSpec::Matchers::Matcher)

    the expected matcher to match against

  • failures (Array<RSpec::PathMatchers::Failure>)

    the array to append



38
39
40
41
42
# File 'lib/rspec/path_matchers/options/parsed_content_base.rb', line 38

def self.matcher_failure_message(actual, expected)
  actual_summary = actual.inspect.length > 100 ? 'it did not' : "was #{actual.inspect}"

  "expected #{content_type} content to #{expected.description}, but #{actual_summary}"
end

.valid_expected_typesObject



10
# File 'lib/rspec/path_matchers/options/parsed_content_base.rb', line 10

def self.valid_expected_types = [TrueClass]