89 lines
2.1 KiB
Go
89 lines
2.1 KiB
Go
package numeric_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.javil.eu/jacob1123/budgeteer/postgres/numeric"
|
|
)
|
|
|
|
type TestCaseMarshalJSON struct {
|
|
Value numeric.Numeric
|
|
Result string
|
|
}
|
|
|
|
func TestMarshalJSON(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []TestCaseMarshalJSON{
|
|
{numeric.Zero(), `0`},
|
|
{numeric.MustParse("1.23"), "1.23"},
|
|
{numeric.MustParse("1,24"), "1.24"},
|
|
{numeric.MustParse("123456789.12345"), "123456789.12345"},
|
|
{numeric.MustParse("-1.23"), "-1.23"},
|
|
{numeric.MustParse("-1,24"), "-1.24"},
|
|
{numeric.MustParse("-123456789.12345"), "-123456789.12345"},
|
|
}
|
|
for i := range tests {
|
|
test := tests[i]
|
|
t.Run(test.Result, func(t *testing.T) {
|
|
t.Parallel()
|
|
z := test.Value
|
|
result, err := z.MarshalJSON()
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
if string(result) != test.Result {
|
|
t.Errorf("Expected %s, got %s", test.Result, string(result))
|
|
return
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type TestCaseParse struct {
|
|
Result numeric.Numeric
|
|
Value string
|
|
}
|
|
|
|
func TestParse(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []TestCaseParse{
|
|
{numeric.FromInt64WithExp(0, 0), `0`},
|
|
{numeric.FromInt64WithExp(1, 0), `1`},
|
|
{numeric.FromInt64WithExp(1, 1), `10`},
|
|
{numeric.FromInt64WithExp(1, 2), `100`},
|
|
{numeric.FromInt64WithExp(123, -2), "1.23"},
|
|
{numeric.FromInt64WithExp(124, -2), "1,24"},
|
|
{numeric.FromInt64WithExp(12345678912345, -5), "123456789.12345"},
|
|
{numeric.FromInt64WithExp(0, 0), `-0`},
|
|
{numeric.FromInt64WithExp(-1, 0), `-1`},
|
|
{numeric.FromInt64WithExp(-1, 1), `-10`},
|
|
{numeric.FromInt64WithExp(-1, 2), `-100`},
|
|
{numeric.FromInt64WithExp(-123, -2), "-1.23"},
|
|
{numeric.FromInt64WithExp(-124, -2), "-1,24"},
|
|
{numeric.FromInt64WithExp(-12345678912345, -5), "-123456789.12345"},
|
|
}
|
|
for i := range tests {
|
|
test := tests[i]
|
|
t.Run(test.Value, func(t *testing.T) {
|
|
t.Parallel()
|
|
result, err := numeric.Parse(test.Value)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
if test.Result.Int.Int64() != result.Int.Int64() {
|
|
t.Errorf("Expected int %d, got %d", test.Result.Int, result.Int)
|
|
return
|
|
}
|
|
|
|
if test.Result.Exp != result.Exp {
|
|
t.Errorf("Expected exp %d, got %d", test.Result.Exp, result.Exp)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
}
|