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

Add contain matcher for SetAlgebra #609

Merged
merged 1 commit into from
Sep 29, 2018
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
27 changes: 24 additions & 3 deletions Sources/Nimble/Matchers/Contain.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import Foundation

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

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

/// A Nimble matcher that succeeds when the actual set contains the expected values.
public func contain<S: SetAlgebra, T: Equatable>(_ items: T...) -> Predicate<S>
where S.Element == T {
return contain(items)
}

/// A Nimble matcher that succeeds when the actual set contains the expected values.
public func contain<S: SetAlgebra, T: Equatable>(_ items: [T]) -> Predicate<S>
where S.Element == T {
return Predicate.simple("contain <\(arrayAsString(items))>") { actualExpression in
if let actual = try actualExpression.evaluate() {
let matches = items.allSatisfy {
return actual.contains($0)
}
return PredicateStatus(bool: matches)
}
return .fail
}
}

/// A Nimble matcher that succeeds when the actual string contains the expected substring.
public func contain(_ substrings: String...) -> Predicate<String> {
return contain(substrings)
Expand Down
43 changes: 41 additions & 2 deletions Tests/NimbleTests/Matchers/ContainTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import XCTest
import Nimble

final class ContainTest: XCTestCase, XCTestCaseProvider {
func testContain() {
func testContainSequence() {
expect([1, 2, 3]).to(contain(1))
expect([1, 2, 3]).toNot(contain(4))
expect([1, 2, 3] as [CInt]).to(contain(1 as CInt))
expect([1, 2, 3] as [CInt]).toNot(contain(4 as CInt))
expect(["foo", "bar", "baz"]).to(contain("baz"))
expect([1, 2, 3]).toNot(contain(4))
expect(["foo", "bar", "baz"]).toNot(contain("ba"))
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
expect(NSArray(array: ["a"])).to(contain(NSString(string: "a")))
Expand All @@ -31,6 +31,25 @@ final class ContainTest: XCTestCase, XCTestCaseProvider {
}
}

func testContainSetAlgebra() {
expect([.a, .b, .c] as TestOptionSet).to(contain(.a))
expect([.a, .b, .c] as TestOptionSet).toNot(contain(.d))

failsWithErrorMessage("expected to contain <8>, got <7>") {
expect([.a, .b, .c] as TestOptionSet).to(contain(.d))
}
failsWithErrorMessage("expected to not contain <2>, got <7>") {
expect([.a, .b, .c] as TestOptionSet).toNot(contain(.b))
}

failsWithErrorMessageForNil("expected to contain <1>, got <nil>") {
expect(nil as TestOptionSet?).to(contain(.a))
}
failsWithErrorMessageForNil("expected to not contain <1>, got <nil>") {
expect(nil as TestOptionSet?).toNot(contain(.a))
}
}

func testContainSubstring() {
expect("foo").to(contain("o"))
expect("foo").to(contain("oo"))
Expand Down Expand Up @@ -83,3 +102,23 @@ final class ContainTest: XCTestCase, XCTestCaseProvider {
}
}
}

private struct TestOptionSet: OptionSet, CustomStringConvertible {
let rawValue: Int

// swiftlint:disable identifier_name
static let a = TestOptionSet(rawValue: 1 << 0)
static let b = TestOptionSet(rawValue: 1 << 1)
static let c = TestOptionSet(rawValue: 1 << 2)
static let d = TestOptionSet(rawValue: 1 << 3)
static let e = TestOptionSet(rawValue: 1 << 4)
// swiftlint:enable identifier_name

init(rawValue: Int) {
self.rawValue = rawValue
}

var description: String {
return "\(rawValue)"
}
}
3 changes: 2 additions & 1 deletion Tests/NimbleTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ extension ContainElementSatisfyingTest {
extension ContainTest {
static let __allTests = [
("testCollectionArguments", testCollectionArguments),
("testContain", testContain),
("testContainObjCSubstring", testContainObjCSubstring),
("testContainSequence", testContainSequence),
("testContainSetAlgebra", testContainSetAlgebra),
("testContainSubstring", testContainSubstring),
("testVariadicArguments", testVariadicArguments),
]
Expand Down