Skip to content

Commit 4283270

Browse files
authored
Merge pull request #639 from Quick/fix-beempty-and-contain-matcher-ambiguity
Fix `beEmpty` and `contain` matcher ambiguity for some types which conform to both Sequence and SetAlgebra
2 parents a27c518 + 0f17558 commit 4283270

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

Sources/Nimble/Matchers/BeEmpty.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import Foundation
44
/// means the are no items in that collection. For strings, it is an empty string.
55
public func beEmpty<S: Sequence>() -> Predicate<S> {
66
return Predicate.simple("be empty") { actualExpression in
7-
let actualSeq = try actualExpression.evaluate()
8-
if actualSeq == nil {
7+
guard let actual = try actualExpression.evaluate() else {
98
return .fail
109
}
11-
var generator = actualSeq!.makeIterator()
10+
var generator = actual.makeIterator()
1211
return PredicateStatus(bool: generator.next() == nil)
1312
}
1413
}
@@ -24,6 +23,17 @@ public func beEmpty<S: SetAlgebra>() -> Predicate<S> {
2423
}
2524
}
2625

26+
/// A Nimble matcher that succeeds when a value is "empty". For collections, this
27+
/// means the are no items in that collection. For strings, it is an empty string.
28+
public func beEmpty<S: Sequence & SetAlgebra>() -> Predicate<S> {
29+
return Predicate.simple("be empty") { actualExpression in
30+
guard let actual = try actualExpression.evaluate() else {
31+
return .fail
32+
}
33+
return PredicateStatus(bool: actual.isEmpty)
34+
}
35+
}
36+
2737
/// A Nimble matcher that succeeds when a value is "empty". For collections, this
2838
/// means the are no items in that collection. For strings, it is an empty string.
2939
public func beEmpty() -> Predicate<String> {

Sources/Nimble/Matchers/Contain.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ public func contain<S: SetAlgebra, T: Equatable>(_ items: [T]) -> Predicate<S>
4040
}
4141
}
4242

43+
/// A Nimble matcher that succeeds when the actual set contains the expected values.
44+
public func contain<S: Sequence & SetAlgebra, T: Equatable>(_ items: T...) -> Predicate<S>
45+
where S.Element == T {
46+
return contain(items)
47+
}
48+
49+
/// A Nimble matcher that succeeds when the actual set contains the expected values.
50+
public func contain<S: Sequence & SetAlgebra, T: Equatable>(_ items: [T]) -> Predicate<S>
51+
where S.Element == T {
52+
return Predicate.simple("contain <\(arrayAsString(items))>") { actualExpression in
53+
if let actual = try actualExpression.evaluate() {
54+
let matches = items.allSatisfy {
55+
return actual.contains($0)
56+
}
57+
return PredicateStatus(bool: matches)
58+
}
59+
return .fail
60+
}
61+
}
62+
4363
/// A Nimble matcher that succeeds when the actual string contains the expected substring.
4464
public func contain(_ substrings: String...) -> Predicate<String> {
4565
return contain(substrings)

Tests/NimbleTests/Matchers/BeEmptyTest.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ final class BeEmptyTest: XCTestCase, XCTestCaseProvider {
1010
expect([] as [CInt]).to(beEmpty())
1111
expect([1] as [CInt]).toNot(beEmpty())
1212

13+
expect([] as Set<Int>).to(beEmpty())
14+
expect([1] as Set<Int>).toNot(beEmpty())
15+
1316
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
1417
expect(NSDictionary() as? [Int: Int]).to(beEmpty())
1518
expect(NSDictionary(object: 1, forKey: 1 as NSNumber) as? [Int: Int]).toNot(beEmpty())
@@ -47,6 +50,13 @@ final class BeEmptyTest: XCTestCase, XCTestCaseProvider {
4750
expect([1]).to(beEmpty())
4851
}
4952

53+
failsWithErrorMessage("expected to not be empty, got <Set([])>") {
54+
expect([] as Set<Int>).toNot(beEmpty())
55+
}
56+
failsWithErrorMessage("expected to be empty, got <Set([1])>") {
57+
expect([1] as Set<Int>).to(beEmpty())
58+
}
59+
5060
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
5161
failsWithErrorMessage("expected to not be empty, got <{()}>") {
5262
expect(NSSet()).toNot(beEmpty())
@@ -93,6 +103,13 @@ final class BeEmptyTest: XCTestCase, XCTestCaseProvider {
93103
expect(nil as [CInt]?).toNot(beEmpty())
94104
}
95105

106+
failsWithErrorMessageForNil("expected to be empty, got <nil>") {
107+
expect(nil as Set<Int>?).to(beEmpty())
108+
}
109+
failsWithErrorMessageForNil("expected to not be empty, got <nil>") {
110+
expect(nil as Set<Int>?).toNot(beEmpty())
111+
}
112+
96113
failsWithErrorMessageForNil("expected to be empty, got <nil>") {
97114
expect(nil as TestOptionSet?).to(beEmpty())
98115
}

Tests/NimbleTests/Matchers/ContainTest.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ final class ContainTest: XCTestCase, XCTestCaseProvider {
5050
}
5151
}
5252

53+
func testContainSequenceAndSetAlgebra() {
54+
let set = [1, 2, 3] as Set<Int>
55+
56+
expect(set).to(contain(1))
57+
expect(set).toNot(contain(4))
58+
59+
failsWithErrorMessage("expected to contain <4>, got <\(set.debugDescription)>") {
60+
expect(set).to(contain(4))
61+
}
62+
failsWithErrorMessage("expected to not contain <2>, got <\(set.debugDescription)>") {
63+
expect(set).toNot(contain(2))
64+
}
65+
66+
failsWithErrorMessageForNil("expected to contain <1>, got <nil>") {
67+
expect(nil as Set<Int>?).to(contain(1))
68+
}
69+
failsWithErrorMessageForNil("expected to not contain <1>, got <nil>") {
70+
expect(nil as Set<Int>?).toNot(contain(1))
71+
}
72+
}
73+
5374
func testContainSubstring() {
5475
expect("foo").to(contain("o"))
5576
expect("foo").to(contain("oo"))

Tests/NimbleTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ extension ContainTest {
202202
("testCollectionArguments", testCollectionArguments),
203203
("testContainObjCSubstring", testContainObjCSubstring),
204204
("testContainSequence", testContainSequence),
205+
("testContainSequenceAndSetAlgebra", testContainSequenceAndSetAlgebra),
205206
("testContainSetAlgebra", testContainSetAlgebra),
206207
("testContainSubstring", testContainSubstring),
207208
("testVariadicArguments", testVariadicArguments),

0 commit comments

Comments
 (0)