/ src / modules / cmdpal / Tests / Microsoft.CmdPal.Ext.TimeDate.UnitTests / TimeAndDateHelperTests.cs
TimeAndDateHelperTests.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Globalization; 7 using Microsoft.CmdPal.Ext.TimeDate.Helpers; 8 using Microsoft.VisualStudio.TestTools.UnitTesting; 9 10 namespace Microsoft.CmdPal.Ext.TimeDate.UnitTests; 11 12 [TestClass] 13 public class TimeAndDateHelperTests 14 { 15 private CultureInfo originalCulture; 16 private CultureInfo originalUiCulture; 17 18 [TestInitialize] 19 public void Setup() 20 { 21 // Set culture to 'en-us' 22 originalCulture = CultureInfo.CurrentCulture; 23 CultureInfo.CurrentCulture = new CultureInfo("en-us", false); 24 originalUiCulture = CultureInfo.CurrentUICulture; 25 CultureInfo.CurrentUICulture = new CultureInfo("en-us", false); 26 } 27 28 [TestCleanup] 29 public void Cleanup() 30 { 31 // Restore original culture 32 CultureInfo.CurrentCulture = originalCulture; 33 CultureInfo.CurrentUICulture = originalUiCulture; 34 } 35 36 [DataTestMethod] 37 [DataRow(-1, null)] // default setting 38 [DataRow(0, CalendarWeekRule.FirstDay)] 39 [DataRow(1, CalendarWeekRule.FirstFullWeek)] 40 [DataRow(2, CalendarWeekRule.FirstFourDayWeek)] 41 [DataRow(30, null)] // wrong setting 42 public void GetCalendarWeekRuleBasedOnPluginSetting(int setting, CalendarWeekRule? valueExpected) 43 { 44 // Act 45 var result = TimeAndDateHelper.GetCalendarWeekRule(setting); 46 47 // Assert 48 if (valueExpected is null) 49 { 50 // falls back to system setting. 51 Assert.AreEqual(DateTimeFormatInfo.CurrentInfo.CalendarWeekRule, result); 52 } 53 else 54 { 55 Assert.AreEqual(valueExpected, result); 56 } 57 } 58 59 [DataTestMethod] 60 [DataRow(-1, null)] // default setting 61 [DataRow(0, DayOfWeek.Sunday)] 62 [DataRow(1, DayOfWeek.Monday)] 63 [DataRow(2, DayOfWeek.Tuesday)] 64 [DataRow(3, DayOfWeek.Wednesday)] 65 [DataRow(4, DayOfWeek.Thursday)] 66 [DataRow(5, DayOfWeek.Friday)] 67 [DataRow(6, DayOfWeek.Saturday)] 68 [DataRow(30, null)] // wrong setting 69 public void GetFirstDayOfWeekBasedOnPluginSetting(int setting, DayOfWeek? valueExpected) 70 { 71 // Act 72 var result = TimeAndDateHelper.GetFirstDayOfWeek(setting); 73 74 // Assert 75 if (valueExpected is null) 76 { 77 // falls back to system setting. 78 Assert.AreEqual(DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek, result); 79 } 80 else 81 { 82 Assert.AreEqual(valueExpected, result); 83 } 84 } 85 86 [DataTestMethod] 87 [DataRow("yyyy-MM-dd", "2023-12-25")] 88 [DataRow("MM/dd/yyyy", "12/25/2023")] 89 [DataRow("dd.MM.yyyy", "25.12.2023")] 90 public void GetDateTimeFormatTest(string format, string expectedPattern) 91 { 92 // Setup 93 var testDate = new DateTime(2023, 12, 25); 94 95 // Act 96 var result = testDate.ToString(format, CultureInfo.CurrentCulture); 97 98 // Assert 99 Assert.AreEqual(expectedPattern, result); 100 } 101 102 [TestMethod] 103 public void GetCurrentTimeFormatTest() 104 { 105 // Setup 106 var testDateTime = new DateTime(2023, 12, 25, 14, 30, 45); 107 108 // Act 109 var timeResult = testDateTime.ToString("T", CultureInfo.CurrentCulture); 110 var dateResult = testDateTime.ToString("d", CultureInfo.CurrentCulture); 111 112 // Assert 113 Assert.AreEqual("2:30:45 PM", timeResult); 114 Assert.AreEqual("12/25/2023", dateResult); 115 } 116 117 [DataTestMethod] 118 [DataRow("yyyy-MM-dd HH:mm:ss", "2023-12-25 14:30:45")] 119 [DataRow("dddd, MMMM dd, yyyy", "Monday, December 25, 2023")] 120 [DataRow("HH:mm:ss tt", "14:30:45 PM")] 121 public void ValidateCustomDateTimeFormats(string format, string expectedResult) 122 { 123 // Setup 124 var testDate = new DateTime(2023, 12, 25, 14, 30, 45); 125 126 // Act 127 var result = testDate.ToString(format, CultureInfo.CurrentCulture); 128 129 // Assert 130 Assert.AreEqual(expectedResult, result); 131 } 132 }