Skip to content

Commit 590ff03

Browse files
committed
Add contain matcher for SetAlgebra
1 parent a145269 commit 590ff03

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

Sources/Nimble/Matchers/Contain.swift

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import Foundation
22

3-
/// A Nimble matcher that succeeds when the actual sequence contains the expected value.
3+
/// A Nimble matcher that succeeds when the actual sequence contains the expected values.
44
public func contain<S: Sequence, T: Equatable>(_ items: T...) -> Predicate<S>
5-
where S.Iterator.Element == T {
5+
where S.Element == T {
66
return contain(items)
77
}
88

9+
/// A Nimble matcher that succeeds when the actual sequence contains the expected values.
910
public func contain<S: Sequence, T: Equatable>(_ items: [T]) -> Predicate<S>
10-
where S.Iterator.Element == T {
11+
where S.Element == T {
1112
return Predicate.simple("contain <\(arrayAsString(items))>") { actualExpression in
1213
if let actual = try actualExpression.evaluate() {
1314
let matches = items.allSatisfy {
@@ -19,6 +20,26 @@ public func contain<S: Sequence, T: Equatable>(_ items: [T]) -> Predicate<S>
1920
}
2021
}
2122

23+
/// A Nimble matcher that succeeds when the actual set contains the expected values.
24+
public func contain<S: SetAlgebra, T: Equatable>(_ items: T...) -> Predicate<S>
25+
where S.Element == T {
26+
return contain(items)
27+
}
28+
29+
/// A Nimble matcher that succeeds when the actual set contains the expected values.
30+
public func contain<S: SetAlgebra, T: Equatable>(_ items: [T]) -> Predicate<S>
31+
where S.Element == T {
32+
return Predicate.simple("contain <\(arrayAsString(items))>") { actualExpression in
33+
if let actual = try actualExpression.evaluate() {
34+
let matches = items.allSatisfy {
35+
return actual.contains($0)
36+
}
37+
return PredicateStatus(bool: matches)
38+
}
39+
return .fail
40+
}
41+
}
42+
2243
/// A Nimble matcher that succeeds when the actual string contains the expected substring.
2344
public func contain(_ substrings: String...) -> Predicate<String> {
2445
return contain(substrings)

Tests/NimbleTests/Matchers/ContainTest.swift

+41-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import XCTest
33
import Nimble
44

55
final class ContainTest: XCTestCase, XCTestCaseProvider {
6-
func testContain() {
6+
func testContainSequence() {
77
expect([1, 2, 3]).to(contain(1))
8+
expect([1, 2, 3]).toNot(contain(4))
89
expect([1, 2, 3] as [CInt]).to(contain(1 as CInt))
910
expect([1, 2, 3] as [CInt]).toNot(contain(4 as CInt))
1011
expect(["foo", "bar", "baz"]).to(contain("baz"))
11-
expect([1, 2, 3]).toNot(contain(4))
1212
expect(["foo", "bar", "baz"]).toNot(contain("ba"))
1313
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
1414
expect(NSArray(array: ["a"])).to(contain(NSString(string: "a")))
@@ -31,6 +31,25 @@ final class ContainTest: XCTestCase, XCTestCaseProvider {
3131
}
3232
}
3333

34+
func testContainSetAlgebra() {
35+
expect([.a, .b, .c] as TestOptionSet).to(contain(.a))
36+
expect([.a, .b, .c] as TestOptionSet).toNot(contain(.d))
37+
38+
failsWithErrorMessage("expected to contain <8>, got <7>") {
39+
expect([.a, .b, .c] as TestOptionSet).to(contain(.d))
40+
}
41+
failsWithErrorMessage("expected to not contain <2>, got <7>") {
42+
expect([.a, .b, .c] as TestOptionSet).toNot(contain(.b))
43+
}
44+
45+
failsWithErrorMessageForNil("expected to contain <1>, got <nil>") {
46+
expect(nil as TestOptionSet?).to(contain(.a))
47+
}
48+
failsWithErrorMessageForNil("expected to not contain <1>, got <nil>") {
49+
expect(nil as TestOptionSet?).toNot(contain(.a))
50+
}
51+
}
52+
3453
func testContainSubstring() {
3554
expect("foo").to(contain("o"))
3655
expect("foo").to(contain("oo"))
@@ -83,3 +102,23 @@ final class ContainTest: XCTestCase, XCTestCaseProvider {
83102
}
84103
}
85104
}
105+
106+
private struct TestOptionSet: OptionSet, CustomStringConvertible {
107+
let rawValue: Int
108+
109+
// swiftlint:disable identifier_name
110+
static let a = TestOptionSet(rawValue: 1 << 0)
111+
static let b = TestOptionSet(rawValue: 1 << 1)
112+
static let c = TestOptionSet(rawValue: 1 << 2)
113+
static let d = TestOptionSet(rawValue: 1 << 3)
114+
static let e = TestOptionSet(rawValue: 1 << 4)
115+
// swiftlint:enable identifier_name
116+
117+
init(rawValue: Int) {
118+
self.rawValue = rawValue
119+
}
120+
121+
var description: String {
122+
return "\(rawValue)"
123+
}
124+
}

Tests/NimbleTests/XCTestManifests.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ extension ContainElementSatisfyingTest {
200200
extension ContainTest {
201201
static let __allTests = [
202202
("testCollectionArguments", testCollectionArguments),
203-
("testContain", testContain),
204203
("testContainObjCSubstring", testContainObjCSubstring),
204+
("testContainSequence", testContainSequence),
205+
("testContainSetAlgebra", testContainSetAlgebra),
205206
("testContainSubstring", testContainSubstring),
206207
("testVariadicArguments", testVariadicArguments),
207208
]

0 commit comments

Comments
 (0)