diff --git a/Afterpay.xcodeproj/project.pbxproj b/Afterpay.xcodeproj/project.pbxproj index b5075b89..ed0c7591 100644 --- a/Afterpay.xcodeproj/project.pbxproj +++ b/Afterpay.xcodeproj/project.pbxproj @@ -22,6 +22,7 @@ 6672982A25357D80001D1C5A /* SVGConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6672982925357D80001D1C5A /* SVGConfiguration.swift */; }; 667AD3542497121200BF94E5 /* CheckoutWebViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 667AD3532497121100BF94E5 /* CheckoutWebViewController.swift */; }; 6689536C24C96CB5005090B4 /* Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6689536B24C96CB5005090B4 /* Configuration.swift */; }; + 66C3F7FB25397A810086DD0A /* CurrencyFormatterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66C3F7FA25397A810086DD0A /* CurrencyFormatterTests.swift */; }; 66D685B224BD3FB900C7287C /* SwiftUIWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66D685B124BD3FB900C7287C /* SwiftUIWrapper.swift */; }; 66DAAC8B24E0CF0100127460 /* PriceBreakdown.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66DAAC8A24E0CF0100127460 /* PriceBreakdown.swift */; }; 66DAAC8D24E109D200127460 /* PriceBreakdownTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66DAAC8C24E109D200127460 /* PriceBreakdownTests.swift */; }; @@ -71,6 +72,7 @@ 66B57E55248F5C7D0020C642 /* Project-Shared.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = "Project-Shared.xcconfig"; sourceTree = ""; }; 66B57E56248F5C7D0020C642 /* Project-Release.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = "Project-Release.xcconfig"; sourceTree = ""; }; 66B57E57248F5C7D0020C642 /* Afterpay-Release.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = "Afterpay-Release.xcconfig"; sourceTree = ""; }; + 66C3F7FA25397A810086DD0A /* CurrencyFormatterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyFormatterTests.swift; sourceTree = ""; }; 66D685B124BD3FB900C7287C /* SwiftUIWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUIWrapper.swift; sourceTree = ""; }; 66DAAC8A24E0CF0100127460 /* PriceBreakdown.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriceBreakdown.swift; sourceTree = ""; }; 66DAAC8C24E109D200127460 /* PriceBreakdownTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriceBreakdownTests.swift; sourceTree = ""; }; @@ -142,6 +144,7 @@ isa = PBXGroup; children = ( 6635B95E24CAA9F000EBB3A6 /* ConfigurationTests.swift */, + 66C3F7FA25397A810086DD0A /* CurrencyFormatterTests.swift */, 665FC57B2488766C00A5A93E /* Info.plist */, 66DAAC8C24E109D200127460 /* PriceBreakdownTests.swift */, ); @@ -418,6 +421,7 @@ files = ( 6635B95F24CAA9F000EBB3A6 /* ConfigurationTests.swift in Sources */, 66DAAC8D24E109D200127460 /* PriceBreakdownTests.swift in Sources */, + 66C3F7FB25397A810086DD0A /* CurrencyFormatterTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/AfterpayTests/CurrencyFormatterTests.swift b/AfterpayTests/CurrencyFormatterTests.swift new file mode 100644 index 00000000..32862570 --- /dev/null +++ b/AfterpayTests/CurrencyFormatterTests.swift @@ -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") + } + +} diff --git a/Sources/Afterpay/Model/CurrencyFormatter.swift b/Sources/Afterpay/Model/CurrencyFormatter.swift index 438a006e..46e1c17a 100644 --- a/Sources/Afterpay/Model/CurrencyFormatter.swift +++ b/Sources/Afterpay/Model/CurrencyFormatter.swift @@ -29,7 +29,7 @@ struct CurrencyFormatter { formatter.locale = currencyLocale formatter.currencyCode = currencyCode let formattedString = formatter.string(from: decimal as NSDecimalNumber) - return currencyLocale?.currencySymbol == locale.currencySymbol + return currencyLocale?.currencySymbol == Locales.unitedStates.currencySymbol ? formattedString?.appending(" \(currencyCode)") : formattedString }