Skip to content

Enable IntelliJ <Click to see difference> feature when result comparison fails#36

Merged
gmessinese merged 1 commit intoHubSpot:masterfrom
gmessinese:enable-intellij-click-to-see-difference-error-feature
Jan 10, 2024
Merged

Enable IntelliJ <Click to see difference> feature when result comparison fails#36
gmessinese merged 1 commit intoHubSpot:masterfrom
gmessinese:enable-intellij-click-to-see-difference-error-feature

Conversation

@gmessinese
Copy link
Contributor

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:
Screenshot 2023-11-03 at 10 58 01 AM

After:
Screenshot 2023-11-03 at 10 52 18 AM

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));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mcobrien
Copy link

🚢

@gmessinese gmessinese merged commit bb2e8dd into HubSpot:master Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants