From ee9b49e840b695f9af36eadb63e5309085dd76ae Mon Sep 17 00:00:00 2001 From: Daniel Hall Date: Thu, 11 May 2023 22:28:36 -0600 Subject: [PATCH] Added optional setup closure for when statements --- .../RequirementsKit/StatementHandler.swift | 71 +++++++++++++++---- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/Sources/RequirementsKit/StatementHandler.swift b/Sources/RequirementsKit/StatementHandler.swift index 9323318..be0869a 100644 --- a/Sources/RequirementsKit/StatementHandler.swift +++ b/Sources/RequirementsKit/StatementHandler.swift @@ -62,6 +62,9 @@ public struct StatementHandler { } public extension StatementHandler { + + // MARK: - if - + static func `if`(_ statement: Regex, timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void) -> StatementHandler { .init(statementType: .if, statement: statement, timeout: timeout, handler: handler, setup: nil) } @@ -71,6 +74,9 @@ public extension StatementHandler { static func `if`(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping () throws -> Void) -> StatementHandler { .init(statementType: .if, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: nil) } + + // MARK: - given - + static func given(_ statement: Regex, timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void) -> StatementHandler { .if(statement, timeout: timeout, handler: handler) } @@ -80,18 +86,45 @@ public extension StatementHandler { static func given(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping () throws -> Void) -> StatementHandler { .init(statementType: .if, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: nil) } + + // MARK: - when - + + static func when(_ statement: Regex, timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void, setup: @escaping (Input) throws -> Void) -> StatementHandler { + .init(statementType: .when, statement: statement, timeout: timeout, handler: handler, setup: setup) + } + static func when(_ statement: Regex, timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void, setup: @escaping () throws -> Void) -> StatementHandler { + .init(statementType: .when, statement: statement, timeout: timeout, handler: handler, setup: { _ in try setup() }) + } + static func when(_ statement: Regex, timeout: TimeInterval? = nil, handler: @escaping () throws -> Void, setup: @escaping (Input) throws -> Void) -> StatementHandler { + .init(statementType: .when, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: setup) + } + static func when(_ statement: Regex, timeout: TimeInterval? = nil, handler: @escaping () throws -> Void, setup: @escaping () throws -> Void) -> StatementHandler { + .init(statementType: .when, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: { _ in try setup() }) + } static func when(_ statement: Regex, timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void) -> StatementHandler { .init(statementType: .when, statement: statement, timeout: timeout, handler: handler, setup: nil) } + static func when(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void, setup: @escaping (Input) throws -> Void) -> StatementHandler { + .init(statementType: .when, statement: statement, timeout: timeout, handler: handler, setup: setup) + } + static func when(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void, setup: @escaping () throws -> Void) -> StatementHandler { + .init(statementType: .when, statement: statement, timeout: timeout, handler: handler, setup: { _ in try setup() }) + } + static func when(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping () throws -> Void, setup: @escaping (Input) throws -> Void) -> StatementHandler { + .init(statementType: .when, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: setup) + } + static func when(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping () throws -> Void, setup: @escaping () throws -> Void) -> StatementHandler { + .init(statementType: .when, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: { _ in try setup() }) + } static func when(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void) -> StatementHandler { .init(statementType: .when, statement: statement, timeout: timeout, handler: handler, setup: nil) } static func when(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping () throws -> Void) -> StatementHandler { .init(statementType: .when, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: nil) } - static func expect(_ statement: Regex, timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void) -> StatementHandler { - .init(statementType: .expect, statement: statement, timeout: timeout, handler: handler, setup: nil) - } + + // MARK: - expect - + static func expect(_ statement: Regex, timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void, setup: @escaping (Input) throws -> Void) -> StatementHandler { .init(statementType: .expect, statement: statement, timeout: timeout, handler: handler, setup: setup) } @@ -104,21 +137,30 @@ public extension StatementHandler { static func expect(_ statement: Regex, timeout: TimeInterval? = nil, handler: @escaping () throws -> Void, setup: @escaping () throws -> Void) -> StatementHandler { .init(statementType: .expect, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: { _ in try setup() }) } - static func expect(_ statement: Regex, timeout: TimeInterval? = nil, handler: @escaping () throws -> Void) -> StatementHandler { - .init(statementType: .expect, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: nil) + static func expect(_ statement: Regex, timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void) -> StatementHandler { + .init(statementType: .expect, statement: statement, timeout: timeout, handler: handler, setup: nil) } - static func expect(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void, setup: ((Input) throws -> Void)? = nil) -> StatementHandler { + static func expect(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void, setup: @escaping (Input) throws -> Void) -> StatementHandler { .init(statementType: .expect, statement: statement, timeout: timeout, handler: handler, setup: setup) } - static func expect(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping () throws -> Void, setup: ((Input) throws -> Void)? = nil) -> StatementHandler { + static func expect(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping () throws -> Void, setup: @escaping (Input) throws -> Void) -> StatementHandler { .init(statementType: .expect, statement: statement, timeout: timeout, handler: {_ in try handler() }, setup: setup) } - static func expect(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void, setup: (() throws -> Void)? = nil) -> StatementHandler { - .init(statementType: .expect, statement: statement, timeout: timeout, handler: handler, setup: { _ in try setup?() }) + static func expect(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void, setup: @escaping () throws -> Void) -> StatementHandler { + .init(statementType: .expect, statement: statement, timeout: timeout, handler: handler, setup: { _ in try setup() }) } - static func expect(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping () throws -> Void, setup: (() throws -> Void)? = nil) -> StatementHandler { - .init(statementType: .expect, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: { _ in try setup?() }) + static func expect(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping () throws -> Void, setup: @escaping () throws -> Void) -> StatementHandler { + .init(statementType: .expect, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: { _ in try setup() }) + } + static func expect(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void) -> StatementHandler { + .init(statementType: .expect, statement: statement, timeout: timeout, handler: handler, setup: nil) + } + static func expect(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping () throws -> Void) -> StatementHandler { + .init(statementType: .expect, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: nil) } + + // MARK: - then - + static func then(_ statement: Regex, timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void, setup: @escaping (Input) throws -> Void) -> StatementHandler { .expect(statement, timeout: timeout, handler: handler, setup: setup) } @@ -143,13 +185,12 @@ public extension StatementHandler { static func then(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void, setup: @escaping () throws -> Void) -> StatementHandler { .init(statementType: .expect, statement: statement, timeout: timeout, handler: handler, setup: { _ in try setup() }) } - static func then(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void) -> StatementHandler { - .init(statementType: .expect, statement: statement, timeout: timeout, handler: handler, setup: nil) - } static func then(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping () throws -> Void, setup: @escaping () throws -> Void) -> StatementHandler { .init(statementType: .expect, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: { _ in try setup() }) } - + static func then(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping (Input) throws -> Void) -> StatementHandler { + .init(statementType: .expect, statement: statement, timeout: timeout, handler: handler, setup: nil) + } static func then(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping () throws -> Void) -> StatementHandler { .init(statementType: .expect, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: nil) }