But XCTest doesn't do this natively, so this will have to be in your own code—which won't be covered.
If the team feels strongly about one or the other, I'd be willing to limit my use to one or the other of these functions, if one of them could do it. Right now, it's impossible to do this with any sort of precondition/fatal error mechanism of which I am aware.
Not if the message contains information about why this should be unreachable, which a real-world example would.
For a more fleshed-out example of a code path that should never be reachable, imagine we have a type that wraps several more primitive interfaces, providing a common interface to each. The backing of this may be implemented something like:
public class MyWrapperThingy {
private enum Backing {
case bleep(bleep_t)
case bloop(bloop_t)
}
private let backing: Backing
public init(bleep: bleep_t) {
self.backing = .bleep(bleep)
}
public init(bloop: bloop_t) {
self.backing = .bloop(bloop)
}
public func doSomething() {
switch self.backing {
case .bleep(let bleep):
// do something with bleep
case .bloop(let bloop):
// do something with bloop
}
}
}
Now, suppose we want to add support for a backing type blorp_t that was introduced in a recent version of the OS. We will need to use availability markers:
public class MyWrapperThingy {
private enum Backing {
...
case blorp(Any) // Any because otherwise we will not be able to compile for earlier OS versions
}
...
@available(macOS whatever, *)
public init(blorp: blorp_t) {
self.backing = .blorp(blorp)
}
And then in doSomething:
public func doSomething() {
switch self.backing {
...
case blorp(let blorp):
guard #available(macOS whatever, *) else {
fatalError("Should be impossible to reach this point on old macOS")
}
... do stuff with blorp ...
}
}
}
In the above case, it should be literally impossible to reach that fatalError, but we have to have it to satisfy the compiler. Even if there were a good way to test fatalError, in order to even manipulate things into a state where that path would even be reachable would require polluting the code with artificial spaghetti just to cause what should just be a programmer error.