Swift's advanced type-checking has ushered with it an entire coding practice of maximizing compile-time type-checking.
A lot has been said about traditional unit-testing vs. type-safety in code. I find myself on the type-safety side of the equation, i.e., I've been trying to make my code raise as many compile-time errors as possible when used incorrectly.
However I'm wondering is if there's a good way to unit-test type-safety itself.
Of course, type-safety errors would be exhibited as compile-time errors. Therefore, what this ultimately boils down to is unit-testing compile-time errors.
There is already an answer for achieving this with make in C++: Unit test compile-time error
Ideally, I would be able to do the following:
struct Username: Stringish{ ... }
struct SQLQuery: Stringish{ ... }
...
XCTAssertUncompilable{
func foo(a: Username, b: SQLQuery) {
...
if (a == b) { ... } // Error: cannot compare type Username to type SQLQuery
}
XCTAssertUncompilable{
let user1 = Username("Joe")
let selectAllUsers = SQLQuery("SELECT * FROM USERS")
user1 == selectAllUsers // Error: cannot compare type Username to type SQLQuery
}
This is probably a bit more than can be hacked together without some serious support Xcode-side.
So the next best thing would be to have something similar to the solution presented in the link from before, i.e. some separate script that can test whether or not specific files can compile. Of course, it would have to be able to import the proper frameworks/packages.
I'd be happy with some idea of how to make such a script for iOS-Swift without too much of a hassle.
Bonus points if it can all be automatically called during the usual testing phase of Xcode. Extra bonus points if the "real" testing only passes if this external script also passes.