aboutsummaryrefslogtreecommitdiff
path: root/backend/internal/utils/money_test.go
blob: 27ace064db15643044f0d748454bf1aa6c046399 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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)
		})
	}
}