diff --git a/bint.go b/bint.go index 63ba5c7..3d644ae 100644 --- a/bint.go +++ b/bint.go @@ -72,7 +72,7 @@ func (u bint) Cmp(v bint) int { } func errInvalidFormat(s []byte) error { - return fmt.Errorf("%w: can't parse '%s' to Decimal", ErrInvalidFormat, s) + return fmt.Errorf("%w: can't parse '%s'", ErrInvalidFormat, s) } func parseBint(s []byte) (bool, bint, uint8, error) { diff --git a/codec.go b/codec.go index 3b713a8..c3dacc6 100644 --- a/codec.go +++ b/codec.go @@ -306,6 +306,10 @@ func (d Decimal) MarshalText() ([]byte, error) { func (d *Decimal) UnmarshalText(data []byte) error { var err error *d, err = parseBytes(data) + if err != nil { + return fmt.Errorf("error unmarshaling to Decimal: %w", err) + } + return err } diff --git a/codec_test.go b/codec_test.go index 98bd747..f406327 100644 --- a/codec_test.go +++ b/codec_test.go @@ -90,25 +90,30 @@ func TestMarshalText(t *testing.T) { func TestUnmarshalText(t *testing.T) { testcases := []struct { - in string - wantErr error + in string + wantErr string + wantErrType error }{ - {"", ErrEmptyString}, - {" ", ErrInvalidFormat}, - {"abc", ErrInvalidFormat}, - {"1234567890123.1234567890123", nil}, - {"1234567890123.12345678901234567899", ErrPrecOutOfRange}, + {"", "error unmarshaling to Decimal: can't parse empty string", ErrEmptyString}, + {" ", "error unmarshaling to Decimal: invalid format: can't parse ' '", ErrInvalidFormat}, + {"abc", "error unmarshaling to Decimal: invalid format: can't parse 'abc'", ErrInvalidFormat}, + {"1234567890123.12345678901234567899", "error unmarshaling to Decimal: precision out of range. Only support maximum 19 digits after the decimal point", ErrPrecOutOfRange}, + {"1234567890123.1234567890123", "", nil}, } for _, tc := range testcases { t.Run(tc.in, func(t *testing.T) { var d Decimal err := d.UnmarshalText([]byte(tc.in)) - require.ErrorIs(t, err, tc.wantErr) - if tc.wantErr == nil { - require.Equal(t, MustParse(tc.in), d) + if tc.wantErr != "" { + require.EqualError(t, err, tc.wantErr) + require.ErrorIs(t, err, tc.wantErrType) + return } + + require.NoError(t, err) + require.Equal(t, MustParse(tc.in), d) }) } } diff --git a/decimal.go b/decimal.go index c719e1e..0dfb880 100644 --- a/decimal.go +++ b/decimal.go @@ -96,7 +96,7 @@ var ( ErrPrecOutOfRange = fmt.Errorf("precision out of range. Only support maximum %d digits after the decimal point", defaultPrec) // ErrEmptyString is returned when the input string is empty - ErrEmptyString = fmt.Errorf("parse empty string") + ErrEmptyString = fmt.Errorf("can't parse empty string") // ErrMaxStrLen is returned when the input string exceeds the maximum length // Maximum length is arbitrarily set to 200 so string length value can fit in 1 byte (for MarshalBinary). diff --git a/decimal_test.go b/decimal_test.go index 11dde5d..cd9b1b4 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -128,31 +128,31 @@ func TestParse(t *testing.T) { {"340282366920938463463374607431768211459", "340282366920938463463374607431768211459", nil}, {"340282366920938463463374607431768211459.123", "340282366920938463463374607431768211459.123", nil}, {"+340282366920938463463374607431768211459", "340282366920938463463374607431768211459", nil}, - {"340282366920938463463374607431768211459.", "", fmt.Errorf("%w: can't parse '340282366920938463463374607431768211459.' to Decimal", ErrInvalidFormat)}, - {"--340282366920938463463374607431768211459", "", fmt.Errorf("%w: can't parse '--340282366920938463463374607431768211459' to Decimal", ErrInvalidFormat)}, - {".1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '.1234567890123456789012345678901234567890123456' to Decimal", ErrInvalidFormat)}, - {"+.1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '+.1234567890123456789012345678901234567890123456' to Decimal", ErrInvalidFormat)}, - {"-.1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '-.1234567890123456789012345678901234567890123456' to Decimal", ErrInvalidFormat)}, - {"1.12345678901234567890123.45678901234567890123456", "", fmt.Errorf("%w: can't parse '1.12345678901234567890123.45678901234567890123456' to Decimal", ErrInvalidFormat)}, - {"340282366920938463463374607431768211459.123+--", "", fmt.Errorf("%w: can't parse '340282366920938463463374607431768211459.123+--' to Decimal", ErrInvalidFormat)}, + {"340282366920938463463374607431768211459.", "", fmt.Errorf("%w: can't parse '340282366920938463463374607431768211459.'", ErrInvalidFormat)}, + {"--340282366920938463463374607431768211459", "", fmt.Errorf("%w: can't parse '--340282366920938463463374607431768211459'", ErrInvalidFormat)}, + {".1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '.1234567890123456789012345678901234567890123456'", ErrInvalidFormat)}, + {"+.1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '+.1234567890123456789012345678901234567890123456'", ErrInvalidFormat)}, + {"-.1234567890123456789012345678901234567890123456", "", fmt.Errorf("%w: can't parse '-.1234567890123456789012345678901234567890123456'", ErrInvalidFormat)}, + {"1.12345678901234567890123.45678901234567890123456", "", fmt.Errorf("%w: can't parse '1.12345678901234567890123.45678901234567890123456'", ErrInvalidFormat)}, + {"340282366920938463463374607431768211459.123+--", "", fmt.Errorf("%w: can't parse '340282366920938463463374607431768211459.123+--'", ErrInvalidFormat)}, {"", "", ErrEmptyString}, {"1.234567890123456789012348901", "", ErrPrecOutOfRange}, {"1.123456789012345678912345678901234567890123456", "", ErrPrecOutOfRange}, - {".", "", fmt.Errorf("%w: can't parse '.' to Decimal", ErrInvalidFormat)}, - {"123.", "", fmt.Errorf("%w: can't parse '123.' to Decimal", ErrInvalidFormat)}, - {"-123.", "", fmt.Errorf("%w: can't parse '-123.' to Decimal", ErrInvalidFormat)}, - {"-.123456", "", fmt.Errorf("%w: can't parse '-.123456' to Decimal", ErrInvalidFormat)}, - {"12c45.123456", "", fmt.Errorf("%w: can't parse '12c45.123456' to Decimal", ErrInvalidFormat)}, - {"1245.-123456", "", fmt.Errorf("%w: can't parse '1245.-123456' to Decimal", ErrInvalidFormat)}, - {"1245.123.456", "", fmt.Errorf("%w: can't parse '1245.123.456' to Decimal", ErrInvalidFormat)}, - {"12345..123456", "", fmt.Errorf("%w: can't parse '12345..123456' to Decimal", ErrInvalidFormat)}, - {"123456.123c456", "", fmt.Errorf("%w: can't parse '123456.123c456' to Decimal", ErrInvalidFormat)}, - {"+.", "", fmt.Errorf("%w: can't parse '+.' to Decimal", ErrInvalidFormat)}, - {"+", "", fmt.Errorf("%w: can't parse '+' to Decimal", ErrInvalidFormat)}, - {"-", "", fmt.Errorf("%w: can't parse '-' to Decimal", ErrInvalidFormat)}, - {"abc.1234567890123456789", "", fmt.Errorf("%w: can't parse 'abc.1234567890123456789' to Decimal", ErrInvalidFormat)}, - {"123.1234567890123456abc", "", fmt.Errorf("%w: can't parse '123.1234567890123456abc' to Decimal", ErrInvalidFormat)}, - {"12345678901234567890123456789012345679801234567890.", "", fmt.Errorf("%w: can't parse '12345678901234567890123456789012345679801234567890.' to Decimal", ErrInvalidFormat)}, + {".", "", fmt.Errorf("%w: can't parse '.'", ErrInvalidFormat)}, + {"123.", "", fmt.Errorf("%w: can't parse '123.'", ErrInvalidFormat)}, + {"-123.", "", fmt.Errorf("%w: can't parse '-123.'", ErrInvalidFormat)}, + {"-.123456", "", fmt.Errorf("%w: can't parse '-.123456'", ErrInvalidFormat)}, + {"12c45.123456", "", fmt.Errorf("%w: can't parse '12c45.123456'", ErrInvalidFormat)}, + {"1245.-123456", "", fmt.Errorf("%w: can't parse '1245.-123456'", ErrInvalidFormat)}, + {"1245.123.456", "", fmt.Errorf("%w: can't parse '1245.123.456'", ErrInvalidFormat)}, + {"12345..123456", "", fmt.Errorf("%w: can't parse '12345..123456'", ErrInvalidFormat)}, + {"123456.123c456", "", fmt.Errorf("%w: can't parse '123456.123c456'", ErrInvalidFormat)}, + {"+.", "", fmt.Errorf("%w: can't parse '+.'", ErrInvalidFormat)}, + {"+", "", fmt.Errorf("%w: can't parse '+'", ErrInvalidFormat)}, + {"-", "", fmt.Errorf("%w: can't parse '-'", ErrInvalidFormat)}, + {"abc.1234567890123456789", "", fmt.Errorf("%w: can't parse 'abc.1234567890123456789'", ErrInvalidFormat)}, + {"123.1234567890123456abc", "", fmt.Errorf("%w: can't parse '123.1234567890123456abc'", ErrInvalidFormat)}, + {"12345678901234567890123456789012345679801234567890.", "", fmt.Errorf("%w: can't parse '12345678901234567890123456789012345679801234567890.'", ErrInvalidFormat)}, } for _, tc := range testcases { @@ -204,15 +204,15 @@ func TestMustParse(t *testing.T) { {"340282366920938463463374607431768211459", nil}, {"1.234567890123456789012348901", ErrPrecOutOfRange}, {"", ErrEmptyString}, - {".", fmt.Errorf("%w: can't parse '.' to Decimal", ErrInvalidFormat)}, - {"123.", fmt.Errorf("%w: can't parse '123.' to Decimal", ErrInvalidFormat)}, - {"-123.", fmt.Errorf("%w: can't parse '-123.' to Decimal", ErrInvalidFormat)}, - {"-.123456", fmt.Errorf("%w: can't parse '-.123456' to Decimal", ErrInvalidFormat)}, - {"12c45.123456", fmt.Errorf("%w: can't parse '12c45.123456' to Decimal", ErrInvalidFormat)}, - {"12345..123456", fmt.Errorf("%w: can't parse '12345..123456' to Decimal", ErrInvalidFormat)}, - {"+.", fmt.Errorf("%w: can't parse '+.' to Decimal", ErrInvalidFormat)}, - {"+", fmt.Errorf("%w: can't parse '+' to Decimal", ErrInvalidFormat)}, - {"-", fmt.Errorf("%w: can't parse '-' to Decimal", ErrInvalidFormat)}, + {".", fmt.Errorf("%w: can't parse '.'", ErrInvalidFormat)}, + {"123.", fmt.Errorf("%w: can't parse '123.'", ErrInvalidFormat)}, + {"-123.", fmt.Errorf("%w: can't parse '-123.'", ErrInvalidFormat)}, + {"-.123456", fmt.Errorf("%w: can't parse '-.123456'", ErrInvalidFormat)}, + {"12c45.123456", fmt.Errorf("%w: can't parse '12c45.123456'", ErrInvalidFormat)}, + {"12345..123456", fmt.Errorf("%w: can't parse '12345..123456'", ErrInvalidFormat)}, + {"+.", fmt.Errorf("%w: can't parse '+.'", ErrInvalidFormat)}, + {"+", fmt.Errorf("%w: can't parse '+'", ErrInvalidFormat)}, + {"-", fmt.Errorf("%w: can't parse '-'", ErrInvalidFormat)}, } for _, tc := range testcases { diff --git a/doc_test.go b/doc_test.go index 2b91f3d..4ea1104 100644 --- a/doc_test.go +++ b/doc_test.go @@ -95,8 +95,8 @@ func ExampleParse() { // -1234567890123456789.1234567890123456789 // -0.00007890123456789 // 0 precision out of range. Only support maximum 19 digits after the decimal point - // 0 parse empty string - // 0 invalid format: can't parse '1.123.123' to Decimal + // 0 can't parse empty string + // 0 invalid format: can't parse '1.123.123' } func ExampleNewFromHiLo() {