Skip to content

Commit

Permalink
Merge pull request Quick#594 from Quick/setalgebra-beempty
Browse files Browse the repository at this point in the history
Add `beEmpty` matcher for `SetAlgebra`
  • Loading branch information
ikesyo authored Aug 21, 2018
2 parents 83718a4 + 0f24060 commit f606010
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
11 changes: 11 additions & 0 deletions Sources/Nimble/Matchers/BeEmpty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ public func beEmpty<S: Sequence>() -> Predicate<S> {
}
}

/// A Nimble matcher that succeeds when a value is "empty". For collections, this
/// means the are no items in that collection. For strings, it is an empty string.
public func beEmpty<S: SetAlgebra>() -> Predicate<S> {
return Predicate.simple("be empty") { actualExpression in
guard let actual = try actualExpression.evaluate() else {
return .fail
}
return PredicateStatus(bool: actual.isEmpty)
}
}

/// A Nimble matcher that succeeds when a value is "empty". For collections, this
/// means the are no items in that collection. For strings, it is an empty string.
public func beEmpty() -> Predicate<String> {
Expand Down
54 changes: 47 additions & 7 deletions Tests/NimbleTests/Matchers/BeEmptyTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,12 @@ final class BeEmptyTest: XCTestCase, XCTestCaseProvider {

expect("").to(beEmpty())
expect("foo").toNot(beEmpty())

expect([] as TestOptionSet).to(beEmpty())
expect(TestOptionSet.one).toNot(beEmpty())
}

func testBeEmptyNegative() {
failsWithErrorMessageForNil("expected to be empty, got <nil>") {
expect(nil as NSString?).to(beEmpty())
}
failsWithErrorMessageForNil("expected to not be empty, got <nil>") {
expect(nil as [CInt]?).toNot(beEmpty())
}

failsWithErrorMessage("expected to not be empty, got <()>") {
expect(NSArray()).toNot(beEmpty())
}
Expand Down Expand Up @@ -73,5 +69,49 @@ final class BeEmptyTest: XCTestCase, XCTestCaseProvider {
failsWithErrorMessage("expected to be empty, got <foo>") {
expect("foo").to(beEmpty())
}

failsWithErrorMessage("expected to not be empty, got <TestOptionSet(rawValue: 0)>") {
expect([] as TestOptionSet).toNot(beEmpty())
}
failsWithErrorMessage("expected to be empty, got <TestOptionSet(rawValue: 1)>") {
expect(TestOptionSet.one).to(beEmpty())
}
}

func testNilMatches() {
failsWithErrorMessageForNil("expected to be empty, got <nil>") {
expect(nil as NSString?).to(beEmpty())
}
failsWithErrorMessageForNil("expected to not be empty, got <nil>") {
expect(nil as NSString?).toNot(beEmpty())
}

failsWithErrorMessageForNil("expected to be empty, got <nil>") {
expect(nil as [CInt]?).to(beEmpty())
}
failsWithErrorMessageForNil("expected to not be empty, got <nil>") {
expect(nil as [CInt]?).toNot(beEmpty())
}

failsWithErrorMessageForNil("expected to be empty, got <nil>") {
expect(nil as TestOptionSet?).to(beEmpty())
}
failsWithErrorMessageForNil("expected to not be empty, got <nil>") {
expect(nil as TestOptionSet?).toNot(beEmpty())
}
}
}

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

static let one = TestOptionSet(rawValue: 1 << 0)

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

var description: String {
return "TestOptionSet(rawValue: \(rawValue))"
}
}
1 change: 1 addition & 0 deletions Tests/NimbleTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ extension BeEmptyTest {
static let __allTests = [
("testBeEmptyNegative", testBeEmptyNegative),
("testBeEmptyPositive", testBeEmptyPositive),
("testNilMatches", testNilMatches),
]
}

Expand Down

0 comments on commit f606010

Please sign in to comment.