-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move CancelableProcedure to CommonInternal
- Loading branch information
Showing
3 changed files
with
123 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,131 @@ | ||
import XCTest | ||
@testable import VueFlux | ||
@testable import VueFluxReactive | ||
|
||
private protocol CancelableProcedureProtocol { | ||
associatedtype Value | ||
|
||
var isCanceled: Bool { get } | ||
|
||
init(_ execute: @escaping (Value) -> Void) | ||
func execute(with value: @autoclosure () -> Value) | ||
func cancel() | ||
} | ||
|
||
extension VueFlux.CancelableProcedure: CancelableProcedureProtocol {} | ||
extension VueFluxReactive.CancelableProcedure: CancelableProcedureProtocol {} | ||
|
||
final class CancelableProcedureTests: XCTestCase { | ||
func testVoidProcedure() { | ||
var value = 0 | ||
|
||
let procedure = CancelableProcedure<Void> { | ||
value += 1 | ||
func runTest<CancelableProcedure: CancelableProcedureProtocol>(for type: CancelableProcedure.Type) where CancelableProcedure.Value == Void { | ||
|
||
var value = 0 | ||
|
||
let procedure = CancelableProcedure { | ||
value += 1 | ||
} | ||
|
||
XCTAssertEqual(value, 0) | ||
|
||
procedure.execute(with: ()) | ||
|
||
XCTAssertEqual(value, 1) | ||
|
||
procedure.cancel() | ||
procedure.execute(with: ()) | ||
|
||
XCTAssertEqual(value, 1) | ||
|
||
procedure.cancel() | ||
procedure.execute(with: ()) | ||
|
||
XCTAssertEqual(value, 1) | ||
} | ||
|
||
XCTAssertEqual(value, 0) | ||
|
||
procedure.execute() | ||
|
||
XCTAssertEqual(value, 1) | ||
|
||
procedure.cancel() | ||
procedure.execute() | ||
|
||
XCTAssertEqual(value, 1) | ||
|
||
procedure.cancel() | ||
procedure.execute() | ||
|
||
XCTAssertEqual(value, 1) | ||
runTest(for: VueFlux.CancelableProcedure<Void>.self) | ||
runTest(for: VueFluxReactive.CancelableProcedure<Void>.self) | ||
} | ||
|
||
func testValueWorkItem() { | ||
var value = 0 | ||
|
||
let procedure = CancelableProcedure<Int> { int in | ||
value = int | ||
func runTest<CancelableProcedure: CancelableProcedureProtocol>(for type: CancelableProcedure.Type) where CancelableProcedure.Value == Int { | ||
var value = 0 | ||
|
||
let procedure = CancelableProcedure { int in | ||
value = int | ||
} | ||
|
||
XCTAssertEqual(value, 0) | ||
|
||
procedure.execute(with: 1) | ||
|
||
XCTAssertFalse(procedure.isCanceled) | ||
XCTAssertEqual(value, 1) | ||
|
||
procedure.cancel() | ||
procedure.execute(with: 2) | ||
|
||
XCTAssertTrue(procedure.isCanceled) | ||
XCTAssertEqual(value, 1) | ||
|
||
procedure.cancel() | ||
procedure.execute(with: 3) | ||
|
||
XCTAssertTrue(procedure.isCanceled) | ||
XCTAssertEqual(value, 1) | ||
} | ||
|
||
XCTAssertEqual(value, 0) | ||
|
||
procedure.execute(with: 1) | ||
|
||
XCTAssertFalse(procedure.isCanceled) | ||
XCTAssertEqual(value, 1) | ||
|
||
procedure.cancel() | ||
procedure.execute(with: 2) | ||
|
||
XCTAssertTrue(procedure.isCanceled) | ||
XCTAssertEqual(value, 1) | ||
|
||
procedure.cancel() | ||
procedure.execute(with: 3) | ||
|
||
XCTAssertTrue(procedure.isCanceled) | ||
XCTAssertEqual(value, 1) | ||
runTest(for: VueFlux.CancelableProcedure<Int>.self) | ||
runTest(for: VueFluxReactive.CancelableProcedure<Int>.self) | ||
} | ||
|
||
func testCancelProcedureAsync() { | ||
let queue = DispatchQueue(label: "testCancelProcedureAsync") | ||
func runTest<CancelableProcedure: CancelableProcedureProtocol>(for type: CancelableProcedure.Type) where CancelableProcedure.Value == Int { | ||
let queue = DispatchQueue(label: "testCancelProcedureAsync") | ||
|
||
var value = 0 | ||
|
||
let expectation = self.expectation(description: "testCancelProcedureAsync") | ||
|
||
let procedure = CancelableProcedure { int in | ||
value = int | ||
} | ||
|
||
XCTAssertFalse(procedure.isCanceled) | ||
|
||
queue.suspend() | ||
|
||
queue.async { | ||
procedure.execute(with: 1) | ||
} | ||
|
||
procedure.cancel() | ||
queue.resume() | ||
|
||
queue.async(execute: expectation.fulfill) | ||
|
||
waitForExpectations(timeout: 1) { _ in | ||
XCTAssertTrue(procedure.isCanceled) | ||
XCTAssertEqual(value, 0) | ||
} | ||
} | ||
|
||
runTest(for: VueFlux.CancelableProcedure<Int>.self) | ||
runTest(for: VueFluxReactive.CancelableProcedure<Int>.self) | ||
} | ||
|
||
func testExecuteVoidProcedure() { | ||
var value = 0 | ||
|
||
let expectation = self.expectation(description: "testCancelProcedureAsync") | ||
|
||
let procedure = CancelableProcedure<Int> { int in | ||
value = int | ||
let vueFluxProcedure = VueFlux.CancelableProcedure<Void> { | ||
value = 1 | ||
} | ||
|
||
XCTAssertFalse(procedure.isCanceled) | ||
|
||
queue.suspend() | ||
|
||
queue.async { | ||
procedure.execute(with: 1) | ||
let vueFluxReactiveProcedure = VueFluxReactive.CancelableProcedure<Void> { | ||
value = 2 | ||
} | ||
|
||
procedure.cancel() | ||
queue.resume() | ||
|
||
queue.async(execute: expectation.fulfill) | ||
vueFluxProcedure.execute() | ||
vueFluxReactiveProcedure.execute() | ||
|
||
waitForExpectations(timeout: 1) { _ in | ||
XCTAssertTrue(procedure.isCanceled) | ||
XCTAssertEqual(value, 0) | ||
} | ||
XCTAssertEqual(value, 2) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters