Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistency in Attribute assertions #731

Closed
jnyrup opened this issue Jan 9, 2018 · 3 comments · Fixed by #1095
Closed

Consistency in Attribute assertions #731

jnyrup opened this issue Jan 9, 2018 · 3 comments · Fixed by #1095

Comments

@jnyrup
Copy link
Member

jnyrup commented Jan 9, 2018

If #727 gets merged, we should also revise other calls to
GetCustomAttributes(bool inherit) and
GetCustomAttributes(Type attributeType, bool inherit)

E.g TypeSelector,ThatAreDecoratedWith currently calls GetCustomAttributes(typeof(TAttribute), true) which would be inconsistent with the difference between BeDecoratedWith and BeDecoratedWithOrInherit.

@jnyrup
Copy link
Member Author

jnyrup commented Jan 21, 2018

@dennisdoomen in TypeExtensions.cs we are inconsistent whether we use HasAttribute or IsDecoratedWith and whether inherited attributes are considered.

The only other API methods that have Attribute in their name are the HaveAttribute methods from XElementAssertions and XmlElementAssertions.

The current API methods are:

bool HasAttribute<TAttribute>(this MemberInfo method);
bool HasMatchingAttribute<TAttribute>(this MemberInfo type, Expression<Func<TAttribute, bool>> isMatchingAttributePredicate);
bool HasMatchingAttribute<TAttribute>(this Type type, Expression<Func<TAttribute, bool>> isMatchingAttributePredicate, bool inherit = false);
bool IsDecoratedWith<TAttribute>(this MemberInfo type);
bool IsDecoratedWith<TAttribute>(this Type type, bool inherit = false);
HasMatchingAttribute<TAttribute>(this MemberInfo type, Expression<Func<TAttribute, bool>> isMatchingAttributePredicate)
--> `GetCustomAttributes<TAttribute>(MemberInfo type)`
--> type.GetCustomAttributes(false).OfType<TAttribute>()

but

public static bool HasAttribute<TAttribute>(this MemberInfo method)
--> method.GetCustomAttributes(typeof(TAttribute), true)

If it's not too late to change, I propose the following API:

IsDecoratedWith(this Type type)
IsDecoratedWith(this TypeInfo type)
IsDecoratedWith(this MemberInfo type)

IsDecoratedWithOrInherits(this Type type)
IsDecoratedWithOrInherits(this TypeInfo type)
IsDecoratedWithOrInherits(this MemberInfo type)

IsDecoratedWith(this Type type, Expression<Func<TAttribute, bool>> isMatchingAttributePredicate)
IsDecoratedWith(this TypeInfo type, Expression<Func<TAttribute, bool>> isMatchingAttributePredicate)
IsDecoratedWith(this MemberInfo type, Expression<Func<TAttribute, bool>> isMatchingAttributePredicate)

IsDecoratedWithOrInherits(this Type type, Expression<Func<TAttribute, bool>> isMatchingAttributePredicate)
IsDecoratedWithOrInherits(this TypeInfo type, Expression<Func<TAttribute, bool>> isMatchingAttributePredicate)
IsDecoratedWithOrInherits(this MemberInfo type, Expression<Func<TAttribute, bool>> isMatchingAttributePredicate)

This should make the API consistent and remove the optional booleans from the API.

@Evangelink
Copy link
Contributor

@dennisdoomen, @jnyrup, are you expecting some help (see help wanted label) to implement the suggested API or to discuss about the API?

@dennisdoomen
Copy link
Member

To implement it ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants