-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update utils coverage * update utils coverage
- Loading branch information
Showing
6 changed files
with
332 additions
and
14 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
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
77 changes: 77 additions & 0 deletions
77
Utils/Tests/UtilsTests/EventBus/DispatcherMiddlewareTests.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,77 @@ | ||
import bls | ||
import Foundation | ||
import Testing | ||
|
||
@testable import Utils | ||
|
||
final class MiddlewareTests { | ||
actor OrderManager { | ||
private(set) var order: [Int] = [] | ||
|
||
func appendOrder(_ value: Int) { | ||
order.append(value) | ||
} | ||
} | ||
|
||
@Test func testParallelDispatcher() async throws { | ||
let orderManager = OrderManager() | ||
|
||
let firstMiddleware = Middleware.noop | ||
let secondMiddleware = Middleware.noop | ||
|
||
let parallelMiddleware = Middleware.parallel(firstMiddleware, secondMiddleware) | ||
|
||
let handler: MiddlewareHandler<Void> = { _ in | ||
await orderManager.appendOrder(2) | ||
} | ||
|
||
try await parallelMiddleware.handle((), next: { | ||
await orderManager.appendOrder(1) | ||
}) | ||
|
||
let order = await orderManager.order | ||
#expect(order.count == 2) | ||
} | ||
|
||
@Test func testSerialDispatcher() async throws { | ||
let orderManager = OrderManager() | ||
|
||
let firstMiddleware = Middleware.noop | ||
let secondMiddleware = Middleware.noop | ||
|
||
let serialMiddleware = Middleware.serial(firstMiddleware, secondMiddleware) | ||
|
||
let handler: MiddlewareHandler<Void> = { _ in | ||
await orderManager.appendOrder(2) | ||
} | ||
|
||
try await serialMiddleware.handle((), next: { | ||
await orderManager.appendOrder(1) | ||
try await handler(()) | ||
}) | ||
|
||
let order = await orderManager.order | ||
#expect(order == [1, 2]) | ||
} | ||
|
||
@Test func testMiddlewareChain() async throws { | ||
let orderManager = OrderManager() | ||
|
||
let middleware1 = Middleware.noop | ||
let middleware2 = Middleware.noop | ||
|
||
let middlewareChain = Middleware.serial(middleware1, middleware2) | ||
|
||
let handler: MiddlewareHandler<Void> = { _ in | ||
await orderManager.appendOrder(2) | ||
} | ||
|
||
try await middlewareChain.handle((), next: { | ||
await orderManager.appendOrder(1) | ||
try await handler(()) | ||
}) | ||
|
||
let order = await orderManager.order | ||
#expect(order == [1, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Foundation | ||
import Testing | ||
|
||
@testable import Utils | ||
|
||
struct OptionalTests { | ||
@Test func testUnwrapSuccess() throws { | ||
let optionalValue: Int? = 42 | ||
|
||
let result = try optionalValue.unwrap() | ||
|
||
#expect(result == 42) | ||
} | ||
|
||
@Test func testUnwrapFailure() throws { | ||
let optionalValue: Int? = nil | ||
|
||
#expect(throws: OptionalError.nilValue) { | ||
_ = try optionalValue.unwrap() | ||
} | ||
} | ||
|
||
@Test func testUnwrapOrErrorSuccess() throws { | ||
let optionalValue: Int? = 42 | ||
|
||
let result = try optionalValue.unwrap(orError: NSError(domain: "", code: 1)) | ||
|
||
#expect(result == 42) | ||
} | ||
|
||
@Test func testExpectSuccess() throws { | ||
let optionalValue: Int? = 42 | ||
|
||
let result = optionalValue.expect("Value should be present") | ||
|
||
#expect(result == 42) | ||
} | ||
} |