Skip to content

Commit 548c1f0

Browse files
authored
Merge pull request #860 from younata/satisfyAllOf_should_fail_when_matchers_fail
Make the satisfyAllOf and satifyAnyOf matchers fail correctly when the submatchers fail
2 parents 090a66b + 96ef000 commit 548c1f0

File tree

6 files changed

+58
-8
lines changed

6 files changed

+58
-8
lines changed

Nimble.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@
320320
7B5358BE1C38479700A23FAA /* SatisfyAnyOf.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B5358BD1C38479700A23FAA /* SatisfyAnyOf.swift */; };
321321
7B5358BF1C38479700A23FAA /* SatisfyAnyOf.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B5358BD1C38479700A23FAA /* SatisfyAnyOf.swift */; };
322322
7B5358C01C38479700A23FAA /* SatisfyAnyOf.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B5358BD1C38479700A23FAA /* SatisfyAnyOf.swift */; };
323+
898F28B025D9F4C30052B8D0 /* AlwaysFailMatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 898F28AF25D9F4C30052B8D0 /* AlwaysFailMatcher.swift */; };
324+
898F28B125D9F4C30052B8D0 /* AlwaysFailMatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 898F28AF25D9F4C30052B8D0 /* AlwaysFailMatcher.swift */; };
325+
898F28B225D9F4C30052B8D0 /* AlwaysFailMatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 898F28AF25D9F4C30052B8D0 /* AlwaysFailMatcher.swift */; };
323326
964CFEFD1C4FF48900513336 /* ThrowAssertion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 964CFEFC1C4FF48900513336 /* ThrowAssertion.swift */; };
324327
964CFEFE1C4FF48900513336 /* ThrowAssertion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 964CFEFC1C4FF48900513336 /* ThrowAssertion.swift */; };
325328
964CFEFF1C4FF48900513336 /* ThrowAssertion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 964CFEFC1C4FF48900513336 /* ThrowAssertion.swift */; };
@@ -617,6 +620,7 @@
617620
7B5358B91C3846C900A23FAA /* SatisfyAnyOfTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SatisfyAnyOfTest.swift; sourceTree = "<group>"; };
618621
7B5358BD1C38479700A23FAA /* SatisfyAnyOf.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SatisfyAnyOf.swift; sourceTree = "<group>"; };
619622
7B5358C11C39155600A23FAA /* ObjCSatisfyAnyOfTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ObjCSatisfyAnyOfTest.m; sourceTree = "<group>"; };
623+
898F28AF25D9F4C30052B8D0 /* AlwaysFailMatcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlwaysFailMatcher.swift; sourceTree = "<group>"; };
620624
8DF1C3F61C94FC75004B2D36 /* ObjcStringersTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ObjcStringersTest.m; sourceTree = "<group>"; };
621625
964CFEFC1C4FF48900513336 /* ThrowAssertion.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ThrowAssertion.swift; sourceTree = "<group>"; };
622626
965B0D081B62B8ED0005AE66 /* ObjCUserDescriptionTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ObjCUserDescriptionTest.m; sourceTree = "<group>"; };
@@ -707,6 +711,7 @@
707711
children = (
708712
1F14FB63194180C5009F2A08 /* utils.swift */,
709713
1F0648CB19639F5A001F9C46 /* ObjectWithLazyProperty.swift */,
714+
898F28AF25D9F4C30052B8D0 /* AlwaysFailMatcher.swift */,
710715
);
711716
path = Helpers;
712717
sourceTree = "<group>";
@@ -1404,6 +1409,7 @@
14041409
buildActionMask = 2147483647;
14051410
files = (
14061411
1F4A569A1A3B3539009E1637 /* ObjCEqualTest.m in Sources */,
1412+
898F28B125D9F4C30052B8D0 /* AlwaysFailMatcher.swift in Sources */,
14071413
1F925EEC195C12C800ED456B /* RaisesExceptionTest.swift in Sources */,
14081414
62FB326A23B78D500047BED9 /* BeginWithPrefixTest.swift in Sources */,
14091415
1F925EFF195C187600ED456B /* EndWithTest.swift in Sources */,
@@ -1545,6 +1551,7 @@
15451551
buildActionMask = 2147483647;
15461552
files = (
15471553
CD79C9AD1D2CC848004B6F9A /* ObjCBeTrueTest.m in Sources */,
1554+
898F28B225D9F4C30052B8D0 /* AlwaysFailMatcher.swift in Sources */,
15481555
CD79C9B41D2CC848004B6F9A /* ObjCRaiseExceptionTest.m in Sources */,
15491556
62FB326923B78D4F0047BED9 /* BeginWithPrefixTest.swift in Sources */,
15501557
1F5DF1A31BDCA10200C3A531 /* BeLogicalTest.swift in Sources */,
@@ -1691,6 +1698,7 @@
16911698
buildActionMask = 2147483647;
16921699
files = (
16931700
1F4A569B1A3B3539009E1637 /* ObjCEqualTest.m in Sources */,
1701+
898F28B025D9F4C30052B8D0 /* AlwaysFailMatcher.swift in Sources */,
16941702
1F925EED195C12C800ED456B /* RaisesExceptionTest.swift in Sources */,
16951703
62FB326B23B78D510047BED9 /* BeginWithPrefixTest.swift in Sources */,
16961704
1F925F00195C187600ED456B /* EndWithTest.swift in Sources */,

Sources/Nimble/Matchers/SatisfyAllOf.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ public func satisfyAllOf<T, U>(_ matchers: U...) -> Predicate<T>
1717
public func satisfyAllOf<T>(_ predicates: [Predicate<T>]) -> Predicate<T> {
1818
return Predicate.define { actualExpression in
1919
var postfixMessages = [String]()
20-
var matches = true
20+
var status: PredicateStatus = .matches
2121
for predicate in predicates {
2222
let result = try predicate.satisfies(actualExpression)
23-
if result.toBoolean(expectation: .toNotMatch) {
24-
matches = false
23+
if result.status == .fail {
24+
status = .fail
25+
} else if result.status == .doesNotMatch, status != .fail {
26+
status = .doesNotMatch
2527
}
2628
postfixMessages.append("{\(result.message.expectedMessage)}")
2729
}
@@ -38,7 +40,7 @@ public func satisfyAllOf<T>(_ predicates: [Predicate<T>]) -> Predicate<T> {
3840
)
3941
}
4042

41-
return PredicateResult(bool: matches, message: msg)
43+
return PredicateResult(status: status, message: msg)
4244
}
4345
}
4446

Sources/Nimble/Matchers/SatisfyAnyOf.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ public func satisfyAnyOf<T, U>(_ matchers: U...) -> Predicate<T>
1717
public func satisfyAnyOf<T>(_ predicates: [Predicate<T>]) -> Predicate<T> {
1818
return Predicate.define { actualExpression in
1919
var postfixMessages = [String]()
20-
var matches = false
20+
var status: PredicateStatus = .doesNotMatch
2121
for predicate in predicates {
2222
let result = try predicate.satisfies(actualExpression)
23-
if result.toBoolean(expectation: .toMatch) {
24-
matches = true
23+
if result.status == .fail {
24+
status = .fail
25+
} else if result.status == .matches, status != .fail {
26+
status = .matches
2527
}
2628
postfixMessages.append("{\(result.message.expectedMessage)}")
2729
}
@@ -38,7 +40,7 @@ public func satisfyAnyOf<T>(_ predicates: [Predicate<T>]) -> Predicate<T> {
3840
)
3941
}
4042

41-
return PredicateResult(bool: matches, message: msg)
43+
return PredicateResult(status: status, message: msg)
4244
}
4345
}
4446

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import XCTest
2+
import Nimble
3+
4+
func alwaysFail<T>() -> Predicate<T> {
5+
return Predicate { _ throws -> PredicateResult in
6+
return PredicateResult(status: .fail, message: .fail("This matcher should always fail"))
7+
}
8+
}
9+
10+
final class AlwaysFailTest: XCTestCase {
11+
func testAlwaysFail() {
12+
failsWithErrorMessage(
13+
"This matcher should always fail") {
14+
expect(true).toNot(alwaysFail())
15+
}
16+
17+
failsWithErrorMessage(
18+
"This matcher should always fail") {
19+
expect(true).to(alwaysFail())
20+
}
21+
}
22+
}

Tests/NimbleTests/Matchers/SatisfyAllOfTest.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ final class SatisfyAllOfTest: XCTestCase {
2828
"expected to not match all of: {be greater than <10.5>}, and {be less than <100.75>}, and {be close to <50.1> (within 0.0001)}, got 50.10001") {
2929
expect(50.10001).toNot(satisfyAllOf(beGreaterThan(10.5), beLessThan(100.75), beCloseTo(50.1)))
3030
}
31+
failsWithErrorMessage(
32+
"expected to not match all of: {This matcher should always fail}, and {This matcher should always fail}, got true") {
33+
expect(true).toNot(satisfyAllOf(alwaysFail(), alwaysFail()))
34+
}
35+
failsWithErrorMessage(
36+
"expected to match all of: {This matcher should always fail}, and {This matcher should always fail}, got true") {
37+
expect(true).to(satisfyAllOf(alwaysFail(), alwaysFail()))
38+
}
3139
}
3240

3341
func testOperatorAnd() {

Tests/NimbleTests/Matchers/SatisfyAnyOfTest.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ final class SatisfyAnyOfTest: XCTestCase {
2828
"expected to not match one of: {be less than <10.5>}, or {be greater than <100.75>}, or {be close to <50.1> (within 0.0001)}, got 50.10001") {
2929
expect(50.10001).toNot(satisfyAnyOf(beLessThan(10.5), beGreaterThan(100.75), beCloseTo(50.1)))
3030
}
31+
failsWithErrorMessage(
32+
"expected to match one of: {This matcher should always fail}, or {This matcher should always fail}, got true") {
33+
expect(true).to(satisfyAnyOf(alwaysFail(), alwaysFail()))
34+
}
35+
failsWithErrorMessage(
36+
"expected to not match one of: {This matcher should always fail}, or {This matcher should always fail}, got true") {
37+
expect(true).toNot(satisfyAnyOf(alwaysFail(), alwaysFail()))
38+
}
3139
}
3240

3341
func testOperatorOr() {

0 commit comments

Comments
 (0)