DateFormatterTests.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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: XCTestCase {
  23. // 1.2.2020 13:14:15.00016 in the current system time zone
  24. static var testDate: Date {
  25. var dateComponents = DateComponents()
  26. dateComponents.day = 1
  27. dateComponents.month = 2
  28. dateComponents.year = 2020
  29. dateComponents.hour = 13
  30. dateComponents.minute = 14
  31. dateComponents.second = 15
  32. dateComponents.nanosecond = 16
  33. dateComponents.timeZone = Calendar.current.timeZone
  34. return Calendar.current.date(from: dateComponents)!
  35. }
  36. // Test dates for relative formatting
  37. // 20.5.2020 13:14:15.00016 GMT+1
  38. static var testDateThisYear: Date {
  39. var dateComponents = DateComponents()
  40. dateComponents.day = 20
  41. dateComponents.month = 5
  42. dateComponents.year = 2020
  43. dateComponents.hour = 13
  44. dateComponents.minute = 14
  45. dateComponents.second = 15
  46. dateComponents.nanosecond = 16
  47. dateComponents.timeZone = TimeZone(abbreviation: "GMT+1")
  48. return Calendar.current.date(from: dateComponents)!
  49. }
  50. // 31.12.2019 22:23:24.00025 GMT+1
  51. static var testDateLastCalendarYear: Date {
  52. var dateComponents = DateComponents()
  53. dateComponents.day = 31
  54. dateComponents.month = 12
  55. dateComponents.year = 2019
  56. dateComponents.hour = 22
  57. dateComponents.minute = 23
  58. dateComponents.second = 24
  59. dateComponents.nanosecond = 25
  60. dateComponents.timeZone = TimeZone(abbreviation: "GMT+1")
  61. return Calendar.current.date(from: dateComponents)!
  62. }
  63. // 1.2.2019 13:14:15.00016 GMT+1
  64. static var testDateMoreThanAYearAgo: Date {
  65. var dateComponents = DateComponents()
  66. dateComponents.day = 1
  67. dateComponents.month = 2
  68. dateComponents.year = 2019
  69. dateComponents.hour = 13
  70. dateComponents.minute = 14
  71. dateComponents.second = 15
  72. dateComponents.nanosecond = 16
  73. dateComponents.timeZone = TimeZone(abbreviation: "GMT+1")
  74. return Calendar.current.date(from: dateComponents)!
  75. }
  76. func testGetDateForWeb() {
  77. let expected = "20200102-131415"
  78. let actual = DateFormatter.getDateForWeb(DateFormatterTests.testDate)
  79. XCTAssertEqual(actual, expected)
  80. }
  81. func testTimeFormattedSeconds() {
  82. let expected = "00:59"
  83. let inputSeconds = 59
  84. let actual = DateFormatter.timeFormatted(inputSeconds)
  85. XCTAssertEqual(actual, expected)
  86. }
  87. func testTimeFormattedMinutes() {
  88. let expected = "01:11"
  89. let inputSeconds = 71
  90. let actual = DateFormatter.timeFormatted(inputSeconds)
  91. XCTAssertEqual(actual, expected)
  92. }
  93. func testTimeFormattedHour() {
  94. let expected = "02:11:03"
  95. let inputSeconds = 7863
  96. let actual = DateFormatter.timeFormatted(inputSeconds)
  97. XCTAssertEqual(actual, expected)
  98. }
  99. func testTotalSeconds() {
  100. let expected = 541
  101. let inputString = "09:01"
  102. let actual = DateFormatter.totalSeconds(inputString)
  103. XCTAssertEqual(actual, expected)
  104. }
  105. func testTotalSecondsNoLeadingZero() {
  106. let expected = 482
  107. let inputString = "8:02"
  108. let actual = DateFormatter.totalSeconds(inputString)
  109. XCTAssertEqual(actual, expected)
  110. }
  111. func testTotalSecondsWithHours() {
  112. let expected = 8411
  113. let inputString = "02:20:11"
  114. let actual = DateFormatter.totalSeconds(inputString)
  115. XCTAssertEqual(actual, expected)
  116. }
  117. func testTotalSecondsWithZeroHours() {
  118. let expected = 1211
  119. let inputString = "00:20:11"
  120. let actual = DateFormatter.totalSeconds(inputString)
  121. XCTAssertEqual(actual, expected)
  122. }
  123. func testTotalSecondsWithCharacterInput() {
  124. let expected = 3611
  125. let inputString = "01:f:11"
  126. let actual = DateFormatter.totalSeconds(inputString)
  127. XCTAssertEqual(actual, expected)
  128. }
  129. func testTotalSecondsWithWnlySecondsInput() {
  130. let expected = 51
  131. let inputString = "51"
  132. let actual = DateFormatter.totalSeconds(inputString)
  133. XCTAssertEqual(actual, expected)
  134. }
  135. func testReversingOfTimeFormattedWithTotalSecondsShort() {
  136. let expected = 120
  137. let actual = DateFormatter.totalSeconds(DateFormatter.timeFormatted(expected))
  138. XCTAssertEqual(actual, expected)
  139. }
  140. func testReversingOfTimeFormattedWithTotalSecondsLong() {
  141. let expected = 53294
  142. let actual = DateFormatter.totalSeconds(DateFormatter.timeFormatted(expected))
  143. XCTAssertEqual(actual, expected)
  144. }
  145. func testReversingOfTotalSecondsWithTimeFormattedShort() {
  146. let expected = "04:58"
  147. let actual = DateFormatter.timeFormatted(DateFormatter.totalSeconds(expected))
  148. XCTAssertEqual(actual, expected)
  149. }
  150. func testReversingOfTotalSecondsWithTimeFormattedShortNoLeadingZero() {
  151. let input = "4:58"
  152. let expected = "04:58"
  153. let actual = DateFormatter.timeFormatted(DateFormatter.totalSeconds(input))
  154. XCTAssertEqual(actual, expected)
  155. }
  156. func testReversingOfTotalSecondsWithTimeFormattedLong() {
  157. let expected = "13:04:58"
  158. let actual = DateFormatter.timeFormatted(DateFormatter.totalSeconds(expected))
  159. XCTAssertEqual(actual, expected)
  160. }
  161. // MARK: - Test to `Date` converter
  162. func testGetDateFromDayMonthAndYearDateStringWithEmptyString() {
  163. let result = DateFormatter.getDateFromDayMonthAndYearDateString("")
  164. XCTAssertNil(result)
  165. }
  166. func testGetDateFromFullDateStringWithEmptyString() {
  167. let result = DateFormatter.getDateFromFullDateString("")
  168. XCTAssertNil(result)
  169. }
  170. }