// _____ _ // |_ _| |_ _ _ ___ ___ _ __ __ _ // | | | ' \| '_/ -_) -_) ' \/ _` |_ // |_| |_||_|_| \___\___|_|_|_\__,_(_) // // Threema iOS Client // Copyright (c) 2020 Threema GmbH // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License, version 3, // as published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . import XCTest @testable import ThreemaFramework class DateFormatterTests_de_DE: XCTestCase { let localeIdentifier = "de_DE" override func setUp() { DateFormatter.locale = Locale(identifier: localeIdentifier) } // Expected date strings let expectedShortStyleDateTime_de_DE = "01.02.20, 13:14" let expectedShortStyleDateTimeSeconds_de_DE = "01.02.20, 13:14:15" let expectedMediumStyleDateTime_de_DE = "01.02.2020, 13:14:15" let expectedMediumStyleDateShortStyleTime_de_DE = "01.02.2020, 13:14" let expectedLongStyleDateTime_de_DE_prefix = "1. Februar 2020 um 13:14:15 " let expectedShortStyleTimeNoDate_de_DE = "13:14" let expectedGetShortDate_de_DE = "1.2.2020" let expectedGetDayMonthAndYear_de_DE = "Sa. 01. Feb. 2020" let expectedGetFullDateFor_de_DE = "Sa. 01. Feb. 2020, 13:14" let expectedRelativeMediumDateYesterday_de_DE = "Gestern" let expectedRelativeMediumDateThisYear_de_DE = "Mi. 20. Mai" let expectedRelativeMediumDateLastCalendarYear_de_DE = "Di. 31. Dez. 2019" let expectedRelativeMediumDateMoreThanAYearAgo_de_DE = "Fr. 01. Feb. 2019" let expectedAccessibilityDateTime_de_DE = "1. Februar 2020, 13:14" let expectedAccessibilityRelativeDayTime_de_DE = "1. Februar 2020 um 13:14" // MARK: - Test formats provided by the system func testShortStyleDateTime() { let actual: String = DateFormatter.shortStyleDateTime(DateFormatterTests.testDate) XCTAssertEqual(actual, expectedShortStyleDateTime_de_DE) } func testShortStyleDateTimeSeconds() { let actual: String = DateFormatter.shortStyleDateTimeSeconds(DateFormatterTests.testDate) XCTAssertEqual(actual, expectedShortStyleDateTimeSeconds_de_DE) } func testMediumStyleDateTime() { let actual: String = DateFormatter.mediumStyleDateTime(DateFormatterTests.testDate) XCTAssertEqual(actual, expectedMediumStyleDateTime_de_DE) } func testMediumStyleDateShortStyleTime() { let actual = DateFormatter.mediumStyleDateShortStyleTime(DateFormatterTests.testDate) XCTAssertEqual(actual, expectedMediumStyleDateShortStyleTime_de_DE) } func testLongStyleDateTime() { let actual: String = DateFormatter.longStyleDateTime(DateFormatterTests.testDate) // As time zone suffix changes between system time zones we can only test for the prefix XCTAssertTrue(actual.hasPrefix(expectedLongStyleDateTime_de_DE_prefix)) } func testShortStyleTimeNoDate() { let actual = DateFormatter.shortStyleTimeNoDate(DateFormatterTests.testDate) XCTAssertEqual(actual, expectedShortStyleTimeNoDate_de_DE) } // MARK: - Test custom formats func testGetShortDate() { let actual = DateFormatter.getShortDate(DateFormatterTests.testDate) XCTAssertEqual(actual, expectedGetShortDate_de_DE) } func testGetDayMonthAndYear() { let actual = DateFormatter.getDayMonthAndYear(DateFormatterTests.testDate) XCTAssertEqual(actual, expectedGetDayMonthAndYear_de_DE) } func testGetFullDateFor() { let actual = DateFormatter.getFullDate(for: DateFormatterTests.testDate) XCTAssertEqual(actual, expectedGetFullDateFor_de_DE) } func testGetFullDateForWithReinitalization() { DateFormatter.forceReinitialize() DateFormatter.locale = Locale(identifier: localeIdentifier) let actual = DateFormatter.getFullDate(for: DateFormatterTests.testDate) XCTAssertEqual(actual, expectedGetFullDateFor_de_DE) } // MARK: - Test to `Date` converter func testGetDateFromDayMonthAndYearDateStringWithReinitalization() throws { DateFormatter.forceReinitialize() DateFormatter.locale = Locale(identifier: localeIdentifier) let actual = try XCTUnwrap(DateFormatter.getDateFromDayMonthAndYearDateString(expectedGetDayMonthAndYear_de_DE)) let comparisonResult = Calendar.current.compare(actual, to: DateFormatterTests.testDate, toGranularity: .day) XCTAssertEqual(comparisonResult, .orderedSame, "The dates are not equal up to the day") } func testGetDateFromFullDateStringWithReinitalization() throws { DateFormatter.forceReinitialize() DateFormatter.locale = Locale(identifier: localeIdentifier) let actual = try XCTUnwrap(DateFormatter.getDateFromFullDateString(expectedGetFullDateFor_de_DE)) let comparisonResult = Calendar.current.compare(actual, to: DateFormatterTests.testDate, toGranularity: .minute) XCTAssertEqual(comparisonResult, .orderedSame, "The dates are not equal up to the second") } // MARK: - Test relative custom formats func testRelativeMediumDateYesterday() throws { let twentyFourHoursAgo = Date(timeIntervalSinceNow: -(60 * 60 * 24)) let actual = DateFormatter.relativeMediumDate(for: twentyFourHoursAgo) XCTAssertEqual(actual, expectedRelativeMediumDateYesterday_de_DE) } // This test will fail each May. // 1. Update `DateFormatterTests.testDateThisYear` by increasing the year by 1 // 2. Update expected string by increasing year by 1 func testRelativeMediumDateThisYearWithReset() { DateFormatter.forceReinitialize() DateFormatter.locale = Locale(identifier: localeIdentifier) let actual = DateFormatter.relativeMediumDate(for: DateFormatterTests.testDateThisYear) XCTAssertEqual(actual, expectedRelativeMediumDateThisYear_de_DE) } // This test will fail each New Year. // 1. Update `DateFormatterTests.testDateLastCalendarYear` by increasing the year by 1 // 2. Update expected string by increasing year by 1 func testRelativeMediumDateLastCalendarYear() { let actual = DateFormatter.relativeMediumDate(for: DateFormatterTests.testDateLastCalendarYear) XCTAssertEqual(actual, expectedRelativeMediumDateLastCalendarYear_de_DE) } func testRelativeMediumDateLastYear() { let actual = DateFormatter.relativeMediumDate(for: DateFormatterTests.testDateMoreThanAYearAgo) XCTAssertEqual(actual, expectedRelativeMediumDateMoreThanAYearAgo_de_DE) } // MARK: - Test accessibility formats func testAccessibilityDateTime() { let actual = DateFormatter.accessibilityDateTime(DateFormatterTests.testDate) XCTAssertEqual(actual, expectedAccessibilityDateTime_de_DE) } func testAccessibilityRelativeDayTime() { let actual = DateFormatter.accessibilityRelativeDayTime(DateFormatterTests.testDate) XCTAssertEqual(actual, expectedAccessibilityRelativeDayTime_de_DE) } }