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

satisfyAllOf and satisfyAnyOf should only evaluate the expression once. #1045

Merged
merged 2 commits into from
Apr 4, 2023
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
9 changes: 9 additions & 0 deletions Sources/Nimble/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,13 @@ public struct Expression<Value> {
isClosure: isClosure
)
}

public func withCaching() -> Expression<Value> {
return Expression(
memoizedExpression: memoizedClosure { try self.evaluate() },
location: self.location,
withoutCaching: false,
isClosure: isClosure
)
}
}
2 changes: 1 addition & 1 deletion Sources/Nimble/Matchers/PostNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private func _postNotifications<Out>(
withoutCaching: true
)

assert(pthread_equal(mainThread, pthread_self()) != 0, "Only expecting closure to be evaluated on main thread.")
assert(Thread.isMainThread, "Only expecting closure to be evaluated on main thread.")
if !once {
once = true
_ = try actualExpression.evaluate()
Expand Down
5 changes: 3 additions & 2 deletions Sources/Nimble/Matchers/SatisfyAllOf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ public func satisfyAllOf<T>(_ predicates: Predicate<T>...) -> Predicate<T> {
/// provided in the array of matchers.
public func satisfyAllOf<T>(_ predicates: [Predicate<T>]) -> Predicate<T> {
return Predicate.define { actualExpression in
let cachedExpression = actualExpression.withCaching()
var postfixMessages = [String]()
var status: PredicateStatus = .matches
for predicate in predicates {
let result = try predicate.satisfies(actualExpression)
let result = try predicate.satisfies(cachedExpression)
if result.status == .fail {
status = .fail
} else if result.status == .doesNotMatch, status != .fail {
Expand All @@ -21,7 +22,7 @@ public func satisfyAllOf<T>(_ predicates: [Predicate<T>]) -> Predicate<T> {
}

var msg: ExpectationMessage
if let actualValue = try actualExpression.evaluate() {
if let actualValue = try cachedExpression.evaluate() {
msg = .expectedCustomValueTo(
"match all of: " + postfixMessages.joined(separator: ", and "),
actual: "\(actualValue)"
Expand Down
5 changes: 3 additions & 2 deletions Sources/Nimble/Matchers/SatisfyAnyOf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ public func satisfyAnyOf<T>(_ predicates: Predicate<T>...) -> Predicate<T> {
/// provided in the array of matchers.
public func satisfyAnyOf<T>(_ predicates: [Predicate<T>]) -> Predicate<T> {
return Predicate.define { actualExpression in
let cachedExpression = actualExpression.withCaching()
var postfixMessages = [String]()
var status: PredicateStatus = .doesNotMatch
for predicate in predicates {
let result = try predicate.satisfies(actualExpression)
let result = try predicate.satisfies(cachedExpression)
if result.status == .fail {
status = .fail
} else if result.status == .matches, status != .fail {
Expand All @@ -21,7 +22,7 @@ public func satisfyAnyOf<T>(_ predicates: [Predicate<T>]) -> Predicate<T> {
}

var msg: ExpectationMessage
if let actualValue = try actualExpression.evaluate() {
if let actualValue = try cachedExpression.evaluate() {
msg = .expectedCustomValueTo(
"match one of: " + postfixMessages.joined(separator: ", or "),
actual: "\(actualValue)"
Expand Down
12 changes: 12 additions & 0 deletions Tests/NimbleTests/Matchers/SatisfyAllOfTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,16 @@ final class SatisfyAllOfTest: XCTestCase {
expect(false).toNot(beTrue() && beFalse())
expect(true).toNot(beTruthy() && beFalsy())
}

func testSatisfyAllOfCachesExpressionBeforePassingToPredicates() {
// This is not a great example of assertion writing - functions being asserted on in Expressions should not have side effects.
// But we should still handle those cases anyway.
var value: Int = 0
func testFunction() -> Int {
value += 1
return value
}

expect(testFunction()).toEventually(satisfyAllOf(equal(1), equal(1)))
}
}
14 changes: 14 additions & 0 deletions Tests/NimbleTests/Matchers/SatisfyAnyOfTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,18 @@ final class SatisfyAnyOfTest: XCTestCase {
expect(false).to(beTrue() || beFalse())
expect(true).to(beTruthy() || beFalsy())
}

func testSatisfyAllOfCachesExpressionBeforePassingToPredicates() {
// This is not a great example of assertion writing - functions being asserted on in Expressions should not have side effects.
// But we should still handle those cases anyway.
var value: Int = 0
func testFunction() -> Int {
value += 1
return value
}

// This demonstrates caching because the first time this is evaluated, the function should return 1, which doesn't pass the `equal(0)`.
// Next time, it'll return 2, which doesn't pass the `equal(1)`.
expect(testFunction()).toEventually(satisfyAnyOf(equal(0), equal(1)))
}
}