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 a71a206
Showing
20 changed files
with
337 additions
and
14 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
40 changes: 40 additions & 0 deletions
40
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,40 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace FluentAssertions.Extensibility; | ||
|
||
/// <summary> | ||
/// Marks an assembly as containing extension methods for FluentAssertions, optionally allowing the assembly to | ||
/// be initialized before the first assertion happens. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] | ||
public sealed class ExtensionAssemblyAttribute : Attribute | ||
{ | ||
private readonly string methodName; | ||
private readonly Type type; | ||
|
||
/// <summary> | ||
/// Marks the assembly as containing extension methods for FluentAssertions and which does not require any initialization. | ||
/// </summary> | ||
public ExtensionAssemblyAttribute() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Marks the assembly as containing extension methods for FluentAssertions and which requires initialization. | ||
/// </summary> | ||
/// <param name="type">The static type that contains the initialization method.</param> | ||
/// <param name="methodName">The static parameterless and void-returning method to invoke before the first initialization</param> | ||
#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
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
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
52 changes: 52 additions & 0 deletions
52
Tests/FluentAssertions.Extensibility.Specs/ExtensibilitySpecs.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,52 @@ | ||
using System; | ||
using Xunit.Sdk; | ||
|
||
namespace FluentAssertions.Extensibility.Specs; | ||
|
||
public class ExtensibilitySpecs | ||
{ | ||
[Fact] | ||
public void Methods_in_extension_libraries_do_not_need_to_be_annoted_as_custom_assertions() | ||
{ | ||
// Arrange | ||
var myClient = new MyCustomer | ||
{ | ||
Active = false | ||
}; | ||
|
||
// Act | ||
Action act = () => myClient.Should().BeActive("because we don't work with old clients"); | ||
|
||
// Assert | ||
act.Should().Throw<XunitException>().WithMessage( | ||
"Expected myClient to be true because we don't work with old clients, but found False."); | ||
} | ||
} | ||
|
||
public class MyCustomer | ||
{ | ||
public bool Active { get; set; } | ||
} | ||
|
||
public static class MyCustomerExtensions | ||
{ | ||
public static MyCustomerAssertions Should(this MyCustomer customer) | ||
{ | ||
return new MyCustomerAssertions(customer); | ||
} | ||
} | ||
|
||
public class MyCustomerAssertions | ||
{ | ||
private readonly MyCustomer customer; | ||
|
||
public MyCustomerAssertions(MyCustomer customer) | ||
{ | ||
this.customer = customer; | ||
} | ||
|
||
public void BeActive(string because = "", params object[] becauseArgs) | ||
{ | ||
customer.Active.Should().BeTrue(because, becauseArgs); | ||
} | ||
} |
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 = () => FileContainingAssemblyLevelAttributes.ShouldBeCalledOnlyOnce.Should().Be(1); | ||
|
||
act.Should().NotThrow(); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
Tests/FluentAssertions.Extensibility.Specs/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,25 @@ | ||
using System.Threading; | ||
using FluentAssertions.Extensibility; | ||
using FluentAssertions.Extensibility.Specs; | ||
|
||
// 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.Extensibility.Specs; | ||
|
||
public static class FileContainingAssemblyLevelAttributes | ||
{ | ||
private static int shouldBeCalledOnlyOnce; | ||
|
||
public static int ShouldBeCalledOnlyOnce => shouldBeCalledOnlyOnce; | ||
|
||
public static void InitializeBeforeFirstAssertion() | ||
{ | ||
Interlocked.Increment(ref shouldBeCalledOnlyOnce); | ||
} | ||
} |
Oops, something went wrong.