From 7a0da8e2439d02de2ed6a09f570dca251d1becee Mon Sep 17 00:00:00 2001 From: Paul Keen <125715+pftg@users.noreply.github.com> Date: Sun, 10 Dec 2023 11:46:30 +0100 Subject: [PATCH] refact: use build --- lib/capybara/screenshot/diff/screenshot_matcher.rb | 2 +- .../screenshot/diff/stable_screenshoter.rb | 2 +- scripts/benchmark/find_region_benchmark.rb | 2 +- .../capybara/screenshot/diff/image_compare_test.rb | 14 +++++++------- test/support/stub_test_methods.rb | 2 +- test/test_helper.rb | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/capybara/screenshot/diff/screenshot_matcher.rb b/lib/capybara/screenshot/diff/screenshot_matcher.rb index 1366b7fd..71ff1676 100644 --- a/lib/capybara/screenshot/diff/screenshot_matcher.rb +++ b/lib/capybara/screenshot/diff/screenshot_matcher.rb @@ -53,7 +53,7 @@ def build_screenshot_matches_job # Add comparison job in the queue [ screenshot_full_name, - ImageCompare.new(screenshot_path.to_s, base_screenshot_path.to_s, driver_options) + ImageCompare.build(screenshot_path.to_s, base_screenshot_path.to_s, driver_options) ] end diff --git a/lib/capybara/screenshot/diff/stable_screenshoter.rb b/lib/capybara/screenshot/diff/stable_screenshoter.rb index d371c376..239cd6c8 100644 --- a/lib/capybara/screenshot/diff/stable_screenshoter.rb +++ b/lib/capybara/screenshot/diff/stable_screenshoter.rb @@ -64,7 +64,7 @@ def take_stable_screenshot(screenshot_path) private def build_comparison_for(attempt_path, previous_attempt_path) - ImageCompare.new(attempt_path, previous_attempt_path, @comparison_options) + ImageCompare.build(attempt_path, previous_attempt_path, @comparison_options) end def annotate_attempts_and_fail!(screenshot_path) diff --git a/scripts/benchmark/find_region_benchmark.rb b/scripts/benchmark/find_region_benchmark.rb index 1f6007de..a14ead8a 100644 --- a/scripts/benchmark/find_region_benchmark.rb +++ b/scripts/benchmark/find_region_benchmark.rb @@ -42,7 +42,7 @@ def for_small_images def experiment_for(x, driver, method, suffix, new_path, base_path) x.report("[#{suffix}] #{driver}##{method}") do 50.times do - ImageCompare.new(new_path, base_path, driver: driver).public_send(method) + ImageCompare.build(new_path, base_path, driver: driver).public_send(method) Vips.cache_set_max(0) Vips.cache_set_max(1000) diff --git a/test/capybara/screenshot/diff/image_compare_test.rb b/test/capybara/screenshot/diff/image_compare_test.rb index 06dbf19b..35de1ed3 100644 --- a/test/capybara/screenshot/diff/image_compare_test.rb +++ b/test/capybara/screenshot/diff/image_compare_test.rb @@ -16,18 +16,18 @@ class ImageCompareTest < ActionDispatch::IntegrationTest include TestMethodsStub test "it can be instantiated with chunky_png driver" do - comparison = ImageCompare.new("images/b.png", "images/b.base.png") + comparison = ImageCompare.build("images/b.png", "images/b.base.png") assert_kind_of Drivers::ChunkyPNGDriver, comparison.driver end test "it can be instantiated with explicit chunky_png adapter" do - comparison = ImageCompare.new("images/b.png", "images/b.base.png", driver: :chunky_png) + comparison = ImageCompare.build("images/b.png", "images/b.base.png", driver: :chunky_png) assert_kind_of Drivers::ChunkyPNGDriver, comparison.driver end test "it can be instantiated with vips adapter" do skip "VIPS not present. Skipping VIPS driver tests." unless defined?(Vips) - comparison = ImageCompare.new("images/b.png", "images/b.base.png", driver: :vips) + comparison = ImageCompare.build("images/b.png", "images/b.base.png", driver: :vips) assert_kind_of Drivers::VipsDriver, comparison.driver end @@ -60,23 +60,23 @@ class ImageCompareTest < ActionDispatch::IntegrationTest end test "could pass use tolerance for chunky_png driver" do - ImageCompare.new("images/b.png", "images/b.base.png", driver: :chunky_png, tolerance: 0.02) + ImageCompare.build("images/b.png", "images/b.base.png", driver: :chunky_png, tolerance: 0.02) end test "it can be instantiated with dimensions" do - assert ImageCompare.new("images/b.png", "images/b.base.png", dimensions: [80, 80]) + assert ImageCompare.build("images/b.png", "images/b.base.png", dimensions: [80, 80]) end test "for driver: :auto returns first from available drivers" do skip "VIPS not present. Skipping VIPS driver tests." unless defined?(Vips) - comparison = ImageCompare.new("images/b.png", "images/b.base.png", driver: :auto) + comparison = ImageCompare.build("images/b.png", "images/b.base.png", driver: :auto) assert_kind_of Drivers::VipsDriver, comparison.driver end test "for driver: :auto raise error if no drivers are available" do Capybara::Screenshot::Diff.stub_const(:AVAILABLE_DRIVERS, []) do assert_raise(RuntimeError) do - ImageCompare.new("images/b.png", "images/b.base.png", driver: :auto) + ImageCompare.build("images/b.png", "images/b.base.png", driver: :auto) end end end diff --git a/test/support/stub_test_methods.rb b/test/support/stub_test_methods.rb index 655379c9..4fdc09f0 100644 --- a/test/support/stub_test_methods.rb +++ b/test/support/stub_test_methods.rb @@ -24,7 +24,7 @@ def make_comparison(fixture_base_image, fixture_new_image, destination: nil, **o set_test_images(destination, fixture_base_image, fixture_new_image) - ImageCompare.new(destination, ScreenshotMatcher.base_image_path_from(destination), **options) + ImageCompare.build(destination, ScreenshotMatcher.base_image_path_from(destination), **options) end def set_test_images(destination, original_base_image, original_new_image) diff --git a/test/test_helper.rb b/test/test_helper.rb index 056f692f..44c9c609 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -59,6 +59,6 @@ def optional_test def assert_same_images(expected_image_name, image_path) expected_image_path = file_fixture("files/comparisons/#{expected_image_name}") - assert_predicate(Capybara::Screenshot::Diff::ImageCompare.new(image_path, expected_image_path), :quick_equal?) + assert_predicate(Capybara::Screenshot::Diff::ImageCompare.build(image_path, expected_image_path), :quick_equal?) end end