Skip to content
Open
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
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import PackageDescription
let package = Package(
name: "rorschach",
platforms: [
.iOS(.v13)
.iOS(.v13),
.macOS(.v10_15)
],
products: [
.library(
Expand Down
14 changes: 12 additions & 2 deletions Sources/Rorschach/Assertion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ import XCTest
public struct Assertion {

let title: String?
private let content: () -> Void
private var content: (() -> Void)?
private var asyncThrowingContent: (() async throws -> Void)?

public init(_ title: String? = nil, content: @escaping () async throws -> Void) {
self.title = title
self.asyncThrowingContent = content
}

public init(_ title: String? = nil, content: @escaping () -> Void) {
self.title = title
self.content = content
}

public func assert() async throws {
try await asyncThrowingContent?()
}

public func assert() {
content()
content?()
}
}
9 changes: 7 additions & 2 deletions Sources/Rorschach/Then.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public struct Then {
}
var contentOnlyTitle: String?

public init(_ title: String, content: @escaping () async throws -> Void) {
contentOnlyTitle = title
assertion = Assertion(content: content)
}

public init(_ title: String, content: @escaping () -> Void) {
contentOnlyTitle = title
assertion = Assertion(content: content)
Expand All @@ -30,7 +35,7 @@ public struct Then {
assertion = content()
}

func assert() {
assertion?.assert()
func assert() async throws {
try await assertion?.assert()
}
}
20 changes: 12 additions & 8 deletions Sources/Rorschach/XCTestCase+BDDStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public protocol Expecting {
/// }
/// }
/// ```
func expect(@ExpectationBuilder _ content: () -> (given: Given, when: When, then: Then))
func expect(@ExpectationBuilder _ content: @escaping () -> (given: Given, when: When, then: Then))

/// A test's expectation containing only the ``When`` and the ``Then`` sections.
///
Expand All @@ -49,13 +49,13 @@ public protocol Expecting {
/// }
/// ```

func expect(@ExpectationBuilder _ content: () -> (when: When, then: Then))
func expect(@ExpectationBuilder _ content: @escaping () -> (when: When, then: Then))
}

/// This extension adds support for test expectations to _XCTestCase_.
extension XCTestCase: Expecting {

public func expect(@ExpectationBuilder _ content: () -> (given: Given, when: When, then: Then)) {
public func expect(@ExpectationBuilder _ content: @escaping () -> (given: Given, when: When, then: Then)) {

XCTContext.runActivity(named: content().given.title ) { _ in
content().given.execute()
Expand All @@ -65,19 +65,23 @@ extension XCTestCase: Expecting {
content().when.execute()
}

XCTContext.runActivity(named: content().then.title ) { _ in
content().then.assert()
_ = XCTContext.runActivity(named: content().then.title ) { _ in
Task {
try await content().then.assert()
}
}
}

public func expect(@ExpectationBuilder _ content: () -> (when: When, then: Then)) {
public func expect(@ExpectationBuilder _ content: @escaping () -> (when: When, then: Then)) {

XCTContext.runActivity(named: content().when.title ) { _ in
content().when.execute()
}

XCTContext.runActivity(named: content().then.title ) { _ in
content().then.assert()
_ = XCTContext.runActivity(named: content().then.title ) { _ in
Task {
try await content().then.assert()
}
}
}

Expand Down
46 changes: 46 additions & 0 deletions Tests/RorschachTests/AssertionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// AssertionTests.swift
//
//
// Created by Martin Kim Dung-Pham on 25.01.24.
//

import Foundation
import XCTest

@testable import Rorschach

class AssertionTests: XCTestCase {

func testDefault() {
let expectation = expectation(description: "")

let sut = Assertion("abc") {
_ = ""
expectation.fulfill()
}

sut.assert()

wait(for: [expectation], timeout: 0.1)
XCTAssertEqual(sut.title, "abc")
}

func testThrowing() async throws {
let expectation = expectation(description: "")

let sut = Assertion("abc") {
_ = try JSONDecoder().decode([String: String].self, from: self.json)
expectation.fulfill()
}

try await sut.assert()

await fulfillment(of: [expectation])
XCTAssertEqual(sut.title, "abc")
}

private let json = """
{"a":"A"}
""".data(using: .utf8)!
}