forked from fluentassertions/fluentassertions
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f94f597
commit 8d61009
Showing
1 changed file
with
86 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
using FluentAssertions.Extensibility; | ||
using JetBrains.Annotations; | ||
|
||
namespace FluentAssertions; | ||
|
||
public static class AssertionEngine | ||
{ | ||
private static readonly object Lockable = new(); | ||
private static bool isInitialized; | ||
|
||
static AssertionEngine() | ||
{ | ||
EnsureInitialized(); | ||
} | ||
|
||
[PublicAPI] | ||
public static void ResetToDefaults() | ||
{ | ||
isInitialized = false; | ||
EnsureInitialized(); | ||
} | ||
|
||
internal static void EnsureInitialized() | ||
{ | ||
if (isInitialized) | ||
{ | ||
return; | ||
} | ||
|
||
lock (Lockable) | ||
{ | ||
if (!isInitialized) | ||
{ | ||
ExecuteCustomInitializers(); | ||
} | ||
} | ||
} | ||
|
||
private static void ExecuteCustomInitializers() | ||
{ | ||
var currentAssembly = Assembly.GetExecutingAssembly(); | ||
var currentAssemblyName = currentAssembly.GetName(); | ||
|
||
AssertionEngineInitializerAttribute[] attributes = []; | ||
|
||
try | ||
{ | ||
attributes = AppDomain.CurrentDomain | ||
.GetAssemblies() | ||
.Where(assembly => assembly != currentAssembly && !assembly.IsDynamic && !IsFramework(assembly)) | ||
.Where(a => a.GetReferencedAssemblies().Any(r => r.FullName == currentAssemblyName.FullName)) | ||
.SelectMany(a => a.GetCustomAttributes<AssertionEngineInitializerAttribute>()) | ||
.ToArray(); | ||
} | ||
catch | ||
{ | ||
// Just ignore any exceptions that might happen while trying to find the attributes | ||
} | ||
|
||
foreach (var attribute in attributes) | ||
{ | ||
try | ||
{ | ||
attribute.Initialize(); | ||
} | ||
catch | ||
{ | ||
// Just ignore any exceptions that might happen while trying to find the attributes | ||
} | ||
} | ||
} | ||
|
||
private static bool IsFramework(Assembly assembly) | ||
{ | ||
#if NET6_0_OR_GREATER | ||
return assembly!.FullName?.StartsWith("Microsoft.", StringComparison.OrdinalIgnoreCase) == true || | ||
assembly.FullName?.StartsWith("System.", StringComparison.OrdinalIgnoreCase) == true; | ||
#else | ||
return assembly.FullName.StartsWith("Microsoft.", StringComparison.OrdinalIgnoreCase) || | ||
assembly.FullName.StartsWith("System.", StringComparison.OrdinalIgnoreCase); | ||
#endif | ||
} | ||
} |