[PT Run] TimeDate plugin (#16662)

* create plugin

* Update plugin code

* fix deps

* last changes

* unix

* new results and small changes

* Update settings name

* make spellcheck happy

* new time/date formats

* add comment

* code cleanup, installer, signing pipeline

* fix unix result

* UnitTests

* spell fix

* Update tests, Timestamp query feature

* new formats

* last changes

* last changes

* unit tests and fixes

* cjhanges and fixes

* fix installer

* fix settings class init

* context menu

* fix tests

* add settings tests

* update/fix DateTimeResult tests

* small improvements

* update pipeline

* enable analyzer

* fixes and improvements

* spell fix

* dev docs

* doc fixes

* spell fix

* last changes

* changes and fixes

* fixes and test updates

* improvements

* last changes

* try to fix tests

* remove obsolete code

* add info to test log

* fix search

* tag fix

* tests

* change tests

* update dev docs

* fix spelling

* fix culture for ui strings

* improvements based on feedback

* improve global search

* improve text

* docs improvement

* add settings note

* fix and update tests

* fix spelling
This commit is contained in:
Heiko
2022-03-17 20:33:05 +01:00
committed by GitHub
parent 5914fc1ffd
commit 34e4e7e5bd
36 changed files with 3028 additions and 4 deletions

View File

@@ -0,0 +1,131 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Globalization;
using System.Linq;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Wox.Infrastructure;
using Wox.Plugin;
namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
{
[TestClass]
public class ImageTests
{
private CultureInfo originalCulture;
private CultureInfo originalUiCulture;
[TestInitialize]
public void Setup()
{
StringMatcher.Instance = new StringMatcher();
// Set culture to 'en-us'
originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = new CultureInfo("en-us");
originalUiCulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentUICulture = new CultureInfo("en-us");
}
[DataTestMethod]
[DataRow("time", "Time -", "Images\\time.dark.png")]
[DataRow("time u", "Time UTC -", "Images\\time.dark.png")]
[DataRow("date", "Date -", "Images\\calendar.dark.png")]
[DataRow("now", "Now -", "Images\\timeDate.dark.png")]
[DataRow("now u", "Now UTC -", "Images\\timeDate.dark.png")]
[DataRow("unix", "Unix epoch time -", "Images\\timeDate.dark.png")]
[DataRow("hour", "Hour -", "Images\\time.dark.png")]
[DataRow("minute", "Minute -", "Images\\time.dark.png")]
[DataRow("second", "Second -", "Images\\time.dark.png")]
[DataRow("millisecond", "Millisecond -", "Images\\time.dark.png")]
[DataRow("file", "Windows file time (Int64 number) -", "Images\\timeDate.dark.png")]
[DataRow("day", "Day (Week day) -", "Images\\calendar.dark.png")]
[DataRow("day of week", "Day of the week (Week day) -", "Images\\calendar.dark.png")]
[DataRow("day of month", "Day of the month -", "Images\\calendar.dark.png")]
[DataRow("day of year", "Day of the year -", "Images\\calendar.dark.png")]
[DataRow("week of month", "Week of the month -", "Images\\calendar.dark.png")]
[DataRow("week of year", "Week of the year (Calendar week, Week number) -", "Images\\calendar.dark.png")]
[DataRow("month", "Month -", "Images\\calendar.dark.png")]
[DataRow("month of year", "Month of the year -", "Images\\calendar.dark.png")]
[DataRow("month and", "Month and day -", "Images\\calendar.dark.png")]
[DataRow("year", "Year -", "Images\\calendar.dark.png")]
[DataRow("era", "Era -", "Images\\calendar.dark.png")]
[DataRow("era abb", "Era abbreviation -", "Images\\calendar.dark.png")]
[DataRow("month and", "Month and year -", "Images\\calendar.dark.png")]
[DataRow("universal", "Universal time format: YYYY-MM-DD hh:mm:ss -", "Images\\timeDate.dark.png")]
[DataRow("iso", "ISO 8601 -", "Images\\timeDate.dark.png")]
[DataRow("iso utc", "ISO 8601 UTC - ", "Images\\timeDate.dark.png")]
[DataRow("iso zone", "ISO 8601 with time zone - ", "Images\\timeDate.dark.png")]
[DataRow("iso utc zone", "ISO 8601 UTC with time zone - ", "Images\\timeDate.dark.png")]
[DataRow("rfc", "RFC1123 -", "Images\\timeDate.dark.png")]
public void IconThemeDarkTest(string typedString, string subTitleMatch, string expectedResult)
{
// Setup
Mock<Main> main = new ();
main.Object.IconTheme = "dark";
Query expectedQuery = new ("(" + typedString, "(");
// Act
string result = main.Object.Query(expectedQuery).FirstOrDefault(predicate: x => x.SubTitle.StartsWith(subTitleMatch, System.StringComparison.CurrentCulture)).IcoPath;
// Assert
Assert.AreEqual(expectedResult, result);
}
[DataTestMethod]
[DataRow("time", "Time -", "Images\\time.light.png")]
[DataRow("time u", "Time UTC -", "Images\\time.light.png")]
[DataRow("date", "Date -", "Images\\calendar.light.png")]
[DataRow("now", "Now -", "Images\\timeDate.light.png")]
[DataRow("now u", "Now UTC -", "Images\\timeDate.light.png")]
[DataRow("unix", "Unix epoch time -", "Images\\timeDate.light.png")]
[DataRow("hour", "Hour -", "Images\\time.light.png")]
[DataRow("minute", "Minute -", "Images\\time.light.png")]
[DataRow("second", "Second -", "Images\\time.light.png")]
[DataRow("millisecond", "Millisecond -", "Images\\time.light.png")]
[DataRow("file", "Windows file time (Int64 number) -", "Images\\timeDate.light.png")]
[DataRow("day", "Day (Week day) -", "Images\\calendar.light.png")]
[DataRow("day of week", "Day of the week (Week day) -", "Images\\calendar.light.png")]
[DataRow("day of month", "Day of the month -", "Images\\calendar.light.png")]
[DataRow("day of year", "Day of the year -", "Images\\calendar.light.png")]
[DataRow("week of month", "Week of the month -", "Images\\calendar.light.png")]
[DataRow("week of year", "Week of the year (Calendar week, Week number) -", "Images\\calendar.light.png")]
[DataRow("month", "Month -", "Images\\calendar.light.png")]
[DataRow("month of year", "Month of the year -", "Images\\calendar.light.png")]
[DataRow("month and", "Month and day -", "Images\\calendar.light.png")]
[DataRow("year", "Year -", "Images\\calendar.light.png")]
[DataRow("era", "Era -", "Images\\calendar.light.png")]
[DataRow("era abb", "Era abbreviation -", "Images\\calendar.light.png")]
[DataRow("Month and", "Month and year -", "Images\\calendar.light.png")]
[DataRow("universal", "Universal time format: YYYY-MM-DD hh:mm:ss -", "Images\\timeDate.light.png")]
[DataRow("iso", "ISO 8601 -", "Images\\timeDate.light.png")]
[DataRow("iso utc", "ISO 8601 UTC - ", "Images\\timeDate.light.png")]
[DataRow("iso zone", "ISO 8601 with time zone - ", "Images\\timeDate.light.png")]
[DataRow("iso utc zone", "ISO 8601 UTC with time zone - ", "Images\\timeDate.light.png")]
[DataRow("rfc", "RFC1123 -", "Images\\timeDate.light.png")]
public void IconThemeLightTest(string typedString, string subTitleMatch, string expectedResult)
{
// Setup
Mock<Main> main = new ();
main.Object.IconTheme = "light";
Query expectedQuery = new ("(" + typedString, "(");
// Act
var result = main.Object.Query(expectedQuery).FirstOrDefault(x => x.SubTitle.StartsWith(subTitleMatch, System.StringComparison.CurrentCulture)).IcoPath;
// Assert
Assert.AreEqual(expectedResult, result);
}
[TestCleanup]
public void CleanUp()
{
// Set culture to original value
CultureInfo.CurrentCulture = originalCulture;
CultureInfo.CurrentUICulture = originalUiCulture;
}
}
}

View File

@@ -0,0 +1,46 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<IsPackable>false</IsPackable>
<Platforms>x64</Platforms>
<RootNamespace>Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests</RootNamespace>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisMode>Recommended</AnalysisMode>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.PowerToys.Run.Plugin.TimeDate\Microsoft.PowerToys.Run.Plugin.TimeDate.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\..\codeAnalysis\GlobalSuppressions.cs">
<Link>GlobalSuppressions.cs</Link>
</Compile>
<AdditionalFiles Include="..\..\..\..\codeAnalysis\StyleCop.json">
<Link>StyleCop.json</Link>
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers">
<Version>1.1.118</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,63 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Reflection;
using Microsoft.PowerToys.Run.Plugin.TimeDate.Components;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
{
[TestClass]
public class PluginSettingsTests
{
[TestMethod]
public void SettingsCount()
{
// Setup
PropertyInfo[] settings = TimeDateSettings.Instance?.GetType()?.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance);
// Act
var result = settings?.Length;
// Assert
Assert.AreEqual(4, result);
}
[DataTestMethod]
[DataRow("OnlyDateTimeNowGlobal")]
[DataRow("TimeWithSeconds")]
[DataRow("DateWithWeekday")]
[DataRow("HideNumberMessageOnGlobalQuery")]
public void DoesSettingExist(string name)
{
// Setup
Type settings = TimeDateSettings.Instance?.GetType();
// Act
var result = settings?.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Instance);
// Assert
Assert.IsNotNull(result);
}
[DataTestMethod]
[DataRow("OnlyDateTimeNowGlobal", true)]
[DataRow("TimeWithSeconds", false)]
[DataRow("DateWithWeekday", false)]
[DataRow("HideNumberMessageOnGlobalQuery", false)]
public void DefaultValues(string name, bool valueExpected)
{
// Setup
TimeDateSettings setting = TimeDateSettings.Instance;
// Act
PropertyInfo propertyInfo = setting?.GetType()?.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Instance);
var result = propertyInfo?.GetValue(setting);
// Assert
Assert.AreEqual(valueExpected, result);
}
}
}

View File

@@ -0,0 +1,243 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Wox.Infrastructure;
using Wox.Plugin;
namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
{
[TestClass]
public class QueryTests
{
private CultureInfo originalCulture;
private CultureInfo originalUiCulture;
[TestInitialize]
public void Setup()
{
StringMatcher.Instance = new StringMatcher();
// Set culture to 'en-us'
originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = new CultureInfo("en-us");
originalUiCulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentUICulture = new CultureInfo("en-us");
}
[DataTestMethod]
[DataRow("time", 2)]
[DataRow("date", 2)]
[DataRow("now", 3)]
[DataRow("current", 3)]
[DataRow("year", 0)]
[DataRow("", 0)]
[DataRow("time::10:10:10", 2)]
[DataRow("date::10/10/10", 2)]
public void CountWithoutPluginKeyword(string typedString, int expectedResultCount)
{
// Setup
Mock<Main> main = new ();
Query expectedQuery = new (typedString);
// Act
var result = main.Object.Query(expectedQuery).Count;
// Assert
Assert.AreEqual(expectedResultCount, result, "Result depends on default plugin settings!");
}
[DataTestMethod]
[DataRow("(time", 16)]
[DataRow("(date", 24)]
[DataRow("(year", 7)]
[DataRow("(now", 30)]
[DataRow("(current", 30)]
[DataRow("(", 30)]
[DataRow("(now::10:10:10", 1)] // Windows file time
[DataRow("(current::10:10:10", 0)]
public void CountWithPluginKeyword(string typedString, int expectedResultCount)
{
// Setup
Mock<Main> main = new ();
Query expectedQuery = new (typedString, "(");
// Act
var result = main.Object.Query(expectedQuery);
// Assert
Assert.AreEqual(expectedResultCount, result.Count, result.ToString());
}
[DataTestMethod]
[DataRow("time", 2)] // Match if first word is a full word match
[DataRow("ime", 0)] // Don't match if first word is not a full match
[DataRow("and", 0)] // Don't match for only conjunctions
[DataRow("and time", 1)] // match if term is conjunction and other words
[DataRow("date and time", 1)] // Match if first word is a full word match
[DataRow("ate and time", 0)] // Don't match if first word is not a full word match
public void ValidateBehaviorOnGlobalQueries(string typedString, int expectedResultCount)
{
// Setup
Mock<Main> main = new ();
Query expectedQuery = new (typedString);
// Act
var result = main.Object.Query(expectedQuery);
// Assert
Assert.AreEqual(expectedResultCount, result.Count, result.ToString());
}
[DataTestMethod]
[DataRow("(time", "Time -")]
[DataRow("(time u", "Time UTC -")]
[DataRow("(date", "Date -")]
[DataRow("(now", "Now -")]
[DataRow("(now u", "Now UTC -")]
[DataRow("(unix", "Unix epoch time -")]
[DataRow("(file", "Windows file time (Int64 number) ")]
[DataRow("(hour", "Hour -")]
[DataRow("(minute", "Minute -")]
[DataRow("(second", "Second -")]
[DataRow("(millisecond", "Millisecond -")]
[DataRow("(day", "Day (Week day) -")]
[DataRow("(day of week", "Day of the week (Week day) -")]
[DataRow("(day of month", "Day of the month -")]
[DataRow("(day of year", "Day of the year -")]
[DataRow("(week of month", "Week of the month -")]
[DataRow("(week of year", "Week of the year (Calendar week, Week number) -")]
[DataRow("(month", "Month -")]
[DataRow("(month of year", "Month of the year -")]
[DataRow("(month and d", "Month and day -")]
[DataRow("(month and y", "Month and year -")]
[DataRow("(year", "Year -")]
[DataRow("(era", "Era -")]
[DataRow("(era a", "Era abbreviation -")]
[DataRow("(universal", "Universal time format: YYYY-MM-DD hh:mm:ss -")]
[DataRow("(iso", "ISO 8601 -")]
[DataRow("(iso utc", "ISO 8601 UTC -")]
[DataRow("(iso zone", "ISO 8601 with time zone - ")]
[DataRow("(iso utc zone", "ISO 8601 UTC with time zone -")]
[DataRow("(rfc", "RFC1123 -")]
[DataRow("(time::12:30", "Time -")]
[DataRow("(date::10.10.2022", "Date -")]
[DataRow("(time::u1646408119", "Time -")]
[DataRow("(time::ft637820085517321977", "Time -")]
[DataRow("(year", "Era -")]
[DataRow("(date", "Era -")]
[DataRow("(week day", "Day (Week day) -")]
[DataRow("(week day", "Day of the week (Week day) -")]
[DataRow("(cal week", "Week of the year (Calendar week, Week number) -")]
[DataRow("(week num", "Week of the year (Calendar week, Week number) -")]
public void CanFindFormatResult(string typedString, string expectedResult)
{
// Setup
Mock<Main> main = new ();
Query expectedQuery = new (typedString, "(");
// Act
var result = main.Object.Query(expectedQuery).FirstOrDefault(x => x.SubTitle.StartsWith(expectedResult, StringComparison.CurrentCulture));
// Assert
Assert.IsNotNull(result);
Assert.IsTrue(result?.Score >= 1, $"Score: {result?.Score}");
}
[DataTestMethod]
[DataRow("(12:30", "Time -")]
[DataRow("(10.10.2022", "Date -")]
[DataRow("(u1646408119", "Date and time -")]
[DataRow("(ft637820085517321977", "Date and time -")]
public void DateTimeNumberOnlyInput(string typedString, string expectedResult)
{
// Setup
Mock<Main> main = new ();
Query expectedQuery = new (typedString, "(");
// Act
var result = main.Object.Query(expectedQuery).FirstOrDefault(x => x.SubTitle.StartsWith(expectedResult, StringComparison.CurrentCulture));
// Assert
Assert.IsNotNull(result);
}
// [DataRow("(::")] -> Behaves different to PT Run user interface
// [DataRow("(time::")] -> Behaves different to PT Run user interface
// [DataRow("(::time")] -> Behaves different to PT Run user interface
[DataTestMethod]
[DataRow("(abcdefg")]
[DataRow("(timmmmeeee")]
[DataRow("(10.10.20.aa.22")]
[DataRow("(12::55")]
[DataRow("(12:aa:55")]
[DataRow("(timtaaaetetaae::u1646408119")]
[DataRow("(time:eeee")]
[DataRow("(time::eeee")]
[DataRow("(time//eeee")]
[DataRow("(date::12::55")]
[DataRow("(date::12:aa:55")]
public void InvalidInputNotShowsResults(string typedString)
{
// Setup
Mock<Main> main = new ();
Query expectedQuery = new (typedString, "(");
// Act
var result = main.Object.Query(expectedQuery).FirstOrDefault();
// Assert
Assert.IsNull(result, result?.ToString());
}
[DataTestMethod]
[DataRow("(ug1646408119")] // Invalid prefix
[DataRow("(u9999999999999")] // Unix number + prefix is longer than 12 characters
[DataRow("(0123456")] // Missing prefix
[DataRow("(ft63782008ab55173dasdas21977")] // Number contains letters
public void InvalidNumberInputShowsErrorMessage(string typedString)
{
// Setup
Mock<Main> main = new ();
Query expectedQuery = new (typedString, "(");
// Act
var result = main.Object.Query(expectedQuery).FirstOrDefault().Title;
// Assert
Assert.IsTrue(result.StartsWith("Error:", StringComparison.CurrentCulture));
}
[DataTestMethod]
[DataRow("(ft12..548")] // Number contains punctuation
[DataRow("(ft12..54//8")] // Number contains punctuation and other characters
[DataRow("(12..54//8")] // Number contains punctuation and other characters
[DataRow("(ft::1288gg8888")] // Number contains delimiter and other characters
public void InvalidInputNotShowsErrorMessage(string typedString)
{
// Setup
Mock<Main> main = new ();
Query expectedQuery = new (typedString, "(");
// Act
var result = main.Object.Query(expectedQuery).FirstOrDefault();
// Assert
Assert.IsNull(result);
}
[TestCleanup]
public void CleanUp()
{
// Set culture to original value
CultureInfo.CurrentCulture = originalCulture;
CultureInfo.CurrentUICulture = originalUiCulture;
}
}
}

View File

@@ -0,0 +1,60 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Globalization;
using System.Threading;
using Microsoft.PowerToys.Run.Plugin.TimeDate.Components;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
{
[TestClass]
public class StringParserTests
{
private CultureInfo originalCulture;
private CultureInfo originalUiCulture;
[TestInitialize]
public void Setup()
{
// Set culture to 'en-us'
originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = new CultureInfo("en-us");
originalUiCulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentUICulture = new CultureInfo("en-us");
}
[DataTestMethod]
[DataRow("10/29/2022 17:05:10", true, "G", "10/29/2022 5:05:10 PM")]
[DataRow("Saturday, October 29, 2022 5:05:10 PM", true, "G", "10/29/2022 5:05:10 PM")]
[DataRow("10/29/2022", true, "d", "10/29/2022")]
[DataRow("Saturday, October 29, 2022", true, "d", "10/29/2022")]
[DataRow("17:05:10", true, "T", "5:05:10 PM")]
[DataRow("5:05:10 PM", true, "T", "5:05:10 PM")]
[DataRow("10456", false, "", "")]
[DataRow("u10456", true, "", "")] // Value is UTC and can be different based on system
[DataRow("ft10456", true, "", "")] // Value is UTC and can be different based on system
public void ConvertStringToDateTime(string typedString, bool expectedBool, string stringType, string expectedString)
{
// Act
bool boolResult = TimeAndDateHelper.ParseStringAsDateTime(in typedString, out DateTime result);
// Assert
Assert.AreEqual(expectedBool, boolResult);
if (!string.IsNullOrEmpty(expectedString))
{
Assert.AreEqual(expectedString, result.ToString(stringType, CultureInfo.CurrentCulture));
}
}
[TestCleanup]
public void CleanUp()
{
// Set culture to original value
CultureInfo.CurrentCulture = originalCulture;
CultureInfo.CurrentUICulture = originalUiCulture;
}
}
}

View File

@@ -0,0 +1,313 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Globalization;
using System.Linq;
using Microsoft.PowerToys.Run.Plugin.TimeDate.Components;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
{
[TestClass]
public class TimeDateResultTests
{
private CultureInfo originalCulture;
private CultureInfo originalUiCulture;
[TestInitialize]
public void Setup()
{
// Set culture to 'en-us'
originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = new CultureInfo("en-us");
originalUiCulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentUICulture = new CultureInfo("en-us");
}
[DataTestMethod]
[DataRow("time", "10:30 AM")]
[DataRow("date", "3/2/2022")]
[DataRow("date and time", "3/2/2022 10:30 AM")]
[DataRow("hour", "10")]
[DataRow("minute", "30")]
[DataRow("second", "45")]
[DataRow("millisecond", "0")]
[DataRow("day (week day)", "Wednesday")]
[DataRow("day of the week (week day)", "4")]
[DataRow("day of the month", "2")]
[DataRow("day of the year", "61")]
[DataRow("week of the month", "1")]
[DataRow("week of the year (calendar week, week number)", "10")]
[DataRow("month", "March")]
[DataRow("month of the year", "3")]
[DataRow("month and day", "March 2")]
[DataRow("year", "2022")]
[DataRow("month and year", "March 2022")]
[DataRow("ISO 8601", "2022-03-02T10:30:45")]
[DataRow("ISO 8601 with time zone", "2022-03-02T10:30:45")]
[DataRow("RFC1123", "Wed, 02 Mar 2022 10:30:45 GMT")]
public void LocalFormatsWithShortTimeAndShortDate(string formatLabel, string expectedResult)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, false, false, new DateTime(2022, 03, 02, 10, 30, 45));
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedResult, result?.Value, $"Culture {CultureInfo.CurrentCulture.Name}, Culture UI: {CultureInfo.CurrentUICulture.Name}, Calendar: {CultureInfo.CurrentCulture.Calendar}, Region: {RegionInfo.CurrentRegion.Name}");
}
[DataTestMethod]
[DataRow("time", "10:30 AM")]
[DataRow("date", "Wednesday, March 2, 2022")]
[DataRow("date and time", "Wednesday, March 2, 2022 10:30 AM")]
[DataRow("hour", "10")]
[DataRow("minute", "30")]
[DataRow("second", "45")]
[DataRow("millisecond", "0")]
[DataRow("day (week day)", "Wednesday")]
[DataRow("day of the week (week day)", "4")]
[DataRow("day of the month", "2")]
[DataRow("day of the year", "61")]
[DataRow("week of the month", "1")]
[DataRow("week of the year (calendar week, week number)", "10")]
[DataRow("month", "March")]
[DataRow("month of the year", "3")]
[DataRow("month and day", "March 2")]
[DataRow("year", "2022")]
[DataRow("month and year", "March 2022")]
[DataRow("ISO 8601", "2022-03-02T10:30:45")]
[DataRow("ISO 8601 with time zone", "2022-03-02T10:30:45")]
[DataRow("RFC1123", "Wed, 02 Mar 2022 10:30:45 GMT")]
public void LocalFormatsWithShortTimeAndLongDate(string formatLabel, string expectedResult)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, false, true, new DateTime(2022, 03, 02, 10, 30, 45));
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedResult, result?.Value);
}
[DataTestMethod]
[DataRow("time", "10:30:45 AM")]
[DataRow("date", "3/2/2022")]
[DataRow("date and time", "3/2/2022 10:30:45 AM")]
[DataRow("hour", "10")]
[DataRow("minute", "30")]
[DataRow("second", "45")]
[DataRow("millisecond", "0")]
[DataRow("day (week day)", "Wednesday")]
[DataRow("day of the week (week day)", "4")]
[DataRow("day of the month", "2")]
[DataRow("day of the year", "61")]
[DataRow("week of the month", "1")]
[DataRow("week of the year (calendar week, week number)", "10")]
[DataRow("month", "March")]
[DataRow("month of the year", "3")]
[DataRow("month and day", "March 2")]
[DataRow("year", "2022")]
[DataRow("month and year", "March 2022")]
[DataRow("ISO 8601", "2022-03-02T10:30:45")]
[DataRow("ISO 8601 with time zone", "2022-03-02T10:30:45")]
[DataRow("RFC1123", "Wed, 02 Mar 2022 10:30:45 GMT")]
public void LocalFormatsWithLongTimeAndShortDate(string formatLabel, string expectedResult)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, true, false, new DateTime(2022, 03, 02, 10, 30, 45));
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedResult, result?.Value);
}
[DataTestMethod]
[DataRow("time", "10:30:45 AM")]
[DataRow("date", "Wednesday, March 2, 2022")]
[DataRow("date and time", "Wednesday, March 2, 2022 10:30:45 AM")]
[DataRow("hour", "10")]
[DataRow("minute", "30")]
[DataRow("second", "45")]
[DataRow("millisecond", "0")]
[DataRow("day (week day)", "Wednesday")]
[DataRow("day of the week (week day)", "4")]
[DataRow("day of the month", "2")]
[DataRow("day of the year", "61")]
[DataRow("week of the month", "1")]
[DataRow("week of the year (calendar week, week number)", "10")]
[DataRow("month", "March")]
[DataRow("month of the year", "3")]
[DataRow("month and day", "March 2")]
[DataRow("year", "2022")]
[DataRow("month and year", "March 2022")]
[DataRow("ISO 8601", "2022-03-02T10:30:45")]
[DataRow("ISO 8601 with time zone", "2022-03-02T10:30:45")]
[DataRow("RFC1123", "Wed, 02 Mar 2022 10:30:45 GMT")]
public void LocalFormatsWithLongTimeAndLongDate(string formatLabel, string expectedResult)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, true, true, new DateTime(2022, 03, 02, 10, 30, 45));
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedResult, result?.Value);
}
[DataTestMethod]
[DataRow("time utc", "t")]
[DataRow("date and time utc", "g")]
[DataRow("ISO 8601 UTC", "yyyy-MM-ddTHH:mm:ss")]
[DataRow("ISO 8601 UTC with time zone", "yyyy-MM-ddTHH:mm:ss'Z'")]
[DataRow("Universal time format: YYYY-MM-DD hh:mm:ss", "u")]
public void UtcFormatsWithShortTimeAndShortDate(string formatLabel, string expectedFormat)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, false, false, new DateTime(2022, 03, 02, 10, 30, 45));
var expectedResult = new DateTime(2022, 03, 02, 10, 30, 45).ToUniversalTime().ToString(expectedFormat, CultureInfo.CurrentCulture);
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedResult, result?.Value);
}
[DataTestMethod]
[DataRow("time utc", "t")]
[DataRow("date and time utc", "f")]
[DataRow("ISO 8601 UTC", "yyyy-MM-ddTHH:mm:ss")]
[DataRow("ISO 8601 UTC with time zone", "yyyy-MM-ddTHH:mm:ss'Z'")]
[DataRow("Universal time format: YYYY-MM-DD hh:mm:ss", "u")]
public void UtcFormatsWithShortTimeAndLongDate(string formatLabel, string expectedFormat)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, false, true, new DateTime(2022, 03, 02, 10, 30, 45));
var expectedResult = new DateTime(2022, 03, 02, 10, 30, 45).ToUniversalTime().ToString(expectedFormat, CultureInfo.CurrentCulture);
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedResult, result?.Value);
}
[DataTestMethod]
[DataRow("time utc", "T")]
[DataRow("date and time utc", "G")]
[DataRow("ISO 8601 UTC", "yyyy-MM-ddTHH:mm:ss")]
[DataRow("ISO 8601 UTC with time zone", "yyyy-MM-ddTHH:mm:ss'Z'")]
[DataRow("Universal time format: YYYY-MM-DD hh:mm:ss", "u")]
public void UtcFormatsWithLongTimeAndShortDate(string formatLabel, string expectedFormat)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, true, false, new DateTime(2022, 03, 02, 10, 30, 45));
var expectedResult = new DateTime(2022, 03, 02, 10, 30, 45).ToUniversalTime().ToString(expectedFormat, CultureInfo.CurrentCulture);
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedResult, result?.Value);
}
[DataTestMethod]
[DataRow("time utc", "T")]
[DataRow("date and time utc", "F")]
[DataRow("ISO 8601 UTC", "yyyy-MM-ddTHH:mm:ss")]
[DataRow("ISO 8601 UTC with time zone", "yyyy-MM-ddTHH:mm:ss'Z'")]
[DataRow("Universal time format: YYYY-MM-DD hh:mm:ss", "u")]
public void UtcFormatsWithLongTimeAndLongDate(string formatLabel, string expectedFormat)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, true, true, new DateTime(2022, 03, 02, 10, 30, 45));
var expectedResult = new DateTime(2022, 03, 02, 10, 30, 45).ToUniversalTime().ToString(expectedFormat, CultureInfo.CurrentCulture);
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedResult, result?.Value);
}
[TestMethod]
public void UnixTimestampFormat()
{
// Setup
string formatLabel = "Unix epoch time";
DateTime timeValue = DateTime.Now.ToUniversalTime();
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue);
var expectedResult = (long)timeValue.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedResult.ToString(CultureInfo.CurrentCulture), result?.Value);
}
[TestMethod]
public void WindowsFileTimeFormat()
{
// Setup
string formatLabel = "Windows file time (Int64 number)";
DateTime timeValue = DateTime.Now;
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue);
var expectedResult = timeValue.Ticks.ToString(CultureInfo.CurrentCulture);
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedResult, result?.Value);
}
[TestMethod]
public void ValidateEraResult()
{
// Setup
string formatLabel = "Era";
DateTime timeValue = DateTime.Now;
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue);
var expectedResult = DateTimeFormatInfo.CurrentInfo.GetEraName(CultureInfo.CurrentCulture.Calendar.GetEra(timeValue));
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedResult, result?.Value);
}
[TestMethod]
public void ValidateEraAbbreviationResult()
{
// Setup
string formatLabel = "Era abbreviation";
DateTime timeValue = DateTime.Now;
var helperResults = AvailableResultsList.GetList(true, false, false, timeValue);
var expectedResult = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedEraName(CultureInfo.CurrentCulture.Calendar.GetEra(timeValue));
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
// Assert
Assert.AreEqual(expectedResult, result?.Value);
}
[TestCleanup]
public void CleanUp()
{
// Set culture to original value
CultureInfo.CurrentCulture = originalCulture;
CultureInfo.CurrentUICulture = originalUiCulture;
}
}
}