-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move all the code for the tests to a Shared folder for re-use.
- Loading branch information
1 parent
4361023
commit 0fa5e53
Showing
15 changed files
with
1,164 additions
and
1,176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 11 additions & 11 deletions
22
tests/Logging.XUnit.Tests/Constructor.cs → tests/Shared/Constructor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
108
tests/Logging.XUnit.Tests/Examples.cs → tests/Shared/Examples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
114 changes: 57 additions & 57 deletions
114
...Unit.Tests/Integration/DatabaseFixture.cs → tests/Shared/Integration/DatabaseFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
30 changes: 15 additions & 15 deletions
30
....XUnit.Tests/Integration/DatabaseTests.cs → tests/Shared/Integration/DatabaseTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 15 additions & 15 deletions
30
...Integration/PrintableDiagnosticMessage.cs → ...Integration/PrintableDiagnosticMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.