Skip to content

[SR-12575] Over-fulfilling XCTestExpectation records test failure even when assertForOverFulfill is disabled #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions Sources/XCTest/Public/Asynchronous/XCTestExpectation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,19 +259,12 @@ open class XCTestExpectation {
// expectations have completed. Similarly, this should cause an
// error as well.

if queue_isFulfilled {
// FIXME: No regression tests exist for this feature. We may break it
// without ever realizing (similar to `continueAfterFailure`).
if _assertForOverFulfill {
fatalError("API violation - multiple calls made to fulfill() for \(queue_expectationDescription)")
}

if let testCase = XCTCurrentTestCase {
testCase.recordFailure(
description: "API violation - multiple calls made to XCTestExpectation.fulfill() for \(queue_expectationDescription).",
at: sourceLocation,
expected: false)
}
if queue_isFulfilled, _assertForOverFulfill, let testCase = XCTCurrentTestCase {
testCase.recordFailure(
description: "API violation - multiple calls made to XCTestExpectation.fulfill() for \(queue_expectationDescription).",
at: sourceLocation,
expected: false)

return nil
}

Expand Down
43 changes: 40 additions & 3 deletions Tests/Functional/Asynchronous/Expectations/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,39 @@ class ExpectationsTestCase: XCTestCase {
waitForExpectations(timeout: 0.2)
}

// PRAGMA MARK: - assertForOverFulfill

// CHECK: Test Case 'ExpectationsTestCase.test_assertForOverfulfill_disabled' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: Test Case 'ExpectationsTestCase.test_assertForOverfulfill_disabled' passed \(\d+\.\d+ seconds\)
func test_assertForOverfulfill_disabled() {
let foo = XCTestExpectation(description: "foo")
XCTAssertFalse(foo.assertForOverFulfill, "assertForOverFulfill should be disabled by default")
foo.fulfill()
foo.fulfill()
}

// CHECK: Test Case 'ExpectationsTestCase.test_assertForOverfulfill_failure' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: .*[/\\]Tests[/\\]Functional[/\\]Asynchronous[/\\]Expectations[/\\]main.swift:[[@LINE+7]]: error: ExpectationsTestCase.test_assertForOverfulfill_failure : API violation - multiple calls made to XCTestExpectation.fulfill\(\) for rob.
// CHECK: .*[/\\]Tests[/\\]Functional[/\\]Asynchronous[/\\]Expectations[/\\]main.swift:[[@LINE+16]]: error: ExpectationsTestCase.test_assertForOverfulfill_failure : API violation - multiple calls made to XCTestExpectation.fulfill\(\) for rob.
// CHECK: Test Case 'ExpectationsTestCase.test_assertForOverfulfill_failure' failed \(\d+\.\d+ seconds\)
func test_assertForOverfulfill_failure() {
let expectation = self.expectation(description: "rob")
expectation.assertForOverFulfill = true
expectation.fulfill()
expectation.fulfill()
// FIXME: The behavior here is subtly different from Objective-C XCTest.
// Objective-C XCTest would stop executing the test on the line
// above, and so would not report a failure for this line below.
// In total, it would highlight one line as a failure in this
// test.
//
// swift-corelibs-xctest continues to execute the test, and so
// highlights both the lines above and below as failures.
// This should be fixed such that the behavior is identical.
expectation.fulfill()
self.waitForExpectations(timeout: 0.1)
}

// PRAGMA MARK: - Interrupted Waiters

// Disabled due to non-deterministic ordering of XCTWaiterDelegate callbacks, see [SR-10034] and <rdar://problem/49123061>
Expand Down Expand Up @@ -497,6 +530,10 @@ class ExpectationsTestCase: XCTestCase {
("test_countedConditionPassBeforeWaiting", test_countedConditionPassBeforeWaiting),
("test_countedConditionFail", test_countedConditionFail),

// assertForOverFulfill
("test_assertForOverfulfill_disabled", test_assertForOverfulfill_disabled),
("test_assertForOverfulfill_failure", test_assertForOverfulfill_failure),

// Interrupted Waiters
// ("test_outerWaiterTimesOut_InnerWaitersAreInterrupted", test_outerWaiterTimesOut_InnerWaitersAreInterrupted),
("test_outerWaiterCompletes_InnerWaiterTimesOut", test_outerWaiterCompletes_InnerWaiterTimesOut),
Expand All @@ -513,11 +550,11 @@ class ExpectationsTestCase: XCTestCase {
}()
}
// CHECK: Test Suite 'ExpectationsTestCase' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 30 tests, with 14 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed 32 tests, with 16 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds

XCTMain([testCase(ExpectationsTestCase.allTests)])

// CHECK: Test Suite '.*\.xctest' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 30 tests, with 14 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed 32 tests, with 16 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: Test Suite 'All tests' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 30 tests, with 14 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed 32 tests, with 16 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
28 changes: 3 additions & 25 deletions Tests/Functional/Asynchronous/Misuse/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,19 @@ class MisuseTestCase: XCTestCase {
self.waitForExpectations(timeout: 0.1)
}

// CHECK: Test Case 'MisuseTestCase.test_whenExpectationIsFulfilledMultipleTimes_fails' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: .*[/\\]Tests[/\\]Functional[/\\]Asynchronous[/\\]Misuse[/\\]main.swift:[[@LINE+6]]: error: MisuseTestCase.test_whenExpectationIsFulfilledMultipleTimes_fails : API violation - multiple calls made to XCTestExpectation.fulfill\(\) for rob.
// CHECK: .*[/\\]Tests[/\\]Functional[/\\]Asynchronous[/\\]Misuse[/\\]main.swift:[[@LINE+15]]: error: MisuseTestCase.test_whenExpectationIsFulfilledMultipleTimes_fails : API violation - multiple calls made to XCTestExpectation.fulfill\(\) for rob.
// CHECK: Test Case 'MisuseTestCase.test_whenExpectationIsFulfilledMultipleTimes_fails' failed \(\d+\.\d+ seconds\)
func test_whenExpectationIsFulfilledMultipleTimes_fails() {
let expectation = self.expectation(description: "rob")
expectation.fulfill()
expectation.fulfill()
// FIXME: The behavior here is subtly different from Objective-C XCTest.
// Objective-C XCTest would stop executing the test on the line
// above, and so would not report a failure for this line below.
// In total, it would highlight one line as a failure in this
// test.
//
// swift-corelibs-xctest continues to execute the test, and so
// highlights both the lines above and below as failures.
// This should be fixed such that the behavior is identical.
expectation.fulfill()
self.waitForExpectations(timeout: 0.1)
}

static var allTests = {
return [
("test_whenExpectationsAreMade_butNotWaitedFor_fails", test_whenExpectationsAreMade_butNotWaitedFor_fails),
("test_whenNoExpectationsAreMade_butTheyAreWaitedFor_fails", test_whenNoExpectationsAreMade_butTheyAreWaitedFor_fails),
("test_whenExpectationIsFulfilledMultipleTimes_fails", test_whenExpectationIsFulfilledMultipleTimes_fails),
]
}()
}
// CHECK: Test Suite 'MisuseTestCase' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 3 tests, with 4 failures \(4 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed 2 tests, with 2 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds

XCTMain([testCase(MisuseTestCase.allTests)])

// CHECK: Test Suite '.*\.xctest' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 3 tests, with 4 failures \(4 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed 2 tests, with 2 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: Test Suite 'All tests' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 3 tests, with 4 failures \(4 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: \t Executed 2 tests, with 2 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds