From ad90328fe74654782981a46312288d2d9d0320db Mon Sep 17 00:00:00 2001 From: Daniel Hilton Date: Wed, 16 Jul 2025 13:58:48 +0100 Subject: [PATCH 1/2] chore: Add in gomock `Matches` receiver func --- decimal.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/decimal.go b/decimal.go index b48cfa9..c062b51 100644 --- a/decimal.go +++ b/decimal.go @@ -2333,3 +2333,14 @@ func (d Decimal) Tan() Decimal { } return y } + +// Matches is a gomock match helper. +// It allows for equal numbers with different internal representations to match successfully in tests. +func (d Decimal) Matches(other interface{}) bool { + otherDecimal, ok := other.(Decimal) + if !ok { + return false + } + + return d.Equal(otherDecimal) +} From 5da0fa3a8e6ff39d8b7eccf349c39a6647a69f8c Mon Sep 17 00:00:00 2001 From: Daniel Hilton Date: Wed, 16 Jul 2025 14:17:36 +0100 Subject: [PATCH 2/2] tested new matches func --- decimal_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/decimal_test.go b/decimal_test.go index d288821..768b909 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -3173,6 +3173,25 @@ func TestDecimal_CoefficientInt64(t *testing.T) { } } +func TestDecimal_Matches(t *testing.T) { + type Inp struct { + d1 Decimal + d2 Decimal + } + + testCases := map[Inp]bool{ + {d1: Decimal{value: big.NewInt(100), exp: 0}, d2: Decimal{value: big.NewInt(100), exp: 0}}: true, + {d1: Decimal{value: big.NewInt(100), exp: 0}, d2: Decimal{value: big.NewInt(1), exp: 2}}: true, + {d1: Decimal{value: big.NewInt(1), exp: 0}, d2: Decimal{value: big.NewInt(2), exp: 0}}: false, + } + + for input, matchExpected := range testCases { + if input.d1.Matches(input.d2) != matchExpected { + t.Errorf("expected %s and %s to be %t", input.d1, input.d2, matchExpected) + } + } +} + func TestNullDecimal_Scan(t *testing.T) { // test the Scan method that implements the // sql.Scanner interface