-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from amzn/mock_and_mock_throwing_implementations
Mock and mock throwing implementations
- Loading branch information
Showing
8 changed files
with
734 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// | ||
// MockClientProtocol.swift | ||
// | ||
|
||
import NIO | ||
|
||
public protocol MockClientProtocol { | ||
|
||
} | ||
|
||
/** | ||
Implementations for a mock service client. | ||
|
||
A function override directly returning a result and/or an EventLoopFuture override returning an | ||
`EventLoopFuture` that will provide a result at a later time can be provided. | ||
|
||
If the function override is provided, the implementation will return - via a future - the result | ||
provided by this override or will throw any error thrown by the override. | ||
|
||
Otherwise, if the `EventLoopFuture` override is provided, the implementation will return the result | ||
provided by the `EventLoopFuture` or will throw any error that fails the future. This override is ignored if the first | ||
function override is provided. | ||
|
||
Otherwise, the implementation will return the default value provided. | ||
*/ | ||
public extension MockClientProtocol { | ||
|
||
func mockEventLoopFutureExecuteWithInputWithOutput<InputType, OutputType>( | ||
input: InputType, | ||
defaultResult: OutputType, | ||
eventLoop: EventLoop, | ||
functionOverride: ((InputType) throws -> OutputType)?, | ||
eventLoopFutureFunctionOverride: ((InputType) -> EventLoopFuture<OutputType>)?) -> EventLoopFuture<OutputType> { | ||
let promise = eventLoop.makePromise(of: OutputType.self) | ||
|
||
if let functionOverride = functionOverride { | ||
do { | ||
let overrideResult = try functionOverride(input) | ||
promise.succeed(overrideResult) | ||
} catch { | ||
promise.fail(error) | ||
} | ||
|
||
return promise.futureResult | ||
} | ||
|
||
if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride { | ||
return eventLoopFutureFunctionOverride(input) | ||
} | ||
|
||
promise.succeed(defaultResult) | ||
|
||
return promise.futureResult | ||
} | ||
|
||
func mockEventLoopFutureExecuteWithInputWithoutOutput<InputType>( | ||
input: InputType, | ||
eventLoop: EventLoop, | ||
functionOverride: ((InputType) throws -> ())?, | ||
eventLoopFutureFunctionOverride: ((InputType) -> EventLoopFuture<Void>)?) -> EventLoopFuture<Void> { | ||
let promise = eventLoop.makePromise(of: Void.self) | ||
|
||
if let functionOverride = functionOverride { | ||
do { | ||
try functionOverride(input) | ||
promise.succeed(()) | ||
} catch { | ||
promise.fail(error) | ||
} | ||
|
||
return promise.futureResult | ||
} | ||
|
||
if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride { | ||
return eventLoopFutureFunctionOverride(input) | ||
} | ||
|
||
promise.succeed(()) | ||
|
||
return promise.futureResult | ||
} | ||
|
||
func mockEventLoopFutureExecuteWithoutInputWithOutput<OutputType>( | ||
defaultResult: OutputType, | ||
eventLoop: EventLoop, | ||
functionOverride: (() throws -> OutputType)?, | ||
eventLoopFutureFunctionOverride: (() -> EventLoopFuture<OutputType>)?) -> EventLoopFuture<OutputType> { | ||
let promise = eventLoop.makePromise(of: OutputType.self) | ||
|
||
if let functionOverride = functionOverride { | ||
do { | ||
let overrideResult = try functionOverride() | ||
promise.succeed(overrideResult) | ||
} catch { | ||
promise.fail(error) | ||
} | ||
|
||
return promise.futureResult | ||
} | ||
|
||
if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride { | ||
return eventLoopFutureFunctionOverride() | ||
} | ||
|
||
promise.succeed(defaultResult) | ||
|
||
return promise.futureResult | ||
} | ||
|
||
func mockEventLoopFutureExecuteWithoutInputWithoutOutput( | ||
eventLoop: EventLoop, | ||
functionOverride: (() throws -> ())?, | ||
eventLoopFutureFunctionOverride: (() -> EventLoopFuture<Void>)?) -> EventLoopFuture<Void> { | ||
let promise = eventLoop.makePromise(of: Void.self) | ||
|
||
if let functionOverride = functionOverride { | ||
do { | ||
try functionOverride() | ||
promise.succeed(()) | ||
} catch { | ||
promise.fail(error) | ||
} | ||
|
||
return promise.futureResult | ||
} | ||
|
||
if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride { | ||
return eventLoopFutureFunctionOverride() | ||
} | ||
|
||
promise.succeed(()) | ||
|
||
return promise.futureResult | ||
} | ||
} |
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,138 @@ | ||
// | ||
// MockThrowingClientProtocol.swift | ||
// | ||
|
||
import NIO | ||
import NIOHTTP1 | ||
|
||
public protocol MockThrowingClientProtocol { | ||
|
||
} | ||
|
||
/** | ||
Implementations for a mock service client. | ||
|
||
A function override directly returning a result and/or an EventLoopFuture override returning an | ||
`EventLoopFuture` that will provide a result at a later time can be provided. | ||
|
||
If the function override is provided, the implementation will return - via a future - the result | ||
provided by this override or will throw any error thrown by the override. | ||
|
||
Otherwise, if the `EventLoopFuture` override is provided, the implementation will return the result | ||
provided by the `EventLoopFuture` or will throw any error that fails the future. This override is ignored if the first | ||
function override is provided. | ||
|
||
Otherwise, the implementation will throw the error provided. | ||
*/ | ||
public extension MockThrowingClientProtocol { | ||
|
||
func mockThrowingEventLoopFutureExecuteWithInputWithOutput<InputType, OutputType>( | ||
input: InputType, | ||
defaultError: Error, | ||
eventLoop: EventLoop, | ||
functionOverride: ((InputType) throws -> OutputType)?, | ||
eventLoopFutureFunctionOverride: ((InputType) -> EventLoopFuture<OutputType>)?) -> EventLoopFuture<OutputType> { | ||
let promise = eventLoop.makePromise(of: OutputType.self) | ||
|
||
if let functionOverride = functionOverride { | ||
do { | ||
let overrideResult = try functionOverride(input) | ||
promise.succeed(overrideResult) | ||
} catch { | ||
promise.fail(error) | ||
} | ||
|
||
return promise.futureResult | ||
} | ||
|
||
if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride { | ||
return eventLoopFutureFunctionOverride(input) | ||
} | ||
|
||
promise.fail(defaultError) | ||
|
||
return promise.futureResult | ||
} | ||
|
||
func mockThrowingEventLoopFutureExecuteWithInputWithoutOutput<InputType>( | ||
input: InputType, | ||
defaultError: Error, | ||
eventLoop: EventLoop, | ||
functionOverride: ((InputType) throws -> ())?, | ||
eventLoopFutureFunctionOverride: ((InputType) -> EventLoopFuture<Void>)?) -> EventLoopFuture<Void> { | ||
let promise = eventLoop.makePromise(of: Void.self) | ||
|
||
if let functionOverride = functionOverride { | ||
do { | ||
try functionOverride(input) | ||
promise.succeed(()) | ||
} catch { | ||
promise.fail(error) | ||
} | ||
|
||
return promise.futureResult | ||
} | ||
|
||
if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride { | ||
return eventLoopFutureFunctionOverride(input) | ||
} | ||
|
||
promise.fail(defaultError) | ||
|
||
return promise.futureResult | ||
} | ||
|
||
func mockThrowingEventLoopFutureExecuteWithoutInputWithOutput<OutputType>( | ||
defaultError: Error, | ||
eventLoop: EventLoop, | ||
functionOverride: (() throws -> OutputType)?, | ||
eventLoopFutureFunctionOverride: (() -> EventLoopFuture<OutputType>)?) -> EventLoopFuture<OutputType> { | ||
let promise = eventLoop.makePromise(of: OutputType.self) | ||
|
||
if let functionOverride = functionOverride { | ||
do { | ||
let overrideResult = try functionOverride() | ||
promise.succeed(overrideResult) | ||
} catch { | ||
promise.fail(error) | ||
} | ||
|
||
return promise.futureResult | ||
} | ||
|
||
if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride { | ||
return eventLoopFutureFunctionOverride() | ||
} | ||
|
||
promise.fail(defaultError) | ||
|
||
return promise.futureResult | ||
} | ||
|
||
func mockThrowingEventLoopFutureExecuteWithoutInputWithoutOutput( | ||
defaultError: Error, | ||
eventLoop: EventLoop, | ||
functionOverride: (() throws -> ())?, | ||
eventLoopFutureFunctionOverride: (() -> EventLoopFuture<Void>)?) -> EventLoopFuture<Void> { | ||
let promise = eventLoop.makePromise(of: Void.self) | ||
|
||
if let functionOverride = functionOverride { | ||
do { | ||
try functionOverride() | ||
promise.succeed(()) | ||
} catch { | ||
promise.fail(error) | ||
} | ||
|
||
return promise.futureResult | ||
} | ||
|
||
if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride { | ||
return eventLoopFutureFunctionOverride() | ||
} | ||
|
||
promise.fail(defaultError) | ||
|
||
return promise.futureResult | ||
} | ||
} |
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
Oops, something went wrong.