From 0324e2e98daa15ae54288df0ce506d9fc692c0fa Mon Sep 17 00:00:00 2001 From: Joe Heck Date: Wed, 11 Dec 2024 10:44:30 -0800 Subject: [PATCH 1/2] Adds example for the scenario where you don't care (or know) about _what_ error is thrown --- .../testing-for-errors-in-swift-code.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Sources/Testing/Testing.docc/testing-for-errors-in-swift-code.md b/Sources/Testing/Testing.docc/testing-for-errors-in-swift-code.md index 5113202d0..899fd46fb 100644 --- a/Sources/Testing/Testing.docc/testing-for-errors-in-swift-code.md +++ b/Sources/Testing/Testing.docc/testing-for-errors-in-swift-code.md @@ -44,6 +44,22 @@ test that the code throws an error of a given type, or matches an arbitrary Boolean test. Similar overloads of ``require(_:_:sourceLocation:)-5l63q`` stop running your test if the code doesn't throw the expected error. +### Validate that your code throws any error + +To check that the code under test throws any error, or to continue a +longer test function after the code throws an error, pass that `(any Error).self` as the +first argument of ``expect(throws:_:sourceLocation:performing:)-1xr34``, and +pass a closure that calls the code under test: + +```swift +@Test func cannotAddToppingToPizzaBeforeStartOfList() { + var order = PizzaToppings(bases: [.calzone, .deepCrust]) + #expect(throws: (any Error).self) { + try order.add(topping: .mozarella, toPizzasIn: -1..<0) + } +} +``` + ### Validate that your code doesn't throw an error A test function that throws an error fails, which is usually sufficient for From 84b9d88679d9b907e255f51dbe443ff37380dc41 Mon Sep 17 00:00:00 2001 From: Joseph Heck Date: Wed, 11 Dec 2024 23:49:34 -0800 Subject: [PATCH 2/2] Update Sources/Testing/Testing.docc/testing-for-errors-in-swift-code.md Co-authored-by: Stuart Montgomery --- .../Testing.docc/testing-for-errors-in-swift-code.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/Testing/Testing.docc/testing-for-errors-in-swift-code.md b/Sources/Testing/Testing.docc/testing-for-errors-in-swift-code.md index 899fd46fb..244b7bd98 100644 --- a/Sources/Testing/Testing.docc/testing-for-errors-in-swift-code.md +++ b/Sources/Testing/Testing.docc/testing-for-errors-in-swift-code.md @@ -46,10 +46,10 @@ running your test if the code doesn't throw the expected error. ### Validate that your code throws any error -To check that the code under test throws any error, or to continue a -longer test function after the code throws an error, pass that `(any Error).self` as the -first argument of ``expect(throws:_:sourceLocation:performing:)-1xr34``, and -pass a closure that calls the code under test: +To check that the code under test throws an error of any type, pass +`(any Error).self` as the first argument to either +``expect(throws:_:sourceLocation:performing:)-1xr34`` or +``require(_:_:sourceLocation:)-5l63q``: ```swift @Test func cannotAddToppingToPizzaBeforeStartOfList() {