Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jan 8, 2024
1 parent 9df0a65 commit e9a0fb8
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/Verify/Compare/SharedSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

public static partial class VerifierSettings
{
static Dictionary<string, StringCompare> stringComparers = [];
static Dictionary<string, StreamCompare> streamComparers = [];
static IDictionary<string, StringCompare> stringComparers = new Dictionary<string, StringCompare>();
static IDictionary<string, StreamCompare> streamComparers = new Dictionary<string, StreamCompare>();
static StringCompare? defaultStringComparer;

internal static bool TryGetStreamComparer(string extension, [NotNullWhen(true)] out StreamCompare? comparer) =>
Expand Down
11 changes: 11 additions & 0 deletions src/Verify/Counter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

public partial class Counter
{
internal static void FirstRun()
{
#if NET8_0_OR_GREATER
globalNamedDates = globalNamedDates.ToFrozenDictionary();
globalNamedDateTimes = globalNamedDateTimes.ToFrozenDictionary();
globalNamedDateTimeOffsets = globalNamedDateTimeOffsets.ToFrozenDictionary();
globalNamedGuids = globalNamedGuids.ToFrozenDictionary();
globalNamedTimes = globalNamedTimes.ToFrozenDictionary();
#endif
}

#if NET6_0_OR_GREATER
Dictionary<Date, string> namedDates;
Dictionary<Time, string> namedTimes;
Expand Down
3 changes: 1 addition & 2 deletions src/Verify/Counter_Date.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ namespace VerifyTests;
public partial class Counter
{
ConcurrentDictionary<Date, (int intValue, string stringValue)> dateCache = [];
static Dictionary<Date, string> globalNamedDates = [];
static IDictionary<Date, string> globalNamedDates = new Dictionary<Date, string>();
int currentDate;

internal static void AddNamed(Date value, string name)
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Counter_DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class Counter
{
ConcurrentDictionary<DateTime, (int intValue, string stringValue)> dateTimeCache = new(new DateTimeComparer());
static Dictionary<DateTime, string> globalNamedDateTimes = [];
static IDictionary<DateTime, string> globalNamedDateTimes = new Dictionary<DateTime, string>();

class DateTimeComparer : IEqualityComparer<DateTime>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Counter_DateTimeOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class Counter
{
ConcurrentDictionary<DateTimeOffset, (int intValue, string stringValue)> dateTimeOffsetCache = new(new DateTimeOffsetComparer());
static Dictionary<DateTimeOffset, string> globalNamedDateTimeOffsets = [];
static IDictionary<DateTimeOffset, string> globalNamedDateTimeOffsets = new Dictionary<DateTimeOffset, string>();

class DateTimeOffsetComparer :
IEqualityComparer<DateTimeOffset>
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Counter_Guid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class Counter
{
ConcurrentDictionary<Guid, (int intValue, string stringValue)> guidCache = [];
static Dictionary<Guid, string> globalNamedGuids = [];
static IDictionary<Guid, string> globalNamedGuids = new Dictionary<Guid, string>();
int currentGuid;

public int Next(Guid input) =>
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Counter_Time.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace VerifyTests;
public partial class Counter
{
ConcurrentDictionary<Time, (int intValue, string stringValue)> timeCache = [];
static Dictionary<Time, string> globalNamedTimes = [];
static IDictionary<Time, string> globalNamedTimes = new Dictionary<Time, string>();
int currentTime;

internal static void AddNamed(Time time, string name)
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Naming/VerifierSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static partial class VerifierSettings
{
internal static Namer SharedNamer = new();

static Dictionary<Type, Func<object, string>> parameterToNameLookup = new()
static IDictionary<Type, Func<object, string>> parameterToNameLookup = new Dictionary<Type, Func<object, string>>
{
{
typeof(bool), _ => ((bool) _).ToString(Culture.InvariantCulture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public static partial class VerifierSettings
{
internal static Dictionary<string, List<Action<StringBuilder, Counter>>> ExtensionMappedGlobalScrubbers = [];
internal static IDictionary<string, List<Action<StringBuilder, Counter>>> ExtensionMappedGlobalScrubbers = new Dictionary<string, List<Action<StringBuilder, Counter>>>();

/// <summary>
/// Modify the resulting test content using custom code.
Expand Down
4 changes: 2 additions & 2 deletions src/Verify/Serialization/VerifierSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static bool TryGetToString(
return typeToString.TryGetValue(target.GetType(), out toString);
}

static Dictionary<Type, Func<object, IReadOnlyDictionary<string, object>, AsStringResult>> typeToString = new()
static IDictionary<Type, Func<object, IReadOnlyDictionary<string, object>, AsStringResult>> typeToString = new Dictionary<Type, Func<object, IReadOnlyDictionary<string, object>, AsStringResult>>
{
#region typeToStringMapping

Expand Down Expand Up @@ -135,7 +135,7 @@ public static void TreatAsString<T>(AsString<T>? toString = null)

internal static void Reset()
{
InnerVerifier.verifyHasBeenRun = false;
InnerVerifier.verifyHasBeenRun = 0;
DateCountingEnabled = true;
StrictJson = false;
scrubProjectDir = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public static partial class VerifierSettings
{
static Dictionary<Type, Dictionary<string, ConvertTargetMember>> membersConverters = [];
static IDictionary<Type, Dictionary<string, ConvertTargetMember>> membersConverters = new Dictionary<Type, Dictionary<string, ConvertTargetMember>>();

internal static ConvertTargetMember? GetMemberConverter(MemberInfo member) =>
GetMemberConverter(member.DeclaringType, member.Name);
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Splitters/Settings_Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public static partial class VerifierSettings
{
static Dictionary<string, AsyncConversion<Stream>> extensionConverters = [];
static IDictionary<string, AsyncConversion<Stream>> extensionConverters = new Dictionary<string, AsyncConversion<Stream>>();

internal static bool TryGetExtensionConverter(string extension, [NotNullWhen(true)] out AsyncConversion<Stream>? converter) =>
extensionConverters.TryGetValue(extension, out converter);
Expand Down
15 changes: 12 additions & 3 deletions src/Verify/Verifier/InnerVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public partial class InnerVerifier :
GetIndexedFileNames getIndexedFileNames = null!;
IEnumerable<string> verifiedFiles = null!;
Counter counter;
internal static bool verifyHasBeenRun;
internal static int verifyHasBeenRun;

[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowIfVerifyHasBeenRun()
{
if (!verifyHasBeenRun)
if (verifyHasBeenRun == 0)
{
return;
}
Expand All @@ -38,7 +38,7 @@ public InnerVerifier(
Guard.AgainstEmpty(sourceFile);
Guard.AgainstEmpty(typeName);
Guard.AgainstEmpty(methodName);
verifyHasBeenRun = true;
FirstRun();
VerifierSettings.RunBeforeCallbacks();
this.settings = settings;

Expand All @@ -63,6 +63,15 @@ public InnerVerifier(
}
}

static void FirstRun()
{
if (Interlocked.Exchange(ref verifyHasBeenRun, 1) == 0)
{
VerifierSettings.FirstRun();
Counter.FirstRun();
}
}

/// <summary>
/// Initialize a new instance of the <see cref="InnerVerifier" /> class for verifying the entire file (not just a specific type)
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions src/Verify/VerifierSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ public static partial class VerifierSettings
public static void OmitContentFromException() =>
omitContentFromException = true;

internal static void FirstRun()
{
#if NET8_0_OR_GREATER
stringComparers = stringComparers.ToFrozenDictionary();
streamComparers = streamComparers.ToFrozenDictionary();
parameterToNameLookup = parameterToNameLookup.ToFrozenDictionary();
ExtensionMappedGlobalScrubbers = ExtensionMappedGlobalScrubbers.ToFrozenDictionary();
typeToString = typeToString.ToFrozenDictionary();
membersConverters = membersConverters.ToFrozenDictionary();
extensionConverters = extensionConverters.ToFrozenDictionary();
#endif
}

/// <summary>
/// Automatically accept the results of all tests.
/// </summary>
Expand Down

0 comments on commit e9a0fb8

Please sign in to comment.