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.
Introduced a new attribute to allow global initialization of the asse…
…rtion engine
- Loading branch information
1 parent
f4c3d56
commit bc727d5
Showing
18 changed files
with
284 additions
and
9 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
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
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
30 changes: 30 additions & 0 deletions
30
Src/FluentAssertions/Extensibility/AssertionEngineInitializerAttribute.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace FluentAssertions.Extensibility; | ||
|
||
/// <summary> | ||
/// Can be added to an assembly so it gets a change to initialize Fluent Assertions before the first assertion happens. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] | ||
public sealed class AssertionEngineInitializerAttribute : Attribute | ||
{ | ||
private readonly string methodName; | ||
private readonly Type type; | ||
|
||
/// <summary> | ||
/// Defines the static void-returned and parameterless method that should be invoked before the first assertion happens. | ||
/// </summary> | ||
#pragma warning disable CA1019 | ||
public AssertionEngineInitializerAttribute(Type type, string methodName) | ||
#pragma warning restore CA1019 | ||
{ | ||
this.type = type; | ||
this.methodName = methodName; | ||
} | ||
|
||
internal void Initialize() | ||
{ | ||
type?.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static)?.Invoke(obj: null, parameters: null); | ||
} | ||
} |
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
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
20 changes: 20 additions & 0 deletions
20
Tests/FluentAssertions.Extensibility.Specs/AssertionEngineInitializer.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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Threading; | ||
|
||
// With specific initialization code to invoke before the first assertion happens | ||
[assembly: FluentAssertions.Extensibility.AssertionEngineInitializer( | ||
typeof(FluentAssertions.Extensibility.Specs.AssertionEngineInitializer), | ||
nameof(FluentAssertions.Extensibility.Specs.AssertionEngineInitializer.InitializeBeforeFirstAssertion))] | ||
|
||
namespace FluentAssertions.Extensibility.Specs; | ||
|
||
public static class AssertionEngineInitializer | ||
{ | ||
private static int shouldBeCalledOnlyOnce; | ||
|
||
public static int ShouldBeCalledOnlyOnce => shouldBeCalledOnlyOnce; | ||
|
||
public static void InitializeBeforeFirstAssertion() | ||
{ | ||
Interlocked.Increment(ref shouldBeCalledOnlyOnce); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Tests/FluentAssertions.Extensibility.Specs/ExtensionAssemblyAttributeSpecs.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace FluentAssertions.Extensibility.Specs; | ||
|
||
public class ExtensionAssemblyAttributeSpecs | ||
{ | ||
[Fact] | ||
public void Calls_assembly_initialization_code_only_once() | ||
{ | ||
for (int i = 0; i < 10; i++) | ||
{ | ||
var act = () => AssertionEngineInitializer.ShouldBeCalledOnlyOnce.Should().Be(1); | ||
|
||
act.Should().NotThrow(); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Tests/FluentAssertions.Extensibility.Specs/FluentAssertions.Extensibility.Specs.csproj
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,48 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net47</TargetFrameworks> | ||
<SignAssembly>True</SignAssembly> | ||
<AssemblyOriginatorKeyFile>..\..\Src\FluentAssertions\FluentAssertions.snk</AssemblyOriginatorKeyFile> | ||
<IsPackable>false</IsPackable> | ||
<NoWarn>$(NoWarn),IDE0052,1573,1591,1712</NoWarn> | ||
<DebugType>full</DebugType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' == 'net47'"> | ||
<!-- | ||
(SRCU = System.Runtime.CompilerServices.Unsafe) | ||
FluentAssertions.csproj depends on SRCU 4.5.0 (AssemblyVersion 4.0.4.0) | ||
System.Collections.Immutable 1.6.0+ depends on SRCU 4.5.2+ (AssemblyVersion 4.0.4.1) | ||
FluentAssertions.Specs.csproj for net47 does not generate a binding redirect from 4.0.4.0 to 4.0.4.1 | ||
Upgrading to 1.6.0 gives "Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0" | ||
--> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'"> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="PolySharp" Version="1.13.2"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="xunit" Version="2.5.0" /> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" PrivateAssets="all"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Src\FluentAssertions\FluentAssertions.csproj" /> | ||
</ItemGroup> | ||
</Project> |
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 @@ | ||
global using Xunit; |
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
Oops, something went wrong.