Class: RSpec::Snapshot::Matchers::MatchSnapShot

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/snapshot/matchers/match_snapshot.rb

Instance Method Summary collapse

Constructor Details

#initialize(metadata, snapshot_name) ⇒ MatchSnapShot

Returns a new instance of MatchSnapShot.



7
8
9
10
# File 'lib/rspec/snapshot/matchers/match_snapshot.rb', line 7

def initialize(, snapshot_name)
  @metadata = 
  @snapshot_name = snapshot_name
end

Instance Method Details

#failure_messageObject



32
33
34
# File 'lib/rspec/snapshot/matchers/match_snapshot.rb', line 32

def failure_message
  "\nexpected: #{@expect}\n     got: #{@actual}\n"
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rspec/snapshot/matchers/match_snapshot.rb', line 12

def matches?(actual)
  @actual = actual
  filename = "#{@snapshot_name}.snap"
  snap_path = File.join(snapshot_dir, filename)
  FileUtils.mkdir_p(File.dirname(snap_path)) unless Dir.exist?(File.dirname(snap_path))
  if File.exist?(snap_path)
    file = File.new(snap_path)
    @expect = file.read
    file.close
    @actual == @expect
  else
    RSpec.configuration.reporter.message "Generate #{snap_path}"
    file = File.new(snap_path, "w+")
    file.write(@actual)
    file.close
    true
  end
end

#snapshot_dirObject



36
37
38
39
40
41
42
# File 'lib/rspec/snapshot/matchers/match_snapshot.rb', line 36

def snapshot_dir
  if RSpec.configuration.snapshot_dir.to_s == 'relative'
    File.dirname(@metadata[:file_path]) << "/__snapshots__"
  else
    RSpec.configuration.snapshot_dir
  end
end