-
Notifications
You must be signed in to change notification settings - Fork 126
Update Comment to use CustomTestStringConvertible during string interpolation.
#936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Comment to use CustomTestStringConvertible during string interpolation.
#936
Conversation
…nterpolation.
This PR ensures that `Comment`, when constructed from a string literal with
interpolations, stringifies interpolated values using
`String.init(descriptionForTest:)` rather than the default behaviour (i.e.
`String.init(description:)`.)
For example:
```swift
enum E: CustomTestStringConvertible {
case a
case b
case c
var testDescription: String {
switch self {
case .a:
"Once I was the King of Spain"
case .b:
"Now I eat humble pie"
case .c:
"Now I vacuum the turf at SkyDome™"
}
}
}
#expect(1 == 2, "\(E.a) / \(E.b) / \(E.a) / \(E.c)")
```
Before:
> 🛑 Expectation failed: 1 == 2 - .a / .b / .a / .c
After:
> 🛑 Expectation failed: 1 == 2 - Once I was the King of Spain / Now I eat
> humble pie / Once I was the King of Spain / Now I vacuum the turf at SkyDome™
|
@swift-ci test |
Sources/Testing/Traits/Comment.swift
Outdated
| // This overload is provided so that the compiler does not emit warnings | ||
| // about optional values in string interpolations (which we are fine with | ||
| // when constructing Comment instances.) | ||
| rawValue += String(describingForTest: value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this, might be too magical and surprising compared to how string literal interpolation usually works. Is there a strong reason to include it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My expectation here (heh) is that if you pass an optional value in a testing context, you want to see its value. Also the warning's fix-it is emitted by the compiler and tells you to use String(describing:), not String(describingForTest:), which is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't say it's wrong for the compiler to suggest that; users are welcome to call String(describing:) themselves. I think String(describingForTest:) is primarily for the testing library to call on values users provide, and (to a lesser extent) for use by test authors when writing helpers/utilities.
I think this violates the principle of least surprise. Regardless of what we think of the compiler's fix-it, users are generally accustomed to needing to handle optionals in string interpolations, and I don't know of a reason to deviate from that precedent here. It makes it seem as if the rules are more lax just because it's a test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The compiler emits that diagnostic because the default string representation of an optional value may either be "Optional(x)" (where x is the wrapped value's description) or "nil", and neither of those values is likely expected by a developer who wants to just print x. In our case, that doesn't hold because optional values already have special handling in the library.
I would expect the following code to "just work" rather than emitting any diagnostics, for instance:
let x: Int? = f()
#expect(x == 10, "\(x) should be 10!")
stmontgomery
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great enhancement overall. I'm still not convinced about the merit of the overload which suppresses warnings for optionals, and worry it could be surprising or confusing. But it's admittedly a minor concern and I don't want to stand in the way of the broader PR.
… unexpected quote marks
|
@swift-ci test |
This PR ensures that
Comment, when constructed from a string literal with interpolations, stringifies interpolated values usingString.init(descriptionForTest:)rather than the default behaviour (i.e.String.init(description:).)For example:
Before:
After:
Checklist: