Skip to content

Improved the Fluent API by reworking methods that take string parameters to match against the name of types or members #353

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

31 changes: 15 additions & 16 deletions ArchUnitNET/Domain/Extensions/AttributeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,26 @@ namespace ArchUnitNET.Domain.Extensions
{
public static class AttributeExtensions
{
public static bool HasAttribute(
this IHasAttributes a,
string pattern,
bool useRegularExpressions = false
)
public static bool HasAttribute(this IHasAttributes a, string fullName)
{
return a.Attributes.Any(attribute =>
attribute.FullNameMatches(pattern, useRegularExpressions)
);
return a.Attributes.Any(attribute => attribute.FullNameEquals(fullName));
}

public static bool OnlyHasAttributes(
this IHasAttributes a,
string pattern,
bool useRegularExpressions = false
)
public static bool HasAttributeMatching(this IHasAttributes a, string pattern)
{
return a.Attributes.Any(attribute => attribute.FullNameMatches(pattern));
}

public static bool OnlyHasAttributes(this IHasAttributes a, string name)
{
return a.Attributes.IsNullOrEmpty()
|| a.Attributes.All(attribute => attribute.FullNameEquals(name));
}

public static bool OnlyHasAttributesMatching(this IHasAttributes a, string pattern)
{
return a.Attributes.IsNullOrEmpty()
|| a.Attributes.All(attribute =>
attribute.FullNameMatches(pattern, useRegularExpressions)
);
|| a.Attributes.All(attribute => attribute.FullNameMatches(pattern));
}
}
}
42 changes: 21 additions & 21 deletions ArchUnitNET/Domain/Extensions/DependencyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ namespace ArchUnitNET.Domain.Extensions
{
public static class DependencyExtensions
{
public static bool CallsMethod(
this IHasDependencies type,
string pattern,
bool useRegularExpressions = false
)
public static bool CallsMethod(this IHasDependencies type, string fullName)
{
return type.GetCalledMethods().Any(member => member.FullNameEquals(fullName));
}

public static bool CallsMethodMatching(this IHasDependencies type, string pattern)
{
return type.GetCalledMethods()
.Any(member => member.FullNameMatches(pattern, useRegularExpressions));
return type.GetCalledMethods().Any(member => member.FullNameMatches(pattern));
}

public static IEnumerable<MethodMember> GetCalledMethods(this IHasDependencies type)
Expand All @@ -37,24 +37,24 @@ public static IEnumerable<FieldMember> GetAccessedFieldMembers(this IHasDependen
.Select(dependency => (FieldMember)dependency.TargetMember);
}

public static bool DependsOn(
this IHasDependencies c,
string pattern,
bool useRegularExpressions = false
)
public static bool DependsOnType(this IHasDependencies c, string fullName)
{
return c.GetTypeDependencies()
.Any(d => d.FullNameMatches(pattern, useRegularExpressions));
return c.GetTypeDependencies().Any(d => d.FullNameEquals(fullName));
}

public static bool OnlyDependsOn(
this IHasDependencies c,
string pattern,
bool useRegularExpressions = false
)
public static bool DependsOnTypeMatching(this IHasDependencies c, string pattern)
{
return c.GetTypeDependencies().Any(d => d.FullNameMatches(pattern));
}

public static bool OnlyDependsOnType(this IHasDependencies c, string fullName)
{
return c.GetTypeDependencies().All(d => d.FullNameEquals(fullName));
}

public static bool OnlyDependsOnTypesMatching(this IHasDependencies c, string pattern)
{
return c.GetTypeDependencies()
.All(d => d.FullNameMatches(pattern, useRegularExpressions));
return c.GetTypeDependencies().All(d => d.FullNameMatches(pattern));
}

public static IEnumerable<IType> GetTypeDependencies(this IHasDependencies c)
Expand Down
49 changes: 29 additions & 20 deletions ArchUnitNET/Domain/Extensions/MemberExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ namespace ArchUnitNET.Domain.Extensions
{
public static class MemberExtensions
{
public static bool IsDeclaredIn(
this IMember member,
string pattern,
bool useRegularExpressions = false
)
public static bool IsDeclaredIn(this IMember member, string fullName)
{
return member.DeclaringType.FullNameEquals(fullName);
}

public static bool IsDeclaredInTypeMatching(this IMember member, string pattern)
{
return member.DeclaringType.FullNameMatches(pattern, useRegularExpressions);
return member.DeclaringType.FullNameMatches(pattern);
}

public static bool IsDeclaredIn(this IMember member, IType type)
Expand Down Expand Up @@ -62,17 +63,18 @@ public static bool HasMethodCallDependencies(
return member.GetMethodCallDependencies(getBackwardsDependencies).Any();
}

public static bool IsCalledBy(
this MethodMember member,
string pattern,
bool useRegularExpressions = false
)
public static bool IsCalledByType(this MethodMember member, string fullName)
{
return member
.GetMethodCallDependencies(true)
.Any(dependency =>
dependency.Origin.FullNameMatches(pattern, useRegularExpressions)
);
.Any(dependency => dependency.Origin.FullNameEquals(fullName));
}

public static bool IsCalledByTypeMatching(this MethodMember member, string pattern)
{
return member
.GetMethodCallDependencies(true)
.Any(dependency => dependency.Origin.FullNameMatches(pattern));
}

public static IEnumerable<IType> GetCallingTypes(this MethodMember member)
Expand All @@ -83,17 +85,24 @@ public static IEnumerable<IType> GetCallingTypes(this MethodMember member)
.Distinct();
}

public static bool HasDependencyInMethodBodyTo(
public static bool HasDependencyInMethodBodyToType(
this MethodMember member,
string fullName
)
{
return member
.GetBodyTypeMemberDependencies()
.Any(dependency => dependency.Target.FullNameEquals(fullName));
}

public static bool HasDependencyInMethodBodyToTypeMatching(
this MethodMember member,
string pattern,
bool useRegularExpressions = false
string pattern
)
{
return member
.GetBodyTypeMemberDependencies()
.Any(dependency =>
dependency.Target.FullNameMatches(pattern, useRegularExpressions)
);
.Any(dependency => dependency.Target.FullNameMatches(pattern));
}

public static bool HasFieldTypeDependencies(
Expand Down
32 changes: 12 additions & 20 deletions ArchUnitNET/Domain/Extensions/NamingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,24 @@ public static bool NameContains(
return cls.Name.IndexOf(pattern, stringComparison) >= 0;
}

public static bool NameMatches(
this IHasName cls,
string pattern,
bool useRegularExpressions = false
)
public static bool NameEquals(this IHasName cls, string name)
{
if (useRegularExpressions)
{
return pattern != null && Regex.IsMatch(cls.Name, pattern);
}
return string.Equals(cls.Name, name, StringComparison.OrdinalIgnoreCase);
}

return string.Equals(cls.Name, pattern, StringComparison.OrdinalIgnoreCase);
public static bool NameMatches(this IHasName cls, string pattern)
{
return pattern != null && Regex.IsMatch(cls.Name, pattern);
}

public static bool FullNameMatches(
this IHasName cls,
string pattern,
bool useRegularExpressions = false
)
public static bool FullNameEquals(this IHasName cls, string fullName)
{
if (useRegularExpressions)
{
return pattern != null && Regex.IsMatch(cls.FullName, pattern);
}
return string.Equals(cls.FullName, fullName, StringComparison.OrdinalIgnoreCase);
}

return string.Equals(cls.FullName, pattern, StringComparison.OrdinalIgnoreCase);
public static bool FullNameMatches(this IHasName cls, string pattern)
{
return pattern != null && Regex.IsMatch(cls.FullName, pattern);
}

public static bool FullNameContains(this IHasName cls, string pattern)
Expand Down
85 changes: 49 additions & 36 deletions ArchUnitNET/Domain/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,27 @@ public static bool ImplementsInterface(this IType type, Interface intf)
);
}

public static bool ImplementsInterface(
this IType type,
string pattern,
bool useRegularExpressions = false
)
public static bool ImplementsInterface(this IType type, string fullName)
{
if (type is GenericParameter)
{
return false;
}

return type.ImplementedInterfaces.Any(implementedInterface =>
implementedInterface.FullNameMatches(pattern, useRegularExpressions)
implementedInterface.FullNameEquals(fullName)
);
}

public static bool ImplementsInterfaceMatching(this IType type, string pattern)
{
if (type is GenericParameter)
{
return false;
}

return type.ImplementedInterfaces.Any(implementedInterface =>
implementedInterface.FullNameMatches(pattern)
);
}

Expand All @@ -53,21 +61,24 @@ public static bool IsAssignableTo(this IType type, IType assignableToType)
return type.GetAssignableTypes().Contains(assignableToType);
}

public static bool IsAssignableTo(
this IType type,
string pattern,
bool useRegularExpressions = false
)
public static bool IsAssignableTo(this IType type, string fullName)
{
if (type is GenericParameter genericParameter)
{
return genericParameter.TypeConstraints.All(t =>
t.IsAssignableTo(pattern, useRegularExpressions)
);
return genericParameter.TypeConstraints.All(t => t.IsAssignableTo(fullName));
}

return type.GetAssignableTypes().Any(t => t.FullNameEquals(fullName));
}

public static bool IsAssignableToTypeMatching(this IType type, string pattern)
{
if (type is GenericParameter genericParameter)
{
return genericParameter.TypeConstraints.All(t => t.IsAssignableTo(pattern));
}

return type.GetAssignableTypes()
.Any(t => t.FullNameMatches(pattern, useRegularExpressions));
return type.GetAssignableTypes().Any(t => t.FullNameMatches(pattern));
}

public static bool IsNestedIn(this IType type, IType assignableToType)
Expand Down Expand Up @@ -234,34 +245,36 @@ public static Attribute GetAttributeOfType(this IType type, Class attributeClass
);
}

public static bool ResidesInNamespace(
this IType e,
string pattern,
bool useRegularExpressions = false
)
public static bool ResidesInNamespace(this IType e, string fullName)
{
return e.Namespace.FullNameMatches(pattern, useRegularExpressions);
return e.Namespace.FullNameEquals(fullName);
}

public static bool ResidesInAssembly(
this IType e,
string pattern,
bool useRegularExpressions = false
)
public static bool ResidesInNamespaceMatching(this IType e, string pattern)
{
return e.Assembly.FullNameMatches(pattern, useRegularExpressions);
return e.Namespace.FullNameMatches(pattern);
}

public static bool IsDeclaredAsFieldIn(
this IType type,
string pattern,
bool useRegularExpressions = false
)
public static bool ResidesInAssembly(this IType e, string fullName)
{
return e.Assembly.FullNameEquals(fullName);
}

public static bool ResidesInAssemblyMatching(this IType e, string pattern)
{
return e.Assembly.FullNameMatches(pattern);
}

public static bool IsDeclaredAsFieldIn(this IType type, string fullName)
{
return type.GetFieldTypeDependencies(true)
.Any(dependency =>
dependency.Target.FullNameMatches(pattern, useRegularExpressions)
);
.Any(dependency => dependency.Target.FullNameEquals(fullName));
}

public static bool IsDeclaredAsFieldInTypeMatching(this IType type, string pattern)
{
return type.GetFieldTypeDependencies(true)
.Any(dependency => dependency.Target.FullNameMatches(pattern));
}

public static bool HasDependency(this IType type, ITypeDependency dependency)
Expand Down
26 changes: 0 additions & 26 deletions ArchUnitNET/Domain/PlantUml/Export/DependencyFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ public static Func<ITypeDependency, bool> FocusOn(IEnumerable<IType> types)
};
}

public static Func<ITypeDependency, bool> FocusOn(
string pattern,
bool useRegularExpressions = false
)
{
return dependency =>
dependency.Target.FullNameMatches(pattern, useRegularExpressions)
^ dependency.Origin.FullNameMatches(pattern, useRegularExpressions);
}

public static Func<ITypeDependency, bool> HasOrigin(IType type)
{
return dependency => dependency.Origin.Equals(type);
Expand All @@ -77,14 +67,6 @@ public static Func<ITypeDependency, bool> HasOrigin(IEnumerable<IType> types)
return dependency => types.Contains(dependency.Origin);
}

public static Func<ITypeDependency, bool> HasOrigin(
string pattern,
bool useRegularExpressions = false
)
{
return dependency => dependency.Origin.FullNameMatches(pattern, useRegularExpressions);
}

public static Func<ITypeDependency, bool> HasTarget(IType type)
{
return dependency => dependency.Target.Equals(type);
Expand All @@ -94,13 +76,5 @@ public static Func<ITypeDependency, bool> HasTarget(IEnumerable<IType> types)
{
return dependency => types.Contains(dependency.Target);
}

public static Func<ITypeDependency, bool> HasTarget(
string pattern,
bool useRegularExpressions = false
)
{
return dependency => dependency.Target.FullNameMatches(pattern, useRegularExpressions);
}
}
}
Loading