| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | #include "windows/Common.h" |
| 5 | #include "WSLCCLITestHelpers.h" |
| 6 | |
| 7 | #include "timestamp.hpp" |
| 8 | |
| 9 | using namespace wsl::shared; |
| 10 | using namespace wsl::windows::common::timestamp; |
| 11 | using namespace WSLCTestHelpers; |
| 12 | using namespace WEX::Logging; |
| 13 | using namespace WEX::Common; |
| 14 | using namespace WEX::TestExecution; |
| 15 | |
| 16 | namespace WSLCCLIRelativeTimeUnitTests { |
| 17 | |
| 18 | class WSLCCLIRelativeTimeUnitTests |
| 19 | { |
| 20 | WSLC_TEST_CLASS(WSLCCLIRelativeTimeUnitTests) |
| 21 | |
| 22 | TEST_CLASS_SETUP(TestClassSetup) |
| 23 | { |
| 24 | return true; |
| 25 | } |
| 26 | |
| 27 | TEST_CLASS_CLEANUP(TestClassCleanup) |
| 28 | { |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | static std::wstring FormatElapsed(LONGLONG secondsAgo) |
| 33 | { |
| 34 | return FormatElapsedSeconds(secondsAgo); |
| 35 | } |
| 36 | |
| 37 | TEST_METHOD(RelativeTime_ZeroTimestamp_ReturnsEmpty) |
| 38 | { |
| 39 | VERIFY_ARE_EQUAL(std::wstring{}, FormatRelativeTime(0)); |
| 40 | } |
| 41 | |
| 42 | TEST_METHOD(RelativeTime_NegativeElapsed_ClampsToLessThanASecond) |
| 43 | { |
| 44 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeLessThanASecond(), FormatElapsedSeconds(-600)); |
| 45 | } |
| 46 | |
| 47 | TEST_METHOD(RelativeTime_Seconds) |
| 48 | { |
| 49 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeLessThanASecond(), FormatElapsed(0)); |
| 50 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeOneSecond(), FormatElapsed(1)); |
| 51 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeSeconds(2), FormatElapsed(2)); |
| 52 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeSeconds(59), FormatElapsed(59)); |
| 53 | } |
| 54 | |
| 55 | TEST_METHOD(RelativeTime_Minutes) |
| 56 | { |
| 57 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeAboutAMinute(), FormatElapsed(60)); |
| 58 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeAboutAMinute(), FormatElapsed(119)); |
| 59 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeMinutes(2), FormatElapsed(120)); |
| 60 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeMinutes(59), FormatElapsed(59 * 60)); |
| 61 | } |
| 62 | |
| 63 | TEST_METHOD(RelativeTime_Hours) |
| 64 | { |
| 65 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeAboutAnHour(), FormatElapsed(60 * 60)); |
| 66 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeHours(2), FormatElapsed(2 * 60 * 60)); |
| 67 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeHours(47), FormatElapsed(47 * 60 * 60)); |
| 68 | } |
| 69 | |
| 70 | // The hour count is rounded to the nearest hour rather than truncated. |
| 71 | TEST_METHOD(RelativeTime_HoursAreRounded) |
| 72 | { |
| 73 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeAboutAnHour(), FormatElapsed(89 * 60)); |
| 74 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeHours(2), FormatElapsed(90 * 60)); |
| 75 | } |
| 76 | |
| 77 | TEST_METHOD(RelativeTime_Days) |
| 78 | { |
| 79 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeDays(2), FormatElapsed(48 * 60 * 60)); |
| 80 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeDays(13), FormatElapsed(13 * 24 * 60 * 60)); |
| 81 | } |
| 82 | |
| 83 | TEST_METHOD(RelativeTime_Weeks) |
| 84 | { |
| 85 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeWeeks(2), FormatElapsed(14 * 24 * 60 * 60)); |
| 86 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeWeeks(8), FormatElapsed(59 * 24 * 60 * 60)); |
| 87 | } |
| 88 | |
| 89 | TEST_METHOD(RelativeTime_Months) |
| 90 | { |
| 91 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeMonths(2), FormatElapsed(60 * 24 * 60 * 60)); |
| 92 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeMonths(12), FormatElapsed(365 * 24 * 60 * 60)); |
| 93 | } |
| 94 | |
| 95 | TEST_METHOD(RelativeTime_Years) |
| 96 | { |
| 97 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeYears(2), FormatElapsed(730 * 24 * 60 * 60LL)); |
| 98 | VERIFY_ARE_EQUAL(Localization::WSLCCLI_RelativeTimeYears(3), FormatElapsed(3 * 365 * 24 * 60 * 60LL)); |
| 99 | } |
| 100 | |
| 101 | // The invariant rendering backs machine readable output, so it must stay in English and match the |
| 102 | // strings docker produces through go-units HumanDuration. |
| 103 | TEST_METHOD(RelativeTime_Invariant) |
| 104 | { |
| 105 | VERIFY_ARE_EQUAL(std::wstring{}, FormatInvariantRelativeTime(0)); |
| 106 | VERIFY_ARE_EQUAL(std::wstring{L"Less than a second ago"}, FormatInvariantElapsedSeconds(-600)); |
| 107 | VERIFY_ARE_EQUAL(std::wstring{L"Less than a second ago"}, FormatInvariantElapsedSeconds(0)); |
| 108 | VERIFY_ARE_EQUAL(std::wstring{L"1 second ago"}, FormatInvariantElapsedSeconds(1)); |
| 109 | VERIFY_ARE_EQUAL(std::wstring{L"59 seconds ago"}, FormatInvariantElapsedSeconds(59)); |
| 110 | VERIFY_ARE_EQUAL(std::wstring{L"About a minute ago"}, FormatInvariantElapsedSeconds(60)); |
| 111 | VERIFY_ARE_EQUAL(std::wstring{L"2 minutes ago"}, FormatInvariantElapsedSeconds(120)); |
| 112 | VERIFY_ARE_EQUAL(std::wstring{L"About an hour ago"}, FormatInvariantElapsedSeconds(89 * 60)); |
| 113 | VERIFY_ARE_EQUAL(std::wstring{L"2 hours ago"}, FormatInvariantElapsedSeconds(90 * 60)); |
| 114 | VERIFY_ARE_EQUAL(std::wstring{L"47 hours ago"}, FormatInvariantElapsedSeconds(47 * 60 * 60)); |
| 115 | VERIFY_ARE_EQUAL(std::wstring{L"2 days ago"}, FormatInvariantElapsedSeconds(48 * 60 * 60)); |
| 116 | VERIFY_ARE_EQUAL(std::wstring{L"2 weeks ago"}, FormatInvariantElapsedSeconds(14 * 24 * 60 * 60)); |
| 117 | VERIFY_ARE_EQUAL(std::wstring{L"2 months ago"}, FormatInvariantElapsedSeconds(60 * 24 * 60 * 60)); |
| 118 | VERIFY_ARE_EQUAL(std::wstring{L"2 years ago"}, FormatInvariantElapsedSeconds(730 * 24 * 60 * 60LL)); |
| 119 | } |
| 120 | |
| 121 | // Both renderings share one set of thresholds, so pin the invariant one at every boundary. This |
| 122 | // cannot compare against the localized rendering, which varies with the machine's display language. |
| 123 | TEST_METHOD(RelativeTime_InvariantBoundaries) |
| 124 | { |
| 125 | VERIFY_ARE_EQUAL(std::wstring{L"2 seconds ago"}, FormatInvariantElapsedSeconds(2)); |
| 126 | VERIFY_ARE_EQUAL(std::wstring{L"About a minute ago"}, FormatInvariantElapsedSeconds(119)); |
| 127 | VERIFY_ARE_EQUAL(std::wstring{L"59 minutes ago"}, FormatInvariantElapsedSeconds(59 * 60)); |
| 128 | VERIFY_ARE_EQUAL(std::wstring{L"13 days ago"}, FormatInvariantElapsedSeconds(13 * 24 * 60 * 60)); |
| 129 | VERIFY_ARE_EQUAL(std::wstring{L"8 weeks ago"}, FormatInvariantElapsedSeconds(59 * 24 * 60 * 60)); |
| 130 | VERIFY_ARE_EQUAL(std::wstring{L"12 months ago"}, FormatInvariantElapsedSeconds(365 * 24 * 60 * 60)); |
| 131 | VERIFY_ARE_EQUAL(std::wstring{L"3 years ago"}, FormatInvariantElapsedSeconds(3 * 365 * 24 * 60 * 60LL)); |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | } // namespace WSLCCLIRelativeTimeUnitTests |