Skip to content

Commit

Permalink
1. Refactoring
Browse files Browse the repository at this point in the history
2. Updating dependency versions
  • Loading branch information
BorzdeG committed Nov 13, 2023
1 parent 06042e7 commit d8dffcd
Show file tree
Hide file tree
Showing 17 changed files with 317 additions and 165 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ tmp.*
# Go
bin/
junit-report.xml
coverage.html
.coverage.out
.coverage-*.out

Expand Down
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ linters:
# - interfacebloat

linters-settings:
gomnd:
ignored-files:
- 'internal/converter/test-data/data\.go$'
varnamelen:
ignore-type-assert-ok: true
ignore-map-index-ok: true
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ go-lint: go-dependencies
ginkgolinter ./...
go vet -vettool=$$(go env GOPATH)/bin/shadow ./...

#go-test: go-dependencies go-lint
go-test: go-dependencies
go-test: go-dependencies go-lint
gosec ./...
ginkgo -r -race --cover --coverprofile=.coverage-ginkgo.out --junit-report=junit-report.xml ./...
go tool cover -func=.coverage-ginkgo.out -o=.coverage.out
go tool cover -html=.coverage-ginkgo.out -o=coverage.html
cat .coverage.out

go-all-tests: go-dependencies go-generate go-lint go-test
Expand Down
4 changes: 2 additions & 2 deletions matchers/errors.go → errors/errors.go
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")
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/itbasis/gdecimal
go 1.20

require (
github.com/onsi/ginkgo/v2 v2.13.0
github.com/onsi/ginkgo/v2 v2.13.1
github.com/onsi/gomega v1.30.0
github.com/shopspring/decimal v1.3.1
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a h1:fEBsGL/sjAuJrgah5XqmmYsTLzJp/TO9Lhy39gkverk=
github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
github.com/onsi/ginkgo/v2 v2.13.1 h1:LNGfMbR2OVGBfXjvRZIZ2YCTQdGKtPLvuI1rMCCj3OU=
github.com/onsi/ginkgo/v2 v2.13.1/go.mod h1:XStQ8QcGwLyF4HdfcZB8SFOS/MWCgDuXMSBe6zrvLgM=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
30 changes: 30 additions & 0 deletions internal/converter/converter.go
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
}
14 changes: 14 additions & 0 deletions internal/converter/converter_suite_test.go
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")
}
47 changes: 47 additions & 0 deletions internal/converter/converter_test.go
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,
)
23 changes: 23 additions & 0 deletions internal/converter/test-data/data.go
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),
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
package matchers

import (
"github.com/itbasis/gdecimal/internal/converter"
"github.com/onsi/gomega/format"

"github.com/shopspring/decimal"
)

type BeEquivalentToMatcher struct {
Expected decimal.Decimal
Expected interface{}
}

func (matcher *BeEquivalentToMatcher) Match(actual interface{}) (success bool, err error) {
if actual == nil {
return false, ErrActualNotBeNil
}

if actualDecimal, ok := actual.(decimal.Decimal); ok {
return actualDecimal.Equal(matcher.Expected), nil
actualDecimal, errActual := converter.ToDecimal(actual)
if errActual != nil {
return false, errActual //nolint:wrapcheck
}

if s, ok := actual.(string); ok {
actualDecimal, err := decimal.NewFromString(s)
if err != nil {
return false, ErrFailedConvertStringToDecimal
}

return actualDecimal.Equal(matcher.Expected), nil
expectedDecimal, errExpected := converter.ToDecimal(matcher.Expected)
if errExpected != nil {
return false, errExpected //nolint:wrapcheck
}

return false, ErrNotSupportedValue
return actualDecimal.Equal(expectedDecimal), nil
}

func (matcher *BeEquivalentToMatcher) FailureMessage(actual interface{}) (message string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import (
func TestDecimalBeEquivalentToMatcher(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecs(t, "DecimalBeEquivalentToMatcher Suite")
RunSpecs(t, "Matchers Suite")
}
129 changes: 129 additions & 0 deletions internal/matchers/be_equivalent_to_matcher_test.go
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,
)
},
)
5 changes: 2 additions & 3 deletions matchers.go
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}
}
Loading

0 comments on commit d8dffcd

Please sign in to comment.