Class: AssertDirsEqual::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/assert_dirs_equal/matcher.rb

Overview

Confirms with RSpec3 custom matcher protocol.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected, options = {}) ⇒ Matcher

Returns a new instance of Matcher.

Parameters:

  • expected (String)

    path to a directory which contains expected structure and content.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • exact_match (Boolean) — default: true

    specifies whether ‘expected` should mirror `target`, or just be a subset of it.

Since:

  • 0.1.0



23
24
25
26
# File 'lib/assert_dirs_equal/matcher.rb', line 23

def initialize(expected, options = {})
  @expected = expected
  @exact_match = options.fetch(:exact_match, true)
end

Instance Attribute Details

#failure_messageString (readonly)

Returns:

  • (String)

Since:

  • 0.1.0



17
18
19
# File 'lib/assert_dirs_equal/matcher.rb', line 17

def failure_message
  @failure_message
end

Instance Method Details

#failure_message_when_negatedObject



59
60
61
62
63
64
65
# File 'lib/assert_dirs_equal/matcher.rb', line 59

def failure_message_when_negated
  if @exact_match
    "expected #{@target.inspect} to not be equal to #{@expected.inspect}, but they are equal"
  else
    "expected files from #{@target.inspect} to not be present in #{@expected.inspect}, but they are"
  end
end

#matches?(target) ⇒ true, false

Takes each file that exists in ‘expected` directory and expects to find exactly the same (by name and by content) file in `target` directory.

Since 0.3.0 you can use glob patterns in a filename. This is handy, for example, than you match fingerprinted files and don’t want to deal with fingerprinting algorithm. Pattern should be percent-encoded. Make sure it is strict enough to match only one file in ‘target` directory.

Examples:

Using percent-encoded glob patterns in filenames (file*.txt)

expected\
  file%2A.txt

target\
  file_that_will_be_matched.txt

Parameters:

  • target (String)

    path to a directory which contains result of executing a method under test.

Returns:

  • (true)

    when ‘exact_match` is true and `target` mirrors `expected` directory.

  • (true)

    when ‘exact_match` is false and `target` contains all files from `expected` directory. Extra files in `target` does not affect the result.

  • (false)

    if ‘target` lacks some files, or has extra files (when `exact_match` is true), or any file in subdirectory differs from according file content.

Since:

  • 0.1.0



49
50
51
52
53
54
55
# File 'lib/assert_dirs_equal/matcher.rb', line 49

def matches?(target)
  @target = target
  assert_exists(@expected) && assert_exists(@target) &&
    assert_directory(@expected) && assert_directory(@target) &&
    assert_target_contains_all_expected_entries &&
    refute_extra_files_in_target
end