@@ -28,6 +28,7 @@ class TestDateFormatter: XCTestCase {
28
28
( " test_expectedTimeZone " , test_expectedTimeZone) ,
29
29
( " test_dateFrom " , test_dateFrom) ,
30
30
( " test_dateParseAndFormatWithJapaneseCalendar " , test_dateParseAndFormatWithJapaneseCalendar) ,
31
+ ( " test_orderOfPropertySetters " , test_orderOfPropertySetters) ,
31
32
]
32
33
}
33
34
@@ -443,4 +444,64 @@ class TestDateFormatter: XCTestCase {
443
444
let dateString = formatter. string ( from: Date ( timeIntervalSince1970: 1556633400 ) ) // April 30, 2019, 11:10 PM (JST)
444
445
XCTAssertEqual ( dateString, " 平成31年4月30日 23:10 " )
445
446
}
447
+
448
+ func test_orderOfPropertySetters( ) throws {
449
+
450
+ // This produces a .count factorial number of arrays
451
+ func combinations< T> ( of a: [ T ] ) -> [ [ T ] ] {
452
+ precondition ( !a. isEmpty)
453
+ if a. count == 1 { return [ a] }
454
+ if a. count == 2 { return [ [ a [ 0 ] , a [ 1 ] ] , [ a [ 1 ] , a [ 0 ] ] ] }
455
+
456
+ var result : [ [ T ] ] = [ ]
457
+
458
+ for idx in a. startIndex..< a. endIndex {
459
+ let x = a [ idx]
460
+ var b : [ T ] = a
461
+ b. remove ( at: idx)
462
+
463
+ for var c in combinations ( of: b) {
464
+ c. append ( x)
465
+ result. append ( c)
466
+ }
467
+ }
468
+ return result
469
+ }
470
+
471
+ let formatter = DateFormatter ( )
472
+ formatter. timeZone = TimeZone ( identifier: " CET " )
473
+ formatter. dateFormat = " yyyy-MM-dd'T'HH:mm:ss "
474
+ let date = try formatter. date ( from: " 2019-05-05T12:52:10 " ) . unwrapped ( )
475
+
476
+ let applySettings : [ ( String , ( DateFormatter ) -> Void ) ] =
477
+ [ ( " .timeZone " , {
478
+ $0. timeZone = TimeZone ( identifier: " Europe/Oslo " )
479
+ } ) ,
480
+ ( " .calendar " , {
481
+ $0. calendar = Calendar ( identifier: . gregorian)
482
+ } ) ,
483
+ ( " .locale " , {
484
+ $0. locale = Locale ( identifier: " nb " )
485
+ } ) ,
486
+ ( " .dateStyle " , {
487
+ $0. dateStyle = . medium
488
+ } ) ,
489
+ ( " .timeStyle " , {
490
+ $0. timeStyle = . medium
491
+ } )
492
+ ]
493
+
494
+ // Test all of the combinations of setting the properties produces the same output
495
+ let expected = " 5. mai 2019, 12:52:10 "
496
+ for settings in combinations ( of: applySettings) {
497
+ let f = DateFormatter ( )
498
+ settings. forEach { $0. 1 ( f) }
499
+ XCTAssertEqual ( f. dateFormat, " d. MMM y, HH:mm:ss " )
500
+ let formattedString = f. string ( from: date)
501
+ if formattedString != expected {
502
+ let applied = settings. map { $0. 0 } . joined ( separator: " , " )
503
+ XCTFail ( " \( formattedString) != \( expected) using settings applied in order \( applied) " )
504
+ }
505
+ }
506
+ }
446
507
}
0 commit comments