Skip to content

Commit 9ee858e

Browse files
authored
Merge branch 'master' into pt/externalize-packages
2 parents 1ed9b81 + 3c57f6d commit 9ee858e

File tree

5 files changed

+226
-2
lines changed

5 files changed

+226
-2
lines changed

currency_test.go

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package i18nify
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestFormatCurrency(t *testing.T) {
8+
c := &CurrencyInformation{}
9+
10+
tests := []struct {
11+
name string
12+
amount float64
13+
locale string
14+
wantErr bool
15+
validate func(t *testing.T, result string)
16+
}{
17+
{
18+
name: "valid locale en-US with positive amount",
19+
amount: 1234.56,
20+
locale: "en-US",
21+
wantErr: false,
22+
validate: func(t *testing.T, result string) {
23+
if result == "" {
24+
t.Error("expected non-empty result")
25+
}
26+
// Should format with 2 decimal places
27+
if len(result) < 1 {
28+
t.Errorf("result should contain formatted number, got: %s", result)
29+
}
30+
},
31+
},
32+
{
33+
name: "valid locale en with integer amount",
34+
amount: 1000.0,
35+
locale: "en",
36+
wantErr: false,
37+
validate: func(t *testing.T, result string) {
38+
if result == "" {
39+
t.Error("expected non-empty result")
40+
}
41+
},
42+
},
43+
{
44+
name: "valid locale de-DE with decimal amount",
45+
amount: 99.99,
46+
locale: "de-DE",
47+
wantErr: false,
48+
validate: func(t *testing.T, result string) {
49+
if result == "" {
50+
t.Error("expected non-empty result")
51+
}
52+
},
53+
},
54+
{
55+
name: "zero amount",
56+
amount: 0.0,
57+
locale: "en-US",
58+
wantErr: false,
59+
validate: func(t *testing.T, result string) {
60+
if result == "" {
61+
t.Error("expected non-empty result for zero amount")
62+
}
63+
},
64+
},
65+
{
66+
name: "negative amount",
67+
amount: -100.50,
68+
locale: "en-US",
69+
wantErr: false,
70+
validate: func(t *testing.T, result string) {
71+
if result == "" {
72+
t.Error("expected non-empty result for negative amount")
73+
}
74+
},
75+
},
76+
{
77+
name: "large amount",
78+
amount: 999999.99,
79+
locale: "en-US",
80+
wantErr: false,
81+
validate: func(t *testing.T, result string) {
82+
if result == "" {
83+
t.Error("expected non-empty result for large amount")
84+
}
85+
},
86+
},
87+
{
88+
name: "amount with many decimal places",
89+
amount: 123.456789,
90+
locale: "en-US",
91+
wantErr: false,
92+
validate: func(t *testing.T, result string) {
93+
if result == "" {
94+
t.Error("expected non-empty result")
95+
}
96+
},
97+
},
98+
{
99+
name: "invalid locale",
100+
amount: 100.0,
101+
locale: "invalid-locale-xyz",
102+
wantErr: true,
103+
validate: func(t *testing.T, result string) {
104+
if result != "" {
105+
t.Errorf("expected empty result for invalid locale, got: %s", result)
106+
}
107+
},
108+
},
109+
{
110+
name: "empty locale",
111+
amount: 100.0,
112+
locale: "",
113+
wantErr: true,
114+
validate: func(t *testing.T, result string) {
115+
if result != "" {
116+
t.Errorf("expected empty result for empty locale, got: %s", result)
117+
}
118+
},
119+
},
120+
{
121+
name: "valid locale fr-FR",
122+
amount: 1234.56,
123+
locale: "fr-FR",
124+
wantErr: false,
125+
validate: func(t *testing.T, result string) {
126+
if result == "" {
127+
t.Error("expected non-empty result")
128+
}
129+
},
130+
},
131+
{
132+
name: "valid locale ja-JP",
133+
amount: 1000.00,
134+
locale: "ja-JP",
135+
wantErr: false,
136+
validate: func(t *testing.T, result string) {
137+
if result == "" {
138+
t.Error("expected non-empty result")
139+
}
140+
},
141+
},
142+
}
143+
144+
for _, tt := range tests {
145+
t.Run(tt.name, func(t *testing.T) {
146+
result, err := c.FormatCurrency(tt.amount, tt.locale)
147+
148+
if (err != nil) != tt.wantErr {
149+
t.Errorf("FormatCurrency() error = %v, wantErr %v", err, tt.wantErr)
150+
return
151+
}
152+
153+
if tt.validate != nil {
154+
tt.validate(t, result)
155+
}
156+
})
157+
}
158+
}
159+

packages/i18nify-go/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/razorpay/i18nify/packages/i18nify-go
22

33
go 1.20
44

5-
require github.com/stretchr/testify v1.9.0
5+
require (
6+
github.com/stretchr/testify v1.9.0
7+
golang.org/x/text v0.22.0
8+
)
69

710
require google.golang.org/protobuf v1.31.0 // indirect
811

packages/i18nify-go/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
1414
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
1515
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
1616
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
17+
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
18+
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
1719
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1820
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1921
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

packages/i18nify-go/modules/currency/currency.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ import (
1111
"embed"
1212
"encoding/json"
1313
"fmt"
14+
"golang.org/x/text/language"
15+
"golang.org/x/text/message"
1416
)
1517

18+
type ICurrencyInfo interface {
19+
FormatCurrency(amount float64, locale string) (string, error)
20+
}
21+
1622
//go:embed data
1723
var currencyJsonDir embed.FS
1824

@@ -87,7 +93,7 @@ type CurrencyInformation struct {
8793
// Getters for various fields of CurrencyInformation.
8894

8995
// NewCurrencyInformation creates a new CurrencyInformation instance.
90-
func NewCurrencyInformation(minorUnit string, name string, numericCode string, physicalCurrencyDenominations []string, symbol string) *CurrencyInformation {
96+
func NewCurrencyInformation(minorUnit string, name string, numericCode string, physicalCurrencyDenominations []string, symbol string) ICurrencyInfo {
9197
return &CurrencyInformation{
9298
MinorUnit: minorUnit,
9399
Name: name,
@@ -117,3 +123,16 @@ func GetCurrencySymbol(currencyCode string) (string, error) {
117123

118124
return currencyInfo.Symbol, nil
119125
}
126+
127+
// GetCurrencySymbol retrieves the currency symbol for a specific currency code.
128+
func (c *CurrencyInformation) FormatCurrency(amount float64, locale string) (string, error) {
129+
tag, err := language.Parse(locale)
130+
if err != nil {
131+
return "", err
132+
}
133+
134+
p := message.NewPrinter(tag)
135+
136+
// Format with 2 decimal places
137+
return p.Sprintf("%.2f", amount), nil
138+
}

packages/i18nify-go/modules/currency/currency_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,44 @@ func TestGetCurrencySymbol(t *testing.T) {
130130
}
131131
}
132132
}
133+
134+
func TestFormatCurrency(t *testing.T) {
135+
c := &CurrencyInformation{}
136+
137+
tests := []struct {
138+
name string
139+
amount float64
140+
locale string
141+
wantErr bool
142+
Eexpected string
143+
}{
144+
{
145+
name: "valid locale en-US with positive amount",
146+
amount: 15300000,
147+
locale: "en-US",
148+
wantErr: false,
149+
Eexpected: "15,300,000.00",
150+
},
151+
{
152+
name: "valid locale en-IN with positive amount",
153+
amount: 15300000,
154+
locale: "en-IN",
155+
wantErr: false,
156+
Eexpected: "1,53,00,000.00",
157+
},
158+
{
159+
name: "valid locale en-MY with positive amount",
160+
amount: 15300000,
161+
locale: "en-MY",
162+
wantErr: false,
163+
Eexpected: "15,300,000.00",
164+
},
165+
}
166+
167+
for _, tt := range tests {
168+
t.Run(tt.name, func(t *testing.T) {
169+
result, _ := c.FormatCurrency(tt.amount, tt.locale)
170+
assert.Equal(t, tt.Eexpected, result)
171+
})
172+
}
173+
}

0 commit comments

Comments
 (0)