Skip to content

Commit

Permalink
Merge pull request #32 from Lombiq/issue/OSOE-624
Browse files Browse the repository at this point in the history
OSOE-624: Moving DebugHelper and TestOutputHelperExtensions to Lombiq.Tests
  • Loading branch information
Piedone authored May 16, 2023
2 parents 7ccf190 + b965337 commit 75d9d34
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Extensions/TestOutputHelperExtensions.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
21 changes: 21 additions & 0 deletions Helpers/DebugHelper.cs
Original file line number Diff line number Diff line change
@@ -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}";
}

0 comments on commit 75d9d34

Please sign in to comment.