We are switching to Xcode 10 and are noticing some issues with Xcode 10's code coverage. Entire swaths of code are getting no coverage. So far I have noticed
* Computed vars
* class functions in extensions
* guard statements in extensions
For example:
import UIKit
extension Date {
func dayNumberOfWeek() -> Int? {
return Calendar.current.dateComponents([.weekday], from: self).weekday
}
}
extension UIView {
// Testing with this function compiled will cause no code coverage
// for steveWasHereHelper to be generated.
//
// However, if I comment out steveWasHere, then code coverage for
// steveWasHereHelper is generated, despite that this function is also
// called by wyattWasHere
@discardableResult public class func steveWasHere(value: Int) -> Bool {
return self.steveWasHereHelper(value: value)
}
// The usage of Date here is to try and rule out compiler optimization
@discardableResult public class func steveWasHereHelper(value: Int) -> Bool {
let d = Date().dayNumberOfWeek()
let testValue = value + d!
if testValue >= 42 {
return true
}
return false
}
fileprivate class func neverCalled() {
print("This is never called")
}
// steveWasHereHelper is also called here
@discardableResult public class func wyattWasHere(value: Int) -> Bool {
return self.steveWasHereHelper(value: value)
}
}
In this example, steveWasHereHelper will not get any coverage. It won't be green, it won't be red, its as if the code is not called. If I remove all tests EXCEPT for the direct test of steveWasHereHelper, it still gets no coverage.
HOWEVER, if I then comment out steveWasHere, THEN steveWasHereHelper shows coverage.
I have opened a Radar with Apple on this issue (45468318) but I was wondering if anyone else has seen this (This is not the only example of issues with code coverage in Xcode 10) and if there is a workaround.
I have also tested this in 10.1b3, getting the same results.
self. was added to the function calls just to see if it had any impact.
Narrator: It didn't