-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. Updating dependency versions
- Loading branch information
Showing
17 changed files
with
317 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ tmp.* | |
# Go | ||
bin/ | ||
junit-report.xml | ||
coverage.html | ||
.coverage.out | ||
.coverage-*.out | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package matchers | ||
package errors | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrActualNotBeNil = errors.New("actual must not be nil") | ||
ErrNotBeNil = errors.New("must not be nil") | ||
ErrFailedConvertStringToDecimal = errors.New("DecimalBeEquivalentToMatcher failed to convert string to Decimal") | ||
ErrNotSupportedValue = errors.New("DecimalBeEquivalentToMatcher matcher expects a Decimal or string") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package converter | ||
|
||
import ( | ||
"errors" | ||
|
||
gdecimalErrors "github.com/itbasis/gdecimal/errors" | ||
"github.com/shopspring/decimal" | ||
) | ||
|
||
func ToDecimal(value interface{}) (decimal.Decimal, error) { | ||
if value == nil { | ||
return decimal.Decimal{}, gdecimalErrors.ErrNotBeNil | ||
} | ||
|
||
if d, ok := value.(decimal.Decimal); ok { | ||
return d, nil | ||
} | ||
|
||
if s, ok := value.(string); ok { | ||
d, err := decimal.NewFromString(s) //nolint:varnamelen | ||
|
||
if err != nil { | ||
return d, errors.Join(err, gdecimalErrors.ErrFailedConvertStringToDecimal) | ||
} | ||
|
||
return d, nil | ||
} | ||
|
||
return decimal.Decimal{}, gdecimalErrors.ErrNotSupportedValue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package converter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" //nolint:revive | ||
. "github.com/onsi/gomega" //nolint:revive | ||
) | ||
|
||
func TestConverter(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
|
||
RunSpecs(t, "Converter Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package converter_test | ||
|
||
import ( | ||
gdecimalErrors "github.com/itbasis/gdecimal/errors" | ||
"github.com/itbasis/gdecimal/internal/converter" | ||
converterTestData "github.com/itbasis/gdecimal/internal/converter/test-data" | ||
. "github.com/onsi/ginkgo/v2" //nolint:revive | ||
. "github.com/onsi/gomega" //nolint:revive | ||
"github.com/shopspring/decimal" | ||
) | ||
|
||
var _ = Describe( | ||
"asserting on nil", func() { | ||
Ω(converter.ToDecimal(nil)).Error().To(MatchError(gdecimalErrors.ErrNotBeNil)) | ||
}, | ||
) | ||
|
||
var _ = Describe( | ||
"asserting on Decimal", func() { | ||
Ω(converter.ToDecimal(decimal.Decimal{})).To(Equal(decimal.Decimal{})) | ||
}, | ||
) | ||
|
||
var _ = Describe( | ||
"asserting on string", func() { | ||
DescribeTable( | ||
"passed", func(value string, expect decimal.Decimal) { | ||
Ω(converter.ToDecimal(value)).To(Equal(expect)) | ||
}, | ||
converterTestData.AssertingOnStringPassedEntries, | ||
) | ||
|
||
DescribeTable( | ||
"failure", func(value string) { | ||
Ω(converter.ToDecimal(value)).Error().To(MatchError(gdecimalErrors.ErrFailedConvertStringToDecimal)) | ||
}, | ||
converterTestData.AssertingOnStringFailureEntries, | ||
) | ||
}, | ||
) | ||
|
||
var _ = DescribeTable( | ||
"asserting on another", func(value interface{}) { | ||
Ω(converter.ToDecimal(value)).Error().To(MatchError(gdecimalErrors.ErrNotSupportedValue)) | ||
}, | ||
converterTestData.AssertingOnAnotherEntries, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package testdata | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" //nolint:revive | ||
"github.com/shopspring/decimal" | ||
) | ||
|
||
var AssertingOnStringPassedEntries = []TableEntry{ | ||
Entry(nil, "1", decimal.NewFromInt(1)), | ||
Entry(nil, "-1", decimal.NewFromInt(-1)), | ||
Entry(nil, "1.0", decimal.New(10, -1)), | ||
Entry(nil, "01", decimal.NewFromInt(1)), | ||
} | ||
var AssertingOnStringFailureEntries = []TableEntry{ | ||
Entry(nil, ""), | ||
Entry(nil, " 2"), | ||
Entry(nil, "2 "), | ||
Entry(nil, "2,0"), | ||
} | ||
var AssertingOnAnotherEntries = []TableEntry{ | ||
Entry(nil, 1.0), | ||
Entry(nil, -1.0), | ||
} |
26 changes: 9 additions & 17 deletions
26
matchers/be_equivalent_to_matcher.go → ...rnal/matchers/be_equivalent_to_matcher.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package matchers_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
gdecimalErrors "github.com/itbasis/gdecimal/errors" | ||
converterTestData "github.com/itbasis/gdecimal/internal/converter/test-data" | ||
"github.com/itbasis/gdecimal/internal/matchers" | ||
. "github.com/onsi/ginkgo/v2" //nolint:revive | ||
. "github.com/onsi/gomega" //nolint:revive | ||
"github.com/shopspring/decimal" | ||
) | ||
|
||
var _ = Describe( | ||
"asserting on nil", func() { | ||
success, err := (&matchers.BeEquivalentToMatcher{Expected: decimal.NewFromInt(1)}).Match(nil) | ||
Ω(success).Should(BeFalse()) | ||
Ω(err).Should(HaveOccurred()) | ||
}, | ||
) | ||
|
||
var _ = DescribeTable( | ||
"passed matchers", func(expected decimal.Decimal, actual interface{}) { | ||
Ω((&matchers.BeEquivalentToMatcher{Expected: expected}).Match(actual)).Should(BeTrue()) | ||
}, | ||
Entry(nil, decimal.NewFromInt(1), decimal.NewFromInt(1)), | ||
Entry(nil, decimal.New(1, 1), decimal.New(1, 1)), | ||
Entry(nil, decimal.New(1, 0), decimal.RequireFromString("1")), | ||
Entry(nil, decimal.New(1, 1), decimal.RequireFromString("10")), | ||
Entry(nil, decimal.New(11, -1), decimal.RequireFromString("1.1")), | ||
Entry(nil, decimal.NewFromInt(1), "1"), | ||
Entry(nil, decimal.New(1, 1), "10"), | ||
Entry(nil, decimal.New(11, 1), "110"), | ||
Entry(nil, decimal.New(11, -1), "1.1"), | ||
) | ||
|
||
var _ = DescribeTable( | ||
"passed matchers (Not)", func(expected decimal.Decimal, actual interface{}) { | ||
Ω((&matchers.BeEquivalentToMatcher{Expected: expected}).Match(actual)).ShouldNot(BeTrue()) | ||
}, | ||
Entry(nil, decimal.NewFromInt(1), decimal.NewFromInt(2)), | ||
Entry(nil, decimal.New(1, 1), decimal.New(1, 2)), | ||
Entry(nil, decimal.New(1, 1), decimal.New(2, 1)), | ||
Entry(nil, decimal.NewFromInt(1), "2"), | ||
Entry(nil, decimal.New(1, 1), "1"), | ||
Entry(nil, decimal.New(1, 1), "20"), | ||
Entry(nil, decimal.New(1, 1), "1.1"), | ||
) | ||
|
||
var _ = DescribeTable( | ||
"should use the matcher's failure", | ||
func(expected, actual interface{}, expectedValue, actualValue string) { | ||
matcher := &matchers.BeEquivalentToMatcher{Expected: expected} | ||
Ω(matcher.Match(actual)).Should(BeFalse()) | ||
}, | ||
Entry( | ||
nil, | ||
decimal.NewFromInt(10), | ||
decimal.NewFromInt(1), | ||
"{neg: false, abs: [10]},\n exp: 0,\n }", | ||
"{neg: false, abs: [1]},\n exp: 0,\n }", | ||
), | ||
Entry( | ||
nil, | ||
decimal.New(11, -1), | ||
decimal.NewFromInt(11), | ||
"{neg: false, abs: [11]},\n exp: -1,\n }", | ||
"{neg: false, abs: [11]},\n exp: 0,\n }", | ||
), | ||
) | ||
|
||
var _ = DescribeTable( | ||
"should use the matcher's failure messages", | ||
func(expected, actual interface{}, expectedValue string) { | ||
matcher := &matchers.BeEquivalentToMatcher{Expected: expected} | ||
Ω(matcher.FailureMessage(actual)).Should( | ||
Equal( | ||
fmt.Sprintf( | ||
"Expected\n <string>: %s\nto be equivalent to\n <decimal.Decimal>: {\n value: %s", | ||
actual, | ||
expectedValue, | ||
), | ||
), | ||
) | ||
|
||
Ω(matcher.NegatedFailureMessage(actual)).Should( | ||
Equal( | ||
fmt.Sprintf( | ||
"Expected\n <string>: %s\nnot to be equivalent to\n <decimal.Decimal>: {\n value: %s", | ||
actual, | ||
expectedValue, | ||
), | ||
), | ||
) | ||
}, | ||
Entry( | ||
nil, | ||
decimal.NewFromInt(10), | ||
"1", | ||
"{neg: false, abs: [10]},\n exp: 0,\n }", | ||
), | ||
Entry( | ||
nil, | ||
decimal.New(11, -1), | ||
"11", | ||
"{neg: false, abs: [11]},\n exp: -1,\n }", | ||
), | ||
) | ||
|
||
var _ = Describe( | ||
"asserting failed convert", func() { | ||
DescribeTable( | ||
"actual on failure string", func(value string) { | ||
success, err := (&matchers.BeEquivalentToMatcher{Expected: decimal.Decimal{}}).Match(value) | ||
Ω(success).To(BeFalse()) | ||
Ω(err).Should(MatchError(gdecimalErrors.ErrFailedConvertStringToDecimal)) | ||
}, | ||
converterTestData.AssertingOnStringFailureEntries, | ||
) | ||
DescribeTable( | ||
"expected on failure string", func(value string) { | ||
success, err := (&matchers.BeEquivalentToMatcher{Expected: value}).Match(decimal.Decimal{}) | ||
Ω(success).To(BeFalse()) | ||
Ω(err).Should(MatchError(gdecimalErrors.ErrFailedConvertStringToDecimal)) | ||
}, | ||
converterTestData.AssertingOnStringFailureEntries, | ||
) | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
package gdecimal | ||
|
||
import ( | ||
"github.com/itbasis/gdecimal/matchers" | ||
"github.com/shopspring/decimal" | ||
"github.com/itbasis/gdecimal/internal/matchers" | ||
) | ||
|
||
func BeDecimalEquivalentTo(expect decimal.Decimal) *matchers.BeEquivalentToMatcher { | ||
func BeDecimalEquivalentTo(expect interface{}) *matchers.BeEquivalentToMatcher { | ||
return &matchers.BeEquivalentToMatcher{Expected: expect} | ||
} |
Oops, something went wrong.