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
5e9cae4
commit 6192a96
Showing
8 changed files
with
100 additions
and
5 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
28 changes: 28 additions & 0 deletions
28
Src/FluentAssertions/Extensibility/ExtensionAssemblyAttribute.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,28 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace FluentAssertions.Extensibility; | ||
|
||
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] | ||
public sealed class ExtensionAssemblyAttribute : Attribute | ||
{ | ||
private readonly string methodName; | ||
private readonly Type type; | ||
|
||
public ExtensionAssemblyAttribute() | ||
{ | ||
} | ||
|
||
#pragma warning disable CA1019 | ||
public ExtensionAssemblyAttribute(Type type, string methodName) | ||
#pragma warning restore CA1019 | ||
{ | ||
this.type = type; | ||
this.methodName = methodName; | ||
} | ||
|
||
public 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
15 changes: 15 additions & 0 deletions
15
Tests/FluentAssertions.Specs/Extensibility/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 @@ | ||
using Xunit; | ||
|
||
namespace FluentAssertions.Specs.Extensibility; | ||
|
||
public class ExtensionAssemblyAttributeSpecs | ||
{ | ||
[Fact] | ||
public void Extension_assembly_initialization_code_will_be_called_only_once() | ||
{ | ||
for (int i = 0; i < 10; i++) | ||
{ | ||
FileContainingAssemblyLevelAttributes.ShouldBeCalledOnlyOnce.Should().Be(1); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Tests/FluentAssertions.Specs/Extensibility/FileContainingAssemblyLevelAttributes.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,22 @@ | ||
using FluentAssertions.Extensibility; | ||
using FluentAssertions.Specs.Extensibility; | ||
|
||
// With specific initialization code to invoke before the first assertion happens | ||
[assembly: ExtensionAssembly( | ||
typeof(FileContainingAssemblyLevelAttributes), | ||
nameof(FileContainingAssemblyLevelAttributes.InitializeBeforeFirstAssertion))] | ||
|
||
// Multiple attributes are allowed and no initialization code is required | ||
[assembly: ExtensionAssembly] | ||
|
||
namespace FluentAssertions.Specs.Extensibility; | ||
|
||
public static class FileContainingAssemblyLevelAttributes | ||
{ | ||
public static int ShouldBeCalledOnlyOnce { get; private set; } | ||
|
||
public static void InitializeBeforeFirstAssertion() | ||
{ | ||
++ShouldBeCalledOnlyOnce; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -74,4 +74,5 @@ | |
<ProjectReference Include="..\AssemblyB\AssemblyB.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> | ||
|
||
</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