Skip to content

Commit cbf32a5

Browse files
committed
Don't expect a failure in every propertyCheck call
If a test contains multiple calls to propertyCheck, having a fixed seed will no longer expect every call to raise at least one issue.
1 parent 957a384 commit cbf32a5

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

Sources/PropertyBased/FixedSeedTrait.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public struct FixedSeedTrait: TestTrait, TestScoping {
2323
@TaskLocal
2424
static var fixedRandom: (rng: Xoshiro, location: SourceLocation)?
2525

26-
public func provideScope(for test: Test, testCase: Test.Case?, performing function: () async throws -> Void) async throws {
26+
public func provideScope(for test: Test, testCase: Test.Case?, performing function: () async throws -> Void) async {
2727
if let existing = Self.fixedRandom {
2828
Issue.record("Two different fixed seeds are used in the same test.", sourceLocation: existing.location)
2929
return
@@ -34,8 +34,14 @@ public struct FixedSeedTrait: TestTrait, TestScoping {
3434
return
3535
}
3636

37-
try await Self.$fixedRandom.withValue((rng, location: sourceLocation)) {
38-
try await function()
37+
let foundIssues = await countIssues {
38+
try await Self.$fixedRandom.withValue((rng, location: sourceLocation)) {
39+
try await function()
40+
}
41+
}
42+
43+
if foundIssues == 0 {
44+
Issue.record("A fixed seed was used, but no expectation failure occured. The property was not tested fully.", sourceLocation: sourceLocation)
3945
}
4046
}
4147
}

Sources/PropertyBased/IssueCounting.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88
import Testing
99

10-
func countIssues(isolation: isolated (any Actor)?, perform: () async throws -> Void) async -> Int {
10+
func countIssues(isolation: isolated (any Actor)? = #isolation, perform: () async throws -> Void) async -> Int {
1111
let found = Mutex(0)
12-
12+
1313
// This is currently the only way to get a callback whenever an issue is found within a block.
1414
try? await withKnownIssue(isIntermittent: true, isolation: isolation) {
1515
try await perform()
1616
} matching: { _ in
1717
found.withLock { $0 += 1 }
1818
return false
1919
}
20-
20+
2121
return found.withLock { $0 }
2222
}

Sources/PropertyBased/PropertyCheck.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,5 @@ public func propertyCheck<each Value>(isolation: isolated (any Actor)? = #isolat
111111
}
112112
return
113113
}
114-
115-
if foundIssues == 0, let seedLocation = fixedRng?.location {
116-
Issue.record("A fixed seed was used, but no expectation failure occured. The property was not tested fully.", sourceLocation: seedLocation)
117-
}
118114
}
119115
}

Tests/PropertyBasedTests/FixedSeedTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Gen
1515
let scope = try #require(trait.scopeProvider(for: Test.current!, testCase: Test.Case.current))
1616

1717
let issues = await gatherIssues {
18-
try await scope.provideScope(for: Test.current!, testCase: Test.Case.current) {
18+
await scope.provideScope(for: Test.current!, testCase: Test.Case.current) {
1919
try #require(Bool(false), "block must not be called")
2020
}
2121
}
@@ -30,8 +30,8 @@ import Gen
3030
let trait2 = FixedSeedTrait.fixedSeed("I9kE/glCt1MIxbFsddPUSiKFAAJBGKPHSre93c+Wz9E=")
3131

3232
let issues = await gatherIssues {
33-
try await trait1.provideScope(for: Test.current!, testCase: Test.Case.current) {
34-
try await trait2.provideScope(for: Test.current!, testCase: Test.Case.current) {
33+
await trait1.provideScope(for: Test.current!, testCase: Test.Case.current) {
34+
await trait2.provideScope(for: Test.current!, testCase: Test.Case.current) {
3535
try #require(Bool(false), "block must not be called")
3636
}
3737
}
@@ -45,7 +45,7 @@ import Gen
4545
@Test func testIdenticalInputs() async throws {
4646
let trait = FixedSeedTrait.fixedSeed("4tPCyvymNncnc+napVCI0T4Jc6IYw1lXOQbXlIqyHck=")
4747
let issues = await gatherIssues {
48-
try await trait.provideScope(for: Test.current!, testCase: Test.Case.current) {
48+
await trait.provideScope(for: Test.current!, testCase: Test.Case.current) {
4949
await propertyCheck(input: .int(in: 0...1000000)) { n in
5050
#expect(n == 480813)
5151
}

0 commit comments

Comments
 (0)