Skip to content

Commit fdae553

Browse files
committed
Add tests for propertyCheck function
1 parent a1b6b4f commit fdae553

File tree

3 files changed

+87
-15
lines changed

3 files changed

+87
-15
lines changed

Tests/PropertyBasedTests/FixedSeedTests.swift

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,10 @@
66
//
77

88
import Testing
9-
@testable import PropertyBased
9+
import PropertyBased
1010
import Gen
1111

1212
@Suite struct FixedSeedTests {
13-
private func gatherIssues(block: @Sendable () async -> Void) async -> [Issue] {
14-
let mutex = Mutex([] as [Issue])
15-
16-
// No way to stop issues from still being recorded. Ignore those for now.
17-
await withKnownIssue {
18-
await block()
19-
} matching: { issue in
20-
mutex.withLock { $0.append(issue) }
21-
return true
22-
}
23-
24-
return mutex.withLock { $0 }
25-
}
26-
2713
@Test(.fixedSeed("qwert"))
2814
func testInvalidSeed() async {
2915
let issues = await gatherIssues {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// PropertyCheckTests.swift
3+
// PropertyBased
4+
//
5+
// Created by Lennard Sprong on 06/05/2025.
6+
//
7+
8+
import Testing
9+
@testable import PropertyBased
10+
11+
@Suite struct PropertyCheckTests {
12+
@Test func testFunctionIsCalledWithInputs() async {
13+
let mutex = Mutex((a: 0, b: 0))
14+
15+
await propertyCheck(input: .bool) { bool in
16+
mutex.withLock {
17+
if bool {
18+
$0.a += 1
19+
} else {
20+
$0.b += 1
21+
}
22+
}
23+
}
24+
25+
let counts = mutex.withLock { $0 }
26+
#expect(counts.a > 0 && counts.b > 0)
27+
#expect(counts.a + counts.b == 100)
28+
}
29+
30+
@Test func testCountParameter() async {
31+
await confirmation(expectedCount: 12) { confirm in
32+
await propertyCheck(count: 12) {
33+
confirm()
34+
}
35+
}
36+
37+
await confirmation(expectedCount: 7) { confirm in
38+
await propertyCheck(count: 7) {
39+
confirm()
40+
}
41+
}
42+
}
43+
44+
@Test func testIssuesArePropagated() async {
45+
let issues = await gatherIssues {
46+
await propertyCheck(input: .int(in: 0...10)) { a in
47+
try #require(a > 20)
48+
#expect(a > 50)
49+
}
50+
}
51+
52+
#expect(issues.contains(where: {
53+
$0.description.contains("> 20")
54+
}))
55+
#expect(issues.contains(where: {
56+
$0.description.contains("to reproduce this issue")
57+
}))
58+
59+
#expect(!issues.contains(where: {
60+
$0.description.contains("> 50")
61+
}))
62+
}
63+
}

Tests/PropertyBasedTests/Utils.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Utils.swift
3+
// PropertyBased
4+
//
5+
// Created by Lennard Sprong on 06/05/2025.
6+
//
7+
8+
import Testing
9+
@testable import PropertyBased
10+
11+
func gatherIssues(block: @Sendable () async -> Void) async -> [Issue] {
12+
let mutex = Mutex([] as [Issue])
13+
14+
// No way to stop issues from still being recorded. Ignore those for now.
15+
await withKnownIssue {
16+
await block()
17+
} matching: { issue in
18+
mutex.withLock { $0.append(issue) }
19+
return true
20+
}
21+
22+
return mutex.withLock { $0 }
23+
}

0 commit comments

Comments
 (0)