88
99import XCTest
1010import Dispatch
11- import Foundation
12-
1311import 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