Skip to content

Commit e53d3cf

Browse files
committed
Use swift package traits to make Nimble an optional component.
1 parent 542299c commit e53d3cf

File tree

9 files changed

+227
-193
lines changed

9 files changed

+227
-193
lines changed

.github/workflows/ci-swiftpm.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ jobs:
3737
if: ${{ needs.filter.outputs.should_skip != 'true' }}
3838
- run: swift test
3939
if: ${{ needs.filter.outputs.should_skip != 'true' }}
40+
- run: swift test --traits Include_Nimble
41+
if: ${{ needs.filter.outputs.should_skip != 'true' }}
4042

4143
swiftpm_darwin_15:
4244
name: SwiftPM, Darwin 15, Xcode ${{ matrix.xcode }}
@@ -55,6 +57,8 @@ jobs:
5557
if: ${{ needs.filter.outputs.should_skip != 'true' }}
5658
- run: swift test
5759
if: ${{ needs.filter.outputs.should_skip != 'true' }}
60+
- run: swift test --traits Include_Nimble
61+
if: ${{ needs.filter.outputs.should_skip != 'true' }}
5862

5963
swiftpm_linux:
6064
name: SwiftPM, Linux
@@ -75,6 +79,8 @@ jobs:
7579
if: ${{ needs.filter.outputs.should_skip != 'true' }}
7680
- run: swift test
7781
if: ${{ needs.filter.outputs.should_skip != 'true' }}
82+
- run: swift test --traits Include_Nimble
83+
if: ${{ needs.filter.outputs.should_skip != 'true' }}
7884

7985
swiftpm_android:
8086
name: SwiftPM, Android

Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,63 @@
1-
// swift-tools-version: 6.0
1+
// swift-tools-version: 6.1
22

33
import PackageDescription
44

55
let package = Package(
66
name: "swift-fakes",
77
platforms: [
8-
.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .visionOS(.v1)
8+
.macOS(.v10_15),
9+
.iOS(.v13),
10+
.tvOS(.v13),
11+
.visionOS(.v1)
912
],
1013
products: [
1114
.library(
1215
name: "Fakes",
1316
targets: ["Fakes"]
1417
),
1518
],
19+
traits: [
20+
.init(
21+
name: "Include_Nimble",
22+
description: "Enable Nimble Integration",
23+
enabledTraits: []
24+
),
25+
],
1626
dependencies: [
17-
.package(url: "https://github.com/Quick/Nimble.git", from: "14.0.0"),
27+
.package(
28+
url: "https://github.com/Quick/Nimble.git",
29+
from: "14.0.0"
30+
),
1831
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
1932
],
2033
targets: [
2134
.target(
2235
name: "Fakes",
23-
dependencies: ["Nimble"],
36+
dependencies: [
37+
.product(
38+
name: "Nimble",
39+
package: "Nimble",
40+
condition: .when(
41+
traits: ["Include_Nimble"]
42+
)
43+
)
44+
],
2445
resources: [
2546
.copy("PrivacyInfo.xcprivacy")
2647
]
2748
),
2849
.testTarget(
2950
name: "FakesTests",
30-
dependencies: ["Fakes", "Nimble"]
51+
dependencies: [
52+
"Fakes",
53+
.product(
54+
name: "Nimble",
55+
package: "Nimble",
56+
condition: .when(
57+
traits: ["Include_Nimble"]
58+
)
59+
)
60+
]
3161
),
3262
]
3363
)

Sources/Fakes/Spy/Spy+Nimble.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#if Include_Nimble
12
import Nimble
23

34
// MARK: - Verifying any calls to the Spy.
@@ -303,3 +304,4 @@ private func _mostRecentlyBeCalled<Arguments, Returning>(
303304
)
304305
}
305306
}
307+
#endif
Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,73 @@
11
import Fakes
2-
import Nimble
3-
import XCTest
2+
import Testing
43

5-
final class DynamicResultTests: XCTestCase {
6-
func testSingleStaticValue() {
4+
struct DynamicResultTests {
5+
@Test func testSingleStaticValue() {
76
let subject = DynamicResult<Int, Int>(1)
87

9-
expect(subject.call(1)).to(equal(1))
10-
expect(subject.call(1)).to(equal(1))
11-
expect(subject.call(1)).to(equal(1))
8+
#expect(subject.call(1) == 1)
9+
#expect(subject.call(1) == 1)
10+
#expect(subject.call(1) == 1)
1211
}
1312

14-
func testMultipleStaticValues() {
13+
@Test func testMultipleStaticValues() {
1514
let subject = DynamicResult<Void, Int>(1, 2, 3)
1615

17-
expect(subject.call()).to(equal(1))
18-
expect(subject.call()).to(equal(2))
19-
expect(subject.call()).to(equal(3))
16+
#expect(subject.call() == 1)
17+
#expect(subject.call() == 2)
18+
#expect(subject.call() == 3)
2019
// After the last call, we continue to return the last stub in the list
21-
expect(subject.call()).to(equal(3))
22-
expect(subject.call()).to(equal(3))
20+
#expect(subject.call() == 3)
21+
#expect(subject.call() == 3)
2322
}
2423

25-
func testClosure() {
24+
@Test func testClosure() {
2625
let subject = DynamicResult<Int, Int>({ $0 + 1 })
2726

28-
expect(subject.call(1)).to(equal(2))
29-
expect(subject.call(2)).to(equal(3))
30-
expect(subject.call(3)).to(equal(4))
27+
#expect(subject.call(1) == 2)
28+
#expect(subject.call(2) == 3)
29+
#expect(subject.call(3) == 4)
3130
}
3231

33-
func testStubs() {
32+
@Test func testStubs() {
3433
let subject = DynamicResult<Int, Int>(
3534
.value(1),
3635
.closure({ $0 * 3}),
3736
.value(4)
3837
)
3938

40-
expect(subject.call(1)).to(equal(1))
41-
expect(subject.call(2)).to(equal(6))
42-
expect(subject.call(3)).to(equal(4))
43-
expect(subject.call(3)).to(equal(4))
39+
#expect(subject.call(1) == 1)
40+
#expect(subject.call(2) == 6)
41+
#expect(subject.call(3) == 4)
42+
#expect(subject.call(3) == 4)
4443
}
4544

4645
// MARK: - Replacement Tests
4746

48-
func testReplacementStaticValue() {
47+
@Test func testReplacementStaticValue() {
4948
let subject = DynamicResult<Void, Int>(1)
5049

5150
subject.replace(2, 3, 4, 6)
5251

53-
expect(subject.call()).to(equal(2))
54-
expect(subject.call()).to(equal(3))
55-
expect(subject.call()).to(equal(4))
56-
expect(subject.call()).to(equal(6))
52+
#expect(subject.call() == 2)
53+
#expect(subject.call() == 3)
54+
#expect(subject.call() == 4)
55+
#expect(subject.call() == 6)
5756
// After the last call, we continue to return the last stub in the list
58-
expect(subject.call()).to(equal(6))
57+
#expect(subject.call() == 6)
5958
}
6059

61-
func testReplacementClosure() {
60+
@Test func testReplacementClosure() {
6261
let subject = DynamicResult<Int, Int>(1)
6362

6463
subject.replace({ $0 + 4})
6564

66-
expect(subject.call(1)).to(equal(5))
67-
expect(subject.call(2)).to(equal(6))
68-
expect(subject.call(3)).to(equal(7))
65+
#expect(subject.call(1) == 5)
66+
#expect(subject.call(2) == 6)
67+
#expect(subject.call(3) == 7)
6968
}
7069

71-
func testReplacementStubs() {
70+
@Test func testReplacementStubs() {
7271
let subject = DynamicResult<Int, Int>(1)
7372

7473
subject.replace(
@@ -77,42 +76,42 @@ final class DynamicResultTests: XCTestCase {
7776
.value(4)
7877
)
7978

80-
expect(subject.call(1)).to(equal(2))
81-
expect(subject.call(2)).to(equal(6))
82-
expect(subject.call(3)).to(equal(4))
83-
expect(subject.call(3)).to(equal(4))
79+
#expect(subject.call(1) == 2)
80+
#expect(subject.call(2) == 6)
81+
#expect(subject.call(3) == 4)
82+
#expect(subject.call(3) == 4)
8483
}
8584

8685
// MARK: - Appending Tests
8786

88-
func testAppendingStaticValue() {
87+
@Test func testAppendingStaticValue() {
8988
let subject = DynamicResult<Void, Int>(1, 2, 3)
9089

9190
subject.append(4, 5, 6)
9291

93-
expect(subject.call()).to(equal(1))
94-
expect(subject.call()).to(equal(2))
95-
expect(subject.call()).to(equal(3))
96-
expect(subject.call()).to(equal(4))
97-
expect(subject.call()).to(equal(5))
98-
expect(subject.call()).to(equal(6))
92+
#expect(subject.call() == 1)
93+
#expect(subject.call() == 2)
94+
#expect(subject.call() == 3)
95+
#expect(subject.call() == 4)
96+
#expect(subject.call() == 5)
97+
#expect(subject.call() == 6)
9998
// After the last call, we continue to return the last stub in the list
100-
expect(subject.call()).to(equal(6))
99+
#expect(subject.call() == 6)
101100
}
102101

103-
func testAppendingClosure() {
102+
@Test func testAppendingClosure() {
104103
let subject = DynamicResult<Int, Int>(1, 2, 3)
105104

106105
subject.append({ $0 + 10})
107106

108-
expect(subject.call(1)).to(equal(1))
109-
expect(subject.call(2)).to(equal(2))
110-
expect(subject.call(3)).to(equal(3))
111-
expect(subject.call(4)).to(equal(14))
112-
expect(subject.call(5)).to(equal(15))
107+
#expect(subject.call(1) == 1)
108+
#expect(subject.call(2) == 2)
109+
#expect(subject.call(3) == 3)
110+
#expect(subject.call(4) == 14)
111+
#expect(subject.call(5) == 15)
113112
}
114113

115-
func testAppendingStubs() {
114+
@Test func testAppendingStubs() {
116115
let subject = DynamicResult<Int, Int>(1, 2, 3)
117116

118117
subject.append(
@@ -121,12 +120,12 @@ final class DynamicResultTests: XCTestCase {
121120
.value(6)
122121
)
123122

124-
expect(subject.call(1)).to(equal(1))
125-
expect(subject.call(2)).to(equal(2))
126-
expect(subject.call(3)).to(equal(3))
127-
expect(subject.call(4)).to(equal(4))
128-
expect(subject.call(5)).to(equal(15))
129-
expect(subject.call(6)).to(equal(6))
130-
expect(subject.call(7)).to(equal(6))
123+
#expect(subject.call(1) == 1)
124+
#expect(subject.call(2) == 2)
125+
#expect(subject.call(3) == 3)
126+
#expect(subject.call(4) == 4)
127+
#expect(subject.call(5) == 15)
128+
#expect(subject.call(6) == 6)
129+
#expect(subject.call(7) == 6)
131130
}
132131
}

Tests/FakesTests/PendableTests.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import Fakes
2-
import Nimble
3-
import XCTest
2+
import Testing
43

5-
final class PendableTests: XCTestCase {
6-
func testSingleCall() async throws {
4+
struct PendableTests {
5+
@Test func testSingleCall() async throws {
76
let subject = Pendable<Int>.pending(fallback: 0)
87

98
async let result = subject.call()
@@ -13,10 +12,10 @@ final class PendableTests: XCTestCase {
1312
subject.resolve(with: 2)
1413

1514
let value = await result
16-
expect(value).to(equal(2))
15+
#expect(value == 2)
1716
}
1817

19-
func testMultipleCalls() async throws {
18+
@Test func testMultipleCalls() async throws {
2019
let subject = Pendable<Int>.pending(fallback: 0)
2120

2221
async let result = withTaskGroup(of: Int.self, returning: [Int].self) { taskGroup in
@@ -36,17 +35,20 @@ final class PendableTests: XCTestCase {
3635
subject.resolve(with: 3)
3736

3837
let value = await result
39-
expect(value).to(equal(Array(repeating: 3, count: 100)))
38+
#expect(value == Array(repeating: 3, count: 100))
4039
}
4140

42-
func testAutoresolve() async {
41+
@Test func testAutoresolve() async throws {
4342
let subject = Pendable<Int>.pending(fallback: 3)
43+
let spy = Spy<Int, Void>()
4444

45-
await waitUntil(timeout: .milliseconds(500)) { done in
46-
Task<Void, Never> {
47-
_ = await subject.call(fallbackDelay: 0.1)
48-
done()
49-
}
45+
Task<Void, Never> {
46+
let value = await subject.call(fallbackDelay: 0.1)
47+
spy(value)
5048
}
49+
50+
try await Task.sleep(for: .milliseconds(500))
51+
52+
#expect(spy.wasCalled(with: 3))
5153
}
5254
}

0 commit comments

Comments
 (0)