Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
Oh look! Unit tests found a bug! I was calling image.last.width inste…
Browse files Browse the repository at this point in the history
…ad of image.first.height for the height of the diff :(
  • Loading branch information
carols10cents committed Dec 21, 2011
1 parent 6eb6b32 commit 0e911a5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/compatriot/image_differ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
module Compatriot
class ImageDiffer

def self.diff(results)
def self.diff(results, strategy = :color_difference)
images = results.map{|r| ChunkyPNG::Image.from_file(r) }
f = self.color_difference(images, results.first)
File.join(
File.basename(File.dirname(f)),
File.basename(f)
)
self.send(strategy, images, results.first)
end

def self.same_pixels_exactly(images, name)
output = ChunkyPNG::Image.new(images.first.width, images.last.width, WHITE)
output = ChunkyPNG::Image.new(images.first.width, images.first.height, WHITE)
diff = []

# each_pixel(images.first, images.last) do |x, y|

images.first.height.times do |y|
images.first.row(y).each_with_index do |pixel, x|
output[x,y] = pixel
Expand All @@ -33,11 +31,14 @@ def self.same_pixels_exactly(images, name)
output.rect(x.min, y.min, x.max, y.max, ChunkyPNG::Color.rgb(0,255,0))
filename = "#{name}-same_exactly.png"
output.save(filename)
filename
File.join(
File.basename(File.dirname(filename)),
File.basename(filename)
)
end

def self.color_difference(images, name)
output = ChunkyPNG::Image.new(images.first.width, images.last.width, WHITE)
output = ChunkyPNG::Image.new(images.first.width, images.first.height, WHITE)
diff = []

images.first.height.times do |y|
Expand All @@ -61,7 +62,10 @@ def self.color_difference(images, name)

filename = "#{name}-color_difference.png"
output.save(filename)
filename
File.join(
File.basename(File.dirname(filename)),
File.basename(filename)
)
end
end
end
32 changes: 32 additions & 0 deletions spec/unit/image_differ_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative '../spec_helper'

describe Compatriot::ImageDiffer do
describe "self#diff" do
it "calls chunky_png on each image path" do
file_one = stub
file_two = stub

ChunkyPNG::Image.expects(:from_file).with(file_one)
ChunkyPNG::Image.expects(:from_file).with(file_two)
Compatriot::ImageDiffer.stubs(:color_difference)

Compatriot::ImageDiffer.diff([file_one, file_two])
end

it "returns the filename of the diff" do
end

it "uses the strategy passed in" do
end
end

describe "self#color_difference" do
it "starts a new white image with the same dimensions" do
ChunkyPNG::Image.expects(:new).with(1, 2, ChunkyPNG::Image::WHITE)

image1 = stub(:width => 1, :height => 2)

Compatriot::ImageDiffer.color_difference([image1], stub)
end
end
end

0 comments on commit 0e911a5

Please sign in to comment.