Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
19 changes: 19 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down