amount_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package common
  2. import (
  3. "testing"
  4. )
  5. func TestMoneyNegation(t *testing.T) {
  6. for _, s := range []struct {
  7. amount string // 金额字符串
  8. want string // 期望结果
  9. }{
  10. {
  11. amount: "",
  12. want: "",
  13. },
  14. {
  15. amount: "-100",
  16. want: "100",
  17. }, {
  18. amount: "-1000.00",
  19. want: "1000.00",
  20. },
  21. {
  22. amount: "-100.123",
  23. want: "100.123",
  24. },
  25. {
  26. amount: "1000",
  27. want: "-1000",
  28. },
  29. {
  30. amount: "1200.12",
  31. want: "-1200.12",
  32. },
  33. {
  34. amount: "1200.00",
  35. want: "-1200.00",
  36. },
  37. } {
  38. s1 := MoneyNegation(s.amount)
  39. t.Logf("origin: %s, after: %s", s.amount, s1)
  40. if s1 != s.want {
  41. t.Errorf("Expected %s, got %s", s.want, s1)
  42. }
  43. }
  44. }
  45. func TestMoneyNormalize(t *testing.T) {
  46. for _, tc := range []struct {
  47. s string
  48. want string
  49. }{
  50. {
  51. s: "",
  52. want: "",
  53. },
  54. {
  55. s: "8,200.00",
  56. want: "8200.00",
  57. },
  58. {
  59. s: "123",
  60. want: "123",
  61. },
  62. {
  63. s: "1,234.56",
  64. want: "1234.56",
  65. },
  66. } {
  67. r := MoneyNormalize(tc.s)
  68. if r != tc.want {
  69. t.Errorf("Expected %s, got %s", tc.want, r)
  70. }
  71. }
  72. }
  73. func TestIsAmountEqual(t *testing.T) {
  74. for _, s := range []struct {
  75. amount1 string // 金额字符串1
  76. amount2 string // 金额字符串2
  77. want bool // 期望结果
  78. }{
  79. {
  80. amount1: "1000",
  81. amount2: "1,000",
  82. want: true,
  83. },
  84. {
  85. amount1: "1000.00",
  86. amount2: "1000",
  87. want: true,
  88. },
  89. {
  90. amount1: "-1000.00",
  91. amount2: "-1,000.00",
  92. want: true,
  93. },
  94. {
  95. amount1: "100.123",
  96. amount2: "100.123",
  97. want: true,
  98. },
  99. {
  100. amount1: "1000.00",
  101. amount2: "1000.01",
  102. want: false,
  103. },
  104. {
  105. amount1: "abc",
  106. amount2: "1000",
  107. want: false,
  108. },
  109. {
  110. amount1: "",
  111. amount2: "0",
  112. want: true,
  113. },
  114. {
  115. amount1: "0",
  116. amount2: "",
  117. want: true,
  118. },
  119. {
  120. amount1: "0",
  121. amount2: "0.00",
  122. want: true,
  123. },
  124. {
  125. amount1: "1,200.12",
  126. amount2: "1200.12",
  127. want: true,
  128. },
  129. {
  130. amount1: "-0",
  131. amount2: "0",
  132. want: true,
  133. },
  134. } {
  135. // 执行测试
  136. got := IsAmountEqual(s.amount1, s.amount2)
  137. //t.Logf("amount1: %s, amount2: %s, expected: %t, got: %t", s.amount1, s.amount2, s.want, got)
  138. if got != s.want {
  139. t.Errorf("amount1: %s, amount2: %s: expected %t, but got %t", s.amount1, s.amount2, s.want, got)
  140. }
  141. }
  142. }
  143. func TestMoneyAdd(t *testing.T) {
  144. for _, tc := range []struct {
  145. a, b, want string
  146. }{
  147. {"123.45", "67.89", "191.34"},
  148. {"9.00", "1.00", "10.00"},
  149. {"0.10", "0.20", "0.30"},
  150. {"1.23", "1.23", "2.46"},
  151. {"0.00", "0.00", "0.00"},
  152. } {
  153. r := MoneyAdd(tc.a, tc.b)
  154. if r != tc.want {
  155. t.Errorf("Expected %v, but got %v", tc.want, r)
  156. }
  157. }
  158. }