Skip to content

Commit c078dec

Browse files
pftgclaude
andcommitted
refactor: replace implicit tuple protocol in ScreenshotAssertion
ScreenshotMatcher now constructs ScreenshotAssertion directly instead of passing [caller, name, compare] array to ScreenshotAssertion.from. Removes undocumented array protocol that was easy to break. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6d7e1a6 commit c078dec

5 files changed

Lines changed: 28 additions & 23 deletions

File tree

lib/capybara/screenshot/diff/reporters/default.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Default
88
def initialize(difference)
99
@difference = difference
1010

11-
screenshot_format = difference.comparison.options[:screenshot_format] || comparison.new_image_path.extname.delete_prefix(".").then { |ext| ext.empty? ? "png" : ext }
11+
screenshot_format = difference.comparison.options[:screenshot_format] || comparison.new_image_path.extname.delete_prefix(".").presence || "png"
1212
@annotated_image_path = comparison.new_image_path.sub_ext(".diff.#{screenshot_format}")
1313
@annotated_base_image_path = comparison.base_image_path.sub_ext(".diff.#{screenshot_format}")
1414
@heatmap_diff_path = comparison.new_image_path.sub_ext(".heatmap.diff.#{screenshot_format}")

lib/capybara/screenshot/diff/screenshot_matcher.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,10 @@ def capture_screenshot(capture_options, comparison_options)
8989
end
9090

9191
def create_screenshot_assertion(skip_stack_frames, comparison_options)
92-
CapybaraScreenshotDiff::ScreenshotAssertion.from([
93-
caller(skip_stack_frames + 1),
94-
screenshot_full_name,
95-
ImageCompare.new(@snapshot.path, @snapshot.base_path, comparison_options)
96-
])
92+
assertion = CapybaraScreenshotDiff::ScreenshotAssertion.new(screenshot_full_name)
93+
assertion.caller = caller(skip_stack_frames + 1)
94+
assertion.compare = ImageCompare.new(@snapshot.path, @snapshot.base_path, comparison_options)
95+
assertion
9796
end
9897

9998
def extract_capture_and_comparison_options!(driver_options = {})

lib/capybara_screenshot_diff/screenshot_assertion.rb

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,9 @@ class ScreenshotAssertion
77
attr_reader :name, :args
88
attr_accessor :compare, :caller
99

10-
def initialize(name, **args, &block)
10+
def initialize(name, **args)
1111
@name = name
1212
@args = args
13-
14-
yield(self) if block_given?
15-
end
16-
17-
def self.from(screenshot_job)
18-
return screenshot_job if screenshot_job.is_a?(ScreenshotAssertion)
19-
20-
caller, name, compare = screenshot_job
21-
ScreenshotAssertion.new(name).tap do |assertion|
22-
assertion.caller = caller
23-
assertion.compare = compare
24-
end
2513
end
2614

2715
def validate
@@ -89,8 +77,7 @@ def initialize
8977
end
9078

9179
def add_assertion(assertion)
92-
assertion = ScreenshotAssertion.from(assertion)
93-
return unless assertion.compare
80+
return unless assertion&.compare
9481

9582
@assertions.push(assertion)
9683

test/unit/diff_test.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ def _test_sample_screenshot_error
155155
mock.expect(:base_image_path, Pathname.new("screenshot.base.png"))
156156
mock.expect(:error_message, "expected error message")
157157

158-
assertion = CapybaraScreenshotDiff::ScreenshotAssertion.from([["my_test.rb:42"], "sample_screenshot", mock])
158+
assertion = CapybaraScreenshotDiff::ScreenshotAssertion.new("sample_screenshot")
159+
assertion.caller = ["my_test.rb:42"]
160+
assertion.compare = mock
159161
CapybaraScreenshotDiff.add_assertion(assertion)
160162

161163
assert true
@@ -189,7 +191,9 @@ def _test_sample_screenshot_error
189191
comparison.expect(:base_image_path, Pathname.new("screenshot.base.png"))
190192
comparison.expect(:error_message, "expected error message for non minitest")
191193

192-
assertion = CapybaraScreenshotDiff::ScreenshotAssertion.from([["my_test.rb:42"], "sample_screenshot", comparison])
194+
assertion = CapybaraScreenshotDiff::ScreenshotAssertion.new("sample_screenshot")
195+
assertion.caller = ["my_test.rb:42"]
196+
assertion.compare = comparison
193197
CapybaraScreenshotDiff.add_assertion(assertion)
194198
end
195199
end

test/unit/dsl_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ def assert_no_screenshot_jobs_scheduled
100100
end
101101
end
102102

103+
test "#assert_no_screenshot_changes reports caller from test method" do
104+
Capybara::Screenshot::Diff::Vcs.stub(:checkout_vcs, true) do
105+
assert_no_screenshot_jobs_scheduled
106+
107+
snap = create_snapshot_for(:a, :c)
108+
109+
assert_no_screenshot_changes(snap.full_name)
110+
assert_equal 1, CapybaraScreenshotDiff.assertions.size
111+
assert_match(
112+
%r{/dsl_test.rb},
113+
CapybaraScreenshotDiff.assertions[0].caller.first
114+
)
115+
end
116+
end
117+
103118
test "#screenshot with delayed: false raises error when images differ" do
104119
Capybara::Screenshot::Diff::Vcs.stub(:checkout_vcs, true) do
105120
Capybara::Screenshot::Diff.stub(:delayed, false) do

0 commit comments

Comments
 (0)