|
@@ -0,0 +1,141 @@
|
|
|
+package common
|
|
|
+
|
|
|
+import (
|
|
|
+ "testing"
|
|
|
+)
|
|
|
+
|
|
|
+func TestMoneyNegation(t *testing.T) {
|
|
|
+ for _, s := range []struct {
|
|
|
+ amount string // 金额字符串
|
|
|
+ want string // 期望结果
|
|
|
+ }{
|
|
|
+ {
|
|
|
+ amount: "",
|
|
|
+ want: "",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount: "-100",
|
|
|
+ want: "100",
|
|
|
+ }, {
|
|
|
+ amount: "-1000.00",
|
|
|
+ want: "1000.00",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount: "-100.123",
|
|
|
+ want: "100.123",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount: "1000",
|
|
|
+ want: "-1000",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount: "1200.12",
|
|
|
+ want: "-1200.12",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount: "1200.00",
|
|
|
+ want: "-1200.00",
|
|
|
+ },
|
|
|
+ } {
|
|
|
+ s1 := MoneyNegation(s.amount)
|
|
|
+ t.Logf("origin: %s, after: %s", s.amount, s1)
|
|
|
+ if s1 != s.want {
|
|
|
+ t.Errorf("Expected %s, got %s", s.want, s1)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestMoneyNormalize(t *testing.T) {
|
|
|
+ for _, tc := range []struct {
|
|
|
+ s string
|
|
|
+ want string
|
|
|
+ }{
|
|
|
+ {
|
|
|
+ s: "",
|
|
|
+ want: "",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ s: "8,200.00",
|
|
|
+ want: "8200.00",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ s: "123",
|
|
|
+ want: "123",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ s: "1,234.56",
|
|
|
+ want: "1234.56",
|
|
|
+ },
|
|
|
+ } {
|
|
|
+ r := MoneyNormalize(tc.s)
|
|
|
+ if r != tc.want {
|
|
|
+ t.Errorf("Expected %s, got %s", tc.want, r)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestIsAmountEqual(t *testing.T) {
|
|
|
+ for _, s := range []struct {
|
|
|
+ amount1 string // 金额字符串1
|
|
|
+ amount2 string // 金额字符串2
|
|
|
+ want bool // 期望结果
|
|
|
+ }{
|
|
|
+ {
|
|
|
+ amount1: "1000",
|
|
|
+ amount2: "1,000",
|
|
|
+ want: true,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount1: "1000.00",
|
|
|
+ amount2: "1000",
|
|
|
+ want: true,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount1: "-1000.00",
|
|
|
+ amount2: "-1,000.00",
|
|
|
+ want: true,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount1: "100.123",
|
|
|
+ amount2: "100.123",
|
|
|
+ want: true,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount1: "1000.00",
|
|
|
+ amount2: "1000.01",
|
|
|
+ want: false,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount1: "abc",
|
|
|
+ amount2: "1000",
|
|
|
+ want: false,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount1: "",
|
|
|
+ amount2: "0",
|
|
|
+ want: true,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount1: "0",
|
|
|
+ amount2: "0.00",
|
|
|
+ want: true,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount1: "1,200.12",
|
|
|
+ amount2: "1200.12",
|
|
|
+ want: true,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ amount1: "-0",
|
|
|
+ amount2: "0",
|
|
|
+ want: true,
|
|
|
+ },
|
|
|
+ } {
|
|
|
+ // 执行测试
|
|
|
+ got := IsAmountEqual(s.amount1, s.amount2)
|
|
|
+ //t.Logf("amount1: %s, amount2: %s, expected: %t, got: %t", s.amount1, s.amount2, s.want, got)
|
|
|
+ if got != s.want {
|
|
|
+ t.Errorf("amount1: %s, amount2: %s: expected %t, but got %t", s.amount1, s.amount2, s.want, got)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|