Files
hfb_sys/backend/pkg/money/format_test.go
T
2026-06-09 19:04:11 +08:00

159 lines
3.5 KiB
Go

package money
import "testing"
func TestToCent(t *testing.T) {
tests := []struct {
name string
yuan float64
want int64
}{
{"12.3元转1230分", 12.3, 1230},
{"12.34元转1234分", 12.34, 1234},
{"12.36元转1236分", 12.36, 1236},
{"0.1元转10分", 0.1, 10},
{"100元转10000分", 100.0, 10000},
{"零值", 0.0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ToCent(tt.yuan)
if got != tt.want {
t.Errorf("ToCent(%v) = %v, want %v", tt.yuan, got, tt.want)
}
})
}
}
func TestToDisplayYuan(t *testing.T) {
tests := []struct {
name string
cent int64
want float64
}{
{"123分展示1.2元", 123, 1.2},
{"1234分展示12.3元", 1234, 12.3},
{"1236分展示12.4元", 1236, 12.4},
{"10分展示0.1元", 10, 0.1},
{"1000分展示10.0元", 1000, 10.0},
{"零值", 0, 0.0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ToDisplayYuan(tt.cent)
if got != tt.want {
t.Errorf("ToDisplayYuan(%v) = %v, want %v", tt.cent, got, tt.want)
}
})
}
}
func TestFormat(t *testing.T) {
tests := []struct {
name string
cent int64
want string
}{
{"123分格式化", 123, "1.2"},
{"1234分格式化", 1234, "12.3"},
{"1236分格式化", 1236, "12.4"},
{"10分格式化", 10, "0.1"},
{"1000分格式化", 1000, "10.0"},
{"零值格式化", 0, "0.0"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Format(tt.cent)
if got != tt.want {
t.Errorf("Format(%v) = %v, want %v", tt.cent, got, tt.want)
}
})
}
}
func TestFormatWithSymbol(t *testing.T) {
tests := []struct {
name string
cent int64
want string
}{
{"123分带符号", 123, "¥1.2"},
{"1000分带符号", 1000, "¥10.0"},
{"零值带符号", 0, "¥0.0"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FormatWithSymbol(tt.cent)
if got != tt.want {
t.Errorf("FormatWithSymbol(%v) = %v, want %v", tt.cent, got, tt.want)
}
})
}
}
func TestRound(t *testing.T) {
tests := []struct {
name string
value float64
want float64
}{
{"12.34舍入到12.3", 12.34, 12.3},
{"12.36舍入到12.4", 12.36, 12.4},
{"12.35舍入到12.4", 12.35, 12.4},
{"0.05舍入到0.1", 0.05, 0.1},
{"0.04舍入到0.0", 0.04, 0.0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Round(tt.value)
if got != tt.want {
t.Errorf("Round(%v) = %v, want %v", tt.value, got, tt.want)
}
})
}
}
func TestMinMax(t *testing.T) {
t.Run("Min", func(t *testing.T) {
if got := Min(100, 200); got != 100 {
t.Errorf("Min(100, 200) = %v, want 100", got)
}
if got := Min(200, 100); got != 100 {
t.Errorf("Min(200, 100) = %v, want 100", got)
}
})
t.Run("Max", func(t *testing.T) {
if got := Max(100, 200); got != 200 {
t.Errorf("Max(100, 200) = %v, want 200", got)
}
if got := Max(200, 100); got != 200 {
t.Errorf("Max(200, 100) = %v, want 200", got)
}
})
}
// 测试双向转换的精度
func TestRoundTrip(t *testing.T) {
tests := []struct {
name string
originalYuan float64
expectDisplay float64
}{
{"12.3元往返", 12.3, 12.3},
{"12.34元往返(展示到角)", 12.34, 12.3},
{"100.0元往返", 100.0, 100.0},
{"0.1元往返", 0.1, 0.1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cent := ToCent(tt.originalYuan)
got := ToDisplayYuan(cent)
if got != tt.expectDisplay {
t.Errorf("往返转换:%v元 -> %v分 -> %v元, 期望 %v元",
tt.originalYuan, cent, got, tt.expectDisplay)
}
})
}
}