Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CZEMacLeod committed Aug 26, 2024
1 parent 4f934a9 commit 5d10831
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="8.8.0" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />

<PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
Expand Down
58 changes: 41 additions & 17 deletions test/C3D/Extensions/Logging/Xunit/Test/TestOutputHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using C3D.Extensions.Logging.Xunit.Utilities;
using System.Runtime.CompilerServices;
using Xunit.Abstractions;
using Microsoft.Extensions.Time.Testing;

namespace C3D.Extensions.Logging.Xunit.Test;

Expand Down Expand Up @@ -53,40 +54,72 @@ private IServiceCollection CreateServices()
return (wrapper, factory);
}

const string debug = "Here is some debugging";
const string info = "Here is some information";

[Fact]
public void CreateLogger()
{
WriteFunctionName();

var log = output.CreateLogger<TestOutputHelperTests>();

Assert.NotNull(log);
log.LogInformation("Here is some information");

log.LogInformation(info);
}

[Fact]
public void CreateLoggerWithTimestamp()
{
WriteFunctionName();

var log = output.CreateLogger<TestOutputHelperTests>(configure=>configure.TimeStamp= XunitLoggerTimeStamp.DateTime);
var wrapper = new LoggingTestOutputHelper(output);
var utcNow = new DateTimeOffset(2020, 10, 9, 8, 7, 6, TimeSpan.Zero);
var tp = new FakeTimeProvider(utcNow);

var log = wrapper.CreateLogger<TestOutputHelperTests>(configure => configure
.WithTimeStamp(XunitLoggerTimeStamp.DateTime)
.UseTimeProvider(tp)
.UseCulture(System.Globalization.CultureInfo.InvariantCulture)
);

Assert.NotNull(log);

log.LogInformation("Here is some information");
log.LogInformation(info);

var messages = wrapper.Messages;

var s = Assert.Single(messages);
Assert.Equal("| 2020-10-09 08:07:06Z C3D.Extensions.Logging.Xunit.Test.TestOutputHelperTests Information | Here is some information", s);
}

[Fact]
public void CreateLoggerWithOffset()
{
WriteFunctionName();

var log = output.CreateLogger<TestOutputHelperTests>(configure => configure.TimeStamp = XunitLoggerTimeStamp.Offset);
var wrapper = new LoggingTestOutputHelper(output);
var utcNow = DateTime.UtcNow;
var tp = new FakeTimeProvider(utcNow);

// "| -0:00:00:00.0007472 C3D.Extensions.Logging.Xunit.Test.TestOutputHelperTests Information | Here is some information"


var log = wrapper.CreateLogger<TestOutputHelperTests>(configure => configure
.WithTimeStamp(XunitLoggerTimeStamp.Offset)
.UseTimeProvider(tp)
.Restart()
.UseCulture(System.Globalization.CultureInfo.InvariantCulture)
);

Assert.NotNull(log);

log.LogInformation("Here is some information");
log.LogInformation(info);

var messages = wrapper.Messages;
var s = Assert.Single(messages);
Assert.Equal("| 0:00:00:00.0000000 C3D.Extensions.Logging.Xunit.Test.TestOutputHelperTests Information | Here is some information", s);
}

[Fact]
Expand All @@ -100,9 +133,6 @@ public void CreateLoggerFiltersMessages()

Assert.NotNull(log);

const string debug = "Here is some debugging";
const string info = "Here is some information";

log.LogDebug(debug);
log.LogInformation(info);

Expand All @@ -122,9 +152,6 @@ public void CreateLoggerFromServices()

Assert.NotNull(log);

const string debug = "Here is some debugging";
const string info = "Here is some information";

log.LogDebug(debug);
log.LogInformation(info);

Expand All @@ -134,7 +161,7 @@ public void CreateLoggerFromServices()

var messages = wrapper.Messages;

Assert.Collection(messages, s=>s.EndsWith(debug), s => s.EndsWith(info));
Assert.Collection(messages, s => s.EndsWith(debug), s => s.EndsWith(info));
}

[Fact]
Expand All @@ -148,9 +175,6 @@ public void CreateLoggerFromFactory()

Assert.NotNull(log);

const string debug = "Here is some debugging";
const string info = "Here is some information";

log.LogDebug(debug);
log.LogInformation(info);

Expand Down

0 comments on commit 5d10831

Please sign in to comment.