Enable IntelliJ <Click to see difference> feature when result comparison fails#36
Conversation
| Objects.instance().assertNotNull(info, actual); | ||
| if (!actual.isErr() || !actual.unwrapErrOrElseThrow().equals(value)) { | ||
| throw failures.failure(info, ResultShouldBeErrWithValue.shouldBeOkWithValue(actual, value)); | ||
| throw failures.failure(info, ResultShouldBeErrWithValue.shouldBeErrWithValue(actual, value)); |
There was a problem hiding this comment.
shouldBeOkWithValue() is not the right name here, renamed it.
| } | ||
|
|
||
| public static <T, E> ErrorMessageFactory shouldBeOkWithValue(Result<T, E> actual, Object value) { | ||
| public static <T, E> ErrorMessageFactory shouldBeErrWithValue(Result<T, E> actual, Object error) { |
There was a problem hiding this comment.
as said above, we're comparing that a result is equal to the expected Error result
| if (actual.isErr()) { | ||
| return new ResultShouldBeErrWithValue("Expecting Result to be Err containing <%s> but contained <%s>", value, actual.unwrapErrOrElseThrow()); | ||
| E actualError = actual.unwrapErrOrElseThrow(); | ||
| return new ResultShouldBeErrWithValue("Expecting Result to be Err containing <%s> but contained <%s>. expected:<%s> but was:<%s>", error, actualError, error, actualError); |
There was a problem hiding this comment.
this is all this PR is doing, appending the expected:<%s> but was:<%s> to the error message.
| if (actual.isOk()) { | ||
| return new ResultShouldBeOkWithValue("Expecting Result to be Ok containing <%s> but contained <%s>", value, actual.unwrapOrElseThrow()); | ||
| T actualValue = actual.unwrapOrElseThrow(); | ||
| return new ResultShouldBeOkWithValue("Expecting Result to be Ok containing <%s> but contained <%s>. expected:<%s> but was:<%s>", value, actualValue, value, actualValue); |
There was a problem hiding this comment.
this is all this PR is doing, appending the expected:<%s> but was:<%s> to the error message.
| T actualValue = actual.unwrapOrElseThrow(); | ||
| return new ResultShouldBeOkWithValue("Expecting Result to be Ok containing <%s> but contained <%s>. expected:<%s> but was:<%s>", value, actualValue, value, actualValue); | ||
| } else { | ||
| return new ResultShouldBeOkWithValue("Expecting Result to be Ok containing <%s> but was Err containing <%s>", value, actual.unwrapErrOrElseThrow()); |
There was a problem hiding this comment.
No need to do the same for this case imo. If the actual Result is err while we're expecting an ok result, the two objects to compare are two different things conceptually.
| return new ResultShouldBeErrWithValue("Expecting Result to be Err containing <%s> but contained <%s>. expected:<%s> but was:<%s>", error, actualError, error, actualError); | ||
| } else { | ||
| return new ResultShouldBeErrWithValue("Expecting Result to be Err containing <%s> but was Ok containing <%s>", value, actual.unwrapOrElseThrow()); | ||
| return new ResultShouldBeErrWithValue("Expecting Result to be Err containing <%s> but was Ok containing <%s>", error, actual.unwrapOrElseThrow()); |
There was a problem hiding this comment.
No need to do the same for this case imo. If the actual Result is ok while we're expecting an err result, the two objects to compare are two different things conceptually.
|
🚢 |
This PR enables the IntelliJ when comparing two Results. It doesn't change the comparison logic, but just the assertion message that is returned.
By appending
expected:<%s> but was:<%s>initelliJ recognizes there was an error comparing two different values and shows the very handy<Click to see difference>button in the error message.Before:

After:
