From b96533764b63d2cad8c869604be7525602741c2b Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Tue, 16 May 2023 18:31:42 +0200 Subject: [PATCH] Moving TestOutputHelperExtensions and DebugHelper to Lombiq.Tests --- Extensions/TestOutputHelperExtensions.cs | 20 ++++++++++++++++++++ Helpers/DebugHelper.cs | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 Extensions/TestOutputHelperExtensions.cs create mode 100644 Helpers/DebugHelper.cs diff --git a/Extensions/TestOutputHelperExtensions.cs b/Extensions/TestOutputHelperExtensions.cs new file mode 100644 index 0000000..d20b46a --- /dev/null +++ b/Extensions/TestOutputHelperExtensions.cs @@ -0,0 +1,20 @@ +using Lombiq.Tests.Helpers; + +namespace Xunit.Abstractions; + +public static class TestOutputHelperExtensions +{ + public static void WriteLineTimestampedAndDebug(this ITestOutputHelper testOutputHelper, string format, params object[] args) + { + testOutputHelper.WriteLineTimestamped(format, args); + DebugHelper.WriteLineTimestamped(format, args); + } + + public static void WriteLineTimestamped(this ITestOutputHelper testOutputHelper, string format, params object[] args) + { + // Preventing "FormatException : Input string was not in a correct format." exceptions if the message contains + // characters used in string formatting but it shouldn't actually be formatted. + if (args == null || args.Length == 0) testOutputHelper.WriteLine(DebugHelper.PrefixWithTimestamp(format)); + else testOutputHelper.WriteLine(DebugHelper.PrefixWithTimestamp(format), args); + } +} diff --git a/Helpers/DebugHelper.cs b/Helpers/DebugHelper.cs new file mode 100644 index 0000000..556f875 --- /dev/null +++ b/Helpers/DebugHelper.cs @@ -0,0 +1,21 @@ +using System; +using System.Diagnostics; +using System.Globalization; + +namespace Lombiq.Tests.Helpers; + +public static class DebugHelper +{ + public static void WriteLineTimestamped(string format, params object[] args) + { + // Preventing "FormatException : Input string was not in a correct format." exceptions if the message contains + // characters used in string formatting but it shouldn't actually be formatted. + if (args == null || args.Length == 0) Debug.WriteLine(PrefixWithTimestamp(format)); + else Debug.WriteLine(PrefixWithTimestamp(format), args); + } + + // Note that this uses UTC, while Atata's log uses the local time zone: + // https://github.com/atata-framework/atata/issues/483. + public static string PrefixWithTimestamp(string message) => + $"{DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.ffff", CultureInfo.InvariantCulture)} - {message}"; +}