Skip to content

Commit

Permalink
feat: support autoclosure for custom assertion messages using `assert…
Browse files Browse the repository at this point in the history
…That()` (#110)
  • Loading branch information
cgrindel authored Sep 15, 2023
1 parent 4f35e70 commit 1db4ac4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
59 changes: 49 additions & 10 deletions Sources/Truth/AssertThat.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import XCTest

/// Return a subject for the target.
public func assertThat<T>(
_ actual: T,
using failureStrategy: FailureStrategy
) -> Subject<T> {
return Subject(actual: actual, failureStrategy: failureStrategy)
}

/// Return a subject for the target.
public func assertThat<T>(
_ actual: T,
using failureStrategy: FailureStrategy,
with customMessage: MessageClosure? = nil
with customMessage: @escaping @autoclosure MessageClosure
) -> Subject<T> {
var customMessages = [MessageClosure]()
if let customMessage = customMessage {
customMessages.append(customMessage)
}
customMessages.append(customMessage)
return Subject(actual: actual, failureStrategy: failureStrategy, customMessages: customMessages)
}

Expand All @@ -20,10 +26,24 @@ public func assertThat<T>(
public func assertThat<T>(
_ actual: T,
using failureStrategy: FailureStrategy,
with customMessage: MessageClosure? = nil,
_ consumer: (Subject<T>) -> Void
) -> Subject<T> {
let subject = assertThat(actual, using: failureStrategy, with: customMessage)
let subject = assertThat(actual, using: failureStrategy)
consumer(subject)
return subject
}

// Create a subject for the target and pass it to the provided consumer. This form is useful when
// wanting to perform a number of assertions on a single subject. This avoids massive compilation
// times when chaining assertions.
@discardableResult
public func assertThat<T>(
_ actual: T,
using failureStrategy: FailureStrategy,
with customMessage: @escaping @autoclosure MessageClosure,
_ consumer: (Subject<T>) -> Void
) -> Subject<T> {
let subject = assertThat(actual, using: failureStrategy, with: customMessage())
consumer(subject)
return subject
}
Expand All @@ -38,8 +58,27 @@ public func assertThat<T>(

public extension XCTestCase {
/// Return a subject for the target.
func assertThat<T>(_ actual: T, with customMessage: MessageClosure? = nil) -> Subject<T> {
return Truth.assertThat(actual, using: failureStrategy, with: customMessage)
func assertThat<T>(_ actual: T) -> Subject<T> {
return Truth.assertThat(actual, using: failureStrategy)
}

/// Return a subject for the target.
func assertThat<T>(
_ actual: T,
with customMessage: @escaping @autoclosure MessageClosure
) -> Subject<T> {
return Truth.assertThat(actual, using: failureStrategy, with: customMessage())
}

// Create a subject for the target and pass it to the provided consumer. This form is useful when
// wanting to perform a number of assertions on a single subject. This avoids massive compilation
// times when chaining assertions.
@discardableResult
func assertThat<T>(
_ actual: T,
_ consumer: (Subject<T>) -> Void
) -> Subject<T> {
return Truth.assertThat(actual, using: failureStrategy, consumer)
}

// Create a subject for the target and pass it to the provided consumer. This form is useful when
Expand All @@ -48,10 +87,10 @@ public extension XCTestCase {
@discardableResult
func assertThat<T>(
_ actual: T,
with customMessage: MessageClosure? = nil,
with customMessage: @autoclosure @escaping MessageClosure,
_ consumer: (Subject<T>) -> Void
) -> Subject<T> {
return Truth.assertThat(actual, using: failureStrategy, with: customMessage, consumer)
return Truth.assertThat(actual, using: failureStrategy, with: customMessage(), consumer)
}

/// Return a subject for the throwable expression.
Expand Down
4 changes: 2 additions & 2 deletions Tests/TruthTests/AssertThatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class AssertThatTests: XCTestCase {

func test_assertThat_WithCustomMessage() {
let message = "Custom message."
var result = assertThat(target, with: { message })
var result = assertThat(target, with: message)
XCTAssertEqual(result.actual, target)
XCTAssertEqual(result.customMessages.map { $0() }, [message])

result = assertThat(target, with: { message }) {
result = assertThat(target, with: message) {
XCTAssertEqual($0.actual, target)
XCTAssertEqual($0.customMessages.map { $0() }, [message])
}
Expand Down

0 comments on commit 1db4ac4

Please sign in to comment.