A context-aware StandardSubjectBuilder for contextual assertions
- Dominant language
- Java
- Stars
- 2.8k
- Forks
- 275
- Avg merge
- 7m
- Merged PRs (30d)
- 4
Description
Off-the-wall idea, posting here to see what others think:
I'm envisioning using Truth to verify some data processing involving nested structures (think traversing a tree and asserting things about individual nodes). The API might look something like:
```java
try (ContextualTruth contextualAssert =
Truth.withContext("Tree is in state %s", tree.getState()) {
// this, on the other hand, will include the "Tree is in state"
// message in the error if the assertion fails
withContextAssert.that(bar).isEqualTo(expectedBar);
}
```
Once the try-with-resource block ends, the context is removed. Conversely it'd be possible to nest further `.withContext()` calls in order to add additional context.
I'm imagining this would be implemented by decorated `FailureStrategy` instances, managed by the `ContextualTruth` instances.
---
One issue I have with this idea (which makes me lean somewhat away from doing it) is it would make [chained assertions](https://github.com/google/truth/issues/315#issuecomment-303215245) more compelling, since *every* assertion now has the potential to implicitly have additional context. In the abstract I like the idea of this sort of context-injection, but I'm not certain it's worth the costs in terms of complexity. Curious what others think.
Contributor guide
Assessment
This issue has not been assessed yet.