75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package money
|
|
|
|
import "testing"
|
|
|
|
func TestParseYuan(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
value string
|
|
want int64
|
|
}{
|
|
{"整数", "100", 10000},
|
|
{"一位小数", "12.3", 1230},
|
|
{"两位小数", "12.34", 1234},
|
|
{"前导零", "0.05", 5},
|
|
{"负金额", "-1.20", -120},
|
|
{"正号", "+0.1", 10},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := ParseYuan(tt.value)
|
|
if err != nil {
|
|
t.Fatalf("ParseYuan(%q) error: %v", tt.value, err)
|
|
}
|
|
if got != tt.want {
|
|
t.Fatalf("ParseYuan(%q) = %d, want %d", tt.value, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseYuanRejectsInvalid(t *testing.T) {
|
|
for _, value := range []string{"", "12.345", "abc", "1.2.3", "1.-2"} {
|
|
if _, err := ParseYuan(value); err == nil {
|
|
t.Fatalf("ParseYuan(%q) 应返回错误", value)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFormat(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
cent int64
|
|
want string
|
|
}{
|
|
{"两位小数", 1234, "12.34"},
|
|
{"末尾零", 1230, "12.30"},
|
|
{"五分", 5, "0.05"},
|
|
{"整数", 10000, "100.00"},
|
|
{"负数", -120, "-1.20"},
|
|
{"零值", 0, "0.00"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := Format(tt.cent); got != tt.want {
|
|
t.Fatalf("Format(%d) = %q, want %q", tt.cent, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFormatWithSymbol(t *testing.T) {
|
|
if got := FormatWithSymbol(1234); got != "¥12.34" {
|
|
t.Fatalf("FormatWithSymbol(1234) = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestMinMaxCent(t *testing.T) {
|
|
if got := MinCent(100, 200); got != 100 {
|
|
t.Fatalf("MinCent = %d", got)
|
|
}
|
|
if got := MaxCent(100, 200); got != 200 {
|
|
t.Fatalf("MaxCent = %d", got)
|
|
}
|
|
}
|