Class: SnapDiff::AssertionRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/snap_diff/screenshot_assertion.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAssertionRegistry

Returns a new instance of AssertionRegistry.



114
115
116
117
118
119
# File 'lib/snap_diff/screenshot_assertion.rb', line 114

def initialize
  @assertions = []
  @new_screenshots = []
  @checked_out_baselines = Set.new
  @screenshot_namer = SnapDiff::ScreenshotNamer.new
end

Instance Attribute Details

#assertionsObject (readonly)

Returns the value of attribute assertions.



112
113
114
# File 'lib/snap_diff/screenshot_assertion.rb', line 112

def assertions
  @assertions
end

#new_screenshotsObject (readonly)

Returns the value of attribute new_screenshots.



112
113
114
# File 'lib/snap_diff/screenshot_assertion.rb', line 112

def new_screenshots
  @new_screenshots
end

#screenshot_namerObject (readonly)

Returns the value of attribute screenshot_namer.



112
113
114
# File 'lib/snap_diff/screenshot_assertion.rb', line 112

def screenshot_namer
  @screenshot_namer
end

Instance Method Details

#add_assertion(assertion) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/snap_diff/screenshot_assertion.rb', line 129

def add_assertion(assertion)
  return unless assertion&.compare

  @assertions.push(assertion)

  assertion
end

#assertions_present?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/snap_diff/screenshot_assertion.rb', line 137

def assertions_present?
  !@assertions.empty?
end

#failed_assertionsObject



170
171
172
# File 'lib/snap_diff/screenshot_assertion.rb', line 170

def failed_assertions
  assertions.filter { |screenshot_assert| screenshot_assert.compare&.different? }
end

#new_screenshots_present?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/snap_diff/screenshot_assertion.rb', line 159

def new_screenshots_present?
  !@new_screenshots.empty?
end

#record_baseline_checkout(name) ⇒ Object

Called by Snap#checkout_base_screenshot when git really handed us a baseline. Same thread as the reading below -- a test's checkout and its comparison happen in one call stack -- so no synchronization is needed or wanted here.



125
126
127
# File 'lib/snap_diff/screenshot_assertion.rb', line 125

def record_baseline_checkout(name)
  @checked_out_baselines << name
end

#record_new_screenshot(name) ⇒ Object

"No baseline exists" reaches here for two very different reasons. One is legitimate and warned about: nothing was ever committed for this name. The other is impossible in a correct run -- git gave us a baseline moments ago and it is gone now -- and used to be recorded as if it were the first, leaving the test green having compared nothing. That is the silently-wrong case measured in #217: two concurrent tests asserting the SAME name, where one's archive_baseline! moves the baseline the other just checked out.

Raises:



149
150
151
152
153
154
155
156
157
# File 'lib/snap_diff/screenshot_assertion.rb', line 149

def record_new_screenshot(name)
  raise SnapDiff::Error.new(<<~ERROR.chomp, caller) if @checked_out_baselines.include?(name)
    The baseline for '#{name}' was checked out and then disappeared before it could be compared -- nothing was verified.
    Every artifact path derives from the screenshot name alone, so two tests asserting '#{name}' at the same time race on one set of files: the one that finishes first archives the baseline the other is still using.
    Give those screenshots distinct names, or do not run them concurrently.
  ERROR

  @new_screenshots.push(name)
end

#resetObject



174
175
176
177
178
179
# File 'lib/snap_diff/screenshot_assertion.rb', line 174

def reset
  @assertions.clear
  @new_screenshots.clear
  @checked_out_baselines.clear
  @screenshot_namer = SnapDiff::ScreenshotNamer.new
end

#verify(screenshots = assertions) ⇒ Object



163
164
165
166
167
168
# File 'lib/snap_diff/screenshot_assertion.rb', line 163

def verify(screenshots = assertions)
  result = ScreenshotAssertion.verify_screenshots!(screenshots)
  return unless result

  raise SnapDiff::ExpectationNotMet.new(result.join("\n\n"), failed_assertions.first.caller)
end