public static void SerializeToString_DateTime___Should_return_serialized_string_representation_of_value_for_different_DateTimeKind___When_called() { // Arrange // 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(_ => ObcDateTimeStringSerializer.SerializeToString(_.Value)).ToList(); // Assert actuals.Must().BeEqualTo(scenarios.Select(_ => _.Expected).ToList()); }
private string MakeStringFromObject( object value) { if (value == null) { return(null); } var propertyType = value.GetType(); if (this.configuredTypeToSerializerMap.ContainsKey(propertyType)) { var serializer = this.configuredTypeToSerializerMap[propertyType]; var result = serializer.SerializeToString(value); return(result); } else if (value is DateTime propertyValueAsDateTime) { var result = ObcDateTimeStringSerializer.SerializeToString(propertyValueAsDateTime); return(result); } else { string result; if ((propertyType != typeof(string)) && (value is IEnumerable propertyValueAsEnumerable)) { var values = new List <string>(); foreach (var element in propertyValueAsEnumerable) { var serializedItem = this.MakeStringFromObject(element); values.Add(serializedItem); } result = values.ToCsv(this.dictionaryStringSerializer.NullValueEncoding); } else if (value is ISerializeToString propertyValueAsSerializeToString) { result = propertyValueAsSerializeToString.SerializeToString(); } else { result = value.ToString(); } return(result); } }
public static void SerializeToString_object___Should_throw_ArgumentNullException___When_parameter_value_is_null() { // Arrange var systemUnderTest = new ObcDateTimeStringSerializer(); object value = null; // Act var actual = Record.Exception(() => systemUnderTest.SerializeToString(value)); // Assert actual.AsTest().Must().BeOfType <ArgumentNullException>(); }
public static void RoundtripSerializeDeserialize___Using_local_negative_offset___Works() { // Arrange var expected = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")); var serializer = new ObcDateTimeStringSerializer(); // Act var serialized = serializer.SerializeToString(expected); var actual = serializer.Deserialize <DateTime>(serialized); // Assert actual.Kind.Must().BeEqualTo(expected.Kind); actual.Must().BeEqualTo(expected); }
public static void RoundtripSerializeDeserialize___Using_unspecified___Works() { // Arrange var expected = DateTime.UtcNow.ToUnspecified(); var serializer = new ObcDateTimeStringSerializer(); // Act var serialized = serializer.SerializeToString(expected); var actual = serializer.Deserialize <DateTime>(serialized); // Assert actual.Kind.Must().BeEqualTo(expected.Kind); actual.Must().BeEqualTo(expected); }
public static void SerializeToString_object___Should_throw_ArgumentException___When_parameter_value_is_not_DateTime() { // Arrange var systemUnderTest = new ObcDateTimeStringSerializer(); 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);"); }
public static void LogError(string message) { try { var serializedDateTimeUtcNow = ObcDateTimeStringSerializer.SerializeToString(DateTime.UtcNow); const ushort eventLogCharacterLimit = 31914; Action <string> logToFile = localMessage => { var preppedForFilePathTime = serializedDateTimeUtcNow.Replace(":", "-").Replace(".", "_").Replace("T", "--"); var fileName = preppedForFilePathTime + "--LastDitchLoggedError.log"; File.WriteAllText(fileName, localMessage); }; Action <string> logToEventLog = localMessage => { var preppedMessage = localMessage == null ? "Null Message Provided." : localMessage.Length <= eventLogCharacterLimit ? localMessage : localMessage.Substring(0, eventLogCharacterLimit); using (EventLog eventLog = new EventLog("Application")) { eventLog.Source = "Application"; eventLog.WriteEntry(preppedMessage, EventLogEntryType.Error, 101, 1); } }; Action <string> logToConsoleOut = localMessage => { var preppedMessage = serializedDateTimeUtcNow + "--" + localMessage; Console.Out.WriteLine(preppedMessage); }; Action <string> logToConsoleError = localMessage => { var preppedMessage = serializedDateTimeUtcNow + "--" + localMessage; Console.Error.WriteLine(preppedMessage); }; Action <string> logToTraceOut = localMessage => { var preppedMessage = serializedDateTimeUtcNow + "--" + localMessage; Trace.WriteLine(preppedMessage); }; Action <string> logToTraceError = localMessage => { var preppedMessage = serializedDateTimeUtcNow + "--" + localMessage; Trace.TraceError(preppedMessage); }; IReadOnlyCollection <Action <string> > actionsToRun = new[] { logToFile, logToEventLog, logToConsoleOut, logToConsoleError, logToTraceOut, logToTraceError, }; void RunActionWithExceptionSwallowing(Action <string> actionToRunWithoutAllowingExceptions, string input) { try { actionToRunWithoutAllowingExceptions(input); } catch (Exception) { /* no-op */ } } foreach (var actionToRun in actionsToRun) { RunActionWithExceptionSwallowing(actionToRun, message); } } catch (Exception) { /* no-op */ } }