aboutsummaryrefslogtreecommitdiff
path: root/backend/internal/utils/money_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'backend/internal/utils/money_test.go')
-rw-r--r--backend/internal/utils/money_test.go106
1 files changed, 106 insertions, 0 deletions
diff --git a/backend/internal/utils/money_test.go b/backend/internal/utils/money_test.go
new file mode 100644
index 0000000..27ace06
--- /dev/null
+++ b/backend/internal/utils/money_test.go
@@ -0,0 +1,106 @@
+package utils
+
+import (
+ "testing"
+
+ "github.com/Rhymond/go-money"
+ "github.com/stretchr/testify/assert"
+)
+
+func TestParseMoney(t *testing.T) {
+ tests := []struct {
+ name string
+ input string
+ want *money.Money
+ }{
+ {
+ name: "en-US int no comma",
+ input: "$123",
+ want: money.New(12300, money.USD),
+ },
+ {
+ name: "en-US int comma",
+ input: "$1,123",
+ want: money.New(112300, money.USD),
+ },
+ {
+ name: "en-US decimal comma",
+ input: "$1,123.45",
+ want: money.New(112345, money.USD),
+ },
+ {
+ name: "zh-CN decimal comma",
+ input: "1,234.56 \u5143",
+ want: money.New(123456, money.CNY),
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ m, err := ParseMoney(tt.input)
+ assert.NoError(t, err)
+ assert.Equal(t, tt.want, m)
+ })
+ }
+}
+
+func Test_isCurrency(t *testing.T) {
+ tests := []struct {
+ name string
+ input string
+ currency string
+ numPart string
+ ok bool
+ }{
+ {
+ name: "en-US int no comma",
+ input: "$123",
+ currency: money.USD,
+ numPart: "12300",
+ ok: true,
+ },
+ {
+ name: "en-US int comma",
+ input: "$1,123",
+ currency: money.USD,
+ numPart: "112300",
+ ok: true,
+ },
+ {
+ name: "en-US decimal comma",
+ input: "$1,123.45",
+ currency: money.USD,
+ numPart: "112345",
+ ok: true,
+ },
+ {
+ name: "en-US 1 decimal comma",
+ input: "$1,123.5",
+ currency: money.USD,
+ numPart: "112350",
+ ok: true,
+ },
+ {
+ name: "en-US no grapheme",
+ input: "1,234.56",
+ currency: money.USD,
+ numPart: "",
+ ok: false,
+ },
+ {
+ name: "zh-CN decimal comma",
+ input: "1,234.56 \u5143",
+ currency: money.CNY,
+ numPart: "123456",
+ ok: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ c := money.GetCurrency(tt.currency)
+ numPart, ok := isCurrency(tt.input, c)
+ assert.Equal(t, tt.ok, ok)
+ assert.Equal(t, tt.numPart, numPart)
+ })
+ }
+}