Swift XCTestCase fails to print Printable enums
| Originator: | jlieske | ||
| Number: | rdar://17415068 | Date Originated: | 22-Jun-2014 11:23 PM |
| Status: | Closed | Resolved: | Fixed in Xcode 6 Beta 5 |
| Product: | Developer Tools | Product Version: | Xcode6 beta: Version 6.0 (6A216f) |
| Classification: | Other Bug | Reproducible: | Always |
Enums can implement the Equatable and Printable protocols. An example follows:
enum SimpleToken {
case Name(String)
case Number(Int)
}
extension SimpleToken : Printable {
var description: String { get {
switch self {
case Name(let value): return "Name(\(value))"
case Number(let value): return "Number(\(value))"
}
}}
}
extension SimpleToken : Equatable {
}
func ==(lhs: SimpleToken, rhs: SimpleToken) -> Bool {
switch lhs {
case .Name(let lval): switch rhs {
case .Name(let rval): return lval == rval
default: return false
}
case .Number(let lval): switch rhs {
case .Number(let rval): return lval == rval
default: return false
}
}
}
When such enums are used in XCTest assertions, the error reports should use the printable value of the enum. At present, however, XCTest assertions do not access the description property of the printable value.
At present, Swift does not automatically implement an equality operator for enums, even when the associated values are all supported by the equality operator. For example, the code:
class TryPrintableEnum: XCTestCase {
func testExample() {
XCTAssertEqual(SimpleToken.Number(1), SimpleToken.Name("Hello"))
}
}
This test is expected to fail, and it should display the descriptions of the two values. However, the XCTest report displays the values as “(Enum Value)”. The full report is the following:
fails with the error:
Test Case '-[_TtC16TryPrintableEnum16TryPrintableEnum testExample]' started.
Test Case '-[TryPrintableEnum testExample()]' started.
/Users/jay/Coding/Swift/TrySwift/TryPrintableEnum/TryPrintableEnum.swift:44: error: -[_TtC16TryPrintableEnum16TryPrintableEnum testExample] : ((expressionStr1) equal to (expressionStr2)) failed: ("(Enum Value)") is not equal to ("(Enum Value)") -
/Users/jay/Coding/Swift/TrySwift/TryPrintableEnum/TryPrintableEnum.swift:44: error: -[TryPrintableEnum testExample()] : ((expressionStr1) equal to (expressionStr2)) failed: ("(Enum Value)") is not equal to ("(Enum Value)") -
Test Case '-[_TtC16TryPrintableEnum16TryPrintableEnum testExample]' failed (0.011 seconds).
Test Case '-[TryPrintableEnum testExample()]' failed (0.011 seconds).
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!