예제 #1
0
        public static void SerializeToString_object___Should_return_serialized_string_representation_of_value_for_different_DateTimeKind___When_called()
        {
            // Arrange
            var systemUnderTest = new ObcNullableDateTimeStringSerializer();

            // note that this will return "+00:00" if machine is on UTC time, which is what we expect
            var offset = DateTime.Now.ToString("%K");

            var scenarios = new List <(DateTime?Value, string Expected)>
            {
                (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Utc), "2019-01-05T12:14:58.1920000Z"),
                (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Utc).AddTicks(1), "2019-01-05T12:14:58.1920001Z"),
                (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Utc).AddTicks(20), "2019-01-05T12:14:58.1920020Z"),

                (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Unspecified), "2019-01-05T12:14:58.1920000"),
                (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Unspecified).AddTicks(1), "2019-01-05T12:14:58.1920001"),
                (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Unspecified).AddTicks(20), "2019-01-05T12:14:58.1920020"),

                (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Local), "2019-01-05T12:14:58.1920000" + offset),
                (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Local).AddTicks(1), "2019-01-05T12:14:58.1920001" + offset),
                (new DateTime(2019, 1, 5, 12, 14, 58, 192, DateTimeKind.Local).AddTicks(20), "2019-01-05T12:14:58.1920020" + offset),
            };

            // Act
            var actuals = scenarios.Select(_ => systemUnderTest.SerializeToString(_.Value)).ToList();

            // Assert
            actuals.Must().BeEqualTo(scenarios.Select(_ => _.Expected).ToList());
        }
예제 #2
0
        public static void SerializeToString_object___Should_return_null___When_parameter_value_is_null()
        {
            // Arrange
            var systemUnderTest = new ObcNullableDateTimeStringSerializer();

            object value = null;

            // Act
            var actual = Record.Exception(() => systemUnderTest.SerializeToString(value));

            // Assert
            actual.AsTest().Must().BeNull();
        }
예제 #3
0
        public static void RoundtripSerializeDeserialize___Using_local_negative_offset___Works()
        {
            // Arrange
            DateTime?expected   = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
            var      serializer = new ObcNullableDateTimeStringSerializer();

            // Act
            var serialized = serializer.SerializeToString(expected);
            var actual     = serializer.Deserialize <DateTime?>(serialized);

            // Assert
            ((DateTime)actual).Kind.Must().BeEqualTo(((DateTime)expected).Kind);
            actual.Must().BeEqualTo(expected);
        }
예제 #4
0
        public static void SerializeToString_object___Should_throw_ArgumentException___When_parameter_value_is_not_DateTime()
        {
            // Arrange
            var systemUnderTest = new ObcNullableDateTimeStringSerializer();

            var values = new[] { new object(), A.Dummy <string>(), A.Dummy <int>(), A.Dummy <Guid>() };

            // Act
            var actual = values.Select(_ => Record.Exception(() => systemUnderTest.SerializeToString(_))).ToList();

            // Assert
            actual.Must().Each().BeOfType <ArgumentException>();
            actual.Select(_ => _.Message).Must().Each().ContainString("objectToSerialize.GetType() != typeof(DateTime);");
        }
예제 #5
0
        public static void RoundtripSerializeDeserialize___Using_unspecified___Works()
        {
            // Arrange
            DateTime?expected   = DateTime.UtcNow.ToUnspecified();
            var      serializer = new ObcNullableDateTimeStringSerializer();

            // Act
            var serialized = serializer.SerializeToString(expected);
            var actual     = serializer.Deserialize <DateTime?>(serialized);

            // Assert
            ((DateTime)actual).Kind.Must().BeEqualTo(((DateTime)expected).Kind);
            actual.Must().BeEqualTo(expected);
        }