Provide XCTAssertEqual method that takes one optional and one non-optional
| Originator: | alex | ||
| Number: | rdar://19983627 | Date Originated: | 27-Feb-2015 01:47 PM |
| Status: | Duplicate/17541074 | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode 6.1.1 (6A2008a) and Xcode 6.3 (6D532l) |
| Classification: | Enhancement | Reproducible: | Always |
Summary:
When unit testing code you often want to test whether an optional is not nil and has a specific value. Currently the only option is to use code like the following
// Given an optional a
if let a = a {
XCTAssertEqual(a.value, 3, "Value should be 3")
} else {
XCTFail("A should not be nil")
}
Steps to Reproduce:
Have a look at the attached sample project
Expected Results:
There should be an easy method to test that the object is not nil and performs the test if this is the case. An implementation could look like the following:
func XCTAssertEqual<T: Equatable>(expression1: @autoclosure () -> T?, expression2: @autoclosure () -> T, message: String) {
let evaluated1 = expression1()
if let evaluated1 = evaluated1 {
XCTAssertEqual(evaluated1, expression2, message)
} else {
XCTAssertNotNil(evaluated1, message)
}
}
Actual Results:
A method like the one described above has to be created by hand which shows the failure messages at the wrong line of code or the cumbersome if-else construct has to be used
Version:
Xcode 6.1.1 (6A2008a) and Xcode 6.3 (6D532l)
Notes:
Configuration:
Attachments:
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!