Skip to content

Commit

Permalink
Merge pull request #609 from Quick/setalgebra-contain
Browse files Browse the repository at this point in the history
Add `contain` matcher for `SetAlgebra`
  • Loading branch information
ikesyo authored Sep 29, 2018
2 parents a145269 + 590ff03 commit c72edbb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
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

0 comments on commit c72edbb

Please sign in to comment.