Skip to content

Commit

Permalink
Add comments to explain verification logic
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasmarshall committed Sep 24, 2024
1 parent 7cbeffd commit b3aa2b6
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/minitest/verify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,33 @@ class << self

def verify_fails_without(&block)
if @current_caller
# If @current_caller is set, we are in the verification phase.
# Evaluate the block unless it is the one currently being verified.
block.call unless caller(1..1).first == @current_caller[0]
else
# If @current_caller is not set, we're in the normal test phase.
# Collect the caller (there might be multiple per test) and evaluate the block.
callers << caller
block.call
end
end

def run
# If verification is disabled, run the test normally.
return super unless Verify.enabled

super

# If there are normal failures, don't run verification.
return Result.from(self) if failures.any?

begin
# For each caller, run the test again and verify that it fails.
while (@current_caller = callers.shift)
with_verification { super }
end
rescue VerificationFailedError
# If verification fails, break out of the loop.
end

Result.from(self)
Expand All @@ -45,14 +53,19 @@ def callers
def with_verification
yield

# Fail verification if there is an unexpected error.
# Encountering an unexpected error doesn't imply the test is not a false negative, it might just be broken.
if failures.any? { |f| f.is_a?(Minitest::UnexpectedError) }
raise VerificationFailedError
end

# Remove all assertion failures so the failing test passes.
# If at least one was removed, the test is not a false negative.
if failures.reject! { |f| f.is_a?(Minitest::Assertion) }
return
end

# If there were no assertion failures, we probably have a false negative.
exception = Minitest::Assertion.new("Expected at least one assertion to fail.")
exception.set_backtrace(@current_caller)
failures << exception
Expand Down

0 comments on commit b3aa2b6

Please sign in to comment.