class RSpecSnapshotMatcher::Matchers::MatchSnapshot

Attributes

actual[R]
expected[R]

Public Instance Methods

description() click to toggle source
# File lib/rspec_snapshot_matcher/matchers/snapshot_matcher.rb, line 20
def description
  "match snapshot #{snapshot_filename}"
end
diffable?() click to toggle source
# File lib/rspec_snapshot_matcher/matchers/snapshot_matcher.rb, line 44
def diffable?
  true
end
failure_message() click to toggle source
# File lib/rspec_snapshot_matcher/matchers/snapshot_matcher.rb, line 24
def failure_message
  [
    "expected: #{expected.inspect}",
    "     got: #{actual.inspect}",
    '',
    "(using snapshot #{snapshot_filename})"
  ].join("\n")
end
failure_message_when_negated() click to toggle source
# File lib/rspec_snapshot_matcher/matchers/snapshot_matcher.rb, line 33
def failure_message_when_negated
  [
    'expected value not to match snapshot but it did',
    "snapshot: #{actual.inspect}",
    '',
    "(using snapshot #{snapshot_filename})",
    '',
    'does negating this matcher really make sense?'
  ].join("\n")
end
matches?(actual) click to toggle source
# File lib/rspec_snapshot_matcher/matchers/snapshot_matcher.rb, line 8
def matches?(actual)
  @actual = actual

  if File.exist?(snapshot_filename)
    @expected = YAML.load_file(snapshot_filename)
    return @actual == @expected
  else
    save_snapshot(actual, snapshot_filename)
    return true
  end
end

Private Instance Methods

example() click to toggle source
# File lib/rspec_snapshot_matcher/matchers/snapshot_matcher.rb, line 78
def example
  Thread.current[:rspec_snapshot_matcher_example]
end
example_group_path() click to toggle source
# File lib/rspec_snapshot_matcher/matchers/snapshot_matcher.rb, line 73
def example_group_path
  return [] unless example.example_group
  example.example_group.parent_groups.reverse.map(&:description)
end
save_snapshot(actual, filename) click to toggle source
# File lib/rspec_snapshot_matcher/matchers/snapshot_matcher.rb, line 50
def save_snapshot(actual, filename)
  snapshot_directory = File.dirname(filename)
  FileUtils.mkdir_p(snapshot_directory)

  File.open(snapshot_filename, 'w') do |io|
    io.write(actual.to_yaml)
  end
end
snapshot_filename() click to toggle source
# File lib/rspec_snapshot_matcher/matchers/snapshot_matcher.rb, line 59
def snapshot_filename
  @snapshot_filename ||= begin
    parts = [
      'spec',
      'fixtures',
      'snapshots',
      *example_group_path,
      example.description
    ].map { |part| part.gsub(/\W/, '_') }

    "#{File.join(*parts)}.yml"
  end
end