Commit c0e4f59
authored
Make value capture of optionals more robust. (#277)
When an expression is optional and is passed through `#require()`, and
that requirement fails, we capture the expression's subexpressions and
record them as an expectation-failed event that is eventually propagated
to `stderr`. Yay.
The underlying value-capturing machinery accepts optional values as
input so that they can be lazily evaluated: if a value is `nil` at this
layer, that means that it wasn't evaluated and we present `"<not
evaluated>"` instead of a description of it or `"nil"`.
In the case of `#require()` unwrapping optionals, these values need to
be boxed in a second optional (i.e. `T??` instead of `T?`, or
`Optional<Optional<T>>` instead of `Optional<T>`) so that if the value
is `nil`, it isn't confused with "no value". See the following table:
| Expression | Value of `x as String??` | Presented As |
|-|-|-|
| `try #require(x)` | `.some(.some("Hello World"))` | `"Hello World"` |
| `try #require(x)` | `.some(.none)` | `nil` |
| `try #require("Goodbye Venus" ?? x)` | `.none` | `"<not evaluated>"` |
(This maybe suggests we should use a different enum than `Optional`
here, but that's a debate for another PR I think…)
This PR fixes a couple of spots where we weren't casting our values,
known to be optionals, to double-optionals, resulting in incorrect
output that incorrectly included `"<not evaluated>"`.
### Checklist:
- [x] Code and documentation should follow the style of the [Style
Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md).
- [x] If public symbols are renamed or modified, DocC references should
be updated.1 parent 3a77507 commit c0e4f59
File tree
2 files changed
+24
-2
lines changed- Sources/Testing/Expectations
- Tests/TestingTests
2 files changed
+24
-2
lines changedLines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
557 | 557 | | |
558 | 558 | | |
559 | 559 | | |
560 | | - | |
| 560 | + | |
561 | 561 | | |
562 | 562 | | |
563 | 563 | | |
| |||
731 | 731 | | |
732 | 732 | | |
733 | 733 | | |
734 | | - | |
| 734 | + | |
735 | 735 | | |
736 | 736 | | |
737 | 737 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1161 | 1161 | | |
1162 | 1162 | | |
1163 | 1163 | | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
1164 | 1186 | | |
1165 | 1187 | | |
1166 | 1188 | | |
| |||
0 commit comments