diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2022-09-25 16:34:23 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2022-09-26 16:35:38 +0200 |
| commit | f389956c2cd884df142adbf00ff2ac7e2f69c2b2 (patch) | |
| tree | 5f7d4869324760eb4ba0107c0356edc89efd1457 /ios/OperationsTests/OperationInputInjectionTests.swift | |
| parent | 2e83b1ca27ff243a615ff10c94c20840b38dfd45 (diff) | |
| download | mullvadvpn-f389956c2cd884df142adbf00ff2ac7e2f69c2b2.tar.xz mullvadvpn-f389956c2cd884df142adbf00ff2ac7e2f69c2b2.zip | |
Move AsyncOperation into Operations static library and add separate tests
Diffstat (limited to 'ios/OperationsTests/OperationInputInjectionTests.swift')
| -rw-r--r-- | ios/OperationsTests/OperationInputInjectionTests.swift | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/ios/OperationsTests/OperationInputInjectionTests.swift b/ios/OperationsTests/OperationInputInjectionTests.swift new file mode 100644 index 0000000000..828afd2643 --- /dev/null +++ b/ios/OperationsTests/OperationInputInjectionTests.swift @@ -0,0 +1,89 @@ +// +// OperationInputInjectionTests.swift +// MullvadVPNTests +// +// Created by pronebird on 09/06/2022. +// Copyright © 2022 Mullvad VPN AB. All rights reserved. +// + +import Operations +import XCTest + +class OperationInputInjectionTests: XCTestCase { + func testInject() throws { + let provider = ResultBlockOperation<Int, Error> { + return 1 + } + + let consumer = TransformOperation<Int, Int, Error> { input in + return input + 1 + } + + consumer.inject(from: provider) + + let operationQueue = AsyncOperationQueue() + + operationQueue.addOperations([provider, consumer], waitUntilFinished: true) + + XCTAssertEqual(consumer.output, 2) + } + + func testInjectVia() throws { + let provider = ResultBlockOperation<Int, Error> { + return 1 + } + + let consumer = TransformOperation<String, Int, Error> { input in + return Int(input)! + } + + consumer.inject(from: provider) { output in + return "\(output)" + } + + let operationQueue = AsyncOperationQueue() + + operationQueue.addOperations([provider, consumer], waitUntilFinished: true) + + XCTAssertEqual(consumer.output, 1) + } + + func testInjectMany() throws { + struct Context: OperationInputContext { + var a: Int? + var b: Int? + + func reduce() -> Int? { + guard let a = a, let b = b else { return nil } + + return a + b + } + } + + let operationQueue = AsyncOperationQueue() + + let providerA = ResultBlockOperation<Int, Error> { + return 1 + } + + let providerB = ResultBlockOperation<Int, Error> { + return 2 + } + + let consumer = TransformOperation<Int, String, Error> { input in + return "\(input)" + } + + consumer.injectMany(context: Context()) + .inject(from: providerA, assignOutputTo: \.a) + .inject(from: providerB, assignOutputTo: \.b) + .reduce() + + operationQueue.addOperations( + [providerA, providerB, consumer], + waitUntilFinished: true + ) + + XCTAssertEqual(consumer.output, "3") + } +} |
