123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- 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)
- }
- }
- }
|