Class: ImageSpec::Comparison

Inherits:
Object
  • Object
show all
Defined in:
lib/image_spec/matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected, actual, max_acceptable_score = 0.01) ⇒ Comparison

Returns a new instance of Comparison.



5
6
7
8
# File 'lib/image_spec/matchers.rb', line 5

def initialize(expected, actual, max_acceptable_score = 0.01)
  @expected, @actual = expected, actual
  @max_acceptable_score = max_acceptable_score
end

Instance Method Details

#look_similar?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/image_spec/matchers.rb', line 10

def look_similar?
  score and (score < @max_acceptable_score)
end

#same_size?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/image_spec/matchers.rb', line 40

def same_size?
  size_of(@expected) == size_of(@actual)
end

#same_type?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/image_spec/matchers.rb', line 36

def same_type?
  File.extname(@expected) == File.extname(@actual)
end

#scoreObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/image_spec/matchers.rb', line 14

def score
  raise "Expected image path is blank" unless @expected
  raise "Actual image path is blank" unless @actual

  [@expected, @actual].each do |path|
    raise "No such file! (#{path})" unless File.exists?(path.to_s)
  end

  raise "Files are not the same type!" unless same_type?

  raise "Files are not the same size" unless same_size?

  cmd = "compare -verbose -metric mae #{@expected} #{@actual} #{Rails.root}/tmp/diff"
  Open3.popen3(cmd) do |stdin, stdout, stderr|
    output = stderr.read
    return false if output =~ /images too dissimilar/

    output.match /^\s*all:.*\((.*)\)$/
    $1.to_f
  end
end

#size_of(image) ⇒ Object



44
45
46
47
# File 'lib/image_spec/matchers.rb', line 44

def size_of(image)
  `identify #{image}`.match /\b(\d+x\d+)\b/
  $1.split("x").map(&:to_i)
end