mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
tests: improve tests that are supposed to throw (#1430)
Motivation:
The
do {
try someOperation()
XCTFail("should throw") // easy to forget
} catch error as SomethingError {
XCTAssertEqual(.something, error as? SomethingError)
} catch {
XCTFail("wrong error")
}
pattern is not only very long, it's also very error prone. If you forget
any of the XCTFails, you might not tests what it looks like
XCTAssertThrowsError(try someOperation) { error in
XCTAssertEqual(.something, error as? SomethingError)
}
is much safer and shorter.
Modifcations:
Do many of the above replaces.
Result:
Cleaner, shorter, and safer tests.
This commit is contained in:
1 parent
2512b8e277
commit
8912d1a48b
19 files changed
+282
-736
No files matched your search
@@ -247,13 +247,8 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
|
||||
// larger than the frame max.
|
||||
self.buffer.writeBytes([0x81, 0xFE, 0x40, 0x01])
|
||||
|
||||
do {
|
||||
try self.decoderChannel.writeInbound(self.buffer)
|
||||
XCTFail("did not throw")
|
||||
} catch NIOWebSocketError.invalidFrameLength {
|
||||
// OK
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
|
||||
XCTAssertEqual(.invalidFrameLength, error as? NIOWebSocketError)
|
||||
}
|
||||
|
||||
// We expect that an error frame will have been written out.
|
||||
@@ -267,13 +262,8 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
|
||||
// A fake frame header that claims this is a fragmented ping frame.
|
||||
self.buffer.writeBytes([0x09, 0x00])
|
||||
|
||||
do {
|
||||
try self.decoderChannel.writeInbound(self.buffer)
|
||||
XCTFail("did not throw")
|
||||
} catch NIOWebSocketError.fragmentedControlFrame {
|
||||
// OK
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
|
||||
XCTAssertEqual(.fragmentedControlFrame, error as? NIOWebSocketError)
|
||||
}
|
||||
|
||||
// We expect that an error frame will have been written out.
|
||||
@@ -287,13 +277,8 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
|
||||
// A fake frame header that claims this is a ping frame with 126 bytes of data.
|
||||
self.buffer.writeBytes([0x89, 0x7E, 0x00, 0x7E])
|
||||
|
||||
do {
|
||||
try self.decoderChannel.writeInbound(self.buffer)
|
||||
XCTFail("did not throw")
|
||||
} catch NIOWebSocketError.multiByteControlFrameLength {
|
||||
// OK
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
|
||||
XCTAssertEqual(.multiByteControlFrameLength, error as? NIOWebSocketError)
|
||||
}
|
||||
|
||||
// We expect that an error frame will have been written out.
|
||||
@@ -309,13 +294,8 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
|
||||
// A fake frame header that claims this is a fragmented ping frame.
|
||||
self.buffer.writeBytes([0x09, 0x00])
|
||||
|
||||
do {
|
||||
try self.decoderChannel.writeInbound(self.buffer)
|
||||
XCTFail("did not throw")
|
||||
} catch NIOWebSocketError.fragmentedControlFrame {
|
||||
// OK
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
|
||||
XCTAssertEqual(.fragmentedControlFrame, error as? NIOWebSocketError)
|
||||
}
|
||||
|
||||
// We expect that an error frame will have been written out.
|
||||
@@ -377,13 +357,8 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
|
||||
// larger than the frame max.
|
||||
self.buffer.writeBytes([0x81, 0xFE, 0x40, 0x01])
|
||||
|
||||
do {
|
||||
try self.decoderChannel.writeInbound(self.buffer)
|
||||
XCTFail("did not throw")
|
||||
} catch NIOWebSocketError.invalidFrameLength {
|
||||
// OK
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
|
||||
XCTAssertEqual(.invalidFrameLength, error as? NIOWebSocketError)
|
||||
}
|
||||
|
||||
// No error frame should be written.
|
||||
@@ -399,13 +374,8 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
|
||||
// A fake frame header that claims this is a fragmented ping frame.
|
||||
self.buffer.writeBytes([0x09, 0x00])
|
||||
|
||||
do {
|
||||
try self.decoderChannel.writeInbound(self.buffer)
|
||||
XCTFail("did not throw")
|
||||
} catch NIOWebSocketError.fragmentedControlFrame {
|
||||
// OK
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
|
||||
XCTAssertEqual(.fragmentedControlFrame, error as? NIOWebSocketError)
|
||||
}
|
||||
|
||||
// No error frame should be written.
|
||||
@@ -421,13 +391,8 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
|
||||
// A fake frame header that claims this is a ping frame with 126 bytes of data.
|
||||
self.buffer.writeBytes([0x89, 0x7E, 0x00, 0x7E])
|
||||
|
||||
do {
|
||||
try self.decoderChannel.writeInbound(self.buffer)
|
||||
XCTFail("did not throw")
|
||||
} catch NIOWebSocketError.multiByteControlFrameLength {
|
||||
// OK
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
|
||||
XCTAssertEqual(.multiByteControlFrameLength, error as? NIOWebSocketError)
|
||||
}
|
||||
|
||||
// No error frame should be written.
|
||||
@@ -443,13 +408,8 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
|
||||
// A fake frame header that claims this is a fragmented ping frame.
|
||||
self.buffer.writeBytes([0x09, 0x00])
|
||||
|
||||
do {
|
||||
try self.decoderChannel.writeInbound(self.buffer)
|
||||
XCTFail("did not throw")
|
||||
} catch NIOWebSocketError.fragmentedControlFrame {
|
||||
// OK
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
|
||||
XCTAssertEqual(.fragmentedControlFrame, error as? NIOWebSocketError)
|
||||
}
|
||||
|
||||
// No error frame should be written.
|
||||
@@ -484,13 +444,8 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
|
||||
// larger than the frame max.
|
||||
self.buffer.writeBytes([0x81, 0xFE, 0x40, 0x01])
|
||||
|
||||
do {
|
||||
try self.decoderChannel.writeInbound(self.buffer)
|
||||
XCTFail("did not throw")
|
||||
} catch NIOWebSocketError.invalidFrameLength {
|
||||
// OK
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
|
||||
XCTAssertEqual(.invalidFrameLength, error as? NIOWebSocketError)
|
||||
}
|
||||
|
||||
// We expect that an error frame will have been written out.
|
||||
@@ -507,13 +462,8 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
|
||||
// A fake frame header that claims this is a fragmented ping frame.
|
||||
self.buffer.writeBytes([0x09, 0x00])
|
||||
|
||||
do {
|
||||
try self.decoderChannel.writeInbound(self.buffer)
|
||||
XCTFail("did not throw")
|
||||
} catch NIOWebSocketError.fragmentedControlFrame {
|
||||
// OK
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
|
||||
XCTAssertEqual(.fragmentedControlFrame, error as? NIOWebSocketError)
|
||||
}
|
||||
|
||||
// We expect that an error frame will have been written out.
|
||||
@@ -530,13 +480,8 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
|
||||
// A fake frame header that claims this is a ping frame with 126 bytes of data.
|
||||
self.buffer.writeBytes([0x89, 0x7E, 0x00, 0x7E])
|
||||
|
||||
do {
|
||||
try self.decoderChannel.writeInbound(self.buffer)
|
||||
XCTFail("did not throw")
|
||||
} catch NIOWebSocketError.multiByteControlFrameLength {
|
||||
// OK
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
|
||||
XCTAssertEqual(.multiByteControlFrameLength, error as? NIOWebSocketError)
|
||||
}
|
||||
|
||||
// We expect that an error frame will have been written out.
|
||||
@@ -555,13 +500,8 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
|
||||
// A fake frame header that claims this is a fragmented ping frame.
|
||||
self.buffer.writeBytes([0x09, 0x00])
|
||||
|
||||
do {
|
||||
try self.decoderChannel.writeInbound(self.buffer)
|
||||
XCTFail("did not throw")
|
||||
} catch NIOWebSocketError.fragmentedControlFrame {
|
||||
// OK
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
|
||||
XCTAssertEqual(.fragmentedControlFrame, error as? NIOWebSocketError)
|
||||
}
|
||||
|
||||
// We expect that an error frame will have been written out.
|
||||
@@ -606,13 +546,8 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
|
||||
XCTAssertNotNil(error as? Dummy, "unexpected error: \(error)")
|
||||
}
|
||||
|
||||
do {
|
||||
try self.decoderChannel.writeInbound(self.buffer)
|
||||
XCTFail("did not throw")
|
||||
} catch NIOWebSocketError.invalidFrameLength {
|
||||
// OK
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
|
||||
XCTAssertEqual(.invalidFrameLength, error as? NIOWebSocketError)
|
||||
}
|
||||
|
||||
// We expect that an error frame will have been written out.
|
||||
|
||||
Reference in new issue
Block a user