Skip to content

Commit

Permalink
Move tests to Shared
Browse files Browse the repository at this point in the history
Move all the code for the tests to a Shared folder for re-use.
  • Loading branch information
martincostello committed Dec 20, 2024
1 parent 4361023 commit 0fa5e53
Show file tree
Hide file tree
Showing 15 changed files with 1,164 additions and 1,176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
<Compile Include="..\Shared\**\*.cs" Link="%(Link)" />
<Content Include="..\Shared\xunit.runner.json" Link="%(Link)" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Logging.XUnit\MartinCostello.Logging.XUnit.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,8 @@
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Logging.XUnit.Tests\Constructor.cs" Link="Constructor.cs" />
<Compile Include="..\Logging.XUnit.Tests\Examples.cs" Link="Examples.cs" />
<Compile Include="..\Logging.XUnit.Tests\IntegrationTests.cs" Link="IntegrationTests.cs" />
<Compile Include="..\Logging.XUnit.Tests\Integration\DatabaseFixture.cs" Link="Integration\DatabaseFixture.cs" />
<Compile Include="..\Logging.XUnit.Tests\Integration\DatabaseTests.cs" Link="Integration\DatabaseTests.cs" />
<Compile Include="..\Logging.XUnit.Tests\Integration\HttpApplicationTests.cs" Link="Integration\HttpApplicationTests.cs" />
<Compile Include="..\Logging.XUnit.Tests\Integration\HttpServerCollection.cs" Link="Integration\HttpServerCollection.cs" />
<Compile Include="..\Logging.XUnit.Tests\Integration\HttpServerFixture.cs" Link="Integration\HttpServerFixture.cs" />
<Compile Include="..\Logging.XUnit.Tests\Integration\PrintableDiagnosticMessage.cs" Link="Integration\PrintableDiagnosticMessage.cs" />
<Compile Include="..\Logging.XUnit.Tests\XUnitLoggerExtensionsTests.cs" Link="XUnitLoggerExtensionsTests.cs" />
<Compile Include="..\Logging.XUnit.Tests\XUnitLoggerProviderTests.cs" Link="XUnitLoggerProviderTests.cs" />
<Compile Include="..\Logging.XUnit.Tests\XUnitLoggerTests.cs" Link="XUnitLoggerTests.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="..\Logging.XUnit.Tests\xunit.runner.json" Link="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
<Compile Include="..\Shared\**\*.cs" Link="%(Link)" />
<Content Include="..\Shared\xunit.runner.json" Link="%(Link)" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Logging.XUnit.v3\MartinCostello.Logging.XUnit.v3.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

namespace MartinCostello.Logging.XUnit;

public enum Constructor
{
ITestOutputHelper,

IMessageSink,
}
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

namespace MartinCostello.Logging.XUnit;

public enum Constructor
{
ITestOutputHelper,

IMessageSink,
}
108 changes: 54 additions & 54 deletions tests/Logging.XUnit.Tests/Examples.cs → tests/Shared/Examples.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace MartinCostello.Logging.XUnit;

public class Examples(ITestOutputHelper outputHelper)
{
[Fact]
public void Calculator_Sums_Two_Equal_Integers()
{
// Arrange using conversion to a logger
var calculator = new Calculator(outputHelper.ToLogger<Calculator>());

// Act
int actual = calculator.Sum(2, 2);

// Assert
actual.ShouldBe(4);
}

[Fact]
public void Calculator_Sums_Two_Different_Integers()
{
// Arrange using the logging provider
var services = new ServiceCollection()
.AddLogging((builder) => builder.AddXUnit(outputHelper))
.AddSingleton<Calculator>();

IServiceProvider provider = services.BuildServiceProvider();

var calculator = provider.GetRequiredService<Calculator>();

// Act
int actual = calculator.Sum(1, 2);

// Assert
actual.ShouldBe(3);
}

private sealed class Calculator(ILogger<Calculator> logger)
{
public int Sum(int x, int y)
{
int sum = x + y;

logger.LogInformation("The sum of {X} and {Y} is {Sum}.", x, y, sum);

return sum;
}
}
}
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace MartinCostello.Logging.XUnit;

public class Examples(ITestOutputHelper outputHelper)
{
[Fact]
public void Calculator_Sums_Two_Equal_Integers()
{
// Arrange using conversion to a logger
var calculator = new Calculator(outputHelper.ToLogger<Calculator>());

// Act
int actual = calculator.Sum(2, 2);

// Assert
actual.ShouldBe(4);
}

[Fact]
public void Calculator_Sums_Two_Different_Integers()
{
// Arrange using the logging provider
var services = new ServiceCollection()
.AddLogging((builder) => builder.AddXUnit(outputHelper))
.AddSingleton<Calculator>();

IServiceProvider provider = services.BuildServiceProvider();

var calculator = provider.GetRequiredService<Calculator>();

// Act
int actual = calculator.Sum(1, 2);

// Assert
actual.ShouldBe(3);
}

private sealed class Calculator(ILogger<Calculator> logger)
{
public int Sum(int x, int y)
{
int sum = x + y;

logger.LogInformation("The sum of {X} and {Y} is {Sum}.", x, y, sum);

return sum;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Microsoft.Extensions.Logging;

namespace MartinCostello.Logging.XUnit.Integration;

public sealed class DatabaseFixture : IAsyncLifetime
{
private readonly ILogger _initializeLogger;
private readonly ILogger _disposeLogger;
private string? _connectionString;

public DatabaseFixture(IMessageSink messageSink)
{
using var loggerFactory = new LoggerFactory();

_initializeLogger = loggerFactory.AddXUnit(messageSink, c => c.MessageSinkMessageFactory = CreateMessage).CreateLogger<DatabaseFixture>();
_disposeLogger = messageSink.ToLogger<DatabaseFixture>();

#if XUNIT_V3
static IMessageSinkMessage CreateMessage(string message) => new DiagnosticMessage() { Message = message };
#else
static IMessageSinkMessage CreateMessage(string message) => new PrintableDiagnosticMessage(message);
#endif
}

public string ConnectionString => _connectionString ?? throw new InvalidOperationException("The connection string is only available after InitializeAsync has completed.");

#if XUNIT_V3
ValueTask IAsyncLifetime.InitializeAsync()
{
_initializeLogger.LogInformation("Initializing database");
_connectionString = "Server=localhost";
return ValueTask.CompletedTask;
}

ValueTask IAsyncDisposable.DisposeAsync()
{
_disposeLogger.LogInformation("Disposing database");
return ValueTask.CompletedTask;
}
#else
Task IAsyncLifetime.InitializeAsync()
{
_initializeLogger.LogInformation("Initializing database");
_connectionString = "Server=localhost";
return Task.CompletedTask;
}

Task IAsyncLifetime.DisposeAsync()
{
_disposeLogger.LogInformation("Disposing database");
return Task.CompletedTask;
}
#endif
}
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Microsoft.Extensions.Logging;

namespace MartinCostello.Logging.XUnit.Integration;

public sealed class DatabaseFixture : IAsyncLifetime
{
private readonly ILogger _initializeLogger;
private readonly ILogger _disposeLogger;
private string? _connectionString;

public DatabaseFixture(IMessageSink messageSink)
{
using var loggerFactory = new LoggerFactory();

_initializeLogger = loggerFactory.AddXUnit(messageSink, c => c.MessageSinkMessageFactory = CreateMessage).CreateLogger<DatabaseFixture>();
_disposeLogger = messageSink.ToLogger<DatabaseFixture>();

#if XUNIT_V3
static IMessageSinkMessage CreateMessage(string message) => new DiagnosticMessage() { Message = message };
#else
static IMessageSinkMessage CreateMessage(string message) => new PrintableDiagnosticMessage(message);
#endif
}

public string ConnectionString => _connectionString ?? throw new InvalidOperationException("The connection string is only available after InitializeAsync has completed.");

#if XUNIT_V3
ValueTask IAsyncLifetime.InitializeAsync()
{
_initializeLogger.LogInformation("Initializing database");
_connectionString = "Server=localhost";
return ValueTask.CompletedTask;
}

ValueTask IAsyncDisposable.DisposeAsync()
{
_disposeLogger.LogInformation("Disposing database");
return ValueTask.CompletedTask;
}
#else
Task IAsyncLifetime.InitializeAsync()
{
_initializeLogger.LogInformation("Initializing database");
_connectionString = "Server=localhost";
return Task.CompletedTask;
}

Task IAsyncLifetime.DisposeAsync()
{
_disposeLogger.LogInformation("Disposing database");
return Task.CompletedTask;
}
#endif
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

namespace MartinCostello.Logging.XUnit.Integration;

public class DatabaseTests(DatabaseFixture databaseFixture) : IClassFixture<DatabaseFixture>
{
public DatabaseFixture DatabaseFixture { get; } = databaseFixture;

[Fact]
public void Run_Database_Test()
{
DatabaseFixture.ConnectionString.ShouldNotBeEmpty();
}
}
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

namespace MartinCostello.Logging.XUnit.Integration;

public class DatabaseTests(DatabaseFixture databaseFixture) : IClassFixture<DatabaseFixture>
{
public DatabaseFixture DatabaseFixture { get; } = databaseFixture;

[Fact]
public void Run_Database_Test()
{
DatabaseFixture.ConnectionString.ShouldNotBeEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

#if !XUNIT_V3

namespace MartinCostello.Logging.XUnit.Integration;

/// <summary>
/// See https://github.com/xunit/xunit/pull/2148#issuecomment-839838421.
/// </summary>
internal sealed class PrintableDiagnosticMessage(string message) : DiagnosticMessage(message)
{
public override string ToString() => Message;
}
#endif
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

#if !XUNIT_V3

namespace MartinCostello.Logging.XUnit.Integration;

/// <summary>
/// See https://github.com/xunit/xunit/pull/2148#issuecomment-839838421.
/// </summary>
internal sealed class PrintableDiagnosticMessage(string message) : DiagnosticMessage(message)
{
public override string ToString() => Message;
}
#endif
File renamed without changes.
Loading

0 comments on commit 0fa5e53

Please sign in to comment.