add Test_formatQuantity

This commit is contained in:
c9s 2022-02-25 17:47:54 +08:00
parent 555e8c5253
commit 10612cdfa9

View File

@ -2,6 +2,7 @@ package types
import (
"encoding/json"
"regexp"
"testing"
"time"
@ -87,6 +88,14 @@ func Test_formatPrice(t *testing.T) {
args args
want string
}{
{
name: "no fraction",
args: args{
price: fixedpoint.NewFromFloat(10.0),
tickSize: fixedpoint.NewFromFloat(0.001),
},
want: "10.000",
},
{
name: "fraction truncate",
args: args{
@ -103,12 +112,83 @@ func Test_formatPrice(t *testing.T) {
},
want: "2.3340",
},
{
name: "more fraction",
args: args{
price: fixedpoint.MustNewFromString("2.1234567898888"),
tickSize: fixedpoint.NewFromFloat(0.0001),
},
want: "2.1234",
},
}
binanceFormatRE := regexp.MustCompile("^([0-9]{1,20})(.[0-9]{1,20})?$")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := formatPrice(tt.args.price, tt.args.tickSize); got != tt.want {
got := formatPrice(tt.args.price, tt.args.tickSize);
if got != tt.want {
t.Errorf("formatPrice() = %v, want %v", got, tt.want)
}
assert.Regexp(t, binanceFormatRE, got)
})
}
}
func Test_formatQuantity(t *testing.T) {
type args struct {
quantity fixedpoint.Value
tickSize fixedpoint.Value
}
tests := []struct {
name string
args args
want string
}{
{
name: "no fraction",
args: args{
quantity: fixedpoint.NewFromFloat(10.0),
tickSize: fixedpoint.NewFromFloat(0.001),
},
want: "10.000",
},
{
name: "fraction truncate",
args: args{
quantity: fixedpoint.NewFromFloat(2.334),
tickSize: fixedpoint.NewFromFloat(0.01),
},
want: "2.33",
},
{
name: "fraction",
args: args{
quantity: fixedpoint.NewFromFloat(2.334),
tickSize: fixedpoint.NewFromFloat(0.0001),
},
want: "2.3340",
},
{
name: "more fraction",
args: args{
quantity: fixedpoint.MustNewFromString("2.1234567898888"),
tickSize: fixedpoint.NewFromFloat(0.0001),
},
want: "2.1234",
},
}
binanceFormatRE := regexp.MustCompile("^([0-9]{1,20})(.[0-9]{1,20})?$")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := formatQuantity(tt.args.quantity, tt.args.tickSize);
if got != tt.want {
t.Errorf("formatQuantity() = %v, want %v", got, tt.want)
}
assert.Regexp(t, binanceFormatRE, got)
})
}
}