Skip to content
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

Add hasPrecisionOf(int) assertion for BigDecimal #3174

Closed
wants to merge 1 commit into from
Closed

Add hasPrecisionOf(int) assertion for BigDecimal #3174

wants to merge 1 commit into from

Conversation

armandino
Copy link
Contributor

Check List:

AssertJ provides a hasScaleOf() assertion for the BigDecimal type. This PR adds a companion assertion hasPrecisionOf for verifying the BigDecimal.precision() method. I followed the hasScaleOf() implementation and tests in this PR.

@armandino armandino changed the title Add hasPrecisionOf(int) assertion for BigDecimal Add hasPrecisionOf(int) assertion for BigDecimal Sep 14, 2023
Copy link
Member

@scordio scordio left a comment

Choose a reason for hiding this comment

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

Thanks @armandino! My comments are mostly due to the hasScale() / scale() patterns you probably followed. We would prefer to follow a different style for new assertions.

We might also improve hasScale() / scale() but this can be taken up in a separate issue or PR.

*/
package org.assertj.core.api;

public class BigDecimalPrecisionAssert<T> extends AbstractBigDecimalPrecisionAssert<BigDecimalAssert> {
Copy link
Member

Choose a reason for hiding this comment

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

T seems to be unused, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried removing it, but looks like it is required here:

  public AbstractBigDecimalPrecisionAssert<SELF> precision() {
    isNotNull();
    return new BigDecimalPrecisionAssert(myself);
  }

@SuppressWarnings({ "unchecked", "rawtypes" })
public AbstractBigDecimalPrecisionAssert<SELF> precision() {
requireNonNull(actual, "Can not perform assertions on the precision of a null BigDecimal");
return new BigDecimalPrecisionAssert(myself);
Copy link
Member

Choose a reason for hiding this comment

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

We briefly discussed this approach with the team.

We should add a bit of context to the error messages by showing the root object we navigated from.

For example, assuming:

assertThat(myBigDecimal).scale().isGreaterThan(5L)

If the assertion fails, it will show something like

expecting 2 to be greater than 5 but was not

where the fact that a BigDecimal precision was checked is forgotten.

Instead, the error message should have a better description and be more like:

[checking 2.123 precision] expecting 2 to be greater than 5 but was not

Unfortunately, we don't do this consistently in AssertJ so I didn't manage to find a good example quickly. I'll try to have another look in the next few days.

@armandino
Copy link
Contributor Author

@scordio thanks for the feedback! It was very helpful. I addressed most of the comments, but there are still a few outstanding items.

@armandino
Copy link
Contributor Author

Hi @scordio, I updated the PR. Please let me know if I missed anything.

@armandino armandino closed this by deleting the head repository Dec 27, 2023
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.

None yet

2 participants