-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from afterpay/currency-formatting
Ensure currency formatting matches expected currency formatting
- Loading branch information
Showing
7 changed files
with
160 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// | ||
// CurrencyFormatterTests.swift | ||
// AfterpayTests | ||
// | ||
// Created by Adam Campbell on 16/10/20. | ||
// Copyright © 2020 Afterpay. All rights reserved. | ||
// | ||
|
||
@testable import Afterpay | ||
import XCTest | ||
|
||
class CurrencyFormatterTests: XCTestCase { | ||
|
||
func testAustraliaLocale() { | ||
let formatter: (String?) -> CurrencyFormatter = { currencyCode in | ||
CurrencyFormatter(locale: Locales.australia, currencyCode: currencyCode!) | ||
} | ||
|
||
let audFormatter = formatter(Locales.australia.currencyCode) | ||
let cadFormatter = formatter(Locales.canada.currencyCode) | ||
let gbpFormatter = formatter(Locales.greatBritain.currencyCode) | ||
let nzdFormatter = formatter(Locales.newZealand.currencyCode) | ||
let usdFormatter = formatter(Locales.unitedStates.currencyCode) | ||
|
||
XCTAssertEqual(audFormatter.string(from: 120), "$120.00") | ||
XCTAssertEqual(cadFormatter.string(from: 120), "$120.00 CAD") | ||
XCTAssertEqual(gbpFormatter.string(from: 120), "£120.00") | ||
XCTAssertEqual(nzdFormatter.string(from: 120), "$120.00 NZD") | ||
XCTAssertEqual(usdFormatter.string(from: 120), "$120.00 USD") | ||
} | ||
|
||
func testCanadaLocale() { | ||
let formatter: (String?) -> CurrencyFormatter = { currencyCode in | ||
CurrencyFormatter(locale: Locales.canada, currencyCode: currencyCode!) | ||
} | ||
|
||
let audFormatter = formatter(Locales.australia.currencyCode) | ||
let cadFormatter = formatter(Locales.canada.currencyCode) | ||
let gbpFormatter = formatter(Locales.greatBritain.currencyCode) | ||
let nzdFormatter = formatter(Locales.newZealand.currencyCode) | ||
let usdFormatter = formatter(Locales.unitedStates.currencyCode) | ||
|
||
XCTAssertEqual(audFormatter.string(from: 120), "$120.00 AUD") | ||
XCTAssertEqual(cadFormatter.string(from: 120), "$120.00") | ||
XCTAssertEqual(gbpFormatter.string(from: 120), "£120.00") | ||
XCTAssertEqual(nzdFormatter.string(from: 120), "$120.00 NZD") | ||
XCTAssertEqual(usdFormatter.string(from: 120), "$120.00 USD") | ||
} | ||
|
||
func testGreatBritainLocale() { | ||
let formatter: (String?) -> CurrencyFormatter = { currencyCode in | ||
CurrencyFormatter(locale: Locales.greatBritain, currencyCode: currencyCode!) | ||
} | ||
|
||
let audFormatter = formatter(Locales.australia.currencyCode) | ||
let cadFormatter = formatter(Locales.canada.currencyCode) | ||
let gbpFormatter = formatter(Locales.greatBritain.currencyCode) | ||
let nzdFormatter = formatter(Locales.newZealand.currencyCode) | ||
let usdFormatter = formatter(Locales.unitedStates.currencyCode) | ||
|
||
XCTAssertEqual(audFormatter.string(from: 120), "$120.00 AUD") | ||
XCTAssertEqual(cadFormatter.string(from: 120), "$120.00 CAD") | ||
XCTAssertEqual(gbpFormatter.string(from: 120), "£120.00") | ||
XCTAssertEqual(nzdFormatter.string(from: 120), "$120.00 NZD") | ||
XCTAssertEqual(usdFormatter.string(from: 120), "$120.00 USD") | ||
} | ||
|
||
func testNewZealandLocale() { | ||
let formatter: (String?) -> CurrencyFormatter = { currencyCode in | ||
CurrencyFormatter(locale: Locales.newZealand, currencyCode: currencyCode!) | ||
} | ||
|
||
let audFormatter = formatter(Locales.australia.currencyCode) | ||
let cadFormatter = formatter(Locales.canada.currencyCode) | ||
let gbpFormatter = formatter(Locales.greatBritain.currencyCode) | ||
let nzdFormatter = formatter(Locales.newZealand.currencyCode) | ||
let usdFormatter = formatter(Locales.unitedStates.currencyCode) | ||
|
||
XCTAssertEqual(audFormatter.string(from: 120), "$120.00 AUD") | ||
XCTAssertEqual(cadFormatter.string(from: 120), "$120.00 CAD") | ||
XCTAssertEqual(gbpFormatter.string(from: 120), "£120.00") | ||
XCTAssertEqual(nzdFormatter.string(from: 120), "$120.00") | ||
XCTAssertEqual(usdFormatter.string(from: 120), "$120.00 USD") | ||
} | ||
|
||
func testUnitedStatesLocale() { | ||
let formatter: (String?) -> CurrencyFormatter = { currencyCode in | ||
CurrencyFormatter(locale: Locales.unitedStates, currencyCode: currencyCode!) | ||
} | ||
|
||
let audFormatter = formatter(Locales.australia.currencyCode) | ||
let cadFormatter = formatter(Locales.canada.currencyCode) | ||
let gbpFormatter = formatter(Locales.greatBritain.currencyCode) | ||
let nzdFormatter = formatter(Locales.newZealand.currencyCode) | ||
let usdFormatter = formatter(Locales.unitedStates.currencyCode) | ||
|
||
XCTAssertEqual(audFormatter.string(from: 120), "A$120.00") | ||
XCTAssertEqual(cadFormatter.string(from: 120), "CA$120.00") | ||
XCTAssertEqual(gbpFormatter.string(from: 120), "£120.00") | ||
XCTAssertEqual(nzdFormatter.string(from: 120), "NZ$120.00") | ||
XCTAssertEqual(usdFormatter.string(from: 120), "$120.00") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// CurrencyFormatter.swift | ||
// Afterpay | ||
// | ||
// Created by Adam Campbell on 16/10/20. | ||
// Copyright © 2020 Afterpay. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
private let formatter: NumberFormatter = { | ||
let formatter = NumberFormatter() | ||
formatter.numberStyle = .currency | ||
return formatter | ||
}() | ||
|
||
struct CurrencyFormatter { | ||
|
||
let locale: Locale | ||
let currencyCode: String | ||
|
||
func string(from decimal: Decimal) -> String? { | ||
if locale == Locales.unitedStates || locale.currencyCode == currencyCode { | ||
formatter.locale = locale | ||
formatter.currencyCode = currencyCode | ||
return formatter.string(from: decimal as NSDecimalNumber) | ||
} else { | ||
let currencyLocale = Locales.validSet.first { $0.currencyCode == currencyCode } | ||
formatter.locale = currencyLocale | ||
formatter.currencyCode = currencyCode | ||
let formattedString = formatter.string(from: decimal as NSDecimalNumber) | ||
return currencyLocale?.currencySymbol == Locales.unitedStates.currencySymbol | ||
? formattedString?.appending(" \(currencyCode)") | ||
: formattedString | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters