Skip to content

Commit

Permalink
Merge pull request #10 from daniel-hall/AddOptionalSetupForWhenHandlers
Browse files Browse the repository at this point in the history
Added optional setup closure for when statement handlers
  • Loading branch information
daniel-hall authored May 12, 2023
2 parents f2c5407 + ee9b49e commit 14856b2
Showing 1 changed file with 56 additions and 15 deletions.
71 changes: 56 additions & 15 deletions Sources/RequirementsKit/StatementHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public struct StatementHandler {
}

public extension StatementHandler {

// MARK: - if -

static func `if`<T>(_ statement: Regex<T>, timeout: TimeInterval? = nil, handler: @escaping (Input<T>) throws -> Void) -> StatementHandler {
.init(statementType: .if, statement: statement, timeout: timeout, handler: handler, setup: nil)
}
Expand All @@ -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<T>(_ statement: Regex<T>, timeout: TimeInterval? = nil, handler: @escaping (Input<T>) throws -> Void) -> StatementHandler {
.if(statement, timeout: timeout, handler: handler)
}
Expand All @@ -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<T>(_ statement: Regex<T>, timeout: TimeInterval? = nil, handler: @escaping (Input<T>) throws -> Void, setup: @escaping (Input<T>) throws -> Void) -> StatementHandler {
.init(statementType: .when, statement: statement, timeout: timeout, handler: handler, setup: setup)
}
static func when<T>(_ statement: Regex<T>, timeout: TimeInterval? = nil, handler: @escaping (Input<T>) throws -> Void, setup: @escaping () throws -> Void) -> StatementHandler {
.init(statementType: .when, statement: statement, timeout: timeout, handler: handler, setup: { _ in try setup() })
}
static func when<T>(_ statement: Regex<T>, timeout: TimeInterval? = nil, handler: @escaping () throws -> Void, setup: @escaping (Input<T>) throws -> Void) -> StatementHandler {
.init(statementType: .when, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: setup)
}
static func when<T>(_ statement: Regex<T>, 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<T>(_ statement: Regex<T>, timeout: TimeInterval? = nil, handler: @escaping (Input<T>) 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<AnyRegexOutput>) throws -> Void, setup: @escaping (Input<AnyRegexOutput>) 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<AnyRegexOutput>) 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<AnyRegexOutput>) 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<AnyRegexOutput>) 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<T>(_ statement: Regex<T>, timeout: TimeInterval? = nil, handler: @escaping (Input<T>) throws -> Void) -> StatementHandler {
.init(statementType: .expect, statement: statement, timeout: timeout, handler: handler, setup: nil)
}

// MARK: - expect -

static func expect<T>(_ statement: Regex<T>, timeout: TimeInterval? = nil, handler: @escaping (Input<T>) throws -> Void, setup: @escaping (Input<T>) throws -> Void) -> StatementHandler {
.init(statementType: .expect, statement: statement, timeout: timeout, handler: handler, setup: setup)
}
Expand All @@ -104,21 +137,30 @@ public extension StatementHandler {
static func expect<T>(_ statement: Regex<T>, 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<T>(_ statement: Regex<T>, timeout: TimeInterval? = nil, handler: @escaping () throws -> Void) -> StatementHandler {
.init(statementType: .expect, statement: statement, timeout: timeout, handler: { _ in try handler() }, setup: nil)
static func expect<T>(_ statement: Regex<T>, timeout: TimeInterval? = nil, handler: @escaping (Input<T>) 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<AnyRegexOutput>) throws -> Void, setup: ((Input<AnyRegexOutput>) throws -> Void)? = nil) -> StatementHandler {
static func expect(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping (Input<AnyRegexOutput>) throws -> Void, setup: @escaping (Input<AnyRegexOutput>) 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<AnyRegexOutput>) throws -> Void)? = nil) -> StatementHandler {
static func expect(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping () throws -> Void, setup: @escaping (Input<AnyRegexOutput>) 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<AnyRegexOutput>) 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<AnyRegexOutput>) 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<AnyRegexOutput>) 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<T>(_ statement: Regex<T>, timeout: TimeInterval? = nil, handler: @escaping (Input<T>) throws -> Void, setup: @escaping (Input<T>) throws -> Void) -> StatementHandler {
.expect(statement, timeout: timeout, handler: handler, setup: setup)
}
Expand All @@ -143,13 +185,12 @@ public extension StatementHandler {
static func then(_ statement: String..., timeout: TimeInterval? = nil, handler: @escaping (Input<AnyRegexOutput>) 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<AnyRegexOutput>) 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<AnyRegexOutput>) 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)
}
Expand Down

0 comments on commit 14856b2

Please sign in to comment.