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.



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

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

.match_matcher(actual, expected, failures) ⇒ void

This method returns an undefined value.

Compares the parsed content against the given RSpec matcher.

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
43
# File 'lib/rspec/path_matchers/options/parsed_content_base.rb', line 38

def self.match_matcher(actual, expected, failures)
  return if expected.matches?(actual)

  message = "expected #{content_type} content to #{expected.description}"
  add_failure(message, failures)
end

.valid_expected_typesObject



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

def self.valid_expected_types = [TrueClass]