Skip to content

Conversation

DanielHilton
Copy link

@DanielHilton DanielHilton commented Jul 16, 2025

A pain point using this library is during testing, where equal numbers can be represented differently internally to the Decimal struct.

gomock defines a Matcher interface, of which it requires:

  • Matches(other {}interface) bool
  • String() string

String() is already implemented, so i've implemented the Matches function.

Previously to work around this in places, I've been calling String() on the 2 Decimal values and comparing that. This doesn't work for comparing parameters with nested Decimal values using gomock.

e.g.
myGomock.EXPECT().IncrementMyDecimal(ctx, decimal.NewFromInt(100)).Returns(decimal.NewFromInt(101))

This would fail to pass the expected parameter (100) as correct.

This is because case the internal representation expected is abs 100 exp 0, but during the execution of the code it gets a Decimal which is abs 1 exp 2.

To save from writing complex Matches functions on every single struct which includes a Decimal or from stringing them to compare, I've added this.

If there's any feedback, it's welcome in terms of formatting, code cleanliness, linting etc.

I'm also not sure if it should be a receiver against *Decimal or Decimal, so happy to be steered either way.

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.

1 participant