-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
a0o
committed
Jun 18, 2018
1 parent
82641b7
commit 4b96bfc
Showing
5 changed files
with
114 additions
and
71 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module ImageSimilarity | ||
RSpec::Matchers.define :be_similar_to do |dst| | ||
match do |src| | ||
base64_re = %r{^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}=+)$} | ||
|
||
src_img, dst_img = [src, dst].map do |data| | ||
if src =~ base64_re | ||
Magick::Image.read_inline(src) | ||
elsif (File.exist?(src) rescue nil) | ||
Magick::Image.read(src) | ||
else | ||
tmp = Tempfile.new | ||
tmp.write(src) | ||
tmp.flush | ||
begin | ||
Magick::Image.read(tmp.path) | ||
ensure | ||
tmp.close | ||
tmp.unlink | ||
end | ||
end | ||
end | ||
|
||
@diff_img, @diff_metric = src_img[0].compare_channel(dst_img[0], Magick::MeanSquaredErrorMetric) | ||
@diff_metric <= @mse.to_f | ||
end | ||
|
||
chain :with_mse_less_then do |mse| | ||
@mse = mse | ||
end | ||
|
||
failure_message do |actual| | ||
"expected that mse #{@diff_metric} would be less or equal #{@mse.to_f}" | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module ImageSize | ||
RSpec::Matchers.define :be_same_size_as do |dst| | ||
match do |src| | ||
base64_re = %r{^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}=+)$} | ||
|
||
src_img, dst_img = [src, dst].map do |data| | ||
if src =~ base64_re | ||
Magick::Image.read_inline(src) | ||
elsif (File.exist?(src) rescue nil) | ||
Magick::Image.read(src) | ||
else | ||
tmp = Tempfile.new | ||
tmp.write(src) | ||
tmp.flush | ||
begin | ||
Magick::Image.read(tmp.path) | ||
ensure | ||
tmp.close | ||
tmp.unlink | ||
end | ||
end | ||
end | ||
|
||
@src_width, @src_height = src_img[0].columns, src_img[0].rows | ||
@dst_width, @dst_height = dst_img[0].columns, dst_img[0].rows | ||
@src_width == @dst_width && @src_height == @dst_height | ||
end | ||
|
||
failure_message do |actual| | ||
"expected that size of src image (#{@src_width},#{@src_height}) would be same as of dst image (#{@dst_width},#{@dst_height})" | ||
end | ||
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
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