-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- update tests
- Loading branch information
Alex Belozierov
committed
Jan 14, 2020
1 parent
03c03da
commit 5b340c5
Showing
9 changed files
with
498 additions
and
299 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
68 changes: 68 additions & 0 deletions
68
Tests/SwiftCoroutineTests/CoFutureTests/CoFutureAwaitTests.swift
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// CoFutureAwaitTests.swift | ||
// SwiftCoroutine | ||
// | ||
// Created by Alex Belozierov on 13.01.2020. | ||
// Copyright © 2020 Alex Belozierov. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import SwiftCoroutine | ||
|
||
class CoFutureAwaitTests: XCTestCase { | ||
|
||
func testOutCoroutineCall() { | ||
let promise = CoPromise<Int>() | ||
do { | ||
_ = try promise.await() | ||
XCTFail() | ||
} catch let error as Coroutine.CoroutineError { | ||
return XCTAssertEqual(error, .mustBeCalledInsideCoroutine) | ||
} catch { | ||
XCTFail() | ||
} | ||
} | ||
|
||
func testAwait() { | ||
testAwait(sec: 1) { promise in | ||
XCTAssertEqual(try promise.await(), 1) | ||
} | ||
} | ||
|
||
func testAwaitResult() { | ||
testAwait(sec: 1) { promise in | ||
XCTAssertEqual(promise.awaitResult(), 1) | ||
} | ||
} | ||
|
||
func testAwaitTimeout() { | ||
testAwait(sec: 1) { promise in | ||
XCTAssertEqual(try promise.await(timeout: .now() + 2), 1) | ||
} | ||
} | ||
|
||
func testAwaitTimeout2() { | ||
testAwait(sec: 2) { promise in | ||
do { | ||
_ = try promise.await(timeout: .now() + 1) | ||
} catch let error as CoFutureError { | ||
return XCTAssertEqual(error, .timeout) | ||
} | ||
XCTFail() | ||
} | ||
} | ||
|
||
private func testAwait(sec: Int, test: @escaping (CoPromise<Int>) throws -> Void) { | ||
let exp = expectation(description: "testAwaitTimeout") | ||
let promise = CoPromise<Int>() | ||
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(sec)) { | ||
promise.send(1) | ||
} | ||
coroutine { | ||
try test(promise) | ||
exp.fulfill() | ||
} | ||
wait(for: [exp], timeout: 2) | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
Tests/SwiftCoroutineTests/CoFutureTests/CoFututeCompositeTests.swift
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// CoFututeCompositeTests.swift | ||
// SwiftCoroutine | ||
// | ||
// Created by Alex Belozierov on 13.01.2020. | ||
// Copyright © 2020 Alex Belozierov. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import SwiftCoroutine | ||
|
||
class CoFututeCompositeTests: XCTestCase { | ||
|
||
func testComposite() { | ||
func test<T>(futures: [CoFuture<T>], | ||
@CoFututeComposite<T> builder: @escaping () -> [CoFuture<T>]) { | ||
XCTAssertEqual(futures, builder()) | ||
} | ||
let promises = (0..<3).map { _ in CoPromise<Int>() } | ||
test(futures: promises) { | ||
promises[0] | ||
promises[1] | ||
promises[2] | ||
} | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
Tests/SwiftCoroutineTests/CoFutureTests/CoFututreWaitTests.swift
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// CoFututreWaitTests.swift | ||
// SwiftCoroutine | ||
// | ||
// Created by Alex Belozierov on 13.01.2020. | ||
// Copyright © 2020 Alex Belozierov. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import SwiftCoroutine | ||
|
||
class CoFututreWaitTests: XCTestCase { | ||
|
||
func testWait() { | ||
testWait(sec: 1) { promise in | ||
XCTAssertEqual(try? promise.wait(), 1) | ||
} | ||
} | ||
|
||
func testWaitTimeout() { | ||
testWait(sec: 1) { promise in | ||
XCTAssertEqual(try? promise.wait(timeout: .now() + 2), 1) | ||
} | ||
} | ||
|
||
func testWaitTimeout2() { | ||
testWait(sec: 2) { promise in | ||
do { | ||
_ = try promise.wait(timeout: .now() + 1) | ||
} catch let error as CoFutureError { | ||
return XCTAssertEqual(error, .timeout) | ||
} | ||
XCTFail() | ||
} | ||
} | ||
|
||
private func testWait(sec: Int, test: @escaping (CoPromise<Int>) throws -> Void) { | ||
let promise = CoPromise<Int>() | ||
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(sec)) { | ||
promise.send(1) | ||
} | ||
do { | ||
try test(promise) | ||
} catch { | ||
XCTFail() | ||
} | ||
} | ||
|
||
} |
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,36 +1,36 @@ | ||
//// | ||
//// CoFutureTests.swift | ||
//// SwiftCoroutine | ||
//// | ||
//// Created by Alex Belozierov on 20.12.2019. | ||
//// Copyright © 2019 Alex Belozierov. All rights reserved. | ||
//// | ||
// | ||
// CoFutureTests.swift | ||
// SwiftCoroutine | ||
//import XCTest | ||
//import SwiftCoroutine | ||
// | ||
// Created by Alex Belozierov on 20.12.2019. | ||
// Copyright © 2019 Alex Belozierov. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import SwiftCoroutine | ||
|
||
class CoFutureTests2: XCTestCase { | ||
|
||
func testTransform() { | ||
let expectation = XCTestExpectation(description: "Test Transform") | ||
expectation.expectedFulfillmentCount = 3 | ||
let promise = async { () -> Int in | ||
sleep(1) | ||
return 1 | ||
} | ||
let transformed = promise | ||
.transformOutput { $0 * 2 } | ||
.onError { _ in XCTFail() } | ||
.transformOutput { $0 * 3 } | ||
.onSuccess { XCTAssertEqual($0, 6) } | ||
.onSuccess { _ in expectation.fulfill() } | ||
.transformOutput { $0.description } | ||
.onCompletion { expectation.fulfill() } | ||
transformed.onSuccess(on: .global) { | ||
XCTAssertEqual($0, "6") | ||
expectation.fulfill() | ||
} | ||
wait(for: [expectation], timeout: 5) | ||
} | ||
|
||
} | ||
//class CoFutureTests2: XCTestCase { | ||
// | ||
// func testTransform() { | ||
// let expectation = XCTestExpectation(description: "Test Transform") | ||
// expectation.expectedFulfillmentCount = 3 | ||
// let promise = async { () -> Int in | ||
// sleep(1) | ||
// return 1 | ||
// } | ||
// let transformed = promise | ||
// .transformOutput { $0 * 2 } | ||
// .onError { _ in XCTFail() } | ||
// .transformOutput { $0 * 3 } | ||
// .onSuccess { XCTAssertEqual($0, 6) } | ||
// .onSuccess { _ in expectation.fulfill() } | ||
// .transformOutput { $0.description } | ||
// .onCompletion { expectation.fulfill() } | ||
// transformed.onSuccess(on: .global) { | ||
// XCTAssertEqual($0, "6") | ||
// expectation.fulfill() | ||
// } | ||
// wait(for: [expectation], timeout: 5) | ||
// } | ||
// | ||
//} |
Oops, something went wrong.