From 7762810ba97bdc6f3246f82da4d4a4c525527da3 Mon Sep 17 00:00:00 2001 From: jvrooyen <28042104+jvrooyen@users.noreply.github.com> Date: Thu, 8 Jul 2021 14:34:49 +0200 Subject: [PATCH] Fix DateTime looses significant digits in DynamicData (#875) (#907) --- .../Helpers/DataSerializationHelper.cs | 3 +- .../Helpers/DataSerializationHelperTests.cs | 80 +++++++++++++++++++ .../MSTest.CoreAdapter.Unit.Tests.csproj | 5 +- 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Helpers/DataSerializationHelperTests.cs diff --git a/src/Adapter/MSTest.CoreAdapter/Helpers/DataSerializationHelper.cs b/src/Adapter/MSTest.CoreAdapter/Helpers/DataSerializationHelper.cs index 9b7fb56685..3c99e68b28 100644 --- a/src/Adapter/MSTest.CoreAdapter/Helpers/DataSerializationHelper.cs +++ b/src/Adapter/MSTest.CoreAdapter/Helpers/DataSerializationHelper.cs @@ -17,7 +17,8 @@ internal static class DataSerializationHelper private static readonly DataContractJsonSerializerSettings SerializerSettings = new DataContractJsonSerializerSettings() { UseSimpleDictionaryFormat = true, - EmitTypeInformation = System.Runtime.Serialization.EmitTypeInformation.Always + EmitTypeInformation = System.Runtime.Serialization.EmitTypeInformation.Always, + DateTimeFormat = new System.Runtime.Serialization.DateTimeFormat("O", System.Globalization.CultureInfo.InvariantCulture) }; /// diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Helpers/DataSerializationHelperTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Helpers/DataSerializationHelperTests.cs new file mode 100644 index 0000000000..1123fc8d63 --- /dev/null +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Helpers/DataSerializationHelperTests.cs @@ -0,0 +1,80 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests +{ + extern alias FrameworkV1; + extern alias FrameworkV2; + + using System; + using FluentAssertions; + using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers; + + using TestableImplementations; + using Assert = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.Assert; + using CollectionAssert = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert; + using TestClass = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; + using TestCleanup = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute; + using TestInitialize = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute; + using TestMethod = FrameworkV1::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; + using UTF = FrameworkV2::Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class DataSerializationHelperTests + { + [TestMethod] + public void DataSerializerShouldRoundTripDateTimeOffset() + { + var source = new DateTimeOffset(628381323438126060, TimeSpan.FromHours(-8)); + + var actual = DataSerializationHelper.Deserialize( + DataSerializationHelper.Serialize(new object[] { source })); + + actual.Should().HaveCount(1); + actual[0].Should().BeOfType(); + actual[0].As().Should().Be(source); + } + + [TestMethod] + public void DataSerializerShouldRoundTripDateTime() + { + var source = new DateTime(628381323438126060); + + var actual = DataSerializationHelper.Deserialize( + DataSerializationHelper.Serialize(new object[] { source })); + + actual.Should().HaveCount(1); + actual[0].Should().BeOfType(); + actual[0].As().Should().Be(source); + actual[0].As().Kind.Should().Be(source.Kind); + } + + [TestMethod] + public void DataSerializerShouldRoundTripDateTimeOfKindLocal() + { + var source = new DateTime(628381323438126060, DateTimeKind.Local); + + var actual = DataSerializationHelper.Deserialize( + DataSerializationHelper.Serialize(new object[] { source })); + + actual.Should().HaveCount(1); + actual[0].Should().BeOfType(); + actual[0].As().Should().Be(source); + actual[0].As().Kind.Should().Be(source.Kind); + } + + [TestMethod] + public void DataSerializerShouldRoundTripDateTimeOfKindUtc() + { + var source = new DateTime(628381323438126060, DateTimeKind.Utc); + + var actual = DataSerializationHelper.Deserialize( + DataSerializationHelper.Serialize(new object[] { source })); + + actual.Should().HaveCount(1); + actual[0].Should().BeOfType(); + actual[0].As().Should().Be(source); + actual[0].As().Kind.Should().Be(source.Kind); + } + } +} diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/MSTest.CoreAdapter.Unit.Tests.csproj b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/MSTest.CoreAdapter.Unit.Tests.csproj index 9b008bba93..8fc2c5f132 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/MSTest.CoreAdapter.Unit.Tests.csproj +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/MSTest.CoreAdapter.Unit.Tests.csproj @@ -38,7 +38,7 @@ - + FrameworkV1 True @@ -83,7 +83,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive @@ -116,6 +116,7 @@ +