Skip to content
This repository was archived by the owner on Aug 29, 2022. It is now read-only.

Commit 472e0f4

Browse files
committed
Dupe Deferred's tests to add coverage for new optimizations
1 parent 3d4e4a1 commit 472e0f4

File tree

5 files changed

+625
-63
lines changed

5 files changed

+625
-63
lines changed

Deferred.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@
166166
DB126D951E5369E200054E95 /* Deferred.h in Headers */ = {isa = PBXBuildFile; fileRef = DB524CA11D85200C00DDF16D /* Deferred.h */; settings = {ATTRIBUTES = (Public, ); }; };
167167
DB126D961E5369E300054E95 /* Deferred.h in Headers */ = {isa = PBXBuildFile; fileRef = DB524CA11D85200C00DDF16D /* Deferred.h */; settings = {ATTRIBUTES = (Public, ); }; };
168168
DB126D971E5369E300054E95 /* Deferred.h in Headers */ = {isa = PBXBuildFile; fileRef = DB524CA11D85200C00DDF16D /* Deferred.h */; settings = {ATTRIBUTES = (Public, ); }; };
169+
DB34FC902096D335005D5B82 /* ObjectDeferredTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB34FC8F2096D335005D5B82 /* ObjectDeferredTests.swift */; };
170+
DB34FC912096D335005D5B82 /* ObjectDeferredTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB34FC8F2096D335005D5B82 /* ObjectDeferredTests.swift */; };
171+
DB34FC922096D335005D5B82 /* ObjectDeferredTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB34FC8F2096D335005D5B82 /* ObjectDeferredTests.swift */; };
172+
DB34FC942096DCE1005D5B82 /* FilledDeferredTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB34FC932096DCE1005D5B82 /* FilledDeferredTests.swift */; };
173+
DB34FC952096DCE1005D5B82 /* FilledDeferredTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB34FC932096DCE1005D5B82 /* FilledDeferredTests.swift */; };
174+
DB34FC962096DCE1005D5B82 /* FilledDeferredTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB34FC932096DCE1005D5B82 /* FilledDeferredTests.swift */; };
169175
DB3E3C4620964B2A001F648A /* DeferredVariant.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB3E3C4520964B2A001F648A /* DeferredVariant.swift */; };
170176
DB3E3C4720964B2A001F648A /* DeferredVariant.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB3E3C4520964B2A001F648A /* DeferredVariant.swift */; };
171177
DB3E3C4820964B2A001F648A /* DeferredVariant.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB3E3C4520964B2A001F648A /* DeferredVariant.swift */; };
@@ -231,6 +237,8 @@
231237
DB126CD81E53686900054E95 /* Deferred.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Deferred.framework; sourceTree = BUILT_PRODUCTS_DIR; };
232238
DB126CE01E53686900054E95 /* TVDeferredTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TVDeferredTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
233239
DB126CF41E53687300054E95 /* Deferred.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Deferred.framework; sourceTree = BUILT_PRODUCTS_DIR; };
240+
DB34FC8F2096D335005D5B82 /* ObjectDeferredTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjectDeferredTests.swift; sourceTree = "<group>"; };
241+
DB34FC932096DCE1005D5B82 /* FilledDeferredTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilledDeferredTests.swift; sourceTree = "<group>"; };
234242
DB39B2D01DDE194C00DDE4C0 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
235243
DB3E3C4520964B2A001F648A /* DeferredVariant.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeferredVariant.swift; sourceTree = "<group>"; };
236244
DB4002691DDC21B300382BAE /* SwiftBugTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftBugTests.swift; sourceTree = "<group>"; };
@@ -467,10 +475,12 @@
467475
children = (
468476
DB55F1F01D96968E00FC1439 /* DeferredTests.swift */,
469477
DB55F1F11D96968E00FC1439 /* ExistentialFutureTests.swift */,
478+
DB34FC932096DCE1005D5B82 /* FilledDeferredTests.swift */,
470479
DB55F1F21D96968E00FC1439 /* FutureCustomExecutorTests.swift */,
471480
DB55F1F31D96968E00FC1439 /* FutureIgnoreTests.swift */,
472481
DB55F20B1D969A1B00FC1439 /* FutureTests.swift */,
473482
DB55F1F41D96968E00FC1439 /* LockingTests.swift */,
483+
DB34FC8F2096D335005D5B82 /* ObjectDeferredTests.swift */,
474484
DB8A071B2060D38C00639AB3 /* PerformanceTests.swift */,
475485
DB55F1F51D96968E00FC1439 /* ProtectedTests.swift */,
476486
DB4002691DDC21B300382BAE /* SwiftBugTests.swift */,
@@ -839,10 +849,12 @@
839849
DB126D6C1E5368B900054E95 /* TaskGroupTests.swift in Sources */,
840850
DB126D661E5368B900054E95 /* ProtectedTests.swift in Sources */,
841851
DB126D6E1E5368B900054E95 /* TaskWorkItemTests.swift in Sources */,
852+
DB34FC942096DCE1005D5B82 /* FilledDeferredTests.swift in Sources */,
842853
DB126D6D1E5368B900054E95 /* TaskTests.swift in Sources */,
843854
DB126D621E5368B900054E95 /* FutureCustomExecutorTests.swift in Sources */,
844855
DB126D611E5368B900054E95 /* ExistentialFutureTests.swift in Sources */,
845856
DB126D681E5368B900054E95 /* ResultRecoveryTests.swift in Sources */,
857+
DB34FC902096D335005D5B82 /* ObjectDeferredTests.swift in Sources */,
846858
);
847859
runOnlyForDeploymentPostprocessing = 0;
848860
};
@@ -902,10 +914,12 @@
902914
DB126D7C1E5368B900054E95 /* TaskGroupTests.swift in Sources */,
903915
DB126D761E5368B900054E95 /* ProtectedTests.swift in Sources */,
904916
DB126D7E1E5368B900054E95 /* TaskWorkItemTests.swift in Sources */,
917+
DB34FC952096DCE1005D5B82 /* FilledDeferredTests.swift in Sources */,
905918
DB126D7D1E5368B900054E95 /* TaskTests.swift in Sources */,
906919
DB126D721E5368B900054E95 /* FutureCustomExecutorTests.swift in Sources */,
907920
DB126D711E5368B900054E95 /* ExistentialFutureTests.swift in Sources */,
908921
DB126D781E5368B900054E95 /* ResultRecoveryTests.swift in Sources */,
922+
DB34FC912096D335005D5B82 /* ObjectDeferredTests.swift in Sources */,
909923
);
910924
runOnlyForDeploymentPostprocessing = 0;
911925
};
@@ -965,10 +979,12 @@
965979
DB126D8C1E5368BA00054E95 /* TaskGroupTests.swift in Sources */,
966980
DB126D861E5368BA00054E95 /* ProtectedTests.swift in Sources */,
967981
DB126D8E1E5368BA00054E95 /* TaskWorkItemTests.swift in Sources */,
982+
DB34FC962096DCE1005D5B82 /* FilledDeferredTests.swift in Sources */,
968983
DB126D8D1E5368BA00054E95 /* TaskTests.swift in Sources */,
969984
DB126D821E5368BA00054E95 /* FutureCustomExecutorTests.swift in Sources */,
970985
DB126D811E5368BA00054E95 /* ExistentialFutureTests.swift in Sources */,
971986
DB126D881E5368BA00054E95 /* ResultRecoveryTests.swift in Sources */,
987+
DB34FC922096D335005D5B82 /* ObjectDeferredTests.swift in Sources */,
972988
);
973989
runOnlyForDeploymentPostprocessing = 0;
974990
};

Tests/DeferredTests/DeferredTests.swift

Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
import XCTest
1010
import Dispatch
11-
import Foundation
12-
1311
import Deferred
1412

1513
// swiftlint:disable file_length
@@ -24,18 +22,17 @@ class DeferredTests: XCTestCase {
2422
("testValueBlocksWhileUnfilled", testValueBlocksWhileUnfilled),
2523
("testValueUnblocksWhenUnfilledIsFilled", testValueUnblocksWhenUnfilledIsFilled),
2624
("testFill", testFill),
27-
("testFillMultipleTimes", testFillMultipleTimes),
25+
("testCannotFillMultipleTimes", testCannotFillMultipleTimes),
2826
("testIsFilled", testIsFilled),
29-
("testUponWithFilled", testUponWithFilled),
30-
("testUponNotCalledWhileUnfilled", testUponNotCalledWhileUnfilled),
3127
("testUponCalledWhenFilled", testUponCalledWhenFilled),
28+
("testUponCalledIfAlreadyFilled", testUponCalledIfAlreadyFilled),
29+
("testUponNotCalledWhileUnfilled", testUponNotCalledWhileUnfilled),
3230
("testUponMainQueueCalledWhenFilled", testUponMainQueueCalledWhenFilled),
3331
("testConcurrentUpon", testConcurrentUpon),
3432
("testAllCopiesOfADeferredValueRepresentTheSameDeferredValue", testAllCopiesOfADeferredValueRepresentTheSameDeferredValue),
3533
("testDeferredOptionalBehavesCorrectly", testDeferredOptionalBehavesCorrectly),
3634
("testIsFilledCanBeCalledMultipleTimesNotFilled", testIsFilledCanBeCalledMultipleTimesNotFilled),
3735
("testIsFilledCanBeCalledMultipleTimesWhenFilled", testIsFilledCanBeCalledMultipleTimesWhenFilled),
38-
("testFillAndIsFilledPostcondition", testFillAndIsFilledPostcondition),
3936
("testSimultaneousFill", testSimultaneousFill),
4037
("testDebugDescriptionUnfilled", testDebugDescriptionUnfilled),
4138
("testDebugDescriptionFilled", testDebugDescriptionFilled),
@@ -62,8 +59,9 @@ class DeferredTests: XCTestCase {
6259
}
6360

6461
func testPeekWhenFilled() {
65-
let filled = Deferred(filledWith: 1)
66-
XCTAssertEqual(filled.peek(), 1)
62+
let toBeFilled = Deferred<Int>()
63+
toBeFilled.fill(with: 1)
64+
XCTAssertEqual(toBeFilled.peek(), 1)
6765
}
6866

6967
func testWaitWithTimeout() {
@@ -81,8 +79,9 @@ class DeferredTests: XCTestCase {
8179
}
8280

8381
func testValueOnFilled() {
84-
let filled = Deferred(filledWith: 2)
85-
XCTAssertEqual(filled.value, 2)
82+
let toBeFilled = Deferred<Int>()
83+
toBeFilled.fill(with: 2)
84+
XCTAssertEqual(toBeFilled.value, 2)
8685
}
8786

8887
func testValueBlocksWhileUnfilled() {
@@ -122,10 +121,14 @@ class DeferredTests: XCTestCase {
122121
XCTAssertEqual(toBeFilled.value, 1)
123122
}
124123

125-
func testFillMultipleTimes() {
126-
let toBeFilledRepeatedly = Deferred(filledWith: 1)
124+
func testCannotFillMultipleTimes() {
125+
let toBeFilledRepeatedly = Deferred<Int>()
126+
127+
toBeFilledRepeatedly.fill(with: 1)
127128
XCTAssertEqual(toBeFilledRepeatedly.value, 1)
129+
128130
XCTAssertFalse(toBeFilledRepeatedly.fill(with: 2))
131+
129132
XCTAssertEqual(toBeFilledRepeatedly.value, 1)
130133
}
131134

@@ -142,16 +145,33 @@ class DeferredTests: XCTestCase {
142145
shortWait(for: [ expect ])
143146
}
144147

145-
func testUponWithFilled() {
146-
let deferred = Deferred(filledWith: 1)
148+
func testUponCalledWhenFilled() {
149+
let toBeFilled = Deferred<Int>()
147150
let allExpectations = (0 ..< 10).map { (iteration) -> XCTestExpectation in
148151
let expect = expectation(description: "upon block #\(iteration) called with correct value")
149-
deferred.upon { value in
152+
toBeFilled.upon { value in
153+
XCTAssertEqual(value, 1)
154+
expect.fulfill()
155+
}
156+
return expect
157+
}
158+
toBeFilled.fill(with: 1)
159+
shortWait(for: allExpectations)
160+
}
161+
162+
func testUponCalledIfAlreadyFilled() {
163+
let toBeFilled = Deferred<Int>()
164+
toBeFilled.fill(with: 1)
165+
166+
let allExpectations = (0 ..< 10).map { (iteration) -> XCTestExpectation in
167+
let expect = expectation(description: "upon block #\(iteration) not called while deferred is unfilled")
168+
toBeFilled.upon { value in
150169
XCTAssertEqual(value, 1)
151170
expect.fulfill()
152171
}
153172
return expect
154173
}
174+
155175
shortWait(for: allExpectations)
156176
}
157177

@@ -170,21 +190,6 @@ class DeferredTests: XCTestCase {
170190
shortWait(for: [ expect ])
171191
}
172192

173-
func testUponCalledWhenFilled() {
174-
let deferred = Deferred<Int>()
175-
let allExpectations = (0 ..< 10).map { (iteration) -> XCTestExpectation in
176-
let expect = expectation(description: "upon block #\(iteration) not called while deferred is unfilled")
177-
deferred.upon { value in
178-
XCTAssertEqual(value, 1)
179-
expect.fulfill()
180-
}
181-
return expect
182-
}
183-
184-
deferred.fill(with: 1)
185-
shortWait(for: allExpectations)
186-
}
187-
188193
func testUponMainQueueCalledWhenFilled() {
189194
let deferred = Deferred<Int>()
190195

@@ -251,18 +256,19 @@ class DeferredTests: XCTestCase {
251256
}
252257

253258
func testDeferredOptionalBehavesCorrectly() {
254-
let deferred = Deferred<Int?>(filledWith: nil)
259+
let toBeFilled = Deferred<Int?>()
260+
toBeFilled.fill(with: nil)
255261

256262
let beforeExpect = expectation(description: "already filled with nil optional")
257-
deferred.upon { (value) in
263+
toBeFilled.upon { (value) in
258264
XCTAssertNil(value)
259265
beforeExpect.fulfill()
260266
}
261267

262-
XCTAssertFalse(deferred.fill(with: 42))
268+
XCTAssertFalse(toBeFilled.fill(with: 42))
263269

264270
let afterExpect = expectation(description: "stays filled with same optional")
265-
deferred.upon { (value) in
271+
toBeFilled.upon { (value) in
266272
XCTAssertNil(value)
267273
afterExpect.fulfill()
268274
}
@@ -271,17 +277,20 @@ class DeferredTests: XCTestCase {
271277
}
272278

273279
func testIsFilledCanBeCalledMultipleTimesNotFilled() {
274-
let deferred = Deferred<Int>()
275-
XCTAssertFalse(deferred.isFilled)
276-
XCTAssertFalse(deferred.isFilled)
277-
XCTAssertFalse(deferred.isFilled)
280+
let unfilled = Deferred<Int>()
281+
282+
for _ in 0 ..< 5 {
283+
XCTAssertFalse(unfilled.isFilled)
284+
}
278285
}
279286

280287
func testIsFilledCanBeCalledMultipleTimesWhenFilled() {
281-
let deferred = Deferred<Int>(filledWith: 42)
282-
XCTAssertTrue(deferred.isFilled)
283-
XCTAssertTrue(deferred.isFilled)
284-
XCTAssertTrue(deferred.isFilled)
288+
let toBeFilled = Deferred<Int>()
289+
toBeFilled.fill(with: 42)
290+
291+
for _ in 0 ..< 5 {
292+
XCTAssertTrue(toBeFilled.isFilled)
293+
}
285294
}
286295

287296
// The QoS APIs do not behave as expected on the iOS Simulator, so we only
@@ -329,15 +338,6 @@ class DeferredTests: XCTestCase {
329338

330339
#endif // end QoS tests that require a real device
331340

332-
func testFillAndIsFilledPostcondition() {
333-
let deferred = Deferred<Int>()
334-
XCTAssertFalse(deferred.isFilled)
335-
XCTAssertNil(deferred.peek())
336-
deferred.fill(with: 42)
337-
XCTAssertNotNil(deferred.peek())
338-
XCTAssertTrue(deferred.isFilled)
339-
}
340-
341341
func testSimultaneousFill() {
342342
let deferred = Deferred<Int>()
343343
let startGroup = DispatchGroup()
@@ -362,42 +362,48 @@ class DeferredTests: XCTestCase {
362362
}
363363

364364
func testDebugDescriptionUnfilled() {
365-
let deferred = Deferred<Int>()
366-
XCTAssertEqual("\(deferred)", "Deferred(not filled)")
365+
let unfilled = Deferred<Int>()
366+
XCTAssertEqual("\(unfilled)", "Deferred(not filled)")
367367
}
368368

369369
func testDebugDescriptionFilled() {
370-
let deferred = Deferred<Int>(filledWith: 42)
371-
XCTAssertEqual("\(deferred)", "Deferred(42)")
370+
let toBeFilled = Deferred<Int>(filledWith: 42)
371+
toBeFilled.fill(with: 42)
372+
373+
XCTAssertEqual("\(toBeFilled)", "Deferred(42)")
372374
}
373375

374376
func testDebugDescriptionFilledWhenValueIsVoid() {
375-
let deferred = Deferred<Void>(filledWith: ())
376-
XCTAssertEqual("\(deferred)", "Deferred(filled)")
377+
let toBeFilled = Deferred<Void>()
378+
toBeFilled.fill(with: ())
379+
380+
XCTAssertEqual("\(toBeFilled)", "Deferred(filled)")
377381
}
378382

379383
func testReflectionUnfilled() {
380-
let deferred = Deferred<Int>()
384+
let unfilled = Deferred<Int>()
381385

382-
let magicMirror = Mirror(reflecting: deferred)
386+
let magicMirror = Mirror(reflecting: unfilled)
383387
XCTAssertEqual(magicMirror.displayStyle, .optional)
384388
XCTAssertNil(magicMirror.superclassMirror)
385389
XCTAssertEqual(magicMirror.descendant("isFilled") as? Bool, false)
386390
}
387391

388392
func testReflectionFilled() {
389-
let deferred = Deferred<Int>(filledWith: 42)
393+
let toBeFilled = Deferred<Int>()
394+
toBeFilled.fill(with: 42)
390395

391-
let magicMirror = Mirror(reflecting: deferred)
396+
let magicMirror = Mirror(reflecting: toBeFilled)
392397
XCTAssertEqual(magicMirror.displayStyle, .optional)
393398
XCTAssertNil(magicMirror.superclassMirror)
394399
XCTAssertEqual(magicMirror.descendant(0) as? Int, 42)
395400
}
396401

397402
func testReflectionFilledWhenValueIsVoid() {
398-
let deferred = Deferred<Void>(filledWith: ())
403+
let toBeFilled = Deferred<Void>()
404+
toBeFilled.fill(with: ())
399405

400-
let magicMirror = Mirror(reflecting: deferred)
406+
let magicMirror = Mirror(reflecting: toBeFilled)
401407
XCTAssertEqual(magicMirror.displayStyle, .optional)
402408
XCTAssertNil(magicMirror.superclassMirror)
403409
XCTAssertEqual(magicMirror.descendant("isFilled") as? Bool, true)

0 commit comments

Comments
 (0)