From ed63d7adb2c7d975aa2300956bb81be346af55df Mon Sep 17 00:00:00 2001 From: Sho Ikeda Date: Sat, 11 Aug 2018 01:28:08 +0900 Subject: [PATCH 1/2] Add `beEmpty` matcher for `SetAlgebra` --- Sources/Nimble/Matchers/BeEmpty.swift | 11 ++++++ Tests/NimbleTests/Matchers/BeEmptyTest.swift | 40 ++++++++++++++++---- Tests/NimbleTests/XCTestManifests.swift | 1 + 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/Sources/Nimble/Matchers/BeEmpty.swift b/Sources/Nimble/Matchers/BeEmpty.swift index 8d73dccae..abc8ba287 100644 --- a/Sources/Nimble/Matchers/BeEmpty.swift +++ b/Sources/Nimble/Matchers/BeEmpty.swift @@ -13,6 +13,17 @@ public func beEmpty() -> Predicate { } } +/// 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 { + 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 { diff --git a/Tests/NimbleTests/Matchers/BeEmptyTest.swift b/Tests/NimbleTests/Matchers/BeEmptyTest.swift index 6059e87df..3ad7f843b 100644 --- a/Tests/NimbleTests/Matchers/BeEmptyTest.swift +++ b/Tests/NimbleTests/Matchers/BeEmptyTest.swift @@ -34,16 +34,12 @@ final class BeEmptyTest: XCTestCase, XCTestCaseProvider { expect("").to(beEmpty()) expect("foo").toNot(beEmpty()) + + expect([] as NSSortOptions).to(beEmpty()) + expect(NSSortOptions.concurrent).toNot(beEmpty()) } func testBeEmptyNegative() { - failsWithErrorMessageForNil("expected to be empty, got ") { - expect(nil as NSString?).to(beEmpty()) - } - failsWithErrorMessageForNil("expected to not be empty, got ") { - expect(nil as [CInt]?).toNot(beEmpty()) - } - failsWithErrorMessage("expected to not be empty, got <()>") { expect(NSArray()).toNot(beEmpty()) } @@ -73,5 +69,35 @@ final class BeEmptyTest: XCTestCase, XCTestCaseProvider { failsWithErrorMessage("expected to be empty, got ") { expect("foo").to(beEmpty()) } + + failsWithErrorMessage("expected to not be empty, got ") { + expect([] as NSSortOptions).toNot(beEmpty()) + } + failsWithErrorMessage("expected to be empty, got ") { + expect(NSSortOptions.concurrent).to(beEmpty()) + } + } + + func testNilMatches() { + failsWithErrorMessageForNil("expected to be empty, got ") { + expect(nil as NSString?).to(beEmpty()) + } + failsWithErrorMessageForNil("expected to not be empty, got ") { + expect(nil as NSString?).toNot(beEmpty()) + } + + failsWithErrorMessageForNil("expected to be empty, got ") { + expect(nil as [CInt]?).to(beEmpty()) + } + failsWithErrorMessageForNil("expected to not be empty, got ") { + expect(nil as [CInt]?).toNot(beEmpty()) + } + + failsWithErrorMessageForNil("expected to be empty, got ") { + expect(nil as NSSortOptions?).to(beEmpty()) + } + failsWithErrorMessageForNil("expected to not be empty, got ") { + expect(nil as NSSortOptions?).toNot(beEmpty()) + } } } diff --git a/Tests/NimbleTests/XCTestManifests.swift b/Tests/NimbleTests/XCTestManifests.swift index 65dc6031d..e637676ae 100644 --- a/Tests/NimbleTests/XCTestManifests.swift +++ b/Tests/NimbleTests/XCTestManifests.swift @@ -75,6 +75,7 @@ extension BeEmptyTest { static let __allTests = [ ("testBeEmptyNegative", testBeEmptyNegative), ("testBeEmptyPositive", testBeEmptyPositive), + ("testNilMatches", testNilMatches), ] } From 0f24060069aeb3a7e1ad46d37b27572df7e611eb Mon Sep 17 00:00:00 2001 From: Sho Ikeda Date: Mon, 20 Aug 2018 22:51:22 +0900 Subject: [PATCH 2/2] Add `TestOptionSet` to test `beEmpty()` --- Tests/NimbleTests/Matchers/BeEmptyTest.swift | 30 ++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/Tests/NimbleTests/Matchers/BeEmptyTest.swift b/Tests/NimbleTests/Matchers/BeEmptyTest.swift index 3ad7f843b..637bc7a6c 100644 --- a/Tests/NimbleTests/Matchers/BeEmptyTest.swift +++ b/Tests/NimbleTests/Matchers/BeEmptyTest.swift @@ -35,8 +35,8 @@ final class BeEmptyTest: XCTestCase, XCTestCaseProvider { expect("").to(beEmpty()) expect("foo").toNot(beEmpty()) - expect([] as NSSortOptions).to(beEmpty()) - expect(NSSortOptions.concurrent).toNot(beEmpty()) + expect([] as TestOptionSet).to(beEmpty()) + expect(TestOptionSet.one).toNot(beEmpty()) } func testBeEmptyNegative() { @@ -70,11 +70,11 @@ final class BeEmptyTest: XCTestCase, XCTestCaseProvider { expect("foo").to(beEmpty()) } - failsWithErrorMessage("expected to not be empty, got ") { - expect([] as NSSortOptions).toNot(beEmpty()) + failsWithErrorMessage("expected to not be empty, got ") { + expect([] as TestOptionSet).toNot(beEmpty()) } - failsWithErrorMessage("expected to be empty, got ") { - expect(NSSortOptions.concurrent).to(beEmpty()) + failsWithErrorMessage("expected to be empty, got ") { + expect(TestOptionSet.one).to(beEmpty()) } } @@ -94,10 +94,24 @@ final class BeEmptyTest: XCTestCase, XCTestCaseProvider { } failsWithErrorMessageForNil("expected to be empty, got ") { - expect(nil as NSSortOptions?).to(beEmpty()) + expect(nil as TestOptionSet?).to(beEmpty()) } failsWithErrorMessageForNil("expected to not be empty, got ") { - expect(nil as NSSortOptions?).toNot(beEmpty()) + 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))" + } +}