Skip to content

Commit 599c05d

Browse files
authored
Merge pull request #3083 from YOCKOW/SR-14913
SR-14913: Implement `copy()` in `NumberFormatter`.
2 parents 2e9d97d + 55bb3a3 commit 599c05d

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

Sources/Foundation/NumberFormatter.swift

+81
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,87 @@ open class NumberFormatter : Formatter {
6666
}
6767
}
6868

69+
open override func copy(with zone: NSZone? = nil) -> Any {
70+
let copied = NumberFormatter()
71+
72+
func __copy<T>(_ keyPath: ReferenceWritableKeyPath<NumberFormatter, T>) {
73+
copied[keyPath: keyPath] = self[keyPath: keyPath]
74+
}
75+
76+
func __copy<T>(_ keyPath: ReferenceWritableKeyPath<NumberFormatter, T>) where T: NSCopying {
77+
copied[keyPath: keyPath] = self[keyPath: keyPath].copy(with: zone) as! T
78+
}
79+
80+
func __copy<T>(_ keyPath: ReferenceWritableKeyPath<NumberFormatter, T?>) where T: NSCopying {
81+
copied[keyPath: keyPath] = self[keyPath: keyPath]?.copy(with: zone) as! T?
82+
}
83+
84+
__copy(\.formattingContext)
85+
__copy(\._numberStyle)
86+
__copy(\._locale)
87+
__copy(\._generatesDecimalNumbers)
88+
__copy(\._textAttributesForNegativeValues)
89+
__copy(\._textAttributesForPositiveValues)
90+
__copy(\._allowsFloats)
91+
__copy(\._decimalSeparator)
92+
__copy(\._alwaysShowsDecimalSeparator)
93+
__copy(\._currencyDecimalSeparator)
94+
__copy(\._usesGroupingSeparator)
95+
__copy(\._groupingSeparator)
96+
__copy(\._zeroSymbol)
97+
__copy(\._textAttributesForZero)
98+
__copy(\._nilSymbol)
99+
__copy(\._textAttributesForNil)
100+
__copy(\._notANumberSymbol)
101+
__copy(\._textAttributesForNotANumber)
102+
__copy(\._positiveInfinitySymbol)
103+
__copy(\._textAttributesForPositiveInfinity)
104+
__copy(\._negativeInfinitySymbol)
105+
__copy(\._textAttributesForNegativeInfinity)
106+
__copy(\._positivePrefix)
107+
__copy(\._positiveSuffix)
108+
__copy(\._negativePrefix)
109+
__copy(\._negativeSuffix)
110+
__copy(\._currencyCode)
111+
__copy(\._currencySymbol)
112+
__copy(\._internationalCurrencySymbol)
113+
__copy(\._percentSymbol)
114+
__copy(\._perMillSymbol)
115+
__copy(\._minusSign)
116+
__copy(\._plusSign)
117+
__copy(\._exponentSymbol)
118+
__copy(\._groupingSize)
119+
__copy(\._secondaryGroupingSize)
120+
__copy(\._multiplier)
121+
__copy(\._formatWidth)
122+
__copy(\._paddingCharacter)
123+
__copy(\._paddingPosition)
124+
__copy(\._roundingMode)
125+
__copy(\._roundingIncrement)
126+
__copy(\._minimumIntegerDigits)
127+
__copy(\._maximumIntegerDigits)
128+
__copy(\._minimumFractionDigits)
129+
__copy(\._maximumFractionDigits)
130+
__copy(\._minimum)
131+
__copy(\._maximum)
132+
__copy(\._currencyGroupingSeparator)
133+
__copy(\._lenient)
134+
__copy(\._usesSignificantDigits)
135+
__copy(\._minimumSignificantDigits)
136+
__copy(\._maximumSignificantDigits)
137+
__copy(\._partialStringValidationEnabled)
138+
__copy(\._hasThousandSeparators)
139+
__copy(\._thousandSeparator)
140+
__copy(\._localizesFormat)
141+
__copy(\._positiveFormat)
142+
__copy(\._negativeFormat)
143+
__copy(\._attributedStringForZero)
144+
__copy(\._attributedStringForNotANumber)
145+
__copy(\._roundingBehavior)
146+
147+
return copied
148+
}
149+
69150
// this is for NSUnitFormatter
70151

71152
open var formattingContext: Context = .unknown // default is NSFormattingContextUnknown

Tests/Foundation/Tests/TestNumberFormatter.swift

+29
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,34 @@ class TestNumberFormatter: XCTestCase {
11761176
#endif
11771177
}
11781178

1179+
func test_copy() throws {
1180+
let original = NumberFormatter()
1181+
let copied = try XCTUnwrap(original.copy() as? NumberFormatter)
1182+
XCTAssertFalse(original === copied)
1183+
1184+
func __assert<T>(_ property: KeyPath<NumberFormatter, T>,
1185+
original expectedValueOfOriginalFormatter: T,
1186+
copy expectedValueOfCopiedFormatter: T,
1187+
file: StaticString = #file,
1188+
line: UInt = #line) where T: Equatable {
1189+
XCTAssertEqual(original[keyPath: property], expectedValueOfOriginalFormatter,
1190+
"Unexpected value in `original`.", file: file, line: line)
1191+
XCTAssertEqual(copied[keyPath: property], expectedValueOfCopiedFormatter,
1192+
"Unexpected value in `copied`.", file: file, line: line)
1193+
}
1194+
1195+
copied.numberStyle = .decimal
1196+
__assert(\.numberStyle, original: .none, copy: .decimal)
1197+
__assert(\.maximumIntegerDigits, original: 42, copy: 2_000_000_000)
1198+
__assert(\.maximumFractionDigits, original: 0, copy: 3)
1199+
__assert(\.groupingSize, original: 0, copy: 3)
1200+
1201+
original.numberStyle = .percent
1202+
original.percentSymbol = ""
1203+
__assert(\.numberStyle, original: .percent, copy: .decimal)
1204+
__assert(\.format, original: "#,##0%;0%;#,##0%", copy: "#,##0.###;0;#,##0.###")
1205+
}
1206+
11791207
static var allTests: [(String, (TestNumberFormatter) -> () throws -> Void)] {
11801208
return [
11811209
("test_defaultPropertyValues", test_defaultPropertyValues),
@@ -1238,6 +1266,7 @@ class TestNumberFormatter: XCTestCase {
12381266
("test_usingFormat", test_usingFormat),
12391267
("test_propertyChanges", test_propertyChanges),
12401268
("test_scientificStrings", test_scientificStrings),
1269+
("test_copy", test_copy),
12411270
]
12421271
}
12431272
}

0 commit comments

Comments
 (0)