Skip to content
This repository was archived by the owner on Aug 29, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Deferred.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@
DB126D951E5369E200054E95 /* Deferred.h in Headers */ = {isa = PBXBuildFile; fileRef = DB524CA11D85200C00DDF16D /* Deferred.h */; settings = {ATTRIBUTES = (Public, ); }; };
DB126D961E5369E300054E95 /* Deferred.h in Headers */ = {isa = PBXBuildFile; fileRef = DB524CA11D85200C00DDF16D /* Deferred.h */; settings = {ATTRIBUTES = (Public, ); }; };
DB126D971E5369E300054E95 /* Deferred.h in Headers */ = {isa = PBXBuildFile; fileRef = DB524CA11D85200C00DDF16D /* Deferred.h */; settings = {ATTRIBUTES = (Public, ); }; };
DB34FC902096D335005D5B82 /* ObjectDeferredTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB34FC8F2096D335005D5B82 /* ObjectDeferredTests.swift */; };
DB34FC912096D335005D5B82 /* ObjectDeferredTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB34FC8F2096D335005D5B82 /* ObjectDeferredTests.swift */; };
DB34FC922096D335005D5B82 /* ObjectDeferredTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB34FC8F2096D335005D5B82 /* ObjectDeferredTests.swift */; };
DB34FC942096DCE1005D5B82 /* FilledDeferredTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB34FC932096DCE1005D5B82 /* FilledDeferredTests.swift */; };
DB34FC952096DCE1005D5B82 /* FilledDeferredTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB34FC932096DCE1005D5B82 /* FilledDeferredTests.swift */; };
DB34FC962096DCE1005D5B82 /* FilledDeferredTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB34FC932096DCE1005D5B82 /* FilledDeferredTests.swift */; };
DB8A071C2060D38C00639AB3 /* PerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB8A071B2060D38C00639AB3 /* PerformanceTests.swift */; };
DB8A071D2060D38C00639AB3 /* PerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB8A071B2060D38C00639AB3 /* PerformanceTests.swift */; };
DB8A071E2060D38C00639AB3 /* PerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB8A071B2060D38C00639AB3 /* PerformanceTests.swift */; };
Expand Down Expand Up @@ -223,6 +229,8 @@
DB126CD81E53686900054E95 /* Deferred.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Deferred.framework; sourceTree = BUILT_PRODUCTS_DIR; };
DB126CE01E53686900054E95 /* TVDeferredTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TVDeferredTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
DB126CF41E53687300054E95 /* Deferred.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Deferred.framework; sourceTree = BUILT_PRODUCTS_DIR; };
DB34FC8F2096D335005D5B82 /* ObjectDeferredTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjectDeferredTests.swift; sourceTree = "<group>"; };
DB34FC932096DCE1005D5B82 /* FilledDeferredTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilledDeferredTests.swift; sourceTree = "<group>"; };
DB39B2D01DDE194C00DDE4C0 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
DB4002691DDC21B300382BAE /* SwiftBugTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftBugTests.swift; sourceTree = "<group>"; };
DB524C8C1D851E5B00DDF16D /* Base.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Base.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -455,10 +463,12 @@
children = (
DB55F1F01D96968E00FC1439 /* DeferredTests.swift */,
DB55F1F11D96968E00FC1439 /* ExistentialFutureTests.swift */,
DB34FC932096DCE1005D5B82 /* FilledDeferredTests.swift */,
DB55F1F21D96968E00FC1439 /* FutureCustomExecutorTests.swift */,
DB55F1F31D96968E00FC1439 /* FutureIgnoreTests.swift */,
DB55F20B1D969A1B00FC1439 /* FutureTests.swift */,
DB55F1F41D96968E00FC1439 /* LockingTests.swift */,
DB34FC8F2096D335005D5B82 /* ObjectDeferredTests.swift */,
DB8A071B2060D38C00639AB3 /* PerformanceTests.swift */,
DB55F1F51D96968E00FC1439 /* ProtectedTests.swift */,
DB4002691DDC21B300382BAE /* SwiftBugTests.swift */,
Expand Down Expand Up @@ -825,10 +835,12 @@
DB126D6C1E5368B900054E95 /* TaskGroupTests.swift in Sources */,
DB126D661E5368B900054E95 /* ProtectedTests.swift in Sources */,
DB126D6E1E5368B900054E95 /* TaskWorkItemTests.swift in Sources */,
DB34FC942096DCE1005D5B82 /* FilledDeferredTests.swift in Sources */,
DB126D6D1E5368B900054E95 /* TaskTests.swift in Sources */,
DB126D621E5368B900054E95 /* FutureCustomExecutorTests.swift in Sources */,
DB126D611E5368B900054E95 /* ExistentialFutureTests.swift in Sources */,
DB126D681E5368B900054E95 /* ResultRecoveryTests.swift in Sources */,
DB34FC902096D335005D5B82 /* ObjectDeferredTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -886,10 +898,12 @@
DB126D7C1E5368B900054E95 /* TaskGroupTests.swift in Sources */,
DB126D761E5368B900054E95 /* ProtectedTests.swift in Sources */,
DB126D7E1E5368B900054E95 /* TaskWorkItemTests.swift in Sources */,
DB34FC952096DCE1005D5B82 /* FilledDeferredTests.swift in Sources */,
DB126D7D1E5368B900054E95 /* TaskTests.swift in Sources */,
DB126D721E5368B900054E95 /* FutureCustomExecutorTests.swift in Sources */,
DB126D711E5368B900054E95 /* ExistentialFutureTests.swift in Sources */,
DB126D781E5368B900054E95 /* ResultRecoveryTests.swift in Sources */,
DB34FC912096D335005D5B82 /* ObjectDeferredTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -947,10 +961,12 @@
DB126D8C1E5368BA00054E95 /* TaskGroupTests.swift in Sources */,
DB126D861E5368BA00054E95 /* ProtectedTests.swift in Sources */,
DB126D8E1E5368BA00054E95 /* TaskWorkItemTests.swift in Sources */,
DB34FC962096DCE1005D5B82 /* FilledDeferredTests.swift in Sources */,
DB126D8D1E5368BA00054E95 /* TaskTests.swift in Sources */,
DB126D821E5368BA00054E95 /* FutureCustomExecutorTests.swift in Sources */,
DB126D811E5368BA00054E95 /* ExistentialFutureTests.swift in Sources */,
DB126D881E5368BA00054E95 /* ResultRecoveryTests.swift in Sources */,
DB34FC922096D335005D5B82 /* ObjectDeferredTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
134 changes: 70 additions & 64 deletions Tests/DeferredTests/DeferredTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

import XCTest
import Dispatch
import Foundation

@testable import Deferred
import Deferred

// swiftlint:disable file_length
// swiftlint:disable type_body_length
Expand All @@ -24,18 +22,17 @@ class DeferredTests: XCTestCase {
("testValueBlocksWhileUnfilled", testValueBlocksWhileUnfilled),
("testValueUnblocksWhenUnfilledIsFilled", testValueUnblocksWhenUnfilledIsFilled),
("testFill", testFill),
("testFillMultipleTimes", testFillMultipleTimes),
("testCannotFillMultipleTimes", testCannotFillMultipleTimes),
("testIsFilled", testIsFilled),
("testUponWithFilled", testUponWithFilled),
("testUponNotCalledWhileUnfilled", testUponNotCalledWhileUnfilled),
("testUponCalledWhenFilled", testUponCalledWhenFilled),
("testUponCalledIfAlreadyFilled", testUponCalledIfAlreadyFilled),
("testUponNotCalledWhileUnfilled", testUponNotCalledWhileUnfilled),
("testUponMainQueueCalledWhenFilled", testUponMainQueueCalledWhenFilled),
("testConcurrentUpon", testConcurrentUpon),
("testAllCopiesOfADeferredValueRepresentTheSameDeferredValue", testAllCopiesOfADeferredValueRepresentTheSameDeferredValue),
("testDeferredOptionalBehavesCorrectly", testDeferredOptionalBehavesCorrectly),
("testIsFilledCanBeCalledMultipleTimesNotFilled", testIsFilledCanBeCalledMultipleTimesNotFilled),
("testIsFilledCanBeCalledMultipleTimesWhenFilled", testIsFilledCanBeCalledMultipleTimesWhenFilled),
("testFillAndIsFilledPostcondition", testFillAndIsFilledPostcondition),
("testSimultaneousFill", testSimultaneousFill),
("testDebugDescriptionUnfilled", testDebugDescriptionUnfilled),
("testDebugDescriptionFilled", testDebugDescriptionFilled),
Expand All @@ -62,8 +59,9 @@ class DeferredTests: XCTestCase {
}

func testPeekWhenFilled() {
let filled = Deferred(filledWith: 1)
XCTAssertEqual(filled.peek(), 1)
let toBeFilled = Deferred<Int>()
toBeFilled.fill(with: 1)
XCTAssertEqual(toBeFilled.peek(), 1)
}

func testWaitWithTimeout() {
Expand All @@ -81,8 +79,9 @@ class DeferredTests: XCTestCase {
}

func testValueOnFilled() {
let filled = Deferred(filledWith: 2)
XCTAssertEqual(filled.value, 2)
let toBeFilled = Deferred<Int>()
toBeFilled.fill(with: 2)
XCTAssertEqual(toBeFilled.value, 2)
}

func testValueBlocksWhileUnfilled() {
Expand Down Expand Up @@ -122,10 +121,14 @@ class DeferredTests: XCTestCase {
XCTAssertEqual(toBeFilled.value, 1)
}

func testFillMultipleTimes() {
let toBeFilledRepeatedly = Deferred(filledWith: 1)
func testCannotFillMultipleTimes() {
let toBeFilledRepeatedly = Deferred<Int>()

toBeFilledRepeatedly.fill(with: 1)
XCTAssertEqual(toBeFilledRepeatedly.value, 1)

XCTAssertFalse(toBeFilledRepeatedly.fill(with: 2))

XCTAssertEqual(toBeFilledRepeatedly.value, 1)
}

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

func testUponWithFilled() {
let deferred = Deferred(filledWith: 1)
func testUponCalledWhenFilled() {
let toBeFilled = Deferred<Int>()
let allExpectations = (0 ..< 10).map { (iteration) -> XCTestExpectation in
let expect = expectation(description: "upon block #\(iteration) called with correct value")
deferred.upon { value in
toBeFilled.upon { value in
XCTAssertEqual(value, 1)
expect.fulfill()
}
return expect
}
toBeFilled.fill(with: 1)
shortWait(for: allExpectations)
}

func testUponCalledIfAlreadyFilled() {
let toBeFilled = Deferred<Int>()
toBeFilled.fill(with: 1)

let allExpectations = (0 ..< 10).map { (iteration) -> XCTestExpectation in
let expect = expectation(description: "upon block #\(iteration) not called while deferred is unfilled")
toBeFilled.upon { value in
XCTAssertEqual(value, 1)
expect.fulfill()
}
return expect
}

shortWait(for: allExpectations)
}

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

func testUponCalledWhenFilled() {
let deferred = Deferred<Int>()
let allExpectations = (0 ..< 10).map { (iteration) -> XCTestExpectation in
let expect = expectation(description: "upon block #\(iteration) not called while deferred is unfilled")
deferred.upon { value in
XCTAssertEqual(value, 1)
expect.fulfill()
}
return expect
}

deferred.fill(with: 1)
shortWait(for: allExpectations)
}

func testUponMainQueueCalledWhenFilled() {
let deferred = Deferred<Int>()

Expand Down Expand Up @@ -251,18 +256,19 @@ class DeferredTests: XCTestCase {
}

func testDeferredOptionalBehavesCorrectly() {
let deferred = Deferred<Int?>(filledWith: nil)
let toBeFilled = Deferred<Int?>()
toBeFilled.fill(with: nil)

let beforeExpect = expectation(description: "already filled with nil optional")
deferred.upon { (value) in
toBeFilled.upon { (value) in
XCTAssertNil(value)
beforeExpect.fulfill()
}

XCTAssertFalse(deferred.fill(with: 42))
XCTAssertFalse(toBeFilled.fill(with: 42))

let afterExpect = expectation(description: "stays filled with same optional")
deferred.upon { (value) in
toBeFilled.upon { (value) in
XCTAssertNil(value)
afterExpect.fulfill()
}
Expand All @@ -271,17 +277,20 @@ class DeferredTests: XCTestCase {
}

func testIsFilledCanBeCalledMultipleTimesNotFilled() {
let deferred = Deferred<Int>()
XCTAssertFalse(deferred.isFilled)
XCTAssertFalse(deferred.isFilled)
XCTAssertFalse(deferred.isFilled)
let unfilled = Deferred<Int>()

for _ in 0 ..< 5 {
XCTAssertFalse(unfilled.isFilled)
}
}

func testIsFilledCanBeCalledMultipleTimesWhenFilled() {
let deferred = Deferred<Int>(filledWith: 42)
XCTAssertTrue(deferred.isFilled)
XCTAssertTrue(deferred.isFilled)
XCTAssertTrue(deferred.isFilled)
let toBeFilled = Deferred<Int>()
toBeFilled.fill(with: 42)

for _ in 0 ..< 5 {
XCTAssertTrue(toBeFilled.isFilled)
}
}

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

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

func testFillAndIsFilledPostcondition() {
let deferred = Deferred<Int>()
XCTAssertFalse(deferred.isFilled)
XCTAssertNil(deferred.peek())
deferred.fill(with: 42)
XCTAssertNotNil(deferred.peek())
XCTAssertTrue(deferred.isFilled)
}

func testSimultaneousFill() {
let deferred = Deferred<Int>()
let startGroup = DispatchGroup()
Expand All @@ -362,42 +362,48 @@ class DeferredTests: XCTestCase {
}

func testDebugDescriptionUnfilled() {
let deferred = Deferred<Int>()
XCTAssertEqual("\(deferred)", "Deferred(not filled)")
let unfilled = Deferred<Int>()
XCTAssertEqual("\(unfilled)", "Deferred(not filled)")
}

func testDebugDescriptionFilled() {
let deferred = Deferred<Int>(filledWith: 42)
XCTAssertEqual("\(deferred)", "Deferred(42)")
let toBeFilled = Deferred<Int>(filledWith: 42)
toBeFilled.fill(with: 42)

XCTAssertEqual("\(toBeFilled)", "Deferred(42)")
}

func testDebugDescriptionFilledWhenValueIsVoid() {
let deferred = Deferred<Void>(filledWith: ())
XCTAssertEqual("\(deferred)", "Deferred(filled)")
let toBeFilled = Deferred<Void>()
toBeFilled.fill(with: ())

XCTAssertEqual("\(toBeFilled)", "Deferred(filled)")
}

func testReflectionUnfilled() {
let deferred = Deferred<Int>()
let unfilled = Deferred<Int>()

let magicMirror = Mirror(reflecting: deferred)
let magicMirror = Mirror(reflecting: unfilled)
XCTAssertEqual(magicMirror.displayStyle, .optional)
XCTAssertNil(magicMirror.superclassMirror)
XCTAssertEqual(magicMirror.descendant("isFilled") as? Bool, false)
}

func testReflectionFilled() {
let deferred = Deferred<Int>(filledWith: 42)
let toBeFilled = Deferred<Int>()
toBeFilled.fill(with: 42)

let magicMirror = Mirror(reflecting: deferred)
let magicMirror = Mirror(reflecting: toBeFilled)
XCTAssertEqual(magicMirror.displayStyle, .optional)
XCTAssertNil(magicMirror.superclassMirror)
XCTAssertEqual(magicMirror.descendant(0) as? Int, 42)
}

func testReflectionFilledWhenValueIsVoid() {
let deferred = Deferred<Void>(filledWith: ())
let toBeFilled = Deferred<Void>()
toBeFilled.fill(with: ())

let magicMirror = Mirror(reflecting: deferred)
let magicMirror = Mirror(reflecting: toBeFilled)
XCTAssertEqual(magicMirror.displayStyle, .optional)
XCTAssertNil(magicMirror.superclassMirror)
XCTAssertEqual(magicMirror.descendant("isFilled") as? Bool, true)
Expand Down
Loading