Skip to content

Commit 473d81e

Browse files
#140: Refactor (WIP)
1 parent 8e8507e commit 473d81e

File tree

9 files changed

+219
-108
lines changed

9 files changed

+219
-108
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace Stravaig.Extensions.Logging.Diagnostics.Verify;
4+
5+
[Flags]
6+
public enum ExceptionSetting
7+
{
8+
None = 0x0,
9+
10+
Type = 0x1,
11+
12+
Message = 0x2,
13+
14+
StackTrace = 0x4,
15+
16+
IncludeInnerExceptions = 0x8,
17+
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,16 @@ namespace Stravaig.Extensions.Logging.Diagnostics.Verify;
88

99
internal class LogEntryConverter : WriteOnlyJsonConverter<IEnumerable<LogEntry>>
1010
{
11-
internal const string DefaultNonDeterministicPropertyValueSubstitute = "*** NONDETERMINISTIC ***";
12-
private readonly Settings _settings;
13-
private readonly IReadOnlySet<string> _nonDeterministicPropertyNames;
14-
private readonly string _nonDeterministicPropertyValueSubstitute;
11+
private readonly LoggingCaptureVerifySettings _settings;
1512

1613
public LogEntryConverter()
17-
: this(Settings.Default, ImmutableHashSet<string>.Empty, DefaultNonDeterministicPropertyValueSubstitute)
14+
: this(LoggingCaptureVerifySettings.Default)
1815
{
19-
2016
}
2117

22-
public LogEntryConverter(Settings settings, IReadOnlySet<string> nonDeterministicPropertyNames, string nonDeterministicPropertyValueSubstitute)
18+
public LogEntryConverter(LoggingCaptureVerifySettings settings)
2319
{
2420
_settings = settings;
25-
_nonDeterministicPropertyNames = nonDeterministicPropertyNames;
26-
_nonDeterministicPropertyValueSubstitute = nonDeterministicPropertyValueSubstitute;
2721
}
2822

2923
private class Context
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Immutable;
4+
using System.Linq;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Stravaig.Extensions.Logging.Diagnostics.Verify;
8+
9+
public class LoggingCaptureVerifySettings
10+
{
11+
private const string DefaultNonDeterministicPropertyValueSubstitute = "*** NONDETERMINISTIC ***";
12+
13+
/// <summary>
14+
/// The default settings for verifying captured logs. This is a reasonable
15+
/// set of defaults that should not contain deterministic information.
16+
/// </summary>
17+
public static readonly LoggingCaptureVerifySettings Default = new();
18+
19+
private readonly PropertySetting _properties = PropertySetting.None;
20+
private ImmutableHashSet<string> _nondeterministicPropertyNames = [];
21+
22+
/// <summary>
23+
/// Gets how the Sequence number is to be verified.
24+
/// Default is ShowAsConsecutive.
25+
/// </summary>
26+
public Sequence Sequence { get; init; } = Sequence.ShowAsConsecutive;
27+
28+
/// <summary>
29+
/// Gets the minimum log level to be verified.
30+
/// Default is to verify all (i.e. Trace and above.)
31+
/// </summary>
32+
public LogLevel MinLogLevel { get; init; } = LogLevel.Trace;
33+
34+
/// <summary>
35+
/// Gets whether the category name is to be verified.
36+
/// Default is true.
37+
/// </summary>
38+
public bool CategoryName { get; init; } = true;
39+
40+
/// <summary>
41+
/// Gets how the message is to be verified.
42+
/// Default is Template.
43+
/// </summary>
44+
public MessageSetting Message { get; init; } = MessageSetting.Template;
45+
46+
/// <summary>
47+
/// Gets the parts of the exception to be verified if it exists.
48+
/// Default is Type.
49+
/// </summary>
50+
public ExceptionSetting Exception { get; init; } = ExceptionSetting.Type;
51+
52+
/// <summary>
53+
/// Gets how the properties from the log are to be verified.
54+
/// Default is None.
55+
/// </summary>
56+
public PropertySetting Properties
57+
{
58+
get => _properties;
59+
init
60+
{
61+
_properties = value;
62+
BuildNondeterministicPropertySet(_nondeterministicPropertyNames);
63+
}
64+
}
65+
66+
/// <summary>
67+
/// A list of property names that contain non-deterministic information.
68+
/// </summary>
69+
public IEnumerable<string> NondeterministicPropertyNames
70+
{
71+
get => _nondeterministicPropertyNames;
72+
init => BuildNondeterministicPropertySet(value);
73+
}
74+
75+
/// <summary>
76+
/// Gets the value to replace nondeterministic properties with.
77+
/// </summary>
78+
public string NondeterministicPropertySubstitute { get; init; } = "*** Nondeterministic ***";
79+
80+
private void BuildNondeterministicPropertySet(IEnumerable<string> values)
81+
{
82+
_nondeterministicPropertyNames = values.ToImmutableHashSet((Properties & PropertySetting.CaseInsensitivePropertyMatch) == PropertySetting.CaseInsensitivePropertyMatch ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal);
83+
}
84+
85+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Stravaig.Extensions.Logging.Diagnostics.Verify;
2+
3+
public enum MessageSetting
4+
{
5+
/// <summary>
6+
/// The message is not to be verified.
7+
/// </summary>
8+
None,
9+
10+
/// <summary>
11+
/// The formatted message is to be verified.
12+
/// </summary>
13+
/// <remarks>Formatted messages may contain non-deterministic information,
14+
/// in which case this should not be used to prevent brittle tests.</remarks>
15+
Formatted,
16+
17+
/// <summary>
18+
/// The message template is to be verified.
19+
/// </summary>
20+
Template,
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
3+
namespace Stravaig.Extensions.Logging.Diagnostics.Verify;
4+
5+
/// <summary>
6+
/// The settings for verifying properties.
7+
/// </summary>
8+
[Flags]
9+
public enum PropertySetting
10+
{
11+
/// <summary>
12+
/// The properties are not to be rendered.
13+
/// </summary>
14+
None = 0x0,
15+
16+
/// <summary>
17+
/// All property information is shown.
18+
/// </summary>
19+
/// <remarks>Any nondeterministic properties are ignored, unless
20+
/// RedactNonDeterministic is also set.</remarks>
21+
Verify = 0x1,
22+
23+
/// <summary>
24+
/// Nondeterministic properties are shown in a redacted form.
25+
/// </summary>
26+
/// <remarks>This will have no effect unless Verify is also set.</remarks>
27+
RedactNonDeterministic = 0x2,
28+
29+
/// <summary>
30+
/// When determining property matched against the collection of
31+
/// nondeterministic properties, use a case-insensitive check.
32+
/// </summary>
33+
CaseInsensitivePropertyMatch = 0x4,
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace Stravaig.Extensions.Logging.Diagnostics.Verify;
2+
3+
/// <summary>
4+
/// Indicates the ways that a sequence can be rendered.
5+
/// </summary>
6+
public enum Sequence
7+
{
8+
/// <summary>
9+
/// Hides the sequence number.
10+
/// </summary>
11+
Hide,
12+
13+
/// <summary>
14+
/// Shows the sequence of the logs as a zero based consecutive sequence
15+
/// (i.e. 0, 1, 2, 3...) regardless of the actual sequence number of the log
16+
/// entry.
17+
/// </summary>
18+
ShowAsConsecutive,
19+
20+
/// <summary>
21+
/// Shows the sequence of the logs as a zero based sequence retaining the
22+
/// cadence of the original logs. (e.g. the original sequence is 25, 27, 32
23+
/// then it will be written as 0, 2, 7)
24+
/// </summary>
25+
ShowAsCadence,
26+
}

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

Lines changed: 0 additions & 83 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Verify extensions for Stravaig Logging Capture for Tests.
1717
</Description>
1818
<Nullable>enable</Nullable>
19-
<LangVersion>11</LangVersion>
19+
<LangVersion>12</LangVersion>
2020
<RootNamespace>Stravaig.Extensions.Logging.Diagnostics.Verify</RootNamespace>
2121
</PropertyGroup>
2222

@@ -28,7 +28,7 @@
2828
</ItemGroup>
2929

3030
<ItemGroup>
31-
<PackageReference Include="Verify" Version="22.11.1" />
31+
<PackageReference Include="Verify" Version="23.5.2" />
3232
</ItemGroup>
3333

3434
<ItemGroup>
Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,50 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Collections.Immutable;
4-
using System.Linq;
51
using VerifyTests;
62

73
namespace Stravaig.Extensions.Logging.Diagnostics.Verify;
84

5+
/// <summary>
6+
/// Extension methods for configuring the captured logs verify settings.
7+
/// </summary>
98
public static class VerifySettingsExtensions
109
{
1110
/// <summary>
1211
/// Adds the settings required for verifying captured log entries using Stravaig Logging Diagnostics.
1312
/// </summary>
1413
/// <param name="verifySettings">The verify settings object to modify.</param>
15-
/// <param name="settings">The settings for verifying the tests.</param>
16-
/// <param name="nonDeterministicPropertyNames">A collection of property names to exclude from the output because their values are non-deterministic</param>
17-
/// <returns>The verify Settings object.</returns>
18-
19-
public static VerifySettings AddStravaigTests(this VerifySettings verifySettings, Settings settings = Settings.Default, IEnumerable<string>? nonDeterministicPropertyNames = null, string nonDeterministicPropertyValueSubstitute = LogEntryConverter.DefaultNonDeterministicPropertyValueSubstitute)
14+
/// <param name="settings">The settings for verifying the captured logs.</param>
15+
public static VerifySettings AddCapturedLogs(this VerifySettings verifySettings, LoggingCaptureVerifySettings settings)
2016
{
21-
var nonDeterministicPropertySet = ImmutableHashSet.Create(StringComparer.Ordinal, nonDeterministicPropertyNames?.ToArray() ?? Array.Empty<string>());
2217
verifySettings.AddExtraSettings(jsonSettings =>
2318
{
24-
// Remove the default version if it exists.
19+
// Removes the default version if it exists.
2520
jsonSettings.Converters.RemoveAll(c => c is LogEntryConverter);
26-
27-
// Create replacement converter.
28-
var logEntryConverter = new LogEntryConverter(settings, nonDeterministicPropertySet, nonDeterministicPropertyValueSubstitute);
21+
22+
var logEntryConverter = new LogEntryConverter(settings);
2923
jsonSettings.Converters.Add(logEntryConverter);
3024
});
3125
return verifySettings;
3226
}
27+
28+
/// <summary>
29+
/// Adds the settings required for verifying captured log entries using Stravaig Logging Diagnostics.
30+
/// </summary>
31+
/// <param name="verifySettings">The verify settings object to modify.</param>
32+
/// <param name="settings">The settings for verifying the tests.</param>
33+
/// <param name="nonDeterministicPropertyNames">A collection of property names to exclude from the output because their values are non-deterministic</param>
34+
/// <param name="nonDeterministicPropertyValueSubstitute">The value to substitute to mask out non-deterministic property values.</param>
35+
/// <returns>The verify Settings object.</returns>
36+
// public static VerifySettings AddStravaigTests(this VerifySettings verifySettings, Settings settings = Settings.Default, IEnumerable<string>? nonDeterministicPropertyNames = null, string nonDeterministicPropertyValueSubstitute = LogEntryConverter.DefaultNonDeterministicPropertyValueSubstitute)
37+
// {
38+
// var nonDeterministicPropertySet = ImmutableHashSet.Create(StringComparer.Ordinal, nonDeterministicPropertyNames?.ToArray() ?? Array.Empty<string>());
39+
// verifySettings.AddExtraSettings(jsonSettings =>
40+
// {
41+
// // Remove the default version if it exists.
42+
// jsonSettings.Converters.RemoveAll(c => c is LogEntryConverter);
43+
//
44+
// // Create replacement converter.
45+
// var logEntryConverter = new LogEntryConverter(settings, nonDeterministicPropertySet, nonDeterministicPropertyValueSubstitute);
46+
// jsonSettings.Converters.Add(logEntryConverter);
47+
// });
48+
// return verifySettings;
49+
// }
3350
}

0 commit comments

Comments
 (0)