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

Optional Steps and Assertion #4

Merged
merged 1 commit into from
May 2, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Sources/Rorschach/Builders/GivenBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

@resultBuilder public struct GivenBuilder {

public static func buildBlock(_ steps: Step...) -> [Step] {
public static func buildBlock(_ steps: Step?...) -> [Step?] {
steps
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Rorschach/Builders/ThenBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation


@resultBuilder public struct ThenBuilder {
public static func buildBlock(_ assertion: Assertion) -> Assertion {
public static func buildBlock(_ assertion: Assertion?) -> Assertion? {
assertion
}
}
2 changes: 1 addition & 1 deletion Sources/Rorschach/Builders/WhenBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

@resultBuilder public struct WhenBuilder {

public static func buildBlock(_ steps: Step...) -> [Step] {
public static func buildBlock(_ steps: Step?...) -> [Step?] {
steps
}
}
16 changes: 9 additions & 7 deletions Sources/Rorschach/Given.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ public class Given {

/// Returns "Given I do this"_ when this _Given_ either has content named _"I do this"_ or the first of its steps is titled _"I do this"_.
var title: String {
guard let firstStep = steps.first else { return "Given" }

if let stepTitle = contentOnlyTitle {
return "Given " + stepTitle
}
return "Given " + (steps.first?.title ?? "")
return "Given " + (firstStep?.title ?? "")
}
var contentOnlyTitle: String?

/// The _Step_s to be executed as part of the _Given_.
let steps: [Step]
let steps: [Step?]

init(steps: [Step]) {
init(steps: [Step?]) {
self.steps = steps
}

Expand All @@ -33,18 +35,18 @@ public class Given {
}

/// Initializes a _Given_ with the given _Step_s
public convenience init(@GivenBuilder _ content: () -> [Step]) {
public convenience init(@GivenBuilder _ content: () -> [Step?]) {
self.init(steps: content())
}

func execute() {
steps.forEach { step in
if step != steps.first, let title = step.title {
if step != steps.first, let title = step?.title {
XCTContext.runActivity(named: "And \(title)" ) { _ in
step.execute()
step?.execute()
}
} else {
step.execute()
step?.execute()
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Rorschach/Then.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import XCTest

public struct Then {

let assertion: Assertion
let assertion: Assertion?

var title: String {
if let assertionTitle = contentOnlyTitle {
return "Then " + assertionTitle
}
return "Then " + (assertion.title ?? "")
return "Then " + (assertion?.title ?? "")
}
var contentOnlyTitle: String?

Expand All @@ -25,11 +25,11 @@ public struct Then {
assertion = Assertion(content: content)
}

public init(@ThenBuilder _ content: () -> Assertion) {
public init(@ThenBuilder _ content: () -> Assertion?) {
assertion = content()
}

func assert() {
assertion.assert()
assertion?.assert()
}
}
14 changes: 8 additions & 6 deletions Sources/Rorschach/When.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ public struct When {

/// Returns "When I do this"_ when this _When_ either has content named _"I do this"_ or the first of its steps is titled _"I do this"_.
var title: String {
guard let firstStep = steps.first else { return "When" }

if let stepTitle = contentOnlyTitle {
return "When " + stepTitle
}
return "When " + (steps.first?.title ?? "")
return "When " + (firstStep?.title ?? "")
}
var contentOnlyTitle: String?

/// The _Step_s to be executed as part of the _When_.
let steps: [Step]
let steps: [Step?]

/// Shorthand for a _When_ with a single step with some content
public init(_ title: String, content: @escaping () -> Void) {
Expand All @@ -29,18 +31,18 @@ public struct When {
}

/// Initializes a _When_ with the given _Step_s
public init(@WhenBuilder _ content: () -> [Step]) {
public init(@WhenBuilder _ content: () -> [Step?]) {
steps = content()
}

func execute() {
steps.forEach { step in
if step != steps.first, let title = step.title {
if step != steps.first, let title = step?.title {
XCTContext.runActivity(named: "And \(title)" ) { _ in
step.execute()
step?.execute()
}
} else {
step.execute()
step?.execute()
}
}
}
Expand Down