From f44c868dfbeefd612511f2b3a75e6de1bd959ff7 Mon Sep 17 00:00:00 2001 From: Daniel Thorpe Date: Wed, 19 Oct 2016 21:45:44 +0100 Subject: [PATCH] 2.0.0 (#64) * Updates to Swift 3.0 (#63) * [swift/3.0]: Gets framework building in Swift 3.0 * [swift/3.0]: Swift 3.0 tests passing * [swift/3.0]: Gets the podspec validating * [release/2.0.0]: Updates the version to 2.0.0 --- .jazzy.yml | 6 +- .swift-version | 2 +- CHANGELOG.md | 3 + Cartfile | 2 +- Cartfile.resolved | 2 +- LICENSE | 2 +- Money.podspec | 4 +- Money.xcodeproj/project.pbxproj | 17 +- .../xcshareddata/xcschemes/Money.xcscheme | 3 +- Sources/ApplePay.swift | 48 +- Sources/Autogenerated.swift | 10 +- Sources/Currency.swift | 101 ++-- Sources/Decimal.swift | 12 +- Sources/DecimalNumberType.swift | 131 ++--- Sources/Locale.swift | 8 +- Sources/Money.swift | 53 +- Sources/NSDecimalExtensions.swift | 68 +-- Sources/NSDecimalNumberExtensions.swift | 84 +--- Sources/Support.swift | 38 -- Supporting Files/Generate.swift | 455 +++++++++--------- Supporting Files/Money.xcconfig | 2 +- Supporting Files/Version.xcconfig | 2 +- Tests/ApplePayTests.swift | 46 +- Tests/AutogeneratedTests.swift | 200 ++++---- Tests/DecimalNumberTypeTests.swift | 28 +- Tests/DecimalTests.swift | 22 +- Tests/LocaleTests.swift | 4 +- Tests/MoneyTests.swift | 75 +-- Tests/NSDecimalNumberTests.swift | 30 +- Tests/NSDecimalTests.swift | 38 +- 30 files changed, 656 insertions(+), 840 deletions(-) delete mode 100644 Sources/Support.swift diff --git a/.jazzy.yml b/.jazzy.yml index f96cac6..3a41142 100644 --- a/.jazzy.yml +++ b/.jazzy.yml @@ -1,13 +1,13 @@ author: Daniel Thorpe author_url: http://danthorpe.me module: Money -module_version: 1.8.0 +module_version: 2.0.0 github_url: https://github.com/danthorpe/Money readme: README.md podspec: Money.podspec -swift_version: 2.3 -xcodebuild-arguments: -scheme,Money-iOS +swift_version: 3.0 +xcodebuild-arguments: -scheme,Money custom_categories: - name: Decimals diff --git a/.swift-version b/.swift-version index bb576db..9f55b2c 100644 --- a/.swift-version +++ b/.swift-version @@ -1 +1 @@ -2.3 +3.0 diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fb813f..a259bb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.0 +1. [[MNY-63](https://github.com/danthorpe/Money/pull/63)]: Updates to Swift 3.0. + # 1.8.0 1. [[MNY-61](https://github.com/danthorpe/Money/pull/61)]: Updates to Swift 2.3. diff --git a/Cartfile b/Cartfile index 3bafc3a..56b3ea8 100644 --- a/Cartfile +++ b/Cartfile @@ -1 +1 @@ -github "danthorpe/ValueCoding" "1.5.0" +github "danthorpe/ValueCoding" "2.0.0" diff --git a/Cartfile.resolved b/Cartfile.resolved index 3bafc3a..56b3ea8 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1 +1 @@ -github "danthorpe/ValueCoding" "1.5.0" +github "danthorpe/ValueCoding" "2.0.0" diff --git a/LICENSE b/LICENSE index 3bd0a95..21088f9 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015 Daniel Thorpe +Copyright (c) 2015, 2016 Daniel Thorpe Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Money.podspec b/Money.podspec index c457cd5..5dce1ff 100644 --- a/Money.podspec +++ b/Money.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "Money" - s.version = "1.8.0" + s.version = "2.0.0" s.summary = "Swift types for working with Money." s.description = <<-DESC @@ -30,7 +30,7 @@ Pod::Spec.new do |s| s.watchos.exclude_files = [ 'Sources/ApplePay.swift' ] s.tvos.exclude_files = [ 'Sources/ApplePay.swift' ] - s.dependency 'ValueCoding', '1.5.0' + s.dependency 'ValueCoding', '~> 2' end diff --git a/Money.xcodeproj/project.pbxproj b/Money.xcodeproj/project.pbxproj index 3736e76..49d689f 100644 --- a/Money.xcodeproj/project.pbxproj +++ b/Money.xcodeproj/project.pbxproj @@ -41,7 +41,6 @@ 65599E9B1DB3D2D500EC123C /* Money.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65599E911DB3D2D500EC123C /* Money.swift */; }; 65599E9C1DB3D2D500EC123C /* NSDecimalExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65599E921DB3D2D500EC123C /* NSDecimalExtensions.swift */; }; 65599E9D1DB3D2D500EC123C /* NSDecimalNumberExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65599E931DB3D2D500EC123C /* NSDecimalNumberExtensions.swift */; }; - 65599E9E1DB3D2D500EC123C /* Support.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65599E941DB3D2D500EC123C /* Support.swift */; }; 65ECB2B11DB3DC9900F96F46 /* Money.h in Headers */ = {isa = PBXBuildFile; fileRef = 65599E6A1DB3CFD300EC123C /* Money.h */; settings = {ATTRIBUTES = (Public, ); }; }; /* End PBXBuildFile section */ @@ -91,7 +90,8 @@ 65599E911DB3D2D500EC123C /* Money.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Money.swift; sourceTree = ""; }; 65599E921DB3D2D500EC123C /* NSDecimalExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDecimalExtensions.swift; sourceTree = ""; }; 65599E931DB3D2D500EC123C /* NSDecimalNumberExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDecimalNumberExtensions.swift; sourceTree = ""; }; - 65599E941DB3D2D500EC123C /* Support.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Support.swift; sourceTree = ""; }; + 65ECB2BF1DB4134C00F96F46 /* Autogenerate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Autogenerate.h; sourceTree = ""; }; + 65ECB2C01DB4134C00F96F46 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -145,6 +145,7 @@ 65599E8A1DB3D2D500EC123C /* Sources */, 655155141DB3D59900368D35 /* Tests */, 655155131DB3D56400368D35 /* ... */, + 65ECB2BE1DB4134C00F96F46 /* Autogenerate */, ); sourceTree = ""; }; @@ -183,11 +184,19 @@ 65599E911DB3D2D500EC123C /* Money.swift */, 65599E921DB3D2D500EC123C /* NSDecimalExtensions.swift */, 65599E931DB3D2D500EC123C /* NSDecimalNumberExtensions.swift */, - 65599E941DB3D2D500EC123C /* Support.swift */, ); path = Sources; sourceTree = ""; }; + 65ECB2BE1DB4134C00F96F46 /* Autogenerate */ = { + isa = PBXGroup; + children = ( + 65ECB2BF1DB4134C00F96F46 /* Autogenerate.h */, + 65ECB2C01DB4134C00F96F46 /* Info.plist */, + ); + path = Autogenerate; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ @@ -314,6 +323,7 @@ runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; shellScript = "\"$SCRIPT_INPUT_FILE_0\" \"$SCRIPT_OUTPUT_FILE_0\" \"$SCRIPT_OUTPUT_FILE_1\""; + showEnvVarsInLog = 0; }; 65ECB2B21DB3DD9F00F96F46 /* Copy Carthage Frameworks */ = { isa = PBXShellScriptBuildPhase; @@ -339,7 +349,6 @@ buildActionMask = 2147483647; files = ( 65599E971DB3D2D500EC123C /* Currency.swift in Sources */, - 65599E9E1DB3D2D500EC123C /* Support.swift in Sources */, 65599E9D1DB3D2D500EC123C /* NSDecimalNumberExtensions.swift in Sources */, 65599E961DB3D2D500EC123C /* Bitcoin.swift in Sources */, 65599E9B1DB3D2D500EC123C /* Money.swift in Sources */, diff --git a/Money.xcodeproj/xcshareddata/xcschemes/Money.xcscheme b/Money.xcodeproj/xcshareddata/xcschemes/Money.xcscheme index 975a957..d4186bb 100644 --- a/Money.xcodeproj/xcshareddata/xcschemes/Money.xcscheme +++ b/Money.xcodeproj/xcshareddata/xcschemes/Money.xcscheme @@ -26,7 +26,8 @@ buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" - shouldUseLaunchSchemeArgsEnv = "YES"> + shouldUseLaunchSchemeArgsEnv = "YES" + codeCoverageEnabled = "YES"> diff --git a/Sources/ApplePay.swift b/Sources/ApplePay.swift index 6c1009e..f8c38e2 100644 --- a/Sources/ApplePay.swift +++ b/Sources/ApplePay.swift @@ -42,7 +42,7 @@ import ValueCoding - see: PKPaymentSummaryItemType */ public enum PaymentSummaryItemType: Int { - case Final = 1, Pending + case final = 1, pending } /** @@ -55,7 +55,7 @@ public enum PaymentSummaryItemType: Int { The money type must use `NSDecimalNumber` storage type, and correctly conform to `ValueCoding`. */ -public struct PaymentSummaryItem: Hashable, ValueCoding { +public struct PaymentSummaryItem: Hashable, ValueCoding where Cost.DecimalStorageType == NSDecimalNumber, Cost.Coder: NSCoding, Cost.Coder.Value == Cost { /// The ValueCoding Coder type public typealias Coder = PaymentSummaryItemCoder @@ -100,13 +100,13 @@ public struct PaymentSummaryItem PaymentSummaryItem { + public func set(label newLabel: String) -> PaymentSummaryItem { return PaymentSummaryItem(label: newLabel, cost: cost, type: type) } @@ -128,7 +128,7 @@ extension PaymentSummaryItem { - parameter newCost: the value for the `cost` property in an item copy - returns: a summary item with a new cost value, and previously set label and type. */ - public func setCost(newCost: Cost) -> PaymentSummaryItem { + public func set(cost newCost: Cost) -> PaymentSummaryItem { return PaymentSummaryItem(label: label, cost: newCost, type: type) } @@ -137,7 +137,7 @@ extension PaymentSummaryItem { - parameter newType: the value for the `type` property in an item copy - returns: a summary item with a new type value, and previously set label and cost. */ - public func setType(newType: PaymentSummaryItemType) -> PaymentSummaryItem { + public func set(type newType: PaymentSummaryItemType) -> PaymentSummaryItem { return PaymentSummaryItem(label: label, cost: cost, type: newType) } } @@ -145,7 +145,7 @@ extension PaymentSummaryItem { /** Coding adaptor for `PaymentSummaryItem`. */ -public final class PaymentSummaryItemCoder: NSObject, NSCoding, CodingType { +public final class PaymentSummaryItemCoder: NSObject, NSCoding, CodingProtocol where Cost.DecimalStorageType == NSDecimalNumber, Cost.Coder: NSCoding, Cost.Coder.Value == Cost { public let value: PaymentSummaryItem @@ -154,16 +154,16 @@ public final class PaymentSummaryItemCoder(paymentSummaryItem: PaymentSummaryItem) { + convenience init(paymentSummaryItem: PaymentSummaryItem) where Cost.DecimalStorageType == NSDecimalNumber { self.init() amount = paymentSummaryItem.amount label = paymentSummaryItem.label @@ -209,11 +209,11 @@ public extension PKPaymentRequest { - parameter sellerName: a `String` which is used in the total cost summary item. - returns: a `PKPaymentRequest` which has its payment summary items and currency code set. */ - convenience init(items: [PaymentSummaryItem], sellerName: String) { + convenience init(items: [PaymentSummaryItem], sellerName: String) where Cost.DecimalStorageType == NSDecimalNumber, Cost.Coder: NSCoding, Cost.Coder.Value == Cost { self.init() currencyCode = Cost.Currency.code var items = items - let total = items.map { $0.cost }.reduce(0, combine: +) + let total = items.map { $0.cost }.reduce(0, +) items.append(PaymentSummaryItem(label: sellerName, cost: total)) paymentSummaryItems = items.map { PKPaymentSummaryItem(paymentSummaryItem: $0) } } @@ -221,7 +221,7 @@ public extension PKPaymentRequest { // MARK: - Equality -public func ==(lhs: PaymentSummaryItem, rhs: PaymentSummaryItem) -> Bool { +public func ==(lhs: PaymentSummaryItem, rhs: PaymentSummaryItem) -> Bool where Cost.DecimalStorageType == NSDecimalNumber { return lhs.cost == rhs.cost && lhs.label == rhs.label && lhs.type == rhs.type } diff --git a/Sources/Autogenerated.swift b/Sources/Autogenerated.swift index d644a02..a214e6b 100644 --- a/Sources/Autogenerated.swift +++ b/Sources/Autogenerated.swift @@ -4693,10 +4693,10 @@ in more than one country, an associated value of the country should be provided. For example ```swift -let locale: Locale = .French(.France) +let locale: MNYLocale = .French(.France) ``` */ -public enum Locale { +public enum MNYLocale { /** ### Afrikaans @@ -5428,7 +5428,7 @@ public enum Locale { /** Locale conforms to LanguageType. */ -extension Locale: LanguageType { +extension MNYLocale: LanguageType { /// - returns: the lanauge identifier as a String. public var languageIdentifier: String { @@ -5826,7 +5826,7 @@ extension Locale: LanguageType { /** Locale conforms to CountryType. */ -extension Locale: CountryType { +extension MNYLocale: CountryType { /// - returns: the country identifier as a String. public var countryIdentifier: String { @@ -5935,6 +5935,6 @@ extension Locale: CountryType { } } -extension Locale: LocaleType { +extension MNYLocale: LocaleType { // Uses default implementation } diff --git a/Sources/Currency.swift b/Sources/Currency.swift index 530e414..bd7c1bb 100644 --- a/Sources/Currency.swift +++ b/Sources/Currency.swift @@ -51,17 +51,17 @@ public protocol CurrencyType: DecimalNumberBehaviorType { static var symbol: String? { get } /// Default formatting style - static var defaultFormattingStyle: NSNumberFormatterStyle { get } + static var defaultFormattingStyle: NumberFormatter.Style { get } - static func formattedWithStyle(style: NSNumberFormatterStyle, forLocaleId localeId: String) -> NSDecimalNumber -> String + static func formatted(withStyle: NumberFormatter.Style, forLocaleId localeId: String) -> (NSDecimalNumber) -> String - static func formattedWithStyle(style: NSNumberFormatterStyle, forLocale locale: Locale) -> NSDecimalNumber -> String + static func formatted(withStyle: NumberFormatter.Style, forLocale locale: MNYLocale) -> (NSDecimalNumber) -> String } public extension CurrencyType { - static var defaultFormattingStyle: NSNumberFormatterStyle { - return .CurrencyStyle + static var defaultFormattingStyle: NumberFormatter.Style { + return .currency } /** @@ -73,7 +73,7 @@ public extension CurrencyType { */ static var decimalNumberBehaviors: NSDecimalNumberBehaviors { return NSDecimalNumberHandler( - roundingMode: .RoundBankers, + roundingMode: .bankers, scale: Int16(scale), raiseOnExactness: true, raiseOnOverflow: true, @@ -82,40 +82,31 @@ public extension CurrencyType { ) } - static func formattedWithStyle(style: NSNumberFormatterStyle, forLocale tmp: NSLocale) -> NSDecimalNumber -> String { + /** + Use the provided locale identifier to format a supplied NSDecimalNumber. + + - returns: a NSDecimalNumber -> String closure. + */ + static func formatted(withStyle style: NumberFormatter.Style, forLocaleId localeId: String) -> (NSDecimalNumber) -> String { + let locale = NSLocale(localeIdentifier: NSLocale.canonicalLocaleIdentifier(from: localeId)) + return formatted(withStyle: style, forLocale: locale) + } + + static func formatted(withStyle style: NumberFormatter.Style, forLocale tmp: NSLocale) -> (NSDecimalNumber) -> String { let id = "\(tmp.localeIdentifier)@currency=\(code)" - let locale = NSLocale(localeIdentifier: NSLocale.canonicalLocaleIdentifierFromString(id)) + let canonical = NSLocale.canonicalLocaleIdentifier(from: id) + let nslocale = NSLocale(localeIdentifier: canonical) + let locale = Locale(identifier: canonical) - let formatter = NSNumberFormatter() + let formatter = NumberFormatter() formatter.currencyCode = code formatter.locale = locale formatter.numberStyle = style formatter.maximumFractionDigits = scale - formatter.currencySymbol = symbol ?? locale.mny_currencySymbol + formatter.currencySymbol = symbol ?? nslocale.mny_currencySymbol - return { formatter.stringFromNumber($0)! } - } -} - -/** - Custom currency types should refine CustomCurrencyType. - - This is to benefit from default implementations of string - formatting. -*/ -public protocol CustomCurrencyType: CurrencyType { } - -public extension CustomCurrencyType { - - /** - Use the provided locale identifier to format a supplied NSDecimalNumber. - - - returns: a NSDecimalNumber -> String closure. - */ - static func formattedWithStyle(style: NSNumberFormatterStyle, forLocaleId localeId: String) -> NSDecimalNumber -> String { - let locale = NSLocale(localeIdentifier: NSLocale.canonicalLocaleIdentifierFromString(localeId)) - return formattedWithStyle(style, forLocale: locale) + return { formatter.string(from: $0)! } } /** @@ -123,15 +114,18 @@ public extension CustomCurrencyType { - returns: a NSDecimalNumber -> String closure. */ - static func formattedWithStyle(style: NSNumberFormatterStyle, forLocale locale: Locale) -> NSDecimalNumber -> String { - return formattedWithStyle(style, forLocaleId: locale.localeIdentifier) + static func formatted(withStyle style: NumberFormatter.Style, forLocale locale: MNYLocale) -> (NSDecimalNumber) -> String { + return formatted(withStyle: style, forLocaleId: locale.localeIdentifier) } } /** - Crypto currency types (Bitcoin etc) should refine CryptoCurrencyType. + Custom currency types should refine CustomCurrencyType. +*/ +public protocol CustomCurrencyType: CurrencyType { } - This is to benefit from default implementations. +/** + Crypto currency types (Bitcoin etc) should refine CryptoCurrencyType. */ public protocol CryptoCurrencyType: CustomCurrencyType { } @@ -180,10 +174,10 @@ public extension ISOCurrencyType { - returns: a NSDecimalNumber -> String closure. */ - static func formattedWithStyle(style: NSNumberFormatterStyle, forLocaleId localeId: String) -> NSDecimalNumber -> String { - let id = "\(NSLocale.currentLocale().localeIdentifier)@currency=\(code)" - let locale = NSLocale(localeIdentifier: NSLocale.canonicalLocaleIdentifierFromString(id)) - return formattedWithStyle(style, forLocale: locale) + static func formatted(withStyle style: NumberFormatter.Style, forLocaleId localeId: String) -> (NSDecimalNumber) -> String { + let id = "\(NSLocale.current.identifier)@currency=\(code)" + let locale = NSLocale(localeIdentifier: NSLocale.canonicalLocaleIdentifier(from: id)) + return formatted(withStyle: style, forLocale: locale) } /** @@ -191,10 +185,10 @@ public extension ISOCurrencyType { - returns: a NSDecimalNumber -> String closure. */ - static func formattedWithStyle(style: NSNumberFormatterStyle, forLocale locale: Locale) -> NSDecimalNumber -> String { + static func formatted(withStyle style: NumberFormatter.Style, forLocale locale: MNYLocale) -> (NSDecimalNumber) -> String { let id = "\(locale.localeIdentifier)@currency=\(code)" - let locale = NSLocale(localeIdentifier: NSLocale.canonicalLocaleIdentifierFromString(id)) - return formattedWithStyle(style, forLocale: locale) + let locale = NSLocale(localeIdentifier: NSLocale.canonicalLocaleIdentifier(from: id)) + return formatted(withStyle: style, forLocale: locale) } } @@ -226,14 +220,17 @@ public struct Currency { } convenience init(code: String) { - let locale = NSLocale(localeIdentifier: NSLocale.canonicalLocaleIdentifierFromString(NSLocale.localeIdentifierFromComponents([NSLocaleCurrencyCode: code]))) - let symbol = locale.mny_currencySymbol! + let idFromComponents = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.currencyCode.rawValue: code]) + let canonical = NSLocale.canonicalLocaleIdentifier(from: idFromComponents) + let nslocale = NSLocale(localeIdentifier: canonical) + let locale = Locale(identifier: canonical) + let symbol = nslocale.mny_currencySymbol! - let fmtr = NSNumberFormatter() + let fmtr = NumberFormatter() fmtr.locale = locale - fmtr.numberStyle = .CurrencyStyle + fmtr.numberStyle = .currency fmtr.currencyCode = code - fmtr.currencySymbol = locale.mny_currencySymbol + fmtr.currencySymbol = nslocale.mny_currencySymbol let scale = fmtr.maximumFractionDigits self.init(code: code, scale: scale, symbol: symbol) @@ -243,9 +240,9 @@ public struct Currency { let code = locale.mny_currencyCode! let symbol = locale.mny_currencySymbol - let fmtr = NSNumberFormatter() - fmtr.numberStyle = .CurrencyStyle - fmtr.locale = locale + let fmtr = NumberFormatter() + fmtr.numberStyle = .currency + fmtr.locale = Locale(identifier: locale.localeIdentifier) fmtr.currencyCode = code let scale = fmtr.maximumFractionDigits @@ -261,6 +258,6 @@ public struct Currency { the device's current currency, using `NSLocale.currentLocale()`. */ public final class Local: Currency.Base, ISOCurrencyType { - public static var sharedInstance = Local(locale: NSLocale.currentLocale()) + public static var sharedInstance = Local(locale: NSLocale.current as NSLocale) } } diff --git a/Sources/Decimal.swift b/Sources/Decimal.swift index 7f0d201..6945f2e 100644 --- a/Sources/Decimal.swift +++ b/Sources/Decimal.swift @@ -47,7 +47,7 @@ public struct _Decimal: DecimalNumberType { - parameter storage: a `NSDecimalNumber` defaults to zero. */ - public init(storage: NSDecimalNumber = NSDecimalNumber.zero()) { + public init(storage: NSDecimalNumber = NSDecimalNumber.zero) { self.storage = storage } } @@ -65,7 +65,7 @@ public func <(lhs: _Decimal, rhs: _Decimal) } /// `Decimal` with plain decimal number behavior -public typealias Decimal = _Decimal +public typealias PlainDecimal = _Decimal /// `BankersDecimal` with banking decimal number behavior public typealias BankersDecimal = _Decimal @@ -79,7 +79,7 @@ extension _Decimal: ValueCoding { /** Coding class to support `_Decimal` `ValueCoding` conformance. */ -public final class _DecimalCoder: NSObject, NSCoding, CodingType { +public final class _DecimalCoder: NSObject, NSCoding, CodingProtocol { public let value: _Decimal @@ -88,12 +88,12 @@ public final class _DecimalCoder: NSObject, } public init?(coder aDecoder: NSCoder) { - let storage = aDecoder.decodeObjectForKey("storage") as! NSDecimalNumber + let storage = aDecoder.decodeObject(forKey: "storage") as! NSDecimalNumber value = _Decimal(storage: storage) } - public func encodeWithCoder(aCoder: NSCoder) { - aCoder.encodeObject(value.storage, forKey: "storage") + public func encode(with aCoder: NSCoder) { + aCoder.encode(value.storage, forKey: "storage") } } diff --git a/Sources/DecimalNumberType.swift b/Sources/DecimalNumberType.swift index f528205..d978273 100644 --- a/Sources/DecimalNumberType.swift +++ b/Sources/DecimalNumberType.swift @@ -52,25 +52,25 @@ public struct DecimalNumberBehavior { /// Plain rounding mode public struct Plain: DecimalNumberBehaviorType { - public static let decimalNumberBehaviors = DecimalNumberBehavior.behaviorWithRoundingMode(.RoundPlain) + public static let decimalNumberBehaviors = DecimalNumberBehavior.behavior(withMode: .plain) } /// Round down mode public struct RoundDown: DecimalNumberBehaviorType { - public static let decimalNumberBehaviors = DecimalNumberBehavior.behaviorWithRoundingMode(.RoundDown) + public static let decimalNumberBehaviors = DecimalNumberBehavior.behavior(withMode: .down) } /// Round up mode public struct RoundUp: DecimalNumberBehaviorType { - public static let decimalNumberBehaviors = DecimalNumberBehavior.behaviorWithRoundingMode(.RoundUp) + public static let decimalNumberBehaviors = DecimalNumberBehavior.behavior(withMode: .up) } /// Bankers rounding mode, see `NSRoundingMode.RoundBankers` for info. public struct Bankers: DecimalNumberBehaviorType { - public static let decimalNumberBehaviors = DecimalNumberBehavior.behaviorWithRoundingMode(.RoundBankers) + public static let decimalNumberBehaviors = DecimalNumberBehavior.behavior(withMode: .bankers) } - private static func behaviorWithRoundingMode(mode: NSRoundingMode, scale: Int16 = 38) -> NSDecimalNumberBehaviors { + private static func behavior(withMode mode: NSDecimalNumber.RoundingMode, scale: Int16 = 38) -> NSDecimalNumberBehaviors { return NSDecimalNumberHandler(roundingMode: mode, scale: 38, raiseOnExactness: false, raiseOnOverflow: true, raiseOnUnderflow: true, raiseOnDivideByZero: true) } } @@ -82,7 +82,7 @@ public struct DecimalNumberBehavior { A protocol which defines the necessary interface to support decimal number calculations and operators. */ -public protocol DecimalNumberType: Hashable, SignedNumberType, IntegerLiteralConvertible, FloatLiteralConvertible, CustomStringConvertible { +public protocol DecimalNumberType: Hashable, SignedNumber, ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral, CustomStringConvertible { associatedtype DecimalStorageType associatedtype DecimalNumberBehavior: DecimalNumberBehaviorType @@ -121,8 +121,7 @@ public protocol DecimalNumberType: Hashable, SignedNumberType, IntegerLiteralCon - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result - func subtract(_: Self) -> Self + func subtracting(_: Self) -> Self /** Add a matching `DecimalNumberType` to the receiver. @@ -130,8 +129,7 @@ public protocol DecimalNumberType: Hashable, SignedNumberType, IntegerLiteralCon - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result - func add(_: Self) -> Self + func adding(_: Self) -> Self /** Multiply the receive by 10^n @@ -139,8 +137,7 @@ public protocol DecimalNumberType: Hashable, SignedNumberType, IntegerLiteralCon - parameter n: an `Int` for the 10 power index - returns: another instance of this type. */ - @warn_unused_result - func multiplyByPowerOf10(_: Int) -> Self + func multiplying(byPowerOf10: Int16) -> Self /** Multiply a matching `DecimalNumberType` with the receiver. @@ -148,8 +145,7 @@ public protocol DecimalNumberType: Hashable, SignedNumberType, IntegerLiteralCon - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result - func multiplyBy(_: Self) -> Self + func multiplying(by: Self) -> Self /** Multiply another `DecimalNumberType` with the receiver. The other @@ -159,8 +155,7 @@ public protocol DecimalNumberType: Hashable, SignedNumberType, IntegerLiteralCon - parameter other: another `DecimalNumberType` value of different type. - returns: a different `DecimalNumberType` value. */ - @warn_unused_result - func multiplyBy(_: Other) -> Other + func multiplying(by: Other) -> Other where Other.DecimalStorageType == DecimalStorageType /** Divide the receiver by a matching `DecimalNumberType`. @@ -168,8 +163,7 @@ public protocol DecimalNumberType: Hashable, SignedNumberType, IntegerLiteralCon - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result - func divideBy(_: Self) -> Self + func dividing(by: Self) -> Self /** Divide the receiver by another `DecimalNumberType`. The other @@ -179,8 +173,7 @@ public protocol DecimalNumberType: Hashable, SignedNumberType, IntegerLiteralCon - parameter other: another `DecimalNumberType` value of different type. - returns: another instance of this type. */ - @warn_unused_result - func divideBy(_: Other) -> Other + func dividing(by: Other) -> Other where Other.DecimalStorageType == DecimalStorageType /** The remainder of dividing another `DecimalNumberType` into the receiver. @@ -188,7 +181,6 @@ public protocol DecimalNumberType: Hashable, SignedNumberType, IntegerLiteralCon - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result func remainder(_: Self) -> Self } @@ -208,12 +200,12 @@ public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber { /// The negative of Self. /// - returns: a `_Decimal` var negative: Self { - return Self(storage: storage.negateWithBehaviors(DecimalNumberBehavior.decimalNumberBehaviors)) + return Self(storage: storage.negate(withBehavior: DecimalNumberBehavior.decimalNumberBehaviors)) } /// Access an integer value representation var integerValue: Int { - return storage.integerValue + return storage.intValue } /// Access a float value representation @@ -237,11 +229,11 @@ public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber { init(_ value: Int) { switch value { case 0: - self.init(storage: NSDecimalNumber.zero()) + self.init(storage: NSDecimalNumber.zero) case 1: - self.init(storage: NSDecimalNumber.one()) + self.init(storage: NSDecimalNumber.one) default: - self.init(storage: NSDecimalNumber(integerLiteral: value).decimalNumberByRoundingAccordingToBehavior(DecimalNumberBehavior.decimalNumberBehaviors)) + self.init(storage: NSDecimalNumber(integerLiteral: value).rounding(accordingToBehavior: DecimalNumberBehavior.decimalNumberBehaviors)) } } @@ -314,7 +306,7 @@ public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber { /// - parameter value: an `Double`. /// - returns: an initialized `DecimalNumberType`. init(_ value: Double) { - self.init(storage: NSDecimalNumber(floatLiteral: value).decimalNumberByRoundingAccordingToBehavior(DecimalNumberBehavior.decimalNumberBehaviors)) + self.init(storage: NSDecimalNumber(floatLiteral: value).rounding(accordingToBehavior: DecimalNumberBehavior.decimalNumberBehaviors)) } /// Initialize a new decimal with a `Float`. @@ -339,9 +331,8 @@ public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber { - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result - func subtract(other: Self) -> Self { - return Self(storage: storage.subtract(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors)) + func subtracting(_ other: Self) -> Self { + return Self(storage: storage.subtracting(other.storage, withBehavior: DecimalNumberBehavior.decimalNumberBehaviors)) } /** @@ -350,9 +341,8 @@ public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber { - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result - func add(other: Self) -> Self { - return Self(storage: storage.add(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors)) + func adding(_ other: Self) -> Self { + return Self(storage: storage.adding(other.storage, withBehavior: DecimalNumberBehavior.decimalNumberBehaviors)) } /** @@ -361,9 +351,8 @@ public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber { - parameter n: an `Int` for the 10 power index - returns: another instance of this type. */ - @warn_unused_result - func multiplyByPowerOf10(index: Int) -> Self { - return Self(storage: storage.multiplyByPowerOf10(index, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors)) + func multiplying(byPowerOf10 index: Int16) -> Self { + return Self(storage: storage.multiplying(byPowerOf10: index, withBehavior: DecimalNumberBehavior.decimalNumberBehaviors)) } /** @@ -372,9 +361,8 @@ public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber { - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result - func multiplyBy(other: Self) -> Self { - return Self(storage: storage.multiplyBy(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors)) + func multiplying(by other: Self) -> Self { + return Self(storage: storage.multiplying(by: other.storage, withBehavior: DecimalNumberBehavior.decimalNumberBehaviors)) } /** @@ -384,9 +372,8 @@ public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber { - parameter other: another `DecimalNumberType` with `NSDecimalNumber` storage. - returns: another instance of this type. */ - @warn_unused_result - func multiplyBy(other: Other) -> Other { - return Other(storage: storage.multiplyBy(other.storage, withBehaviors: Other.DecimalNumberBehavior.decimalNumberBehaviors) ) + func multiplying(by other: Other) -> Other where Other.DecimalStorageType == NSDecimalNumber { + return Other(storage: storage.multiplying(by: other.storage, withBehavior: Other.DecimalNumberBehavior.decimalNumberBehaviors) ) } /** @@ -395,9 +382,8 @@ public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber { - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result - func divideBy(other: Self) -> Self { - return Self(storage: storage.divideBy(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors)) + func dividing(by other: Self) -> Self { + return Self(storage: storage.dividing(by: other.storage, withBehavior: DecimalNumberBehavior.decimalNumberBehaviors)) } /** @@ -407,9 +393,8 @@ public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber { - parameter other: another `DecimalNumberType` with `NSDecimalNumber` storage. - returns: another instance of this type. */ - @warn_unused_result - func divideBy(other: Other) -> Other { - return Other(storage: storage.divideBy(other.storage, withBehaviors: Other.DecimalNumberBehavior.decimalNumberBehaviors)) + func dividing(by other: Other) -> Other where Other.DecimalStorageType == NSDecimalNumber { + return Other(storage: storage.dividing(by: other.storage, withBehavior: Other.DecimalNumberBehavior.decimalNumberBehaviors)) } /** @@ -418,9 +403,8 @@ public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber { - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result - func remainder(other: Self) -> Self { - return Self(storage: storage.remainder(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors)) + func remainder(_ other: Self) -> Self { + return Self(storage: storage.remainder(other.storage, withBehavior: DecimalNumberBehavior.decimalNumberBehaviors)) } } @@ -428,7 +412,7 @@ extension DecimalNumberType where Self.IntegerLiteralType == Int { /// Get the reciprocal of the receiver. public var reciprocal: Self { - return Self(integerLiteral: 1).divideBy(self) + return Self(integerLiteral: 1).dividing(by: self) } } @@ -437,121 +421,94 @@ extension DecimalNumberType where Self.IntegerLiteralType == Int { // MARK: - Subtraction -@warn_unused_result public func -(lhs: T, rhs: T) -> T { - return lhs.subtract(rhs) + return lhs.subtracting(rhs) } -@warn_unused_result public func -(lhs: T, rhs: T.IntegerLiteralType) -> T { return lhs - T(integerLiteral: rhs) } -@warn_unused_result public func -(lhs: T.IntegerLiteralType, rhs: T) -> T { return T(integerLiteral: lhs) - rhs } -@warn_unused_result public func -(lhs: T, rhs: T.FloatLiteralType) -> T { return lhs - T(floatLiteral: rhs) } -@warn_unused_result public func -(lhs: T.FloatLiteralType, rhs: T) -> T { return T(floatLiteral: lhs) - rhs } // MARK: - Addition -@warn_unused_result public func +(lhs: T, rhs: T) -> T { - return lhs.add(rhs) + return lhs.adding(rhs) } -@warn_unused_result public func +(lhs: T, rhs: T.IntegerLiteralType) -> T { return lhs + T(integerLiteral: rhs) } -@warn_unused_result public func +(lhs: T.IntegerLiteralType, rhs: T) -> T { return T(integerLiteral: lhs) + rhs } -@warn_unused_result public func +(lhs: T, rhs: T.FloatLiteralType) -> T { return lhs + T(floatLiteral: rhs) } -@warn_unused_result public func +(lhs: T.FloatLiteralType, rhs: T) -> T { return T(floatLiteral: lhs) + rhs } // MARK: - Multiplication -@warn_unused_result public func *(lhs: T, rhs: T) -> T { - return lhs.multiplyBy(rhs) + return lhs.multiplying(by: rhs) } -@warn_unused_result public func *(lhs: T, rhs: T.IntegerLiteralType) -> T { return lhs * T(integerLiteral: rhs) } -@warn_unused_result public func *(lhs: T, rhs: T.FloatLiteralType) -> T { return lhs * T(floatLiteral: rhs) } -@warn_unused_result public func *(lhs: T.IntegerLiteralType, rhs: T) -> T { return rhs * lhs } -@warn_unused_result public func *(lhs: T.FloatLiteralType, rhs: T) -> T { return rhs * lhs } -@warn_unused_result -public func *(lhs: T, rhs: V) -> V { - return lhs.multiplyBy(rhs) +public func *(lhs: T, rhs: V) -> V where T: DecimalNumberType, V: DecimalNumberType, T.DecimalStorageType == V.DecimalStorageType { + return lhs.multiplying(by: rhs) } // MARK: - Division -@warn_unused_result public func /(lhs: T, rhs: T) -> T { - return lhs.divideBy(rhs) + return lhs.dividing(by: rhs) } -@warn_unused_result public func /(lhs: T, rhs: T.IntegerLiteralType) -> T { return lhs / T(integerLiteral: rhs) } -@warn_unused_result public func /(lhs: T, rhs: T.FloatLiteralType) -> T { return lhs / T(floatLiteral: rhs) } -@warn_unused_result -public func /(lhs: T, rhs: V) -> V { - return lhs.divideBy(rhs) +public func /(lhs: T, rhs: V) -> V where T: DecimalNumberType, V: DecimalNumberType, T.DecimalStorageType == V.DecimalStorageType { + return lhs.dividing(by: rhs) } // MARK: - Remainder -@warn_unused_result public func %(lhs: T, rhs: T) -> T { return lhs.remainder(rhs) } diff --git a/Sources/Locale.swift b/Sources/Locale.swift index 0068e4a..737643f 100644 --- a/Sources/Locale.swift +++ b/Sources/Locale.swift @@ -86,7 +86,7 @@ internal extension NSLocale { if #available(iOS 10.0, iOSApplicationExtension 10.0, watchOS 3.0, watchOSApplicationExtension 3.0, tvOS 10.0, tvOSApplicationExtension 10.0, OSX 10.12, OSXApplicationExtension 10.12, *) { return currencyCode } else { - return objectForKey(NSLocaleCurrencyCode) as? String + return object(forKey: NSLocale.Key.currencyCode) as? String } } @@ -95,18 +95,18 @@ internal extension NSLocale { if #available(iOS 10.0, iOSApplicationExtension 10.0, watchOS 3.0, watchOSApplicationExtension 3.0, tvOS 10.0, tvOSApplicationExtension 10.0, OSX 10.12, OSXApplicationExtension 10.12, *) { return currencySymbol } else { - return objectForKey(NSLocaleCurrencySymbol) as? String + return object(forKey: NSLocale.Key.currencySymbol) as? String } } /// - returns: a String? for the currency grouping separator. var mny_currencyGroupingSeparator: String? { - return objectForKey(NSLocaleGroupingSeparator) as? String + return object(forKey: NSLocale.Key.groupingSeparator) as? String } /// - returns: a String? for the currency decimal separator. var mny_currencyDecimalSeparator: String? { - return objectForKey(NSLocaleDecimalSeparator) as? String + return object(forKey: NSLocale.Key.decimalSeparator) as? String } } diff --git a/Sources/Money.swift b/Sources/Money.swift index 062e836..8a241a8 100644 --- a/Sources/Money.swift +++ b/Sources/Money.swift @@ -88,8 +88,8 @@ public extension MoneyType where DecimalStorageType == NSDecimalNumber { - parameter style: the `NSNumberFormatterStyle` to use. - returns: a localized and formatted string for the money amount. */ - func formattedWithStyle(style: NSNumberFormatterStyle) -> String { - return Currency.formattedWithStyle(style, forLocaleId: NSLocale.currentLocale().localeIdentifier)(amount) + func formatted(withStyle style: NumberFormatter.Style) -> String { + return Currency.formatted(withStyle: style, forLocaleId: NSLocale.current.identifier)(amount) } /** @@ -105,11 +105,11 @@ public extension MoneyType where DecimalStorageType == NSDecimalNumber { `.CurrencyAccountingStyle`. - parameter style: the `NSNumberFormatterStyle` to use. - - parameter locale: a `Locale` value + - parameter locale: a `MNYLocale` value - returns: a localized and formatted string for the money amount. */ - func formattedWithStyle(style: NSNumberFormatterStyle, forLocale locale: Locale) -> String { - return Currency.formattedWithStyle(style, forLocale: locale)(amount) + func formatted(withStyle style: NumberFormatter.Style, forLocale locale: MNYLocale) -> String { + return Currency.formatted(withStyle: style, forLocale: locale)(amount) } } @@ -123,9 +123,8 @@ public extension MoneyType where DecimalStorageType == BankersDecimal.DecimalSto - parameter rate: a `BankersDecimal` representing the rate. - returns: another `MoneyType` value. */ - @warn_unused_result - func convertWithRate(rate: BankersDecimal) -> Other { - return multiplyBy(Other(storage: rate.storage)) + func convert(withRate rate: BankersDecimal) -> Other where Other.DecimalStorageType == BankersDecimal.DecimalStorageType { + return multiplying(by: Other(storage: rate.storage)) } } @@ -151,7 +150,7 @@ public struct _Money: MoneyType { /// Access the underlying minor units /// - returns: the `IntegerLiteralType` minor units public var minorUnits: IntegerLiteralType { - return decimal.multiplyByPowerOf10(Currency.scale).integerValue + return decimal.multiplying(byPowerOf10: Int16(Currency.scale)).integerValue } /// Access the underlying decimal storage. @@ -186,7 +185,7 @@ public struct _Money: MoneyType { - parameter minorUnits: a `IntegerLiteralType` */ public init(minorUnits: IntegerLiteralType) { - decimal = _Decimal(integerLiteral: minorUnits).multiplyByPowerOf10(Currency.scale * -1) + decimal = _Decimal(integerLiteral: minorUnits).multiplying(byPowerOf10: Int16(Currency.scale * -1)) } /** @@ -223,9 +222,8 @@ public struct _Money: MoneyType { - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result - public func subtract(other: _Money) -> _Money { - return _Money(decimal.subtract(other.decimal)) + public func subtracting(_ other: _Money) -> _Money { + return _Money(decimal.subtracting(other.decimal)) } /** @@ -234,9 +232,8 @@ public struct _Money: MoneyType { - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result - public func add(other: _Money) -> _Money { - return _Money(decimal.add(other.decimal)) + public func adding(_ other: _Money) -> _Money { + return _Money(decimal.adding(other.decimal)) } /** @@ -245,10 +242,8 @@ public struct _Money: MoneyType { - parameter other: another instance of this type. - returns: another instance of this type. */ - - @warn_unused_result - public func multiplyBy(other: _Money) -> _Money { - return _Money(decimal.multiplyBy(other.decimal)) + public func multiplying(by other: _Money) -> _Money { + return _Money(decimal.multiplying(by: other.decimal)) } /** @@ -257,9 +252,8 @@ public struct _Money: MoneyType { - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result - public func divideBy(other: _Money) -> _Money { - return _Money(decimal.divideBy(other.decimal)) + public func dividing(by other: _Money) -> _Money { + return _Money(decimal.dividing(by: other.decimal)) } /** @@ -268,8 +262,7 @@ public struct _Money: MoneyType { - parameter other: another instance of this type. - returns: another instance of this type. */ - @warn_unused_result - public func remainder(other: _Money) -> _Money { + public func remainder(_ other: _Money) -> _Money { return _Money(decimal.remainder(other.decimal)) } } @@ -295,7 +288,7 @@ extension _Money: CustomStringConvertible { NSNumberFormatterStyle.CurrencyStyle. */ public var description: String { - return formattedWithStyle(C.defaultFormattingStyle) + return formatted(withStyle: C.defaultFormattingStyle) } } @@ -308,7 +301,7 @@ extension _Money: ValueCoding { /** Coding class to support `_Decimal` `ValueCoding` conformance. */ -public final class _MoneyCoder: NSObject, NSCoding, CodingType { +public final class _MoneyCoder: NSObject, NSCoding, CodingProtocol { public let value: _Money @@ -317,12 +310,12 @@ public final class _MoneyCoder: NSObject, NSCoding, CodingType } public init?(coder aDecoder: NSCoder) { - let decimal = _Decimal.decode(aDecoder.decodeObjectForKey("decimal")) + let decimal = _Decimal.decode(aDecoder.decodeObject(forKey: "decimal") as AnyObject?) value = _Money(decimal!) } - public func encodeWithCoder(aCoder: NSCoder) { - aCoder.encodeObject(value.decimal.encoded, forKey: "decimal") + public func encode(with aCoder: NSCoder) { + aCoder.encode(value.decimal.encoded, forKey: "decimal") } } diff --git a/Sources/NSDecimalExtensions.swift b/Sources/NSDecimalExtensions.swift index 68c1e04..7122c7f 100644 --- a/Sources/NSDecimalExtensions.swift +++ b/Sources/NSDecimalExtensions.swift @@ -27,29 +27,13 @@ import Foundation -// MARK: - Equality - -public func == (lhs: NSDecimal, rhs: NSDecimal) -> Bool { - var (lhs, rhs) = (lhs, rhs) - return NSDecimalCompare(&lhs, &rhs) == .OrderedSame -} - -// MARK: - Comparable - -extension NSDecimal: Comparable { } - -public func < (lhs: NSDecimal, rhs: NSDecimal) -> Bool { - var (lhs, rhs) = (lhs, rhs) - return NSDecimalCompare(&lhs, &rhs) == .OrderedAscending -} - /** - # NSDecimal Extension + # Decimal Extension - This is an extension on NSDecimal to support `DecimalNumberType` and - `Decimal`. + This is an extension on Decimal to support `DecimalNumberType` and + `_Decimal`. */ -internal extension NSDecimal { +internal extension Decimal { /** Static function to get the `NSDecimal` representation of @@ -57,8 +41,8 @@ internal extension NSDecimal { - returns: a `NSDecimal` of zero. */ - static func zero() -> NSDecimal { - return NSDecimalNumber.zero().decimalValue + static var zero: Decimal { + return NSDecimalNumber.zero.decimalValue } /** @@ -67,8 +51,8 @@ internal extension NSDecimal { - returns: a `NSDecimal` of one. */ - static func one() -> NSDecimal { - return NSDecimalNumber.one().decimalValue + static var one: Decimal { + return NSDecimalNumber.one.decimalValue } /** @@ -78,11 +62,11 @@ internal extension NSDecimal { - returns: a `Bool` if the value is below zero. */ var isNegative: Bool { - return self < NSDecimal.zero() + return self < Decimal.zero } internal init() { - self = NSDecimal.zero() + self = Decimal.zero } /** @@ -92,10 +76,9 @@ internal extension NSDecimal { - parameter roundingMode: the NSRoundingMode to use for the calculation. - returns: a `NSDecimal`. */ - @warn_unused_result - func subtract(rhs: NSDecimal, withRoundingMode roundingMode: NSRoundingMode) -> NSDecimal { + func subtracting(_ rhs: Decimal, withRoundingMode roundingMode: NSDecimalNumber.RoundingMode) -> Decimal { var (lhs, rhs) = (self, rhs) - var result = NSDecimal() + var result = Decimal() NSDecimalSubtract(&result, &lhs, &rhs, roundingMode) return result } @@ -107,10 +90,9 @@ internal extension NSDecimal { - parameter roundingMode: the NSRoundingMode to use for the calculation. - returns: a `NSDecimal`. */ - @warn_unused_result - func add(rhs: NSDecimal, withRoundingMode roundingMode: NSRoundingMode) -> NSDecimal { + func adding(_ rhs: Decimal, withRoundingMode roundingMode: NSDecimalNumber.RoundingMode) -> Decimal { var (lhs, rhs) = (self, rhs) - var result = NSDecimal() + var result = Decimal() NSDecimalAdd(&result, &lhs, &rhs, roundingMode) return result } @@ -122,10 +104,9 @@ internal extension NSDecimal { - parameter roundingMode: the NSRoundingMode to use for the calculation. - returns: a `NSDecimal`. */ - @warn_unused_result - func multiplyBy(rhs: NSDecimal, withRoundingMode roundingMode: NSRoundingMode) -> NSDecimal { + func multiplying(by rhs: Decimal, withRoundingMode roundingMode: NSDecimalNumber.RoundingMode) -> Decimal { var (lhs, rhs) = (self, rhs) - var result = NSDecimal() + var result = Decimal() NSDecimalMultiply(&result, &lhs, &rhs, roundingMode) return result } @@ -137,10 +118,9 @@ internal extension NSDecimal { - parameter roundingMode: the NSRoundingMode to use for the calculation. - returns: a `NSDecimal`. */ - @warn_unused_result - func divideBy(rhs: NSDecimal, withRoundingMode roundingMode: NSRoundingMode) -> NSDecimal { + func dividing(by rhs: Decimal, withRoundingMode roundingMode: NSDecimalNumber.RoundingMode) -> Decimal { var (lhs, rhs) = (self, rhs) - var result = NSDecimal() + var result = Decimal() NSDecimalDivide(&result, &lhs, &rhs, roundingMode) return result } @@ -151,9 +131,10 @@ internal extension NSDecimal { - parameter roundingMode: the NSRoundingMode to use for the calculation. - returns: a `NSDecimal`. */ - func negateWithRoundingMode(roundingMode: NSRoundingMode) -> NSDecimal { - let negativeOne = NSDecimal.zero().subtract(NSDecimal.one(), withRoundingMode: roundingMode) - let result = multiplyBy(negativeOne, withRoundingMode: roundingMode) + func negate(withRoundingMode roundingMode: NSDecimalNumber.RoundingMode) -> Decimal { + let zero = Decimal.zero + let negativeOne = zero.subtracting(Decimal.one, withRoundingMode: roundingMode) + let result = multiplying(by: negativeOne, withRoundingMode: roundingMode) return result } @@ -164,12 +145,11 @@ internal extension NSDecimal { - parameter roundingMode: the NSRoundingMode to use for the calculation. - returns: a `NSDecimal`. */ - @warn_unused_result - func remainder(_rhs: NSDecimal, withRoundingMode roundingMode: NSRoundingMode) -> NSDecimal { + func remainder(_ _rhs: Decimal, withRoundingMode roundingMode: NSDecimalNumber.RoundingMode) -> Decimal { let lhs = NSDecimalNumber(decimal: self) let rhs = NSDecimalNumber(decimal: _rhs) let behaviors = NSDecimalNumberHandler(roundingMode: roundingMode, scale: 38, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: true) - let result = lhs.remainder(rhs, withBehaviors: behaviors) + let result = lhs.remainder(rhs, withBehavior: behaviors) return result.decimalValue } } diff --git a/Sources/NSDecimalNumberExtensions.swift b/Sources/NSDecimalNumberExtensions.swift index bb6862b..e5b0dd3 100644 --- a/Sources/NSDecimalNumberExtensions.swift +++ b/Sources/NSDecimalNumberExtensions.swift @@ -30,7 +30,7 @@ import Foundation // MARK: - Equality public func ==(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> Bool { - return lhs.isEqualToNumber(rhs) + return lhs.isEqual(to: rhs) } // MARK: - Comparable @@ -38,7 +38,7 @@ public func ==(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> Bool { extension NSDecimalNumber: Comparable { } public func <(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> Bool { - return lhs.compare(rhs) == .OrderedAscending + return lhs.compare(rhs) == .orderedAscending } /** @@ -53,66 +53,7 @@ public func <(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> Bool { internal extension NSDecimalNumber { var isNegative: Bool { - return self < NSDecimalNumber.zero() - } - - /** - Subtract a `NSDecimalNumber` from the receiver. - - - parameter other: another `NSDecimalNumber`. - - parameter behaviors: an optional NSDecimalNumberBehaviors? - - returns: a `NSDecimalNumber`. - */ - @warn_unused_result - func subtract(other: NSDecimalNumber, withBehaviors behaviors: NSDecimalNumberBehaviors?) -> NSDecimalNumber { - return decimalNumberBySubtracting(other, withBehavior: behaviors) - } - - /** - Add a `NSDecimalNumber` to the receiver. - - - parameter other: another `NSDecimalNumber`. - - parameter behaviors: an optional NSDecimalNumberBehaviors? - - returns: a `NSDecimalNumber`. - */ - @warn_unused_result - func add(other: NSDecimalNumber, withBehaviors behaviors: NSDecimalNumberBehaviors?) -> NSDecimalNumber { - return decimalNumberByAdding(other, withBehavior: behaviors) - } - - /** - Multiply the receive by 10^n - - - parameter n: an `Int` for the 10 power index - - returns: another instance of this type. - */ - @warn_unused_result - func multiplyByPowerOf10(index: Int, withBehaviors behaviors: NSDecimalNumberBehaviors?) -> NSDecimalNumber { - return decimalNumberByMultiplyingByPowerOf10(Int16(index), withBehavior: behaviors) - } - - /** - Multiply a `NSDecimalNumber` with the receiver. - - - parameter other: another `NSDecimalNumber`. - - parameter behaviors: an optional NSDecimalNumberBehaviors? - - returns: a `NSDecimalNumber`. - */ - @warn_unused_result - func multiplyBy(other: NSDecimalNumber, withBehaviors behaviors: NSDecimalNumberBehaviors?) -> NSDecimalNumber { - return decimalNumberByMultiplyingBy(other, withBehavior: behaviors) - } - - /** - Divide the receiver by a matching `NSDecimalNumber`. - - - parameter other: another `NSDecimalNumber`. - - parameter behaviors: an optional NSDecimalNumberBehaviors? - - returns: a `NSDecimalNumber`. - */ - @warn_unused_result - func divideBy(other: NSDecimalNumber, withBehaviors behaviors: NSDecimalNumberBehaviors?) -> NSDecimalNumber { - return decimalNumberByDividingBy(other, withBehavior: behaviors) + return self < NSDecimalNumber.zero } /** @@ -121,9 +62,9 @@ internal extension NSDecimalNumber { - parameter behaviors: an optional NSDecimalNumberBehaviors? - returns: a `NSDecimalNumber`. */ - func negateWithBehaviors(behaviors: NSDecimalNumberBehaviors?) -> NSDecimalNumber { + func negate(withBehavior behavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber { let negativeOne = NSDecimalNumber(mantissa: 1, exponent: 0, isNegative: true) - let result = decimalNumberByMultiplyingBy(negativeOne, withBehavior: behaviors) + let result = multiplying(by: negativeOne, withBehavior: behavior) return result } @@ -134,14 +75,15 @@ internal extension NSDecimalNumber { - parameter behaviors: an optional NSDecimalNumberBehaviors? - returns: a `NSDecimalNumber`. */ - @warn_unused_result - func remainder(other: NSDecimalNumber, withBehaviors behaviors: NSDecimalNumberBehaviors?) -> NSDecimalNumber { - let roundingMode: NSRoundingMode = Int(isNegative) ^ Int(other.isNegative) ? .RoundUp : .RoundDown + func remainder(_ other: NSDecimalNumber, withBehavior behavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber { + let a: Int = (isNegative ? 0 : 1) + let b: Int = (other.isNegative ? 0 : 1) + let trueOrFalse = (a ^ b) > 0 ? true : false + let roundingMode: NSDecimalNumber.RoundingMode = trueOrFalse ? NSDecimalNumber.RoundingMode.up : NSDecimalNumber.RoundingMode.down let roundingBehaviors = NSDecimalNumberHandler(roundingMode: roundingMode, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false) - let quotient = divideBy(other, withBehaviors: roundingBehaviors) - let toSubtract = quotient.multiplyBy(other, withBehaviors: behaviors) - let result = subtract(toSubtract, withBehaviors: behaviors) - + let quotient = dividing(by: other, withBehavior: roundingBehaviors) + let toSubtract = quotient.multiplying(by: other, withBehavior: behavior) + let result = subtracting(toSubtract, withBehavior: behavior) return result } } diff --git a/Sources/Support.swift b/Sources/Support.swift deleted file mode 100644 index 6378cd6..0000000 --- a/Sources/Support.swift +++ /dev/null @@ -1,38 +0,0 @@ -// -// Money, https://github.com/danthorpe/Money -// Created by Dan Thorpe, @danthorpe -// -// The MIT License (MIT) -// -// Copyright (c) 2015 Daniel Thorpe -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -import Foundation - -extension Int: BooleanType { - - public var boolValue: Bool { - switch self { - case 0: return false - default: return true - } - } -} - diff --git a/Supporting Files/Generate.swift b/Supporting Files/Generate.swift index db80ab0..b94c36e 100755 --- a/Supporting Files/Generate.swift +++ b/Supporting Files/Generate.swift @@ -23,15 +23,16 @@ protocol TypeGenerator { extension TypeGenerator { var name: String { - return (displayName.capitalizedStringWithLocale(enUS) as NSString) - .stringByReplacingOccurrencesOfString(" ", withString: "") - .stringByReplacingOccurrencesOfString("-", withString: "") - .stringByReplacingOccurrencesOfString("ʼ", withString: "") - .stringByReplacingOccurrencesOfString(".", withString: "") - .stringByReplacingOccurrencesOfString("&", withString: "") - .stringByReplacingOccurrencesOfString("(", withString: "") - .stringByReplacingOccurrencesOfString(")", withString: "") - .stringByReplacingOccurrencesOfString("’", withString: "") + return displayName.capitalized(with: Locale(identifier: "en_US")) + .replacingOccurrences(of: " ", with: "") + .replacingOccurrences(of: " ", with: "") + .replacingOccurrences(of: "-", with: "") + .replacingOccurrences(of: "ʼ", with: "") + .replacingOccurrences(of: ".", with: "") + .replacingOccurrences(of: "&", with: "") + .replacingOccurrences(of: "(", with: "") + .replacingOccurrences(of: ")", with: "") + .replacingOccurrences(of: "’", with: "") } var caseNameValue: String { @@ -45,49 +46,49 @@ extension TypeGenerator { /// MARK: - Currency Info -func createMoneyTypeForCurrency(code: String) -> String { +func createMoneyType(forCurrencyCode code: String) -> String { return "_Money" } -func createExtensionFor(typename: String, writer: Writer, content: Generator) { +func createExtension(for typename: String, withWriter writer: Writer, content: Generator) { writer("extension \(typename) {") content(writer) writer("}") } -func createFrontMatter(line: Writer) { - line("// ") - line("// Money, https://github.com/danthorpe/Money") - line("// Created by Dan Thorpe, @danthorpe") - line("// ") - line("// The MIT License (MIT)") - line("// ") - line("// Copyright (c) 2015 Daniel Thorpe") - line("// ") - line("// Permission is hereby granted, free of charge, to any person obtaining a copy") - line("// of this software and associated documentation files (the \"Software\"), to deal") - line("// in the Software without restriction, including without limitation the rights") - line("// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell") - line("// copies of the Software, and to permit persons to whom the Software is") - line("// furnished to do so, subject to the following conditions:") - line("// ") - line("// The above copyright notice and this permission notice shall be included in all") - line("// copies or substantial portions of the Software.") - line("// ") - line("// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR") - line("// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,") - line("// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE") - line("// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER") - line("// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,") - line("// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE") - line("// SOFTWARE.") - line("// ") - line("// Autogenerated from build scripts, do not manually edit this file.") - line("") +func createFrontMatter(withWriter writer: Writer) { + writer("// ") + writer("// Money, https://github.com/danthorpe/Money") + writer("// Created by Dan Thorpe, @danthorpe") + writer("// ") + writer("// The MIT License (MIT)") + writer("// ") + writer("// Copyright (c) 2015 Daniel Thorpe") + writer("// ") + writer("// Permission is hereby granted, free of charge, to any person obtaining a copy") + writer("// of this software and associated documentation files (the \"Software\"), to deal") + writer("// in the Software without restriction, including without limitation the rights") + writer("// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell") + writer("// copies of the Software, and to permit persons to whom the Software is") + writer("// furnished to do so, subject to the following conditions:") + writer("// ") + writer("// The above copyright notice and this permission notice shall be included in all") + writer("// copies or substantial portions of the Software.") + writer("// ") + writer("// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR") + writer("// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,") + writer("// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE") + writer("// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER") + writer("// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,") + writer("// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE") + writer("// SOFTWARE.") + writer("// ") + writer("// Autogenerated from build scripts, do not manually edit this file.") + writer("") } func createCurrencyTypes(line: Writer) { - for code in NSLocale.ISOCurrencyCodes() { + for code in Locale.isoCurrencyCodes { line("") line(" /// Currency \(code)") line(" public final class \(code): Currency.Base, ISOCurrencyType {") @@ -97,13 +98,13 @@ func createCurrencyTypes(line: Writer) { } } -func createMoneyTypes(line: Writer) { - line("") +func createMoneyTypes(withWriter writer: Writer) { + writer("") - for code in NSLocale.ISOCurrencyCodes() { - line("/// \(code) Money") - let name = createMoneyTypeForCurrency(code) - line("public typealias \(code) = \(name)") + for code in Locale.isoCurrencyCodes { + writer("/// \(code) Money") + let name = createMoneyType(forCurrencyCode: code) + writer("public typealias \(code) = \(name)") } } @@ -121,7 +122,7 @@ struct Country: Comparable, TypeGenerator, CustomStringConvertible { init?(id: String) { self.id = id - guard let countryDisplayName = enUS.displayNameForKey(NSLocaleCountryCode, value: id) else { + guard let countryDisplayName = enUS.displayName(forKey: NSLocale.Key.countryCode, value: id) else { return nil } displayName = countryDisplayName @@ -145,7 +146,7 @@ struct Language: Comparable, TypeGenerator, CustomStringConvertible { init?(id: String) { self.id = id - guard let languageDisplayName = enUS.displayNameForKey(NSLocaleLanguageCode, value: id) else { + guard let languageDisplayName = enUS.displayName(forKey: NSLocale.Key.languageCode, value: id) else { return nil } displayName = languageDisplayName @@ -181,15 +182,15 @@ struct LocaleInfo { let languagesWithMoreThanOneCountry: [Language] init() { - let localeIDs = NSLocale.availableLocaleIdentifiers() + let localeIDs = NSLocale.availableLocaleIdentifiers var _countriesById = CountriesById() var _languagesById = LanguagesById() for id in localeIDs { let locale = NSLocale(localeIdentifier: id) - let countryId = locale.objectForKey(NSLocaleCountryCode) as? String + let countryId = locale.object(forKey: NSLocale.Key.countryCode) as? String let country: Country? = countryId.flatMap { _countriesById[$0] ?? Country(id: $0) } - let languageId = locale.objectForKey(NSLocaleLanguageCode) as? String + let languageId = locale.object(forKey: NSLocale.Key.languageCode) as? String let language: Language? = languageId.flatMap { _languagesById[$0] ?? Language(id: $0) } if let countryId = countryId, var language = language { @@ -206,277 +207,276 @@ struct LocaleInfo { self.languagesById = _languagesById self.countriesById = _countriesById - countries = ([Country])(countriesById.values).sort { $0.langaugeIds.count > $1.langaugeIds.count } - languages = ([Language])(languagesById.values).sort { $0.countryIds.count > $1.countryIds.count } + countries = ([Country])(countriesById.values).sorted { $0.langaugeIds.count > $1.langaugeIds.count } + languages = ([Language])(languagesById.values).sorted { $0.countryIds.count > $1.countryIds.count } - languagesWithLessThanTwoCountries = languages.filter({ $0.countryIds.count < 2 }).sort() - languagesWithMoreThanOneCountry = languages.filter({ $0.countryIds.count > 1 }).sort() + languagesWithLessThanTwoCountries = languages.filter({ $0.countryIds.count < 2 }).sorted() + languagesWithMoreThanOneCountry = languages.filter({ $0.countryIds.count > 1 }).sorted() } } let info = LocaleInfo() -func createLanguageSpeakingCountry(line: Writer, language: Language) { +func createLanguageSpeakingCountry(withWriter writer: Writer, language: Language) { let name = language.languageSpeakingCountryEnumName - line("") - line("/**") - line(" An enum of countries which speak \(language.displayName).") - line("*/") - line("public enum \(name): CountryType {") + writer("") + writer("/**") + writer(" An enum of countries which speak \(language.displayName).") + writer("*/") + writer("public enum \(name): CountryType {") - let _countries = language.countryIds.sort().flatMap({ info.countriesById[$0] }) + let _countries = language.countryIds.sorted().flatMap { info.countriesById[$0] } // Write the cases - line("") + writer("") for country in _countries { - line(" /// \(country.displayName) is a country which speaks \(language.displayName).") - line(" case \(country.name)") + writer(" /// \(country.displayName) is a country which speaks \(language.displayName).") + writer(" case \(country.name)") } // Write a static constant for all let caseNames = _countries.map { $0.caseNameValue } - let joinedCaseNames = caseNames.joinWithSeparator(", ") - line("") - line(" /// - returns: an Array of all the countries which speak \(language.displayName)") - line(" public static let all: [\(name)] = [ \(joinedCaseNames) ]") + let joinedCaseNames = caseNames.joined(separator: ", ") + writer("") + writer(" /// - returns: an Array of all the countries which speak \(language.displayName)") + writer(" public static let all: [\(name)] = [ \(joinedCaseNames) ]") - line("") - line(" /// - returns: the country identifier of a specific \(language.displayName) speaking country.") - line(" public var countryIdentifier: String {") - line(" switch self {") + writer("") + writer(" /// - returns: the country identifier of a specific \(language.displayName) speaking country.") + writer(" public var countryIdentifier: String {") + writer(" switch self {") for country in _countries { - line(" case .\(country.name):") - line(" return \"\(country.id)\"") + writer(" case .\(country.name):") + writer(" return \"\(country.id)\"") } - line(" }") // End of switch - line(" }") // End of var + writer(" }") // End of switch + writer(" }") // End of var - line("}") // End of enum + writer("}") // End of enum } -func createLanguageSpeakingCountries(line: Writer) { - line("") - line("// MARK: - Country Types") +func createLanguageSpeakingCountries(withWriter writer: Writer) { + writer("") + writer("// MARK: - Country Types") for language in info.languagesWithMoreThanOneCountry { - createLanguageSpeakingCountry(line, language: language) + createLanguageSpeakingCountry(withWriter: writer, language: language) } } -func createLocale(line: Writer) { - line("") - line("// MARK: - Locale") +func createLocale(withWriter writer: Writer) { + writer("") + writer("// MARK: - Locale") do { - line("") - line("/**") - line("") - line("Locale is an enum for type safe representation ") - line("of locale identifiers. Its cases are languages ") - line("in US English. For languages which are spoken ") - line("in more than one country, an associated value ") - line("of the country should be provided. For example ") - line("") - line("```swift") - line("let locale: Locale = .French(.France)") - line("```") - line("*/") - line("public enum Locale {") - - for language in info.languages.sort() { - line("") + writer("") + writer("/**") + writer("") + writer("Locale is an enum for type safe representation ") + writer("of locale identifiers. Its cases are languages ") + writer("in US English. For languages which are spoken ") + writer("in more than one country, an associated value ") + writer("of the country should be provided. For example ") + writer("") + writer("```swift") + writer("let locale: MNYLocale = .French(.France)") + writer("```") + writer("*/") + writer("public enum MNYLocale {") + + for language in info.languages.sorted() { + writer("") if language.countryIds.count > 1 { - line(" /**") - line(" ### \(language.displayName)") - line(" - requires: \(language.languageSpeakingCountryEnumName)") - line(" */") - line(" case \(language.name)(\(language.languageSpeakingCountryEnumName))") + writer(" /**") + writer(" ### \(language.displayName)") + writer(" - requires: \(language.languageSpeakingCountryEnumName)") + writer(" */") + writer(" case \(language.name)(\(language.languageSpeakingCountryEnumName))") } else { - line(" /// ### \(language.displayName)") - line(" case \(language.name)") + writer(" /// ### \(language.displayName)") + writer(" case \(language.name)") } } - line("}") // End of enum + writer("}") // End of enum } // Add extension for LanguageType protocol do { - line("") - line("/**") - line(" Locale conforms to LanguageType.") - line("*/") - line("extension Locale: LanguageType {") - line("") - line(" /// - returns: the lanauge identifier as a String.") - line(" public var languageIdentifier: String {") - line(" switch self {") - - for language in info.languages.sort() { + writer("") + writer("/**") + writer(" Locale conforms to LanguageType.") + writer("*/") + writer("extension MNYLocale: LanguageType {") + writer("") + writer(" /// - returns: the lanauge identifier as a String.") + writer(" public var languageIdentifier: String {") + writer(" switch self {") + + for language in info.languages.sorted() { if language.countryIds.count > 1 { - line(" case .\(language.name)(_):") - line(" return \"\(language.id)\"") + writer(" case .\(language.name)(_):") + writer(" return \"\(language.id)\"") } else { - line(" case .\(language.name):") - line(" return \"\(language.id)\"") + writer(" case .\(language.name):") + writer(" return \"\(language.id)\"") } } - line(" }") // End of switch - line(" }") // End of var - line("}") // End of extension + writer(" }") // End of switch + writer(" }") // End of var + writer("}") // End of extension } // Add extension for CountryType protocol do { - line("") - line("/**") - line(" Locale conforms to CountryType.") - line("*/") - line("extension Locale: CountryType {") - line("") - line(" /// - returns: the country identifier as a String.") - line(" public var countryIdentifier: String {") - line(" switch self {") + writer("") + writer("/**") + writer(" Locale conforms to CountryType.") + writer("*/") + writer("extension MNYLocale: CountryType {") + writer("") + writer(" /// - returns: the country identifier as a String.") + writer(" public var countryIdentifier: String {") + writer(" switch self {") let caseNames = info.languagesWithLessThanTwoCountries.map { $0.caseNameValue } - let joinedCaseNames = caseNames.joinWithSeparator(", ") - line(" case \(joinedCaseNames):") - line(" return \"\"") + let joinedCaseNames = caseNames.joined(separator: ", ") + writer(" case \(joinedCaseNames):") + writer(" return \"\"") for language in info.languagesWithMoreThanOneCountry { - line(" case .\(language.name)(let country):") - line(" return country.countryIdentifier") + writer(" case .\(language.name)(let country):") + writer(" return country.countryIdentifier") } - line(" }") // End of switch - line(" }") // End of var - line("}") // End of extension + writer(" }") // End of switch + writer(" }") // End of var + writer("}") // End of extension } // Add extension for LocaleType protocol do { - line("") - line("extension Locale: LocaleType {") - line(" // Uses default implementation") - line("}") // End of extension + writer("") + writer("extension MNYLocale: LocaleType {") + writer(" // Uses default implementation") + writer("}") // End of extension } } -func createLocaleTypes(line: Writer) { +func createLocaleTypes(withWriter writer: Writer) { // Create the (Language)SpeakingCountry enum types - createLanguageSpeakingCountries(line) + createLanguageSpeakingCountries(withWriter: writer) // Create the Locale enum - createLocale(line) - + createLocale(withWriter: writer) } // MARK: - Unit Tests -func createUnitTestImports(line: Writer) { - line("import XCTest") - line("@testable import Money") +func createUnitTestImports(withWriter writer: Writer) { + writer("import XCTest") + writer("@testable import Money") } -func createXCTestCaseNamed(line: Writer, className: String, content: Generator) { - line("") - line("class \(className)AutogeneratedTests: XCTestCase {") - content(line) - line("}") +func createXCTestCaseNamed(withWriter writer: Writer, className: String, content: Generator) { + writer("") + writer("class \(className)AutogeneratedTests: XCTestCase {") + content(writer) + writer("}") } -func createTestForCountryIdentifierFromCountryCaseName(line: Writer, country: Country) { - line("") - line(" func test__country_identifier_for_\(country.name)() {") - line(" country = .\(country.name)") - line(" XCTAssertEqual(country.countryIdentifier, \"\(country.id)\")") - line(" }") +func createTestForCountryIdentifierFromCountryCaseName(withWriter writer: Writer, country: Country) { + writer("") + writer(" func test__country_identifier_for_\(country.name)() {") + writer(" country = .\(country.name)") + writer(" XCTAssertEqual(country.countryIdentifier, \"\(country.id)\")") + writer(" }") } -func createUnitTestsForLanguageSpeakingCountry(line: Writer, language: Language) { +func createUnitTestsForLanguageSpeakingCountry(withWriter writer: Writer, language: Language) { let name = language.languageSpeakingCountryEnumName - createXCTestCaseNamed(line, className: name) { line in - line("") - line(" var country: \(name)!") + createXCTestCaseNamed(withWriter: writer, className: name) { line in + writer("") + writer(" var country: \(name)!") for country in language.countryIds.flatMap({ info.countriesById[$0] }) { - createTestForCountryIdentifierFromCountryCaseName(line, country: country) + createTestForCountryIdentifierFromCountryCaseName(withWriter: writer, country: country) } } } -func createUnitTestsForLanguageSpeakingCountries(line: Writer) { - line("") - line("// MARK: - Country Types Tests") +func createUnitTestsForLanguageSpeakingCountries(withWriter writer: Writer) { + writer("") + writer("// MARK: - Country Types Tests") for language in info.languagesWithMoreThanOneCountry { - createUnitTestsForLanguageSpeakingCountry(line, language: language) + createUnitTestsForLanguageSpeakingCountry(withWriter: writer, language: language) } } -func createTestForLanguageIdentifier(line: Writer, language: Language, country: Country? = .None) { - line("") +func createTestForLanguageIdentifier(withWriter writer: Writer, language: Language, country: Country? = nil) { + writer("") if let country = country { - line(" func test__language_identifier_for_\(language.name)_\(country.name)() {") - line(" locale = .\(language.name)(\(country.caseNameValue))") - line(" XCTAssertEqual(locale.languageIdentifier, \"\(language.id)\")") - line(" XCTAssertEqual(locale.localeIdentifier, \"\(language.id)_\(country.id)\")") - line(" }") + writer(" func test__language_identifier_for_\(language.name)_\(country.name)() {") + writer(" locale = .\(language.name)(\(country.caseNameValue))") + writer(" XCTAssertEqual(locale.languageIdentifier, \"\(language.id)\")") + writer(" XCTAssertEqual(locale.localeIdentifier, \"\(language.id)_\(country.id)\")") + writer(" }") } else { - line(" func test__language_identifier_for_\(language.name)() {") - line(" locale = .\(language.name)") - line(" XCTAssertEqual(locale.languageIdentifier, \"\(language.id)\")") - line(" XCTAssertEqual(locale.localeIdentifier, \"\(language.id)\")") - line(" }") + writer(" func test__language_identifier_for_\(language.name)() {") + writer(" locale = .\(language.name)") + writer(" XCTAssertEqual(locale.languageIdentifier, \"\(language.id)\")") + writer(" XCTAssertEqual(locale.localeIdentifier, \"\(language.id)\")") + writer(" }") } } -func createUnitTestsForLocaleWithLanguage(line: Writer, language: Language) { +func createUnitTestsForLocaleWithLanguage(withWriter writer: Writer, language: Language) { if language.countryIds.count < 2 { - createTestForLanguageIdentifier(line, language: language) + createTestForLanguageIdentifier(withWriter: writer, language: language) } else { for country in language.countryIds.flatMap({ info.countriesById[$0] }) { - createTestForLanguageIdentifier(line, language: language, country: country) + createTestForLanguageIdentifier(withWriter: writer, language: language, country: country) } } } -func createUnitTestsForLocale(line: Writer) { - line("") - line("// MARK: - Locale Tests") +func createUnitTestsForLocale(withWriter writer: Writer) { + writer("") + writer("// MARK: - Locale Tests") for language in info.languagesWithMoreThanOneCountry { - createXCTestCaseNamed(line, className: "Locale\(language.name)Language") { line in - line("") - line(" var locale: Locale!") + createXCTestCaseNamed(withWriter: writer, className: "MNYLocale\(language.name)Language") { writer in + writer("") + writer(" var locale: MNYLocale!") - createUnitTestsForLocaleWithLanguage(line, language: language) + createUnitTestsForLocaleWithLanguage(withWriter: writer, language: language) } } - createXCTestCaseNamed(line, className: "Locale") { line in - line("") - line(" var locale: Locale!") + createXCTestCaseNamed(withWriter: writer, className: "MNYLocale") { line in + writer("") + writer(" var locale: MNYLocale!") for language in info.languagesWithLessThanTwoCountries { - createUnitTestsForLocaleWithLanguage(line, language: language) + createUnitTestsForLocaleWithLanguage(withWriter: writer, language: language) } } } // MARK: - Generators -func generateSourceCode(outputPath: String) { +func generateSourceCode(to outputPath: String) { - guard let outputStream = NSOutputStream(toFileAtPath: outputPath, append: false) else { + guard let outputStream = OutputStream(toFileAtPath: outputPath, append: false) else { fatalError("Unable to create output stream at path: \(outputPath)") } @@ -485,28 +485,28 @@ func generateSourceCode(outputPath: String) { } let write: Writer = { str in - guard let data = str.dataUsingEncoding(NSUTF8StringEncoding) else { + guard let data = str.data(using: String.Encoding.utf8) else { fatalError("Unable to encode str: \(str)") } - outputStream.write(UnsafePointer(data.bytes), maxLength: data.length) + let _ = data.withUnsafeBytes { outputStream.write($0, maxLength: data.count) } } let writeLine: Writer = { write("\($0)\n") } outputStream.open() - createFrontMatter(writeLine) + createFrontMatter(withWriter: writeLine) - createExtensionFor("Currency", writer: writeLine, content: createCurrencyTypes) + createExtension(for: "Currency", withWriter: writeLine, content: createCurrencyTypes) write("\n") - createMoneyTypes(writeLine) + createMoneyTypes(withWriter: writeLine) write("\n") - createLocaleTypes(writeLine) + createLocaleTypes(withWriter: writeLine) } -func generateUnitTests(outputPath: String) { +func generateUnitTests(to outputPath: String) { - guard let outputStream = NSOutputStream(toFileAtPath: outputPath, append: false) else { + guard let outputStream = OutputStream(toFileAtPath: outputPath, append: false) else { fatalError("Unable to create output stream at path: \(outputPath)") } @@ -515,36 +515,31 @@ func generateUnitTests(outputPath: String) { } let write: Writer = { str in - guard let data = str.dataUsingEncoding(NSUTF8StringEncoding) else { + guard let data = str.data(using: String.Encoding.utf8) else { fatalError("Unable to encode str: \(str)") } - outputStream.write(UnsafePointer(data.bytes), maxLength: data.length) + let _ = data.withUnsafeBytes { outputStream.write($0, maxLength: data.count) } } let writeLine: Writer = { write("\($0)\n") } outputStream.open() - createFrontMatter(writeLine) - createUnitTestImports(writeLine) + createFrontMatter(withWriter: writeLine) + + createUnitTestImports(withWriter: writeLine) - createUnitTestsForLanguageSpeakingCountries(writeLine) + createUnitTestsForLanguageSpeakingCountries(withWriter: writeLine) - createUnitTestsForLocale(writeLine) + createUnitTestsForLocale(withWriter: writeLine) } // MARK: - Main() +let process = Process() -print(Process.arguments) - -if Process.arguments.count < 1 { - print("Invalid usage. Requires an output path.") - exit(1) -} - -let pathToSourceCodeFile = Process.arguments[1] -generateSourceCode(pathToSourceCodeFile) +let pathToSourceCodeFile = "\(process.currentDirectoryPath)/Sources/Autogenerated.swift" +generateSourceCode(to: pathToSourceCodeFile) -let pathToUnitTestsFile = Process.arguments[2] -generateUnitTests(pathToUnitTestsFile) +let pathToUnitTestsFile = "\(process.currentDirectoryPath)/Tests/AutogeneratedTests.swift" +generateUnitTests(to: pathToUnitTestsFile) diff --git a/Supporting Files/Money.xcconfig b/Supporting Files/Money.xcconfig index b929dd0..2c59830 100644 --- a/Supporting Files/Money.xcconfig +++ b/Supporting Files/Money.xcconfig @@ -23,7 +23,7 @@ APPLICATION_EXTENSION_API_ONLY_framework = YES APPLICATION_EXTENSION_API_ONLY_xctest = NO APPLICATION_EXTENSION_API_ONLY = $(APPLICATION_EXTENSION_API_ONLY_$(WRAPPER_EXTENSION)) -SWIFT_VERSION = 2.3 +SWIFT_VERSION = 3.0 // Build Settings SUPPORTED_PLATFORMS = macosx iphoneos appletvos watchos appletvsimulator iphonesimulator watchsimulator diff --git a/Supporting Files/Version.xcconfig b/Supporting Files/Version.xcconfig index 89cfafe..02b7324 100644 --- a/Supporting Files/Version.xcconfig +++ b/Supporting Files/Version.xcconfig @@ -1 +1 @@ -MONEY_VERSION = 1.8.0 +MONEY_VERSION = 2.0.0 diff --git a/Tests/ApplePayTests.swift b/Tests/ApplePayTests.swift index f2a2b56..5151905 100644 --- a/Tests/ApplePayTests.swift +++ b/Tests/ApplePayTests.swift @@ -37,7 +37,7 @@ class ApplePayTests: XCTestCase { override func setUp() { super.setUp() - item = PaymentSummaryItem(label: "iPad Pro, 32GB with WiFi", cost: 679, type: .Final) + item = PaymentSummaryItem(label: "iPad Pro, 32GB with WiFi", cost: 679, type: .final) items.insert(item) } } @@ -53,44 +53,44 @@ class PaymentSummaryItemTests: ApplePayTests { } func test__init__sets_type() { - XCTAssertEqual(item.type, PaymentSummaryItemType.Final) + XCTAssertEqual(item.type, PaymentSummaryItemType.final) } func test__set_new_money__sets_money() { - item = item.setCost(799) + item = item.set(cost: 799) XCTAssertEqual(item.cost, 799) XCTAssertEqual(item.label, "iPad Pro, 32GB with WiFi") - XCTAssertEqual(item.type, PaymentSummaryItemType.Final) + XCTAssertEqual(item.type, PaymentSummaryItemType.final) } func test__set_new_label__sets_label() { - item = item.setLabel("iPad Pro, 128GB with WiFi") + item = item.set(label: "iPad Pro, 128GB with WiFi") XCTAssertEqual(item.cost, 679) XCTAssertEqual(item.label, "iPad Pro, 128GB with WiFi") - XCTAssertEqual(item.type, PaymentSummaryItemType.Final) + XCTAssertEqual(item.type, PaymentSummaryItemType.final) } func test__set_new_type__sets_type() { - item = item.setType(.Pending) + item = item.set(type: .pending) XCTAssertEqual(item.cost, 0) XCTAssertEqual(item.label, "iPad Pro, 32GB with WiFi") - XCTAssertEqual(item.type, PaymentSummaryItemType.Pending) + XCTAssertEqual(item.type, PaymentSummaryItemType.pending) } func test__equality() { - XCTAssertEqual(item, PaymentSummaryItem(label: "iPad Pro, 32GB with WiFi", cost: 679, type: .Final)) - XCTAssertNotEqual(item, PaymentSummaryItem(label: "iPad Pro, 128GB with WiFi", cost: 799, type: .Final)) + XCTAssertEqual(item, PaymentSummaryItem(label: "iPad Pro, 32GB with WiFi", cost: 679, type: .final)) + XCTAssertNotEqual(item, PaymentSummaryItem(label: "iPad Pro, 128GB with WiFi", cost: 799, type: .final)) } } class PaymentSummaryItemCodingTests: ApplePayTests { - func archiveEncoded() -> NSData { - return NSKeyedArchiver.archivedDataWithRootObject(item.encoded) + func archiveEncoded() -> Data { + return NSKeyedArchiver.archivedData(withRootObject: item.encoded) } - func unarchive(archive: NSData) -> PaymentSummaryItem? { - return PaymentSummaryItem.decode(NSKeyedUnarchiver.unarchiveObjectWithData(archive)) + func unarchive(_ archive: Data) -> PaymentSummaryItem? { + return PaymentSummaryItem.decode(NSKeyedUnarchiver.unarchiveObject(with: archive) as AnyObject?) } func test__encode_decode() { @@ -101,13 +101,13 @@ class PaymentSummaryItemCodingTests: ApplePayTests { class PKPaymentSummaryItemTypeTests: ApplePayTests { func test__init__final() { - let type = PKPaymentSummaryItemType(paymentSummaryItemType: .Final) - XCTAssertEqual(type, PKPaymentSummaryItemType.Final) + let type = PKPaymentSummaryItemType(paymentSummaryItemType: .final) + XCTAssertEqual(type, PKPaymentSummaryItemType.final) } func test__init__pending() { - let type = PKPaymentSummaryItemType(paymentSummaryItemType: .Pending) - XCTAssertEqual(type, PKPaymentSummaryItemType.Pending) + let type = PKPaymentSummaryItemType(paymentSummaryItemType: .pending) + XCTAssertEqual(type, PKPaymentSummaryItemType.pending) } } @@ -115,23 +115,23 @@ class PKPaymentSummaryItemTests: ApplePayTests { func test__init__with_item() { let summaryItem = PKPaymentSummaryItem(paymentSummaryItem: item) - XCTAssertEqual(summaryItem.amount, NSDecimalNumber(integer: 679)) + XCTAssertEqual(summaryItem.amount, NSDecimalNumber(value: 679)) XCTAssertEqual(summaryItem.label, "iPad Pro, 32GB with WiFi") - XCTAssertEqual(summaryItem.type, PKPaymentSummaryItemType.Final) + XCTAssertEqual(summaryItem.type, PKPaymentSummaryItemType.final) } } class PKPaymentRequestTests: ApplePayTests { func test__init__with_items() { - items.insert(PaymentSummaryItem(label: "iPad Pro, 128GB with WiFi", cost: 799, type: .Final)) - items.insert(PaymentSummaryItem(label: "iPad Pro, 128GB with WiFi + Cellular", cost: 899, type: .Final)) + items.insert(PaymentSummaryItem(label: "iPad Pro, 128GB with WiFi", cost: 799, type: .final)) + items.insert(PaymentSummaryItem(label: "iPad Pro, 128GB with WiFi + Cellular", cost: 899, type: .final)) let request = PKPaymentRequest(items: Array(items), sellerName: "Acme. Inc") XCTAssertEqual(request.currencyCode, GBP.Currency.code) XCTAssertEqual(request.paymentSummaryItems.count, 4) XCTAssertEqual(request.paymentSummaryItems.last!.label, "Acme. Inc") - XCTAssertEqual(request.paymentSummaryItems.last!.amount, items.map { $0.cost }.reduce(0, combine: +).amount) + XCTAssertEqual(request.paymentSummaryItems.last!.amount, items.map { $0.cost }.reduce(0, +).amount) } } diff --git a/Tests/AutogeneratedTests.swift b/Tests/AutogeneratedTests.swift index 844eab0..b79ceda 100644 --- a/Tests/AutogeneratedTests.swift +++ b/Tests/AutogeneratedTests.swift @@ -2118,9 +2118,9 @@ class YorubaSpeakingCountryAutogeneratedTests: XCTestCase { // MARK: - Locale Tests -class LocaleAfrikaansLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleAfrikaansLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Afrikaans_Namibia() { locale = .Afrikaans(.Namibia) @@ -2135,9 +2135,9 @@ class LocaleAfrikaansLanguageAutogeneratedTests: XCTestCase { } } -class LocaleAlbanianLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleAlbanianLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Albanian_Macedonia() { locale = .Albanian(.Macedonia) @@ -2158,9 +2158,9 @@ class LocaleAlbanianLanguageAutogeneratedTests: XCTestCase { } } -class LocaleArabicLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleArabicLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Arabic_Somalia() { locale = .Arabic(.Somalia) @@ -2331,9 +2331,9 @@ class LocaleArabicLanguageAutogeneratedTests: XCTestCase { } } -class LocaleBengaliLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleBengaliLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Bengali_Bangladesh() { locale = .Bengali(.Bangladesh) @@ -2348,9 +2348,9 @@ class LocaleBengaliLanguageAutogeneratedTests: XCTestCase { } } -class LocaleCatalanLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleCatalanLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Catalan_Italy() { locale = .Catalan(.Italy) @@ -2377,9 +2377,9 @@ class LocaleCatalanLanguageAutogeneratedTests: XCTestCase { } } -class LocaleCentralKurdishLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleCentralKurdishLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_CentralKurdish_Iraq() { locale = .CentralKurdish(.Iraq) @@ -2394,9 +2394,9 @@ class LocaleCentralKurdishLanguageAutogeneratedTests: XCTestCase { } } -class LocaleChineseLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleChineseLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Chinese_Taiwan() { locale = .Chinese(.Taiwan) @@ -2429,9 +2429,9 @@ class LocaleChineseLanguageAutogeneratedTests: XCTestCase { } } -class LocaleCroatianLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleCroatianLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Croatian_Croatia() { locale = .Croatian(.Croatia) @@ -2446,9 +2446,9 @@ class LocaleCroatianLanguageAutogeneratedTests: XCTestCase { } } -class LocaleDanishLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleDanishLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Danish_Denmark() { locale = .Danish(.Denmark) @@ -2463,9 +2463,9 @@ class LocaleDanishLanguageAutogeneratedTests: XCTestCase { } } -class LocaleDutchLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleDutchLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Dutch_Belgium() { locale = .Dutch(.Belgium) @@ -2510,9 +2510,9 @@ class LocaleDutchLanguageAutogeneratedTests: XCTestCase { } } -class LocaleEnglishLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleEnglishLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_English_Singapore() { locale = .English(.Singapore) @@ -3313,9 +3313,9 @@ class LocaleEnglishLanguageAutogeneratedTests: XCTestCase { } } -class LocaleEweLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleEweLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Ewe_Togo() { locale = .Ewe(.Togo) @@ -3330,9 +3330,9 @@ class LocaleEweLanguageAutogeneratedTests: XCTestCase { } } -class LocaleFaroeseLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleFaroeseLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Faroese_FaroeIslands() { locale = .Faroese(.FaroeIslands) @@ -3347,9 +3347,9 @@ class LocaleFaroeseLanguageAutogeneratedTests: XCTestCase { } } -class LocaleFrenchLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleFrenchLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_French_Rwanda() { locale = .French(.Rwanda) @@ -3628,9 +3628,9 @@ class LocaleFrenchLanguageAutogeneratedTests: XCTestCase { } } -class LocaleFulahLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleFulahLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Fulah_Senegal() { locale = .Fulah(.Senegal) @@ -3657,9 +3657,9 @@ class LocaleFulahLanguageAutogeneratedTests: XCTestCase { } } -class LocaleGermanLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleGermanLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_German_Belgium() { locale = .German(.Belgium) @@ -3698,9 +3698,9 @@ class LocaleGermanLanguageAutogeneratedTests: XCTestCase { } } -class LocaleGreekLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleGreekLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Greek_Cyprus() { locale = .Greek(.Cyprus) @@ -3715,9 +3715,9 @@ class LocaleGreekLanguageAutogeneratedTests: XCTestCase { } } -class LocaleHausaLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleHausaLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Hausa_Niger() { locale = .Hausa(.Niger) @@ -3738,9 +3738,9 @@ class LocaleHausaLanguageAutogeneratedTests: XCTestCase { } } -class LocaleItalianLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleItalianLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Italian_Italy() { locale = .Italian(.Italy) @@ -3761,9 +3761,9 @@ class LocaleItalianLanguageAutogeneratedTests: XCTestCase { } } -class LocaleKoreanLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleKoreanLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Korean_NorthKorea() { locale = .Korean(.NorthKorea) @@ -3778,9 +3778,9 @@ class LocaleKoreanLanguageAutogeneratedTests: XCTestCase { } } -class LocaleLingalaLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleLingalaLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Lingala_CongoKinshasa() { locale = .Lingala(.CongoKinshasa) @@ -3807,9 +3807,9 @@ class LocaleLingalaLanguageAutogeneratedTests: XCTestCase { } } -class LocaleMalayLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleMalayLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Malay_Brunei() { locale = .Malay(.Brunei) @@ -3830,9 +3830,9 @@ class LocaleMalayLanguageAutogeneratedTests: XCTestCase { } } -class LocaleMasaiLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleMasaiLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Masai_Tanzania() { locale = .Masai(.Tanzania) @@ -3847,9 +3847,9 @@ class LocaleMasaiLanguageAutogeneratedTests: XCTestCase { } } -class LocaleNepaliLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleNepaliLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Nepali_Nepal() { locale = .Nepali(.Nepal) @@ -3864,9 +3864,9 @@ class LocaleNepaliLanguageAutogeneratedTests: XCTestCase { } } -class LocaleNorthernLuriLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleNorthernLuriLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_NorthernLuri_Iraq() { locale = .NorthernLuri(.Iraq) @@ -3881,9 +3881,9 @@ class LocaleNorthernLuriLanguageAutogeneratedTests: XCTestCase { } } -class LocaleNorthernSamiLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleNorthernSamiLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_NorthernSami_Sweden() { locale = .NorthernSami(.Sweden) @@ -3904,9 +3904,9 @@ class LocaleNorthernSamiLanguageAutogeneratedTests: XCTestCase { } } -class LocaleNorwegianBokmålLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleNorwegianBokmålLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_NorwegianBokmål_SvalbardJanMayen() { locale = .NorwegianBokmål(.SvalbardJanMayen) @@ -3921,9 +3921,9 @@ class LocaleNorwegianBokmålLanguageAutogeneratedTests: XCTestCase { } } -class LocaleOromoLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleOromoLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Oromo_Ethiopia() { locale = .Oromo(.Ethiopia) @@ -3938,9 +3938,9 @@ class LocaleOromoLanguageAutogeneratedTests: XCTestCase { } } -class LocaleOsseticLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleOsseticLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Ossetic_Georgia() { locale = .Ossetic(.Georgia) @@ -3955,9 +3955,9 @@ class LocaleOsseticLanguageAutogeneratedTests: XCTestCase { } } -class LocalePersianLanguageAutogeneratedTests: XCTestCase { +class MNYLocalePersianLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Persian_Iran() { locale = .Persian(.Iran) @@ -3972,9 +3972,9 @@ class LocalePersianLanguageAutogeneratedTests: XCTestCase { } } -class LocalePortugueseLanguageAutogeneratedTests: XCTestCase { +class MNYLocalePortugueseLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Portuguese_Brazil() { locale = .Portuguese(.Brazil) @@ -4031,9 +4031,9 @@ class LocalePortugueseLanguageAutogeneratedTests: XCTestCase { } } -class LocalePunjabiLanguageAutogeneratedTests: XCTestCase { +class MNYLocalePunjabiLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Punjabi_Pakistan() { locale = .Punjabi(.Pakistan) @@ -4048,9 +4048,9 @@ class LocalePunjabiLanguageAutogeneratedTests: XCTestCase { } } -class LocaleQuechuaLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleQuechuaLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Quechua_Bolivia() { locale = .Quechua(.Bolivia) @@ -4071,9 +4071,9 @@ class LocaleQuechuaLanguageAutogeneratedTests: XCTestCase { } } -class LocaleRomanianLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleRomanianLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Romanian_Romania() { locale = .Romanian(.Romania) @@ -4088,9 +4088,9 @@ class LocaleRomanianLanguageAutogeneratedTests: XCTestCase { } } -class LocaleRussianLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleRussianLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Russian_Kyrgyzstan() { locale = .Russian(.Kyrgyzstan) @@ -4129,9 +4129,9 @@ class LocaleRussianLanguageAutogeneratedTests: XCTestCase { } } -class LocaleSerbianLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleSerbianLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Serbian_Montenegro() { locale = .Serbian(.Montenegro) @@ -4158,9 +4158,9 @@ class LocaleSerbianLanguageAutogeneratedTests: XCTestCase { } } -class LocaleSomaliLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleSomaliLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Somali_Kenya() { locale = .Somali(.Kenya) @@ -4187,9 +4187,9 @@ class LocaleSomaliLanguageAutogeneratedTests: XCTestCase { } } -class LocaleSpanishLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleSpanishLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Spanish_Bolivia() { locale = .Spanish(.Bolivia) @@ -4348,9 +4348,9 @@ class LocaleSpanishLanguageAutogeneratedTests: XCTestCase { } } -class LocaleSwahiliLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleSwahiliLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Swahili_CongoKinshasa() { locale = .Swahili(.CongoKinshasa) @@ -4377,9 +4377,9 @@ class LocaleSwahiliLanguageAutogeneratedTests: XCTestCase { } } -class LocaleSwedishLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleSwedishLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Swedish_Sweden() { locale = .Swedish(.Sweden) @@ -4400,9 +4400,9 @@ class LocaleSwedishLanguageAutogeneratedTests: XCTestCase { } } -class LocaleSwissGermanLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleSwissGermanLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_SwissGerman_Switzerland() { locale = .SwissGerman(.Switzerland) @@ -4423,9 +4423,9 @@ class LocaleSwissGermanLanguageAutogeneratedTests: XCTestCase { } } -class LocaleTamilLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleTamilLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Tamil_India() { locale = .Tamil(.India) @@ -4452,9 +4452,9 @@ class LocaleTamilLanguageAutogeneratedTests: XCTestCase { } } -class LocaleTesoLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleTesoLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Teso_Kenya() { locale = .Teso(.Kenya) @@ -4469,9 +4469,9 @@ class LocaleTesoLanguageAutogeneratedTests: XCTestCase { } } -class LocaleTibetanLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleTibetanLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Tibetan_India() { locale = .Tibetan(.India) @@ -4486,9 +4486,9 @@ class LocaleTibetanLanguageAutogeneratedTests: XCTestCase { } } -class LocaleTigrinyaLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleTigrinyaLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Tigrinya_Ethiopia() { locale = .Tigrinya(.Ethiopia) @@ -4503,9 +4503,9 @@ class LocaleTigrinyaLanguageAutogeneratedTests: XCTestCase { } } -class LocaleTurkishLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleTurkishLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Turkish_Cyprus() { locale = .Turkish(.Cyprus) @@ -4520,9 +4520,9 @@ class LocaleTurkishLanguageAutogeneratedTests: XCTestCase { } } -class LocaleUrduLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleUrduLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Urdu_Pakistan() { locale = .Urdu(.Pakistan) @@ -4537,9 +4537,9 @@ class LocaleUrduLanguageAutogeneratedTests: XCTestCase { } } -class LocaleUzbekLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleUzbekLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Uzbek_Uzbekistan() { locale = .Uzbek(.Uzbekistan) @@ -4554,9 +4554,9 @@ class LocaleUzbekLanguageAutogeneratedTests: XCTestCase { } } -class LocaleYorubaLanguageAutogeneratedTests: XCTestCase { +class MNYLocaleYorubaLanguageAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Yoruba_Benin() { locale = .Yoruba(.Benin) @@ -4571,9 +4571,9 @@ class LocaleYorubaLanguageAutogeneratedTests: XCTestCase { } } -class LocaleAutogeneratedTests: XCTestCase { +class MNYLocaleAutogeneratedTests: XCTestCase { - var locale: Locale! + var locale: MNYLocale! func test__language_identifier_for_Aghem() { locale = .Aghem diff --git a/Tests/DecimalNumberTypeTests.swift b/Tests/DecimalNumberTypeTests.swift index c571415..f2ca364 100644 --- a/Tests/DecimalNumberTypeTests.swift +++ b/Tests/DecimalNumberTypeTests.swift @@ -29,12 +29,12 @@ import XCTest class DecimalNumberTypeTests: XCTestCase { - var decimal: Decimal! + var decimal: PlainDecimal! var money: Money! func test__init_with_int() { let value: Int = 10 - decimal = Decimal(value) + decimal = PlainDecimal(value) money = Money(value) XCTAssertEqual(decimal, 10) XCTAssertEqual(money, 10) @@ -42,7 +42,7 @@ class DecimalNumberTypeTests: XCTestCase { func test__init_with_uint8() { let value: UInt8 = 10 - decimal = Decimal(value) + decimal = PlainDecimal(value) money = Money(value) XCTAssertEqual(decimal, 10) XCTAssertEqual(money, 10) @@ -50,7 +50,7 @@ class DecimalNumberTypeTests: XCTestCase { func test__init_with_int8() { let value: Int8 = 10 - decimal = Decimal(value) + decimal = PlainDecimal(value) money = Money(value) XCTAssertEqual(decimal, 10) XCTAssertEqual(money, 10) @@ -58,7 +58,7 @@ class DecimalNumberTypeTests: XCTestCase { func test__init_with_uint16() { let value: UInt16 = 10 - decimal = Decimal(value) + decimal = PlainDecimal(value) money = Money(value) XCTAssertEqual(decimal, 10) XCTAssertEqual(money, 10) @@ -66,7 +66,7 @@ class DecimalNumberTypeTests: XCTestCase { func test__init_with_int16() { let value: Int16 = 10 - decimal = Decimal(value) + decimal = PlainDecimal(value) money = Money(value) XCTAssertEqual(decimal, 10) XCTAssertEqual(money, 10) @@ -74,7 +74,7 @@ class DecimalNumberTypeTests: XCTestCase { func test__init_with_uint32() { let value: UInt32 = 10 - decimal = Decimal(value) + decimal = PlainDecimal(value) money = Money(value) XCTAssertEqual(decimal, 10) XCTAssertEqual(money, 10) @@ -82,7 +82,7 @@ class DecimalNumberTypeTests: XCTestCase { func test__init_with_int32() { let value: Int32 = 10 - decimal = Decimal(value) + decimal = PlainDecimal(value) money = Money(value) XCTAssertEqual(decimal, 10) XCTAssertEqual(money, 10) @@ -90,7 +90,7 @@ class DecimalNumberTypeTests: XCTestCase { func test__init_with_uint64() { let value: UInt64 = 10 - decimal = Decimal(value) + decimal = PlainDecimal(value) money = Money(value) XCTAssertEqual(decimal, 10) XCTAssertEqual(money, 10) @@ -98,7 +98,7 @@ class DecimalNumberTypeTests: XCTestCase { func test__init_with_int64() { let value: Int64 = 10 - decimal = Decimal(value) + decimal = PlainDecimal(value) money = Money(value) XCTAssertEqual(decimal, 10) XCTAssertEqual(money, 10) @@ -106,7 +106,7 @@ class DecimalNumberTypeTests: XCTestCase { func test__init_with_float() { let value: Float = 9.0 - decimal = Decimal(value) + decimal = PlainDecimal(value) money = Money(value) XCTAssertEqual(decimal, 9.0) XCTAssertEqual(money, 9.00) @@ -114,14 +114,14 @@ class DecimalNumberTypeTests: XCTestCase { func test__init_with_double() { let value: Double = 9.999 - decimal = Decimal(value) + decimal = PlainDecimal(value) money = Money(value) XCTAssertEqual(decimal, 9.999) XCTAssertEqual(money, 10.00) } func testPerformanceInitInt() { - self.measureBlock { + measure { for value in 1...10_000 { self.money = Money(value) } @@ -130,7 +130,7 @@ class DecimalNumberTypeTests: XCTestCase { func testPerformanceInitDouble() { let value: Double = 9.99 - self.measureBlock { + measure { for _ in 1...10_000 { self.money = Money(value) } diff --git a/Tests/DecimalTests.swift b/Tests/DecimalTests.swift index d0e3ede..9e6fa93 100644 --- a/Tests/DecimalTests.swift +++ b/Tests/DecimalTests.swift @@ -28,8 +28,8 @@ import XCTest import ValueCoding @testable import Money -class DecimalTests: XCTestCase { - var decimal: Decimal! +class PlainDecimalTestCase: XCTestCase { + var decimal: PlainDecimal! override func tearDown() { decimal = nil @@ -37,7 +37,7 @@ class DecimalTests: XCTestCase { } } -class DecimalAccessorTests: DecimalTests { +class DecimalAccessorTests: PlainDecimalTestCase { func test__decimal_integer_value() { decimal = 10.00 @@ -50,7 +50,7 @@ class DecimalAccessorTests: DecimalTests { } } -class DecimalDescriptionTests: DecimalTests { +class DecimalDescriptionTests: PlainDecimalTestCase { func test__decimal_decription1() { decimal = 10.00 @@ -63,7 +63,7 @@ class DecimalDescriptionTests: DecimalTests { } } -class DecimalReciprocalTests: DecimalTests { +class DecimalReciprocalTests: PlainDecimalTestCase { func test__reciprocal() { decimal = 10 @@ -76,7 +76,7 @@ class DecimalReciprocalTests: DecimalTests { } } -class DecimalNumberConversionTests: DecimalTests { +class DecimalNumberConversionTests: PlainDecimalTestCase { var money: GBP! override func setUp() { @@ -103,14 +103,14 @@ class DecimalNumberConversionTests: DecimalTests { } } -class DecimalValueCodingTests: DecimalTests { +class DecimalValueCodingTests: PlainDecimalTestCase { - func archiveEncodedDecimal() -> NSData { - return NSKeyedArchiver.archivedDataWithRootObject(decimal.encoded) + func archiveEncodedDecimal() -> Data { + return NSKeyedArchiver.archivedData(withRootObject: decimal.encoded) } - func unarchive(archive: NSData) -> Decimal? { - return Decimal.decode(NSKeyedUnarchiver.unarchiveObjectWithData(archive)) + func unarchive(_ archive: Data) -> PlainDecimal? { + return PlainDecimal.decode(NSKeyedUnarchiver.unarchiveObject(with: archive) as AnyObject?) } func test__decimal_encodes() { diff --git a/Tests/LocaleTests.swift b/Tests/LocaleTests.swift index a11dc42..782e738 100644 --- a/Tests/LocaleTests.swift +++ b/Tests/LocaleTests.swift @@ -34,8 +34,8 @@ class LocaleTests: XCTestCase { override func setUp() { super.setUp() - en_US = NSLocale(localeIdentifier: Locale.English(.UnitedStates).localeIdentifier) - es_ES = NSLocale(localeIdentifier: Locale.Spanish(.Spain).localeIdentifier) + en_US = NSLocale(localeIdentifier: MNYLocale.English(.UnitedStates).localeIdentifier) + es_ES = NSLocale(localeIdentifier: MNYLocale.Spanish(.Spain).localeIdentifier) } override func tearDown() { diff --git a/Tests/MoneyTests.swift b/Tests/MoneyTests.swift index 8d9dee7..2df5773 100644 --- a/Tests/MoneyTests.swift +++ b/Tests/MoneyTests.swift @@ -27,7 +27,7 @@ import XCTest @testable import Money -class MoneyTests: XCTestCase { +class MoneyTestCase: XCTestCase { var money: Money! var gbp: GBP! @@ -50,7 +50,7 @@ class MoneyTests: XCTestCase { } } -class MoneyInitializerTests: MoneyTests { +class MoneyInitializerTests: MoneyTestCase { func test__money_initialize_with__nothing() { money = Money() @@ -74,7 +74,7 @@ class MoneyInitializerTests: MoneyTests { func test__money_amount() { money = 10 - XCTAssertEqual(money.amount, NSDecimalNumber(integer: 10)) + XCTAssertEqual(money.amount, NSDecimalNumber(value: 10)) } } @@ -99,7 +99,7 @@ class MoneyComparableTests: XCTestCase { func test__money_sorts() { let monies: [Money] = [ 0, 12, 4.50, 9.99, 99, 9.99, 2.49, 16.69] - let sorted = monies.sort() + let sorted = monies.sorted() XCTAssertEqual(sorted, [0, 2.49, 4.50, 9.99, 9.99, 12, 16.69, 99]) } } @@ -123,6 +123,7 @@ class MoneySignedNumberTests: XCTestCase { class MoneySubtractionTests: XCTestCase { let money: JPY = 12_345.67 + let other: JPY = 10_000 func test__subtraction_int_1() { XCTAssertEqual(money - 10_000, 2_345.67) @@ -144,11 +145,11 @@ class MoneySubtractionTests: XCTestCase { class MoneyAddingTests: XCTestCase { let money: INR = 335_577.99 + let other: INR = 446_688.00 func test_addition() { - let cash: INR = 446_688.00 - XCTAssertEqual(money + cash, 782_265.99) - XCTAssertEqual(cash + money, 782_265.99) + XCTAssertEqual(money + other, 782_265.99) + XCTAssertEqual(other + money, 782_265.99) } func test__addition_int_interal() { @@ -259,12 +260,12 @@ class MoneyConversionTests: XCTestCase { let input: GBP = 100 func test__convert_with_rate_to_other() { - let output: EUR = input.convertWithRate(1.2) + let output: EUR = input.convert(withRate: 1.2) XCTAssertEqual(output, 120) } } -class MoneyDescriptionTests: MoneyTests { +class MoneyDescriptionTests: MoneyTestCase { override func setUp() { super.setUp() @@ -279,7 +280,7 @@ class MoneyDescriptionTests: MoneyTests { } func test__money_description() { - XCTAssertEqual(money.description.endIndex, money.description.rangeOfString("3.99")?.endIndex) + XCTAssertEqual(money.description.endIndex, money.description.range(of: "3.99")?.upperBound) } func test__gbp_description() { @@ -290,19 +291,19 @@ class MoneyDescriptionTests: MoneyTests { func test__usd_formatted_with_style() { XCTAssertEqual(usd.currencyCode, "USD") - let formatted = usd.formattedWithStyle(.CurrencyStyle, forLocale: .English(.UnitedStates)) + let formatted = usd.formatted(withStyle: .currency, forLocale: .English(.UnitedStates)) XCTAssertEqual(formatted, "US$99.00") } func test__btc_formatted_with_style() { XCTAssertEqual(btc.currencyCode, "BTC") - let formatted = btc.formattedWithStyle(.CurrencyStyle, forLocale: .English(.UnitedStates)) + let formatted = btc.formatted(withStyle: .currency, forLocale: .English(.UnitedStates)) XCTAssertEqual(formatted, "Ƀ0.002007") } func test__btc_formatted_with_style_for_locale() { XCTAssertEqual(btc.currencyCode, "BTC") - let formatted = btc.formattedWithStyle(.CurrencyStyle, forLocale: .Spanish(.Mexico)) + let formatted = btc.formatted(withStyle: .currency, forLocale: .Spanish(.Mexico)) XCTAssertEqual(formatted, "Ƀ0.002007") } @@ -324,21 +325,19 @@ class MoneyDescriptionTests: MoneyTests { func test__jpy_description() { XCTAssertEqual(jpy.currencyCode, "JPY") XCTAssertEqual(JPY.Currency.scale, 0) - if NSLocale.currentLocale().localeIdentifier == "en_US" { - - } + if NSLocale.current.identifier == "en_US" { } else { XCTAssertEqual(jpy.description, "JP¥32,000") } } func test__jpy_formatted_with_style_for_locale() { - let formatted = jpy.formattedWithStyle(.CurrencyStyle, forLocale: .German(.Germany)) + let formatted = jpy.formatted(withStyle: .currency, forLocale: .German(.Germany)) XCTAssertEqual(formatted, "32.000 JP¥") } } -class MoneyFormattingTests: MoneyTests { +class MoneyFormattingTests: MoneyTestCase { override func setUp() { super.setUp() @@ -352,18 +351,18 @@ class MoneyFormattingTests: MoneyTests { // Tests assume a en_GB test environment func test__locale_identifier_equals_current_locale() { - let gb = NSLocale.currentLocale().localeIdentifier == Locale.English(.UnitedKingdom).localeIdentifier - let us = NSLocale.currentLocale().localeIdentifier == Locale.English(.UnitedStates).localeIdentifier + let gb = NSLocale.current.identifier == MNYLocale.English(.UnitedKingdom).localeIdentifier + let us = NSLocale.current.identifier == MNYLocale.English(.UnitedStates).localeIdentifier XCTAssertTrue(gb || us) } func test__formatted_for_Spanish_Spain() { - let result = gbp.formattedWithStyle(.CurrencyStyle, forLocale: .Spanish(.Spain)) + let result = gbp.formatted(withStyle: .currency, forLocale: .Spanish(.Spain)) XCTAssertEqual(result, "100,00 £") } func test__formatted_for_English_UnitedKingdom() { - let result = gbp.formattedWithStyle(.CurrencyStyle, forLocale: .English(.UnitedKingdom)) + let result = gbp.formatted(withStyle: .currency, forLocale: .English(.UnitedKingdom)) XCTAssertEqual(result, "£100.00") } } @@ -372,12 +371,12 @@ class MoneyValueCodingTests: XCTestCase { var money: Money! - func archiveEncodedMoney() -> NSData { - return NSKeyedArchiver.archivedDataWithRootObject(money.encoded) + func archiveEncodedMoney() -> Data { + return NSKeyedArchiver.archivedData(withRootObject: money.encoded) } - func unarchive(archive: NSData) -> Money? { - return Money.decode(NSKeyedUnarchiver.unarchiveObjectWithData(archive)) + func unarchive(_ archive: Data) -> Money? { + return Money.decode(NSKeyedUnarchiver.unarchiveObject(with: archive) as AnyObject?) } func test__money_encodes() { @@ -410,28 +409,6 @@ class MoneyMinorUnitTests: XCTestCase { class CustomCurrencyWithoutSymbol: CustomCurrencyType { static let code: String = "DAN" static let scale: Int = 3 - static let symbol: String? = .None + static let symbol: String? = nil } -class CurrencyTests: XCTestCase { - - func test__formatted_with_style() { - let formating = Currency.GBP.formattedWithStyle(.CurrencyISOCodeStyle, forLocale: NSLocale.currentLocale()) - let result = formating(NSDecimalNumber(double: 10)) - XCTAssertEqual(result, "GBP10.00") - } - - func test__formatted_with_iso_style__with_no_symbol() { - let formating = CustomCurrencyWithoutSymbol.formattedWithStyle(.CurrencyISOCodeStyle, forLocale: NSLocale.currentLocale()) - let result = formating(NSDecimalNumber(double: 10.1234)) - XCTAssertEqual(result, "DAN10.123") - } - - func test__formatted_with_default_style__with_no_symbol() { - let formating = CustomCurrencyWithoutSymbol.formattedWithStyle(.CurrencyStyle, forLocale: NSLocale.currentLocale()) - let result = formating(NSDecimalNumber(double: 10.1234)) - XCTAssertEqual(result, "DAN10.123") - } -} - - diff --git a/Tests/NSDecimalNumberTests.swift b/Tests/NSDecimalNumberTests.swift index db2b02e..40f410b 100644 --- a/Tests/NSDecimalNumberTests.swift +++ b/Tests/NSDecimalNumberTests.swift @@ -48,48 +48,48 @@ class NSDecimalNumberTests: XCTestCase { } func test__zero_is_not_equal_to_one() { - XCTAssertNotEqual(NSDecimalNumber.zero(), NSDecimalNumber.one()) + XCTAssertNotEqual(NSDecimalNumber.zero, NSDecimalNumber.one) } func test__zero_is_less_than_one() { - XCTAssertTrue(NSDecimalNumber.zero() < NSDecimalNumber.one()) + XCTAssertTrue(NSDecimalNumber.zero < NSDecimalNumber.one) } func test__zero_is_greater_than_negative_one() { - XCTAssertTrue(NSDecimalNumber.zero() > NSDecimalNumber.one().negateWithBehaviors(behaviors)) + XCTAssertTrue(NSDecimalNumber.zero > NSDecimalNumber.one.negate(withBehavior: behaviors)) } func test__negative_one_is_negative() { - XCTAssertTrue(NSDecimalNumber.one().negateWithBehaviors(behaviors).isNegative) + XCTAssertTrue(NSDecimalNumber.one.negate(withBehavior: behaviors).isNegative) } func test__zero_is_not_negative() { - XCTAssertFalse(NSDecimalNumber.zero().isNegative) - XCTAssertFalse(NSDecimalNumber.one().isNegative) + XCTAssertFalse(NSDecimalNumber.zero.isNegative) + XCTAssertFalse(NSDecimalNumber.one.isNegative) } func test__addition() { - let result = a.add(b, withBehaviors: behaviors) + let result = a.adding(b, withBehavior: behaviors) XCTAssertEqual(result, 30) } func test__subtraction() { - let result = a.subtract(b, withBehaviors: behaviors) + let result = a.subtracting(b, withBehavior: behaviors) XCTAssertEqual(result, -10) } func test__multiplication() { - let result = a.multiplyBy(b, withBehaviors: behaviors) + let result = a.multiplying(by: b, withBehavior: behaviors) XCTAssertEqual(result, 200) } func test__division() { - let result = a.divideBy(b, withBehaviors: behaviors) + let result = a.dividing(by: b, withBehavior: behaviors) XCTAssertEqual(result, 0.5) } func test__remainder() { - let result = a.remainder(b, withBehaviors: behaviors) + let result = a.remainder(b, withBehavior: behaviors) XCTAssertEqual(result, 10) } @@ -97,15 +97,15 @@ class NSDecimalNumberTests: XCTestCase { // https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-ID63 a = 9; b = 4 - XCTAssertEqual(a.remainder(b, withBehaviors: behaviors), 1) + XCTAssertEqual(a.remainder(b, withBehavior: behaviors), 1) a = -9; b = 4 - XCTAssertEqual(a.remainder(b, withBehaviors: behaviors), -1) + XCTAssertEqual(a.remainder(b, withBehavior: behaviors), -1) a = 9; b = -4 - XCTAssertEqual(a.remainder(b, withBehaviors: behaviors), 1) + XCTAssertEqual(a.remainder(b, withBehavior: behaviors), 1) a = 8; b = 2.5 - XCTAssertEqual(a.remainder(b, withBehaviors: behaviors), 0.5) + XCTAssertEqual(a.remainder(b, withBehavior: behaviors), 0.5) } } diff --git a/Tests/NSDecimalTests.swift b/Tests/NSDecimalTests.swift index 9bd071a..8632a5b 100644 --- a/Tests/NSDecimalTests.swift +++ b/Tests/NSDecimalTests.swift @@ -27,13 +27,13 @@ import XCTest @testable import Money -class NSDecimalTests: XCTestCase { +class DecimalTests: XCTestCase { var decimalNumberA: NSDecimalNumber! var decimalNumberB: NSDecimalNumber! var behaviors: NSDecimalNumberBehaviors! - var a: NSDecimal! - var b: NSDecimal! + var a: Decimal! + var b: Decimal! override func setUp() { super.setUp() @@ -54,61 +54,61 @@ class NSDecimalTests: XCTestCase { } func test__zero() { - XCTAssertEqual(NSDecimal.zero(), NSDecimalNumber.zero().decimalValue) + XCTAssertEqual(Decimal.zero, NSDecimalNumber.zero.decimalValue) } func test__zero_is_not_equal_to_one() { - XCTAssertNotEqual(NSDecimal.zero(), NSDecimal.one()) + XCTAssertNotEqual(Decimal.zero, Decimal.one) } func test__zero_is_less_than_one() { - XCTAssertTrue(NSDecimal.zero() < NSDecimal.one()) + XCTAssertTrue(Decimal.zero < Decimal.one) } func test__zero_is_greater_than_negative_one() { - XCTAssertTrue(NSDecimal.zero() > NSDecimal.one().negateWithRoundingMode(behaviors.roundingMode())) + XCTAssertTrue(Decimal.zero > Decimal.one.negate(withRoundingMode: behaviors.roundingMode())) } func test__negative_one_is_negative() { - XCTAssertTrue(NSDecimal.one().negateWithRoundingMode(behaviors.roundingMode()).isNegative) + XCTAssertTrue(Decimal.one.negate(withRoundingMode: behaviors.roundingMode()).isNegative) } func test__zero_is_not_negative() { - XCTAssertFalse(NSDecimal.zero().isNegative) - XCTAssertFalse(NSDecimal.one().isNegative) + XCTAssertFalse(Decimal.zero.isNegative) + XCTAssertFalse(Decimal.one.isNegative) } func test__addition() { - let result = a.add(b, withRoundingMode: behaviors.roundingMode()) - let _result = decimalNumberA.add(decimalNumberB, withBehaviors: behaviors) + let result = a.adding(b, withRoundingMode: behaviors.roundingMode()) + let _result = decimalNumberA.adding(decimalNumberB, withBehavior: behaviors) XCTAssertEqual(result, _result.decimalValue) XCTAssertEqual(_result, 30) } func test__subtraction() { - let result = a.subtract(b, withRoundingMode: behaviors.roundingMode()) - let _result = decimalNumberA.subtract(decimalNumberB, withBehaviors: behaviors) + let result = a.subtracting(b, withRoundingMode: behaviors.roundingMode()) + let _result = decimalNumberA.subtracting(decimalNumberB, withBehavior: behaviors) XCTAssertEqual(result, _result.decimalValue) XCTAssertEqual(_result, -10) } func test__multiplication() { - let result = a.multiplyBy(b, withRoundingMode: behaviors.roundingMode()) - let _result = decimalNumberA.multiplyBy(decimalNumberB, withBehaviors: behaviors) + let result = a.multiplying(by: b, withRoundingMode: behaviors.roundingMode()) + let _result = decimalNumberA.multiplying(by: decimalNumberB, withBehavior: behaviors) XCTAssertEqual(result, _result.decimalValue) XCTAssertEqual(_result, 200) } func test__division() { - let result = a.divideBy(b, withRoundingMode: behaviors.roundingMode()) - let _result = decimalNumberA.divideBy(decimalNumberB, withBehaviors: behaviors) + let result = a.dividing(by: b, withRoundingMode: behaviors.roundingMode()) + let _result = decimalNumberA.dividing(by: decimalNumberB, withBehavior: behaviors) XCTAssertEqual(result, _result.decimalValue) XCTAssertEqual(_result, 0.5) } func test__remainder() { let result = a.remainder(b, withRoundingMode: behaviors.roundingMode()) - let _result = decimalNumberA.remainder(decimalNumberB, withBehaviors: behaviors) + let _result = decimalNumberA.remainder(decimalNumberB, withBehavior: behaviors) XCTAssertEqual(result, _result.decimalValue) XCTAssertEqual(_result, 10) }