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

Simplify RaisesExceptionTest #748

Merged
merged 1 commit into from
May 8, 2020
Merged
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
70 changes: 35 additions & 35 deletions Tests/NimbleTests/Matchers/RaisesExceptionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,151 +3,151 @@ import Nimble

#if canImport(Darwin) && !SWIFT_PACKAGE

final class RaisesExceptionTest: XCTestCase {
var anException = NSException(name: NSExceptionName("laugh"), reason: "Lulz", userInfo: ["key": "value"])
let anException = NSException(name: NSExceptionName("laugh"), reason: "Lulz", userInfo: ["key": "value"])

final class RaisesExceptionTest: XCTestCase {
func testPositiveMatches() {
expect { self.anException.raise() }.to(raiseException())
expect { self.anException.raise() }.to(raiseException(named: "laugh"))
expect { self.anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz"))
expect { self.anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz", userInfo: ["key": "value"]))
expect { anException.raise() }.to(raiseException())
expect { anException.raise() }.to(raiseException(named: "laugh"))
expect { anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz"))
expect { anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz", userInfo: ["key": "value"]))
}

func testPositiveMatchesWithClosures() {
expect { self.anException.raise() }.to(raiseException { (exception: NSException) in
expect { anException.raise() }.to(raiseException { (exception: NSException) in
expect(exception.name).to(equal(NSExceptionName("laugh")))
})
expect { self.anException.raise() }.to(raiseException(named: "laugh") { (exception: NSException) in
expect { anException.raise() }.to(raiseException(named: "laugh") { (exception: NSException) in
expect(exception.name.rawValue).to(beginWith("lau"))
})
expect { self.anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz") { (exception: NSException) in
expect { anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz") { (exception: NSException) in
expect(exception.name.rawValue).to(beginWith("lau"))
})
expect { self.anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz", userInfo: ["key": "value"]) { (exception: NSException) in
expect { anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz", userInfo: ["key": "value"]) { (exception: NSException) in
expect(exception.name.rawValue).to(beginWith("lau"))
})

expect { self.anException.raise() }.to(raiseException(named: "laugh") { (exception: NSException) in
expect { anException.raise() }.to(raiseException(named: "laugh") { (exception: NSException) in
expect(exception.name.rawValue).toNot(beginWith("as"))
})
expect { self.anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz") { (exception: NSException) in
expect { anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz") { (exception: NSException) in
expect(exception.name.rawValue).toNot(beginWith("df"))
})
expect { self.anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz", userInfo: ["key": "value"]) { (exception: NSException) in
expect { anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz", userInfo: ["key": "value"]) { (exception: NSException) in
expect(exception.name.rawValue).toNot(beginWith("as"))
})
}

func testNegativeMatches() {
failsWithErrorMessage("expected to raise exception with name <foo>, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }") {
expect { self.anException.raise() }.to(raiseException(named: "foo"))
expect { anException.raise() }.to(raiseException(named: "foo"))
}

failsWithErrorMessage("expected to raise exception with name <laugh> with reason <bar>, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }") {
expect { self.anException.raise() }.to(raiseException(named: "laugh", reason: "bar"))
expect { anException.raise() }.to(raiseException(named: "laugh", reason: "bar"))
}

failsWithErrorMessage(
"expected to raise exception with name <laugh> with reason <Lulz> with userInfo <{k = v;}>, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }") {
expect { self.anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz", userInfo: ["k": "v"]))
expect { anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz", userInfo: ["k": "v"]))
}

failsWithErrorMessage("expected to raise any exception, got no exception") {
expect { self.anException }.to(raiseException())
expect {}.to(raiseException())
}
failsWithErrorMessage("expected to not raise any exception, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }") {
expect { self.anException.raise() }.toNot(raiseException())
expect { anException.raise() }.toNot(raiseException())
}
failsWithErrorMessage("expected to raise exception with name <laugh> with reason <Lulz>, got no exception") {
expect { self.anException }.to(raiseException(named: "laugh", reason: "Lulz"))
expect {}.to(raiseException(named: "laugh", reason: "Lulz"))
}

failsWithErrorMessage("expected to raise exception with name <bar> with reason <Lulz>, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }") {
expect { self.anException.raise() }.to(raiseException(named: "bar", reason: "Lulz"))
expect { anException.raise() }.to(raiseException(named: "bar", reason: "Lulz"))
}
failsWithErrorMessage("expected to not raise exception with name <laugh>, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }") {
expect { self.anException.raise() }.toNot(raiseException(named: "laugh"))
expect { anException.raise() }.toNot(raiseException(named: "laugh"))
}
failsWithErrorMessage("expected to not raise exception with name <laugh> with reason <Lulz>, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }") {
expect { self.anException.raise() }.toNot(raiseException(named: "laugh", reason: "Lulz"))
expect { anException.raise() }.toNot(raiseException(named: "laugh", reason: "Lulz"))
}

failsWithErrorMessage("expected to not raise exception with name <laugh> with reason <Lulz> with userInfo <{key = value;}>, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }") {
expect { self.anException.raise() }.toNot(raiseException(named: "laugh", reason: "Lulz", userInfo: ["key": "value"]))
expect { anException.raise() }.toNot(raiseException(named: "laugh", reason: "Lulz", userInfo: ["key": "value"]))
}
}

func testNegativeMatchesDoNotCallClosureWithoutException() {
failsWithErrorMessage("expected to raise exception that satisfies block, got no exception") {
expect { self.anException }.to(raiseException { (exception: NSException) in
expect {}.to(raiseException { (exception: NSException) in
expect(exception.name).to(equal(NSExceptionName(rawValue: "foo")))
})
}

failsWithErrorMessage("expected to raise exception with name <foo> that satisfies block, got no exception") {
expect { self.anException }.to(raiseException(named: "foo") { (exception: NSException) in
expect {}.to(raiseException(named: "foo") { (exception: NSException) in
expect(exception.name.rawValue).to(equal("foo"))
})
}

failsWithErrorMessage("expected to raise exception with name <foo> with reason <ha> that satisfies block, got no exception") {
expect { self.anException }.to(raiseException(named: "foo", reason: "ha") { (exception: NSException) in
expect {}.to(raiseException(named: "foo", reason: "ha") { (exception: NSException) in
expect(exception.name.rawValue).to(equal("foo"))
})
}

failsWithErrorMessage("expected to raise exception with name <foo> with reason <Lulz> with userInfo <{}> that satisfies block, got no exception") {
expect { self.anException }.to(raiseException(named: "foo", reason: "Lulz", userInfo: [:]) { (exception: NSException) in
expect {}.to(raiseException(named: "foo", reason: "Lulz", userInfo: [:]) { (exception: NSException) in
expect(exception.name.rawValue).to(equal("foo"))
})
}

failsWithErrorMessage("expected to not raise any exception, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }") {
expect { self.anException.raise() }.toNot(raiseException())
expect { anException.raise() }.toNot(raiseException())
}
}

func testNegativeMatchesWithClosure() {
failsWithErrorMessage("expected to raise exception that satisfies block, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }") {
expect { self.anException.raise() }.to(raiseException { (exception: NSException) in
expect { anException.raise() }.to(raiseException { (exception: NSException) in
expect(exception.name.rawValue).to(equal("foo"))
})
}

let innerFailureMessage = "expected to begin with <fo>, got <laugh>"

failsWithErrorMessage([innerFailureMessage, "expected to raise exception with name <laugh> that satisfies block, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }"]) {
expect { self.anException.raise() }.to(raiseException(named: "laugh") { (exception: NSException) in
expect { anException.raise() }.to(raiseException(named: "laugh") { (exception: NSException) in
expect(exception.name.rawValue).to(beginWith("fo"))
})
}

failsWithErrorMessage([innerFailureMessage, "expected to raise exception with name <lol> that satisfies block, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }"]) {
expect { self.anException.raise() }.to(raiseException(named: "lol") { (exception: NSException) in
expect { anException.raise() }.to(raiseException(named: "lol") { (exception: NSException) in
expect(exception.name.rawValue).to(beginWith("fo"))
})
}

failsWithErrorMessage([innerFailureMessage, "expected to raise exception with name <laugh> with reason <Lulz> that satisfies block, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }"]) {
expect { self.anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz") { (exception: NSException) in
expect { anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz") { (exception: NSException) in
expect(exception.name.rawValue).to(beginWith("fo"))
})
}

failsWithErrorMessage([innerFailureMessage, "expected to raise exception with name <lol> with reason <wrong> that satisfies block, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }"]) {
expect { self.anException.raise() }.to(raiseException(named: "lol", reason: "wrong") { (exception: NSException) in
expect { anException.raise() }.to(raiseException(named: "lol", reason: "wrong") { (exception: NSException) in
expect(exception.name.rawValue).to(beginWith("fo"))
})
}

failsWithErrorMessage([innerFailureMessage, "expected to raise exception with name <laugh> with reason <Lulz> with userInfo <{key = value;}> that satisfies block, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }"]) {
expect { self.anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz", userInfo: ["key": "value"]) { (exception: NSException) in
expect { anException.raise() }.to(raiseException(named: "laugh", reason: "Lulz", userInfo: ["key": "value"]) { (exception: NSException) in
expect(exception.name.rawValue).to(beginWith("fo"))
})
}

failsWithErrorMessage([innerFailureMessage, "expected to raise exception with name <lol> with reason <Lulz> with userInfo <{}> that satisfies block, got NSException { name=NSExceptionName(_rawValue: laugh), reason='Lulz', userInfo=[AnyHashable(\"key\"): \"value\"] }"]) {
expect { self.anException.raise() }.to(raiseException(named: "lol", reason: "Lulz", userInfo: [:]) { (exception: NSException) in
expect { anException.raise() }.to(raiseException(named: "lol", reason: "Lulz", userInfo: [:]) { (exception: NSException) in
expect(exception.name.rawValue).to(beginWith("fo"))
})
}
Expand Down