You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[gardening] Migrate from unwrapped() to XCTUnwrap()
Instead of using an extension on Optional only available in Foundation,
we should start using the recently introduced XCTUnwrap available in
XCTest to perform the same job. The XCTest API would probably be more
known in other code bases, so people will be more willing to use it and
learn it.
The commit removes all the usages of unwrapped and replaces them with
XCTUnwrap. Additionally it marks unwrapped() as deprecated, so people
receive a clear message about the disappearance with a helpful hint of
what to use instead.
Copy file name to clipboardexpand all lines: Docs/Testing.md
+4-4
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ swift-corelibs-foundation uses XCTest for its own test suite. This document expl
7
7
### In brief
8
8
9
9
* Tests should fail rather than crashing; swift-corelibs-xctest does not implement any crash recovery
10
-
* You should avoid forced optional unwrapping (e.g.: `aValue!`). Use `try aValue.unwrapped()` instead
10
+
* You should avoid forced optional unwrapping (e.g.: `aValue!`). Use `try XCTUnwrap(aValue)` instead
11
11
* You can test code that is expected to crash; you must mark the whole body of the test method with `assertCrashes(within:)`
12
12
* If a test or a portion of a test is giving the build trouble, use `testExpectedToFail` and write a bug
13
13
@@ -19,7 +19,7 @@ Due to this, it is important to avoid crashing in test code, and to properly han
19
19
20
20
#### Avoiding Forced Unwrapping
21
21
22
-
Forced unwrapping is easily the easiest way to crash the test process, and should be avoided. We have an ergonomic replacement in the form of the `.unwrapped()`extension method on the `Optional` type.
22
+
Forced unwrapping is easily the easiest way to crash the test process, and should be avoided. XCTest have an ergonomic replacement in the form of the `XCTUnwrap()`function.
23
23
24
24
The following code is a liability and code review should flag it:
1. Change the test method to throw errors by adding the `throws` clause. Tests that throw errors will fail and stop the first time an error is thrown, so plan accordingly, but a thrown error will not stop the test run, merely fail this test.
37
-
2. Change the forced unwrapping to `try ….unwrapped()`.
37
+
2. Change the forced unwrapping to `try XCTUnwrap(…)`.
38
38
39
39
For example, the code above can be fixed as follows:
0 commit comments