Skip to content

Commit

Permalink
test: add specs for testing greyscale processing
Browse files Browse the repository at this point in the history
Pixbuf happens to convert everything to RGB, but other processors are not guaranteed to have such property,
thus the additional specs.

I've also added a new matcher to make colourspace check easy.
  • Loading branch information
knarewski committed Nov 14, 2024
1 parent 0c7b47f commit efaec3e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
4 changes: 4 additions & 0 deletions spec/colour_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def generate_test_image_plasma_checkers(at_file_path, width: 600, height: 300)
generate_test_image(at_file_path, fill: fill, width: width, height: height)
end

def generate_test_image_greyscale(at_file_path, width: 600, height: 300)
generate_test_image(at_file_path, fill: 'gradient:white-black', width: width, height: height)
end

def generate_test_image(at_file_path, fill:, width: 600, height: 300)
fill = Array(fill).flatten

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 29 additions & 1 deletion spec/morandi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
let(:processed_image_type) { processed_image_info[0].name }
let(:processed_image_width) { processed_image_info[1] }
let(:processed_image_height) { processed_image_info[2] }
let(:generate_image) do
generate_test_image_plasma_checkers(file_in, width: original_image_width, height: original_image_height)
end

before(:all) do
FileUtils.mkdir_p('sample')
Expand All @@ -29,7 +32,7 @@
before do
next if File.exist?(file_in)

generate_test_image_plasma_checkers(file_in, width: original_image_width, height: original_image_height)
generate_image
end

after do |ex|
Expand Down Expand Up @@ -456,6 +459,31 @@
end
end
end

context 'with a non-rgb image' do
let(:generate_image) do
generate_test_image_greyscale(file_in, width: original_image_width, height: original_image_height)
end

it 'changes greyscale image to srgb' do
expect(file_in).to match_colourspace('gray') # Testing a setup to protect from a hidden regression
process_image

expect(file_out).to match_colourspace('srgb')
end

# Colour filters implementation operates on RGB-based constants, thus a dedicated test
context 'with colour filter' do
let(:options) { super().merge('fx' => 'sepia') }

it 'creates a valid, srgb image' do
process_image

expect(file_out).to match_reference_image('greyscale-with-sepia')
expect(file_out).to match_colourspace('srgb')
end
end
end
end

context 'pixbuf processor' do
Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
require 'super_diff/rspec'
require 'pry'

require 'support/match_reference_image'
Dir['spec/support/**/*.rb'].each { |f| require "./#{f}" }

require_relative 'visual_report_helper'
require_relative 'colour_helper'

Expand Down
21 changes: 21 additions & 0 deletions spec/support/match_colourspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require 'open3'

# For testing that under a given path resides an image with desired colourspace.
# The colourspace is currently extracted using imagemagick's `identify`, resulting in values like 'gray' or 'srgb'
# According to docs (https://www.imagemagick.org/script/escape.php), it may include number of channels and meta channels
RSpec::Matchers.define :match_colourspace do |expected_colourspace|
match do |tested_path|
raise(ArgumentError, "path #{tested_path} is not a file") unless File.file?(tested_path)

@colourspace, status = Open3.capture2('identify', '-format', '%[channels]', tested_path)
raise "Failed to read colorspace of #{tested_path}" unless status.success?

@colourspace == expected_colourspace
end

failure_message do
"Colourspaces don't match. Expected: #{expected_colourspace}, got: #{@colourspace}"
end
end

0 comments on commit efaec3e

Please sign in to comment.