119 lines
3.4 KiB
Go
119 lines
3.4 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("1"), "1"},
|
|
{numeric.MustParse("10"), "10"},
|
|
{numeric.MustParse("100"), "100"},
|
|
{numeric.MustParse("1000"), "1000"},
|
|
{numeric.MustParse("0.1"), "0.1"},
|
|
{numeric.MustParse("0.01"), "0.01"},
|
|
{numeric.MustParse("0.001"), "0.001"},
|
|
{numeric.MustParse("0.0001"), "0.0001"},
|
|
{numeric.MustParse("-1"), "-1"},
|
|
{numeric.MustParse("-10"), "-10"},
|
|
{numeric.MustParse("-100"), "-100"},
|
|
{numeric.MustParse("-1000"), "-1000"},
|
|
{numeric.MustParse("-0.1"), "-0.1"},
|
|
{numeric.MustParse("-0.01"), "-0.01"},
|
|
{numeric.MustParse("-0.001"), "-0.001"},
|
|
{numeric.MustParse("-0.0001"), "-0.0001"},
|
|
{numeric.MustParse("123456789.12345"), "123456789.12345"},
|
|
{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(1, 3), `1000`},
|
|
{numeric.FromInt64WithExp(1, -1), `0.1`},
|
|
{numeric.FromInt64WithExp(1, -2), `0.01`},
|
|
{numeric.FromInt64WithExp(1, -3), `0.001`},
|
|
{numeric.FromInt64WithExp(1, -4), `0.0001`},
|
|
{numeric.FromInt64WithExp(-1, 0), `-1`},
|
|
{numeric.FromInt64WithExp(-1, 1), `-10`},
|
|
{numeric.FromInt64WithExp(-1, 2), `-100`},
|
|
{numeric.FromInt64WithExp(-1, 3), `-1000`},
|
|
{numeric.FromInt64WithExp(-1, -1), `-0.1`},
|
|
{numeric.FromInt64WithExp(-1, -2), `-0.01`},
|
|
{numeric.FromInt64WithExp(-1, -3), `-0.001`},
|
|
{numeric.FromInt64WithExp(-1, -4), `-0.0001`},
|
|
{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
|
|
}
|
|
})
|
|
}
|
|
}
|