DateFormatterTests_de_DE.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import XCTest
  21. @testable import ThreemaFramework
  22. class DateFormatterTests_de_DE: XCTestCase {
  23. let localeIdentifier = "de_DE"
  24. override func setUp() {
  25. DateFormatter.locale = Locale(identifier: localeIdentifier)
  26. }
  27. // Expected date strings
  28. let expectedShortStyleDateTime_de_DE = "01.02.20, 13:14"
  29. let expectedShortStyleDateTimeSeconds_de_DE = "01.02.20, 13:14:15"
  30. let expectedMediumStyleDateTime_de_DE = "01.02.2020, 13:14:15"
  31. let expectedMediumStyleDateShortStyleTime_de_DE = "01.02.2020, 13:14"
  32. let expectedLongStyleDateTime_de_DE_prefix = "1. Februar 2020 um 13:14:15 "
  33. let expectedShortStyleTimeNoDate_de_DE = "13:14"
  34. let expectedGetShortDate_de_DE = "1.2.2020"
  35. let expectedGetDayMonthAndYear_de_DE = "Sa. 01. Feb. 2020"
  36. let expectedGetFullDateFor_de_DE = "Sa. 01. Feb. 2020, 13:14"
  37. let expectedRelativeMediumDateYesterday_de_DE = "Gestern"
  38. let expectedRelativeMediumDateThisYear_de_DE = "Mi. 20. Mai"
  39. let expectedRelativeMediumDateLastCalendarYear_de_DE = "Di. 31. Dez. 2019"
  40. let expectedRelativeMediumDateMoreThanAYearAgo_de_DE = "Fr. 01. Feb. 2019"
  41. let expectedAccessibilityDateTime_de_DE = "1. Februar 2020, 13:14"
  42. let expectedAccessibilityRelativeDayTime_de_DE = "1. Februar 2020 um 13:14"
  43. // MARK: - Test formats provided by the system
  44. func testShortStyleDateTime() {
  45. let actual: String = DateFormatter.shortStyleDateTime(DateFormatterTests.testDate)
  46. XCTAssertEqual(actual, expectedShortStyleDateTime_de_DE)
  47. }
  48. func testShortStyleDateTimeSeconds() {
  49. let actual: String = DateFormatter.shortStyleDateTimeSeconds(DateFormatterTests.testDate)
  50. XCTAssertEqual(actual, expectedShortStyleDateTimeSeconds_de_DE)
  51. }
  52. func testMediumStyleDateTime() {
  53. let actual: String = DateFormatter.mediumStyleDateTime(DateFormatterTests.testDate)
  54. XCTAssertEqual(actual, expectedMediumStyleDateTime_de_DE)
  55. }
  56. func testMediumStyleDateShortStyleTime() {
  57. let actual = DateFormatter.mediumStyleDateShortStyleTime(DateFormatterTests.testDate)
  58. XCTAssertEqual(actual, expectedMediumStyleDateShortStyleTime_de_DE)
  59. }
  60. func testLongStyleDateTime() {
  61. let actual: String = DateFormatter.longStyleDateTime(DateFormatterTests.testDate)
  62. // As time zone suffix changes between system time zones we can only test for the prefix
  63. XCTAssertTrue(actual.hasPrefix(expectedLongStyleDateTime_de_DE_prefix))
  64. }
  65. func testShortStyleTimeNoDate() {
  66. let actual = DateFormatter.shortStyleTimeNoDate(DateFormatterTests.testDate)
  67. XCTAssertEqual(actual, expectedShortStyleTimeNoDate_de_DE)
  68. }
  69. // MARK: - Test custom formats
  70. func testGetShortDate() {
  71. let actual = DateFormatter.getShortDate(DateFormatterTests.testDate)
  72. XCTAssertEqual(actual, expectedGetShortDate_de_DE)
  73. }
  74. func testGetDayMonthAndYear() {
  75. let actual = DateFormatter.getDayMonthAndYear(DateFormatterTests.testDate)
  76. XCTAssertEqual(actual, expectedGetDayMonthAndYear_de_DE)
  77. }
  78. func testGetFullDateFor() {
  79. let actual = DateFormatter.getFullDate(for: DateFormatterTests.testDate)
  80. XCTAssertEqual(actual, expectedGetFullDateFor_de_DE)
  81. }
  82. func testGetFullDateForWithReinitalization() {
  83. DateFormatter.forceReinitialize()
  84. DateFormatter.locale = Locale(identifier: localeIdentifier)
  85. let actual = DateFormatter.getFullDate(for: DateFormatterTests.testDate)
  86. XCTAssertEqual(actual, expectedGetFullDateFor_de_DE)
  87. }
  88. // MARK: - Test to `Date` converter
  89. func testGetDateFromDayMonthAndYearDateStringWithReinitalization() throws {
  90. DateFormatter.forceReinitialize()
  91. DateFormatter.locale = Locale(identifier: localeIdentifier)
  92. let actual = try XCTUnwrap(DateFormatter.getDateFromDayMonthAndYearDateString(expectedGetDayMonthAndYear_de_DE))
  93. let comparisonResult = Calendar.current.compare(actual, to: DateFormatterTests.testDate, toGranularity: .day)
  94. XCTAssertEqual(comparisonResult, .orderedSame, "The dates are not equal up to the day")
  95. }
  96. func testGetDateFromFullDateStringWithReinitalization() throws {
  97. DateFormatter.forceReinitialize()
  98. DateFormatter.locale = Locale(identifier: localeIdentifier)
  99. let actual = try XCTUnwrap(DateFormatter.getDateFromFullDateString(expectedGetFullDateFor_de_DE))
  100. let comparisonResult = Calendar.current.compare(actual, to: DateFormatterTests.testDate, toGranularity: .minute)
  101. XCTAssertEqual(comparisonResult, .orderedSame, "The dates are not equal up to the second")
  102. }
  103. // MARK: - Test relative custom formats
  104. func testRelativeMediumDateYesterday() throws {
  105. let twentyFourHoursAgo = Date(timeIntervalSinceNow: -(60 * 60 * 24))
  106. let actual = DateFormatter.relativeMediumDate(for: twentyFourHoursAgo)
  107. XCTAssertEqual(actual, expectedRelativeMediumDateYesterday_de_DE)
  108. }
  109. // This test will fail each May.
  110. // 1. Update `DateFormatterTests.testDateThisYear` by increasing the year by 1
  111. // 2. Update expected string by increasing year by 1
  112. func testRelativeMediumDateThisYearWithReset() {
  113. DateFormatter.forceReinitialize()
  114. DateFormatter.locale = Locale(identifier: localeIdentifier)
  115. let actual = DateFormatter.relativeMediumDate(for: DateFormatterTests.testDateThisYear)
  116. XCTAssertEqual(actual, expectedRelativeMediumDateThisYear_de_DE)
  117. }
  118. // This test will fail each New Year.
  119. // 1. Update `DateFormatterTests.testDateLastCalendarYear` by increasing the year by 1
  120. // 2. Update expected string by increasing year by 1
  121. func testRelativeMediumDateLastCalendarYear() {
  122. let actual = DateFormatter.relativeMediumDate(for: DateFormatterTests.testDateLastCalendarYear)
  123. XCTAssertEqual(actual, expectedRelativeMediumDateLastCalendarYear_de_DE)
  124. }
  125. func testRelativeMediumDateLastYear() {
  126. let actual = DateFormatter.relativeMediumDate(for: DateFormatterTests.testDateMoreThanAYearAgo)
  127. XCTAssertEqual(actual, expectedRelativeMediumDateMoreThanAYearAgo_de_DE)
  128. }
  129. // MARK: - Test accessibility formats
  130. func testAccessibilityDateTime() {
  131. let actual = DateFormatter.accessibilityDateTime(DateFormatterTests.testDate)
  132. XCTAssertEqual(actual, expectedAccessibilityDateTime_de_DE)
  133. }
  134. func testAccessibilityRelativeDayTime() {
  135. let actual = DateFormatter.accessibilityRelativeDayTime(DateFormatterTests.testDate)
  136. XCTAssertEqual(actual, expectedAccessibilityRelativeDayTime_de_DE)
  137. }
  138. }