StringParserTests.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  
 8  using Microsoft.PowerToys.Run.Plugin.TimeDate.Components;
 9  using Microsoft.VisualStudio.TestTools.UnitTesting;
10  
11  namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
12  {
13      [TestClass]
14      public class StringParserTests
15      {
16          private CultureInfo originalCulture;
17          private CultureInfo originalUiCulture;
18  
19          [TestInitialize]
20          public void Setup()
21          {
22              // Set culture to 'en-us'
23              originalCulture = CultureInfo.CurrentCulture;
24              CultureInfo.CurrentCulture = new CultureInfo("en-us", false);
25              originalUiCulture = CultureInfo.CurrentUICulture;
26              CultureInfo.CurrentUICulture = new CultureInfo("en-us", false);
27          }
28  
29          [DataTestMethod]
30          [DataRow("10/29/2022 17:05:10", true, "G", "10/29/2022 5:05:10 PM")]
31          [DataRow("Saturday, October 29, 2022 5:05:10 PM", true, "G", "10/29/2022 5:05:10 PM")]
32          [DataRow("10/29/2022", true, "d", "10/29/2022")]
33          [DataRow("Saturday, October 29, 2022", true, "d", "10/29/2022")]
34          [DataRow("17:05:10", true, "T", "5:05:10 PM")]
35          [DataRow("5:05:10 PM", true, "T", "5:05:10 PM")]
36          [DataRow("10456", false, "", "")]
37          [DataRow("u10456", true, "", "")] // Value is UTC and can be different based on system
38          [DataRow("u-10456", true, "", "")] // Value is UTC and can be different based on system
39          [DataRow("u+10456", true, "", "")] // Value is UTC and can be different based on system
40          [DataRow("ums10456", true, "", "")] // Value is UTC and can be different based on system
41          [DataRow("ums-10456", true, "", "")] // Value is UTC and can be different based on system
42          [DataRow("ums+10456", true, "", "")] // Value is UTC and can be different based on system
43          [DataRow("ft10456", true, "", "")] // Value is UTC and can be different based on system
44          [DataRow("oa-657434.99999999", true, "G", "1/1/0100 11:59:59 PM")]
45          [DataRow("oa2958465.99999999", true, "G", "12/31/9999 11:59:59 PM")]
46          [DataRow("oa-657435", false, "", "")] // Value to low
47          [DataRow("oa2958466", false, "", "")] // Value to large
48          [DataRow("exc1.99998843", true, "G", "1/1/1900 11:59:59 PM")]
49          [DataRow("exc59.99998843", true, "G", "2/28/1900 11:59:59 PM")]
50          [DataRow("exc61", true, "G", "3/1/1900 12:00:00 AM")]
51          [DataRow("exc62.99998843", true, "G", "3/2/1900 11:59:59 PM")]
52          [DataRow("exc2958465.99998843", true, "G", "12/31/9999 11:59:59 PM")]
53          [DataRow("exc0", false, "", "")] // Day 0 means in Excel 0/1/1900 and this is a fake date.
54          [DataRow("exc0.99998843", false, "", "")] // Day 0 means in Excel 0/1/1900 and this is a fake date.
55          [DataRow("exc60.99998843", false, "", "")] // Day 60 means in Excel 2/29/1900 and this is a fake date in Excel which we cannot support.
56          [DataRow("exc60", false, "", "")] // Day 60 means in Excel 2/29/1900 and this is a fake date in Excel which we cannot support.
57          [DataRow("exc-1", false, "", "")] // Value to low
58          [DataRow("exc2958466", false, "", "")] // Value to large
59          [DataRow("exf0.99998843", true, "G", "1/1/1904 11:59:59 PM")]
60          [DataRow("exf2957003.99998843", true, "G", "12/31/9999 11:59:59 PM")]
61          [DataRow("exf-0.5", false, "", "")] // Value to low
62          [DataRow("exf2957004", false, "", "")] // Value to large
63          public void ConvertStringToDateTime(string typedString, bool expectedBool, string stringType, string expectedString)
64          {
65              // Act
66              bool boolResult = TimeAndDateHelper.ParseStringAsDateTime(in typedString, out DateTime result, out string _);
67  
68              // Assert
69              Assert.AreEqual(expectedBool, boolResult);
70              if (!string.IsNullOrEmpty(expectedString))
71              {
72                  Assert.AreEqual(expectedString, result.ToString(stringType, CultureInfo.CurrentCulture));
73              }
74          }
75  
76          [TestCleanup]
77          public void CleanUp()
78          {
79              // Set culture to original value
80              CultureInfo.CurrentCulture = originalCulture;
81              CultureInfo.CurrentUICulture = originalUiCulture;
82          }
83      }
84  }