-
-
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.
MemberInfo.HasAttribute with a predicate was missing an implementation
- Loading branch information
1 parent
3c395ff
commit 840ecd3
Showing
3 changed files
with
87 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Reflectify.Specs; | ||
|
||
public class MemberInfoExtensionsSpecs | ||
{ | ||
[Fact] | ||
public void Can_determine_a_method_has_an_attribute() | ||
{ | ||
// Arrange | ||
var member = typeof(ClassWithAttributedMember).GetMethod("Method"); | ||
|
||
// Act / Assert | ||
member.HasAttribute<ObsoleteAttribute>().Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void Can_determine_a_method_has_an_attribute_using_a_specific_predicate() | ||
{ | ||
// Arrange | ||
var member = typeof(ClassWithAttributedMember).GetMethod("Method"); | ||
|
||
// Act / Assert | ||
member.HasAttribute<ObsoleteAttribute>(attribute => | ||
attribute.Message!.StartsWith("Specific")).Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void The_predicate_must_not_be_null() | ||
{ | ||
// Arrange | ||
var member = typeof(ClassWithAttributedMember).GetMethod("Method"); | ||
|
||
// Act | ||
var act = () => member.HasAttribute<ObsoleteAttribute>(null).Should().BeTrue(); | ||
|
||
// Assert | ||
act.Should().Throw<ArgumentNullException>().WithMessage("*predicate*"); | ||
} | ||
|
||
[Fact] | ||
public void Can_determine_a_method_has_an_attribute_that_does_not_meet_a_predicate() | ||
{ | ||
// Arrange | ||
var member = typeof(ClassWithAttributedMember).GetMethod("Method"); | ||
|
||
// Act / Assert | ||
member.HasAttribute<ObsoleteAttribute>(predicate => | ||
predicate.Message.Contains("*Other*")).Should().BeFalse(); | ||
} | ||
|
||
private class ClassWithAttributedMember | ||
{ | ||
[Obsolete("Specific reason")] | ||
public void Method() | ||
{ | ||
} | ||
} | ||
} |