-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTestNSNumberFormatter.swift
345 lines (306 loc) · 13.4 KB
/
TestNSNumberFormatter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif
import CoreFoundation
class TestNSNumberFormatter: XCTestCase {
static var allTests: [(String, (TestNSNumberFormatter) -> () throws -> Void)] {
return [
("test_currencyCode", test_currencyCode),
("test_decimalSeparator", test_decimalSeparator),
("test_currencyDecimalSeparator", test_currencyDecimalSeparator),
("test_alwaysShowDecimalSeparator", test_alwaysShowDecimalSeparator),
("test_groupingSeparator", test_groupingSeparator),
("test_percentSymbol", test_percentSymbol),
("test_zeroSymbol", test_zeroSymbol),
("test_notANumberSymbol", test_notANumberSymbol),
("test_positiveInfinitySymbol", test_positiveInfinitySymbol),
("test_minusSignSymbol", test_minusSignSymbol),
("test_plusSignSymbol", test_plusSignSymbol),
("test_currencySymbol", test_currencySymbol),
("test_exponentSymbol", test_exponentSymbol),
("test_minimumIntegerDigits", test_minimumIntegerDigits),
("test_maximumIntegerDigits", test_maximumIntegerDigits),
("test_minimumFractionDigits", test_minimumFractionDigits),
("test_maximumFractionDigits", test_maximumFractionDigits),
("test_groupingSize", test_groupingSize),
("test_secondaryGroupingSize", test_secondaryGroupingSize),
("test_roundingMode", test_roundingMode),
("test_roundingIncrement", test_roundingIncrement),
("test_formatWidth", test_formatWidth),
("test_formatPosition", test_formatPosition),
("test_multiplier", test_multiplier),
("test_positivePrefix", test_positivePrefix),
("test_positiveSuffix", test_positiveSuffix),
("test_negativePrefix", test_negativePrefix),
("test_negativeSuffix", test_negativeSuffix),
("test_internationalCurrencySymbol", test_internationalCurrencySymbol),
("test_currencyGroupingSeparator", test_currencyGroupingSeparator),
("test_lenient", test_lenient),
("test_minimumSignificantDigits", test_minimumSignificantDigits),
("test_maximumSignificantDigits", test_maximumSignificantDigits)
]
}
func test_currencyCode() {
// Disabled due to [SR-250]
/*
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.currencyCode = "T"
numberFormatter.currencyDecimalSeparator = "_"
let formattedString = numberFormatter.stringFromNumber(42)
XCTAssertEqual(formattedString, "T 42_00")
*/
}
func test_decimalSeparator() {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.decimalSeparator = "-"
let formattedString = numberFormatter.string(from: 42.42)
XCTAssertEqual(formattedString, "42-42")
}
func test_currencyDecimalSeparator() {
// Disabled due to [SR-250]
/*
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.currencyDecimalSeparator = "-"
numberFormatter.currencyCode = "T"
let formattedString = numberFormatter.stringFromNumber(42.42)
XCTAssertEqual(formattedString, "T 42-42")
*/
}
func test_alwaysShowDecimalSeparator() {
let numberFormatter = NumberFormatter()
numberFormatter.decimalSeparator = "-"
numberFormatter.alwaysShowsDecimalSeparator = true
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "42-")
}
func test_groupingSeparator() {
let numberFormatter = NumberFormatter()
numberFormatter.usesGroupingSeparator = true
numberFormatter.groupingSeparator = "_"
let formattedString = numberFormatter.string(from: 42_000)
XCTAssertEqual(formattedString, "42_000")
}
func test_percentSymbol() {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .percent
numberFormatter.percentSymbol = "💯"
let formattedString = numberFormatter.string(from: 0.42)
XCTAssertEqual(formattedString, "42💯")
}
func test_zeroSymbol() {
let numberFormatter = NumberFormatter()
numberFormatter.zeroSymbol = "⚽️"
let formattedString = numberFormatter.string(from: 0)
XCTAssertEqual(formattedString, "⚽️")
}
var unknownZero: Int = 0
func test_notANumberSymbol() {
let numberFormatter = NumberFormatter()
numberFormatter.notANumberSymbol = "👽"
let number: Double = -42
let numberObject = NSNumber(value: sqrt(number))
let formattedString = numberFormatter.string(from: numberObject)
XCTAssertEqual(formattedString, "👽")
}
func test_positiveInfinitySymbol() {
let numberFormatter = NumberFormatter()
numberFormatter.positiveInfinitySymbol = "🚀"
let numberObject = NSNumber(value: Double(42.0) / Double(0))
let formattedString = numberFormatter.string(from: numberObject)
XCTAssertEqual(formattedString, "🚀")
}
func test_minusSignSymbol() {
let numberFormatter = NumberFormatter()
numberFormatter.minusSign = "👎"
let formattedString = numberFormatter.string(from: -42)
XCTAssertEqual(formattedString, "👎42")
}
func test_plusSignSymbol() {
//FIXME: How do we show the plus sign from a NSNumberFormatter?
// let numberFormatter = NumberFormatter()
// numberFormatter.plusSign = "👍"
// let formattedString = numberFormatter.stringFromNumber(42)
// XCTAssertEqual(formattedString, "👍42")
}
func test_currencySymbol() {
// Disabled due to [SR-250]
/*
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.currencySymbol = "🍯"
numberFormatter.currencyDecimalSeparator = "_"
let formattedString = numberFormatter.stringFromNumber(42)
XCTAssertEqual(formattedString, "🍯 42_00")
*/
}
func test_exponentSymbol() {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .scientific
numberFormatter.exponentSymbol = "⬆️"
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "4.2⬆️1")
}
func test_minimumIntegerDigits() {
let numberFormatter = NumberFormatter()
numberFormatter.minimumIntegerDigits = 3
let formattedString = numberFormatter.string(from: 0)
XCTAssertEqual(formattedString, "000")
}
func test_maximumIntegerDigits() {
let numberFormatter = NumberFormatter()
numberFormatter.maximumIntegerDigits = 3
let formattedString = numberFormatter.string(from: 1_000)
XCTAssertEqual(formattedString, "000")
}
func test_minimumFractionDigits() {
let numberFormatter = NumberFormatter()
numberFormatter.minimumFractionDigits = 3
numberFormatter.decimalSeparator = "-"
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "42-000")
}
func test_maximumFractionDigits() {
let numberFormatter = NumberFormatter()
numberFormatter.maximumFractionDigits = 3
numberFormatter.decimalSeparator = "-"
let formattedString = numberFormatter.string(from: 42.4242)
XCTAssertEqual(formattedString, "42-424")
}
func test_groupingSize() {
let numberFormatter = NumberFormatter()
numberFormatter.groupingSize = 4
numberFormatter.groupingSeparator = "_"
numberFormatter.usesGroupingSeparator = true
let formattedString = numberFormatter.string(from: 42_000)
XCTAssertEqual(formattedString, "4_2000")
}
func test_secondaryGroupingSize() {
let numberFormatter = NumberFormatter()
numberFormatter.secondaryGroupingSize = 2
numberFormatter.groupingSeparator = "_"
numberFormatter.usesGroupingSeparator = true
let formattedString = numberFormatter.string(from: 42_000_000)
XCTAssertEqual(formattedString, "4_20_00_000")
}
func test_roundingMode() {
let numberFormatter = NumberFormatter()
numberFormatter.maximumFractionDigits = 0
numberFormatter.roundingMode = .ceiling
let formattedString = numberFormatter.string(from: 41.0001)
XCTAssertEqual(formattedString, "42")
}
func test_roundingIncrement() {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.roundingIncrement = 0.2
let formattedString = numberFormatter.string(from: 4.25)
XCTAssertEqual(formattedString, "4.2")
}
func test_formatWidth() {
let numberFormatter = NumberFormatter()
numberFormatter.paddingCharacter = "_"
numberFormatter.formatWidth = 5
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "___42")
}
func test_formatPosition() {
let numberFormatter = NumberFormatter()
numberFormatter.paddingCharacter = "_"
numberFormatter.formatWidth = 5
numberFormatter.paddingPosition = .afterPrefix
let formattedString = numberFormatter.string(from: -42)
XCTAssertEqual(formattedString, "-__42")
}
func test_multiplier() {
let numberFormatter = NumberFormatter()
numberFormatter.multiplier = 2
let formattedString = numberFormatter.string(from: 21)
XCTAssertEqual(formattedString, "42")
}
func test_positivePrefix() {
let numberFormatter = NumberFormatter()
numberFormatter.positivePrefix = "👍"
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "👍42")
}
func test_positiveSuffix() {
let numberFormatter = NumberFormatter()
numberFormatter.positiveSuffix = "👍"
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "42👍")
}
func test_negativePrefix() {
let numberFormatter = NumberFormatter()
numberFormatter.negativePrefix = "👎"
let formattedString = numberFormatter.string(from: -42)
XCTAssertEqual(formattedString, "👎42")
}
func test_negativeSuffix() {
let numberFormatter = NumberFormatter()
numberFormatter.negativeSuffix = "👎"
let formattedString = numberFormatter.string(from: -42)
XCTAssertEqual(formattedString, "-42👎")
}
func test_internationalCurrencySymbol() {
// Disabled due to [SR-250]
/*
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .CurrencyPluralStyle
numberFormatter.internationalCurrencySymbol = "💵"
numberFormatter.currencyDecimalSeparator = "_"
let formattedString = numberFormatter.stringFromNumber(42)
XCTAssertEqual(formattedString, "💵 42_00")
*/
}
func test_currencyGroupingSeparator() {
// Disabled due to [SR-250]
/*
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.currencyGroupingSeparator = "_"
numberFormatter.currencyCode = "T"
numberFormatter.currencyDecimalSeparator = "/"
let formattedString = numberFormatter.stringFromNumber(42_000)
XCTAssertEqual(formattedString, "T 42_000/00")
*/
}
//FIXME: Something is wrong with numberFromString implementation, I don't know exactly why, but it's not working.
func test_lenient() {
// let numberFormatter = NumberFormatter()
// numberFormatter.numberStyle = .CurrencyStyle
// let nilNumberBeforeLenient = numberFormatter.numberFromString("42")
// XCTAssertNil(nilNumberBeforeLenient)
// numberFormatter.lenient = true
// let numberAfterLenient = numberFormatter.numberFromString("42.42")
// XCTAssertEqual(numberAfterLenient, 42.42)
}
func test_minimumSignificantDigits() {
let numberFormatter = NumberFormatter()
numberFormatter.usesSignificantDigits = true
numberFormatter.minimumSignificantDigits = 3
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "42.0")
}
func test_maximumSignificantDigits() {
let numberFormatter = NumberFormatter()
numberFormatter.usesSignificantDigits = true
numberFormatter.maximumSignificantDigits = 3
let formattedString = numberFormatter.string(from: 42.42424242)
XCTAssertEqual(formattedString, "42.4")
}
}