Skip to content

Commit 1be3b32

Browse files
committed
Make MoneyFormatter().formatCurrency unit test pass
1 parent 1a44794 commit 1be3b32

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

WooCommerce/Classes/Tools/Currency/MoneyFormatter.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,22 @@ extension MoneyFormatter {
122122
/// - localizedAmount: a formatted string returned from `localizeAmount()`
123123
/// - currencyPosition: the currency position, either right, left, right_space, or left_space.
124124
/// - currencyCode: the three-letter country code used for a currency, e.g. CAD.
125-
126-
// -TODO: currency position should be an enum somewhere. Maybe in the Money struct?
127-
func formatCurrency(using localizedAmount: String, positionedAt currencyPosition: String, with currencyCode: Currency.Code) -> String? {
128-
return ""
125+
func formatCurrency(using amount: String,
126+
at position: Currency.Position,
127+
with code: Currency.Code) -> String? {
128+
let currency = Currency(amount: amount, code: code, position: position)
129+
let symbol = currency.symbol
130+
131+
switch position {
132+
case .left:
133+
return symbol + amount
134+
case .right:
135+
return amount + symbol
136+
case .leftSpace:
137+
return symbol + "\u{00a0}" + amount
138+
case .rightSpace:
139+
return amount + "\u{00a0}" + symbol
140+
}
129141
}
130142

131143

WooCommerce/WooCommerceTests/Tools/MoneyFormatterTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ extension MoneyFormatter {
476476
let decimalSeparator = ","
477477
let thousandSeparator = "."
478478
let decimalPosition = 3
479-
let currencyPosition = "right_space"
479+
let currencyPosition = Currency.Position.rightSpace
480480
let currencyCode = Currency.Code.JOD
481481
let stringAmount = "-7867818684.64"
482482
let expectedResult = "-7.867.818.684,640 د.ا"
@@ -499,7 +499,7 @@ extension MoneyFormatter {
499499
}
500500

501501
let formattedAmount = MoneyFormatter().formatCurrency(using: localizedAmount,
502-
positionedAt: currencyPosition,
502+
at: currencyPosition,
503503
with: currencyCode)
504504

505505
guard let actualResult = formattedAmount else {

0 commit comments

Comments
 (0)