Class: RSpec::PathMatchers::Options::Base

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

Overview

Abstract base class for all option matchers

@ api public

Class Method Summary collapse

Class Method Details

.description(expected) ⇒ String

The description of the expectation for this option

This is used by RSpec when describing the matcher when tests are run in documentation format or when generating failure messages.

Parameters:

  • expected (Object)

    the expected value to match against the entry

Returns:

  • (String)

    the description of the expectation



78
79
80
# File 'lib/rspec/path_matchers/options/base.rb', line 78

def self.description(expected)
  RSpec::PathMatchers.matcher?(expected) ? expected.description : expected.inspect
end

.keySymbol

This method is abstract.

The option key

For example, if the option key is :owner, then it could be used like this:

expect(path).to be_file(owner: 'alice')

Returns:

  • (Symbol)

    the key for this option matcher

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/rspec/path_matchers/options/base.rb', line 29

def self.key
  raise NotImplementedError, 'Subclasses must implement Base.key'
end

.match(path, expected, failures)

This method returns an undefined value.

Adds to failures if the entry at path does not match the expectation

Entry is a file, directory, or symlink.

You can assume that entry at path exists and is the expected type (file, directory, or symlink).

This is the main method that the matcher (such as be_dir, be_file, or be_symlink) calls to run its check for an option.

failure objects to (if any)

Parameters:

  • path (String)

    the path of the entry to check

  • expected (Object)

    the expected value to match against the entry

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

    the array to append



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rspec/path_matchers/options/base.rb', line 54

def self.match(path, expected, failures)
  actual = fetch_actual(path, failures)
  return if actual == FETCH_ERROR
rescue NotImplementedError
  RSpec.configuration.reporter.message(not_supported_message(path))
else
  if RSpec::PathMatchers.matcher?(expected)
    match_matcher(actual, expected, failures)
  else
    match_literal(actual, expected, failures)
  end
end

.validate_expected(expected, errors)

This method returns an undefined value.

Adds to errors if the value of expected is not valid for this option type

The matcher (such as be_dir, be_file, or be_symlink) calls this method to validate the expected value before running the matcher.

It checks that the expected value is a RSpec matcher or one of the types listed in valid_expected_types.

Parameters:

  • expected (Object)

    the expected value to validate

  • errors (Array<String>)

    the array to append validation errors to



98
99
100
101
102
103
104
105
106
# File 'lib/rspec/path_matchers/options/base.rb', line 98

def self.validate_expected(expected, errors)
  return if expected == NOT_GIVEN ||
            RSpec::PathMatchers.matcher?(expected) ||
            valid_expected_types.any? { |type| expected.is_a?(type) }

  types = ['Matcher', *valid_expected_types.map(&:name)].to_sentence(conjunction: 'or')

  errors << "expected `#{key}:` to be a #{types}, but it was #{expected.inspect}"
end