Skip to content

Commit

Permalink
Test currency formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjcampbell committed Oct 16, 2020
1 parent 7333344 commit 34afe62
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Afterpay.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -71,6 +72,7 @@
66B57E55248F5C7D0020C642 /* Project-Shared.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = "Project-Shared.xcconfig"; sourceTree = "<group>"; };
66B57E56248F5C7D0020C642 /* Project-Release.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = "Project-Release.xcconfig"; sourceTree = "<group>"; };
66B57E57248F5C7D0020C642 /* Afterpay-Release.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = "Afterpay-Release.xcconfig"; sourceTree = "<group>"; };
66C3F7FA25397A810086DD0A /* CurrencyFormatterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyFormatterTests.swift; sourceTree = "<group>"; };
66D685B124BD3FB900C7287C /* SwiftUIWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUIWrapper.swift; sourceTree = "<group>"; };
66DAAC8A24E0CF0100127460 /* PriceBreakdown.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriceBreakdown.swift; sourceTree = "<group>"; };
66DAAC8C24E109D200127460 /* PriceBreakdownTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriceBreakdownTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -142,6 +144,7 @@
isa = PBXGroup;
children = (
6635B95E24CAA9F000EBB3A6 /* ConfigurationTests.swift */,
66C3F7FA25397A810086DD0A /* CurrencyFormatterTests.swift */,
665FC57B2488766C00A5A93E /* Info.plist */,
66DAAC8C24E109D200127460 /* PriceBreakdownTests.swift */,
);
Expand Down Expand Up @@ -418,6 +421,7 @@
files = (
6635B95F24CAA9F000EBB3A6 /* ConfigurationTests.swift in Sources */,
66DAAC8D24E109D200127460 /* PriceBreakdownTests.swift in Sources */,
66C3F7FB25397A810086DD0A /* CurrencyFormatterTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
104 changes: 104 additions & 0 deletions AfterpayTests/CurrencyFormatterTests.swift
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")
}

}
2 changes: 1 addition & 1 deletion Sources/Afterpay/Model/CurrencyFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 34afe62

Please sign in to comment.