Skip to content

Commit

Permalink
Merge pull request #4 from q231950/optional-steps
Browse files Browse the repository at this point in the history
Optional Steps and Assertion
  • Loading branch information
q231950 authored May 2, 2021
2 parents 148c3a1 + 8f3049f commit 1783ccd
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
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

0 comments on commit 1783ccd

Please sign in to comment.