Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flesh out tests for Image#charcoal (#1150)
This introduces a new `match_pixels` test matcher that accepts a delta argument. These tests produce varying results on different operating systems and versions of ImageMagick, so we set the delta. We're filtering out ImageMagick 6.7 because it produces a black and white image.
- Loading branch information
Showing
4 changed files
with
64 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,39 @@ | ||
RSpec.describe Magick::Image, "#charcoal" do | ||
it "works" do | ||
image = described_class.new(20, 20) | ||
def build_image | ||
image = Magick::Image.new(2, 2) | ||
pixels = [[45, 98, 156], [209, 171, 11], [239, 236, 2], [8, 65, 247]] | ||
image.import_pixels(0, 0, 2, 2, "RGB", pixels.flatten) | ||
end | ||
|
||
def gray(pixel_value) | ||
[pixel_value, pixel_value, pixel_value] | ||
end | ||
|
||
it "applies a charcoal effect", unsupported_before('6.8') do | ||
image = build_image | ||
expected_pixels = [gray(53736), gray(48703), gray(9953), gray(51857)] | ||
|
||
expect(image.charcoal).to match_pixels(expected_pixels, delta: 1) | ||
end | ||
|
||
it "applies a charcoal effect with radius", unsupported_before('6.8') do | ||
image = build_image | ||
expected_pixels = [gray(55422), gray(49372), gray(9121), gray(53918)] | ||
|
||
expect(image.charcoal(1.0)).to match_pixels(expected_pixels, delta: 50) | ||
end | ||
|
||
it "applies a charcoal effect with radius and sigma", unsupported_before('6.8') do | ||
image = build_image | ||
expected_pixels = [gray(51460), gray(48203), gray(11918), gray(50352)] | ||
|
||
expect(image.charcoal(1.0, 2.0)).to match_pixels(expected_pixels, delta: 100) | ||
end | ||
|
||
result = image.charcoal | ||
expect(result).to be_instance_of(described_class) | ||
it "raises an error with an incorrect number of arguments" do | ||
image = build_image | ||
|
||
expect { image.charcoal(1.0) }.not_to raise_error | ||
expect { image.charcoal(1.0, 2.0) }.not_to raise_error | ||
expect { image.charcoal(1.0, 2.0, 3.0) }.to raise_error(ArgumentError) | ||
expect { image.charcoal(1.0, 2.0, 3.0) } | ||
.to raise_error(ArgumentError, "wrong number of arguments (3 for 0 to 2)") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require_relative 'matchers/match_pixels_matcher' | ||
|
||
def match_pixels(expected, delta: 0) | ||
Matchers::MatchPixels.new(expected, delta: delta) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Matchers | ||
class MatchPixels | ||
attr_accessor :actual, :expected, :delta | ||
|
||
def initialize(expected, delta:) | ||
self.expected = expected.flatten | ||
self.delta = delta | ||
end | ||
|
||
def matches?(actual) | ||
self.actual = actual | ||
|
||
actual.export_pixels.each_with_index.all? do |value, index| | ||
expected_value = expected[index] | ||
value.between?(expected_value - delta, expected_value + delta) | ||
end | ||
end | ||
|
||
def failure_message | ||
"expected all pixel values to differ by max #{delta}\n" \ | ||
"\nexpected: #{expected}\n got: #{actual.export_pixels}\n\n\n" | ||
end | ||
end | ||
end |