Skip to content

Commit 388963c

Browse files
(#140) Create initial test
1 parent 10d6c32 commit 388963c

File tree

9 files changed

+202
-8
lines changed

9 files changed

+202
-8
lines changed

src/Stravaig.Extensions.Logging.Diagnostics.Tests/Stravaig.Extensions.Logging.Diagnostics.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
1313
<PackageReference Include="Shouldly" Version="4.2.1" />
14+
<PackageReference Include="Verify.NUnit" Version="20.6.0" />
1415
</ItemGroup>
1516

1617
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
@@ -28,6 +29,7 @@
2829
</ItemGroup>
2930

3031
<ItemGroup>
32+
<ProjectReference Include="..\Stravaig.Extensions.Logging.Diagnostics.Verify\Stravaig.Extensions.Logging.Diagnostics.Verify.csproj" />
3133
<ProjectReference Include="..\Stravaig.Extensions.Logging.Diagnostics.XUnit\Stravaig.Extensions.Logging.Diagnostics.XUnit.csproj" />
3234
<ProjectReference Include="..\Stravaig.Extensions.Logging.Diagnostics\Stravaig.Extensions.Logging.Diagnostics.csproj" />
3335
</ItemGroup>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Runtime.CompilerServices;
2+
using DiffEngine;
3+
using Stravaig.Extensions.Logging.Diagnostics.Verify;
4+
5+
namespace Stravaig.Extensions.Logging.Diagnostics.Tests.Verify;
6+
7+
public static class ModuleInitialiser
8+
{
9+
[ModuleInitializer]
10+
public static void InitVerifyStravaigLoggingCapture()
11+
{
12+
DiffTools.UseOrder(DiffTool.Rider, DiffTool.VisualStudioCode);
13+
VerifyStravaigLoggingCapture.Initialise();
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{
3+
Sequence: 0,
4+
LogLevel: Information,
5+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyDefaultSettingsTests,
6+
FormattedMessage: This is the first default log message
7+
},
8+
{
9+
Sequence: 1,
10+
LogLevel: Warning,
11+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyDefaultSettingsTests,
12+
FormattedMessage: This is a warning
13+
},
14+
{
15+
Sequence: 2,
16+
LogLevel: Error,
17+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyDefaultSettingsTests,
18+
FormattedMessage: An exception was thrown. See the exception for details.,
19+
Exception: {
20+
Message: I'm a fake exception to be put in the log.,
21+
Type: System.ApplicationException
22+
}
23+
},
24+
{
25+
Sequence: 3,
26+
LogLevel: Critical,
27+
CategoryName: Stravaig.Extensions.Logging.Diagnostics.Tests.Verify.VerifyDefaultSettingsTests,
28+
FormattedMessage: An exception was thrown, which caused another exception to be thrown.,
29+
Exception: {
30+
Message: An application exception happened,
31+
Type: System.ApplicationException,
32+
InnerException: {
33+
Message: An invalid operation happened.,
34+
Type: System.InvalidOperationException
35+
}
36+
}
37+
}
38+
]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Threading.Tasks;
5+
using Microsoft.Extensions.Logging;
6+
using NUnit.Framework;
7+
using Stravaig.Extensions.Logging.Diagnostics.Verify;
8+
using VerifyNUnit;
9+
using VerifyTests;
10+
11+
namespace Stravaig.Extensions.Logging.Diagnostics.Tests.Verify;
12+
13+
[TestFixture]
14+
public class VerifyDefaultSettingsTests
15+
{
16+
[Test]
17+
public async Task TestExplicitDefaultAsync()
18+
{
19+
var settings = new VerifySettings().AddStravaigTests();
20+
var logs = GetLogEntries();
21+
await Verifier.Verify(logs, settings);
22+
}
23+
24+
public async Task TestImplicitDefaultAwait()
25+
{
26+
// The settings are added as part of the module initialiser
27+
var logs = GetLogEntries();
28+
await Verifier.Verify(logs);
29+
}
30+
31+
private IEnumerable<LogEntry> GetLogEntries()
32+
{
33+
var logger = new TestCaptureLogger<VerifyDefaultSettingsTests>();
34+
logger.LogInformation("This is the first default log message");
35+
logger.LogWarning("This is a warning");
36+
try
37+
{
38+
throw new ApplicationException("I'm a fake exception to be put in the log.");
39+
}
40+
catch (Exception ex)
41+
{
42+
logger.LogError(ex, "An exception was thrown. See the exception for details.");
43+
}
44+
45+
try
46+
{
47+
try
48+
{
49+
throw new InvalidOperationException("An invalid operation happened.");
50+
}
51+
catch (Exception ex)
52+
{
53+
throw new ApplicationException("An application exception happened", ex);
54+
}
55+
}
56+
catch (Exception ex)
57+
{
58+
logger.LogCritical(ex, "An exception was thrown, which caused another exception to be thrown.");
59+
}
60+
61+
return logger.GetLogs();
62+
}
63+
}

src/Stravaig.Extensions.Logging.Diagnostics.Verify/LogEntryConverter.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using VerifyTests;
34

@@ -28,6 +29,10 @@ private class Context
2829
internal bool IsWritingLogLevel => Using(Settings.LogLevel);
2930
internal bool IsWritingCategoryName => Using(Settings.CategoryName);
3031
internal bool IsWritingFormattedMessage => Using(Settings.FormattedMessage);
32+
internal bool IsWritingException => Using(Settings.Exception);
33+
internal bool IsWritingExceptionMessage => Using(Settings.ExceptionMessage);
34+
internal bool IsWritingExceptionType => Using(Settings.ExceptionType);
35+
internal bool IsWritingInnerException => Using(Settings.InnerException);
3136

3237
internal int CurrentSequence(LogEntry logEntry) =>
3338
Using(Settings.KeepSequenceCadence)
@@ -69,6 +74,7 @@ public override void Write(VerifyJsonWriter writer, IEnumerable<LogEntry> logEnt
6974
WriteLogLevel(ctx, logEntry);
7075
WriteCategoryName(ctx, logEntry);
7176
WriteFormattedMessage(ctx, logEntry);
77+
WriteException(ctx, logEntry);
7278

7379
writer.WriteEndObject();
7480
ctx.MoveToNextLogEntry();
@@ -77,6 +83,40 @@ public override void Write(VerifyJsonWriter writer, IEnumerable<LogEntry> logEnt
7783
writer.WriteEndArray();
7884
}
7985

86+
private static void WriteException(Context ctx, LogEntry logEntry)
87+
{
88+
var ex = logEntry.Exception;
89+
if (ex != null && ctx.IsWritingException)
90+
{
91+
ctx.Writer.WritePropertyName(nameof(logEntry.Exception));
92+
WriteException(ctx, ex);
93+
}
94+
}
95+
96+
private static void WriteException(Context ctx, Exception ex)
97+
{
98+
ctx.Writer.WriteStartObject();
99+
if (ctx.IsWritingExceptionMessage)
100+
{
101+
ctx.Writer.WritePropertyName(nameof(ex.Message));
102+
ctx.Writer.WriteValue(ex.Message);
103+
}
104+
105+
if (ctx.IsWritingExceptionType)
106+
{
107+
ctx.Writer.WritePropertyName(nameof(Type));
108+
ctx.Writer.WriteValue(ex.GetType().FullName);
109+
}
110+
111+
if (ex.InnerException != null && ctx.IsWritingInnerException)
112+
{
113+
ctx.Writer.WritePropertyName(nameof(ex.InnerException));
114+
WriteException(ctx, ex.InnerException);
115+
}
116+
117+
ctx.Writer.WriteEndObject();
118+
}
119+
80120
private static void WriteFormattedMessage(Context ctx, LogEntry logEntry)
81121
{
82122
if (ctx.IsWritingFormattedMessage)

src/Stravaig.Extensions.Logging.Diagnostics.Verify/Settings.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,35 @@ public enum Settings
4141
/// </summary>
4242
FormattedMessage = 0x0000_0010,
4343

44+
/// <summary>
45+
/// Indicates that the exception is to be written out, if it exists.
46+
/// </summary>
47+
Exception = 0x0000_0020,
48+
49+
/// <summary>
50+
/// Indicates that the exception type is to be written out.
51+
/// </summary>
52+
ExceptionType = 0x0000_0040,
53+
54+
/// <summary>
55+
/// Indicates that the exception message is to be written out.
56+
/// </summary>
57+
ExceptionMessage = 0x0000_0080,
58+
59+
/// <summary>
60+
/// Indicates that inner exceptions are to be written out.
61+
/// </summary>
62+
InnerException = 0x0000_0100,
63+
4464
/// <summary>
4565
/// Indicates the default settings are to be used.
4666
/// </summary>
47-
Default = Sequence | LogLevel | CategoryName | FormattedMessage,
48-
}
49-
50-
internal static class SettingsExtensions
51-
{
52-
67+
Default = Sequence |
68+
LogLevel |
69+
CategoryName |
70+
FormattedMessage |
71+
Exception |
72+
ExceptionType |
73+
ExceptionMessage |
74+
InnerException,
5375
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using VerifyTests;
2+
3+
namespace Stravaig.Extensions.Logging.Diagnostics.Verify;
4+
5+
public static class VerifySettingsExtensions
6+
{
7+
public static VerifySettings AddStravaigTests(this VerifySettings verifySettings)
8+
{
9+
verifySettings.AddExtraSettings(jsonSettings =>
10+
{
11+
jsonSettings.Converters.Add(new LogEntryConverter());
12+
});
13+
return verifySettings;
14+
}
15+
}

src/Stravaig.Extensions.Logging.Diagnostics.Verify/VerifyStravaigLoggingCapture.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
2-
using System.Text.Json;
32
using Argon;
43
using VerifyTests;
5-
using JsonSerializer = Argon.JsonSerializer;
64

75
namespace Stravaig.Extensions.Logging.Diagnostics.Verify;
86

src/Stravaig.Extensions.Logging.Diagnostics.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=LogMessageIsSentenceProblem/@EntryIndexedValue">DO_NOT_SHOW</s:String>
33
<s:Boolean x:Key="/Default/CodeInspection/Highlighting/RunLongAnalysisInSwa/@EntryValue">True</s:Boolean>
44
<s:String x:Key="/Default/CodeInspection/Highlighting/SweaWarningsMode/@EntryValue">ShowAndRun</s:String>
5+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Initialiser/@EntryIndexedValue">True</s:Boolean>
56
<s:Boolean x:Key="/Default/UserDictionary/Words/=Shouldly/@EntryIndexedValue">True</s:Boolean>
67
<s:Boolean x:Key="/Default/UserDictionary/Words/=Stravaig/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

0 commit comments

Comments
 (0)