Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional setup closure for when statement handlers #10

Merged
merged 1 commit into from
May 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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