Skip to content

Commit

Permalink
added overload for fluent ImplementInterface() method to accept Type …
Browse files Browse the repository at this point in the history
…and Interface as input

Signed-off-by: Fritz Brandhuber <[email protected]>
  • Loading branch information
brandhuf committed Jan 17, 2020
1 parent d89a913 commit 2efaab8
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 5 deletions.
4 changes: 2 additions & 2 deletions ArchUnitNET/Core/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Type(string fullname, string name, Assembly assembly, Namespace namespc,
.OfType<ImplementsInterfaceDependency>()
.Select(dependency => dependency.Target);

public bool ImplementsInterface(IType intf)
public bool ImplementsInterface(Interface intf)
{
return ImplementedInterfaces.Any(implementedInterface =>
Equals(implementedInterface, intf) || Equals(implementedInterface.GenericType, intf));
Expand Down Expand Up @@ -84,7 +84,7 @@ public bool IsAssignableTo(IType assignableToType)
return true;
}

return assignableToType is Interface && ImplementsInterface(assignableToType);
return assignableToType is Interface && ImplementsInterface((Interface) assignableToType);
}

public bool IsAssignableTo(string pattern, bool useRegularExpressions = false)
Expand Down
2 changes: 1 addition & 1 deletion ArchUnitNET/Domain/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public bool? IsStruct
public IType GenericType => Type.GenericType;
public List<IType> GenericTypeArguments => Type.GenericTypeArguments;

public bool ImplementsInterface(IType intf)
public bool ImplementsInterface(Interface intf)
{
return Type.ImplementsInterface(intf);
}
Expand Down
2 changes: 1 addition & 1 deletion ArchUnitNET/Domain/IType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IType : ICanBeAnalyzed
[CanBeNull] IType GenericType { get; }
bool IsNested { get; }
IEnumerable<IType> ImplementedInterfaces { get; }
bool ImplementsInterface(IType intf);
bool ImplementsInterface(Interface intf);
bool ImplementsInterface(string pattern, bool useRegularExpressions = false);
bool IsAssignableTo(IType assignableToType);
bool IsAssignableTo(string pattern, bool useRegularExpressions = false);
Expand Down
2 changes: 1 addition & 1 deletion ArchUnitNET/Domain/Interface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Interface(IType type)
public IType GenericType => Type.GenericType;
public List<IType> GenericTypeArguments => Type.GenericTypeArguments;

public bool ImplementsInterface(IType intf)
public bool ImplementsInterface(Interface intf)
{
return Type.ImplementsInterface(intf);
}
Expand Down
28 changes: 28 additions & 0 deletions ArchUnitNET/Fluent/Syntax/Elements/Types/GivenTypesThat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ public TGivenRuleTypeConjunction ImplementInterface(string pattern, bool useRegu
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction ImplementInterface(Interface intf)
{
_ruleCreator.AddPredicate(
TypePredicatesDefinition<TRuleType>.ImplementInterface(intf));
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction ImplementInterface(Type intf)
{
_ruleCreator.AddPredicate(
TypePredicatesDefinition<TRuleType>.ImplementInterface(intf));
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction ResideInNamespace(string pattern, bool useRegularExpressions = false)
{
_ruleCreator.AddPredicate(
Expand Down Expand Up @@ -205,6 +219,20 @@ public TGivenRuleTypeConjunction DoNotImplementInterface(string pattern, bool us
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction DoNotImplementInterface(Interface intf)
{
_ruleCreator.AddPredicate(
TypePredicatesDefinition<TRuleType>.DoNotImplementInterface(intf));
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction DoNotImplementInterface(Type intf)
{
_ruleCreator.AddPredicate(
TypePredicatesDefinition<TRuleType>.DoNotImplementInterface(intf));
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction DoNotResideInNamespace(string pattern, bool useRegularExpressions = false)
{
_ruleCreator.AddPredicate(
Expand Down
4 changes: 4 additions & 0 deletions ArchUnitNET/Fluent/Syntax/Elements/Types/ITypeConditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public interface ITypeConditions<out TReturnType> : IObjectConditions<TReturnTyp
TReturnType BeAssignableTo(IEnumerable<IType> types);
TReturnType BeAssignableTo(IEnumerable<Type> types);
TReturnType ImplementInterface(string pattern, bool useRegularExpressions = false);
TReturnType ImplementInterface(Interface intf);
TReturnType ImplementInterface(Type intf);
TReturnType ResideInNamespace(string pattern, bool useRegularExpressions = false);
TReturnType ResideInAssembly(string pattern, bool useRegularExpressions = false);
TReturnType ResideInAssembly(Assembly assembly, params Assembly[] moreAssemblies);
Expand All @@ -46,6 +48,8 @@ public interface ITypeConditions<out TReturnType> : IObjectConditions<TReturnTyp
TReturnType NotBeAssignableTo(IEnumerable<IType> types);
TReturnType NotBeAssignableTo(IEnumerable<Type> types);
TReturnType NotImplementInterface(string pattern, bool useRegularExpressions = false);
TReturnType NotImplementInterface(Interface intf);
TReturnType NotImplementInterface(Type intf);
TReturnType NotResideInNamespace(string pattern, bool useRegularExpressions = false);
TReturnType NotResideInAssembly(string pattern, bool useRegularExpressions = false);
TReturnType NotResideInAssembly(Assembly assembly, params Assembly[] moreAssemblies);
Expand Down
4 changes: 4 additions & 0 deletions ArchUnitNET/Fluent/Syntax/Elements/Types/ITypePredicates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public interface ITypePredicates<out TReturnType> : IObjectPredicates<TReturnTyp
TReturnType AreAssignableTo(IEnumerable<IType> types);
TReturnType AreAssignableTo(IEnumerable<Type> types);
TReturnType ImplementInterface(string pattern, bool useRegularExpressions = false);
TReturnType ImplementInterface(Interface intf);
TReturnType ImplementInterface(Type intf);
TReturnType ResideInNamespace(string pattern, bool useRegularExpressions = false);
TReturnType ResideInAssembly(string pattern, bool useRegularExpressions = false);
TReturnType ResideInAssembly(Assembly assembly, params Assembly[] moreAssemblies);
Expand All @@ -46,6 +48,8 @@ public interface ITypePredicates<out TReturnType> : IObjectPredicates<TReturnTyp
TReturnType AreNotAssignableTo(IEnumerable<IType> types);
TReturnType AreNotAssignableTo(IEnumerable<Type> types);
TReturnType DoNotImplementInterface(string pattern, bool useRegularExpressions = false);
TReturnType DoNotImplementInterface(Interface intf);
TReturnType DoNotImplementInterface(Type intf);
TReturnType DoNotResideInNamespace(string pattern, bool useRegularExpressions = false);
TReturnType DoNotResideInAssembly(string pattern, bool useRegularExpressions = false);
TReturnType DoNotResideInAssembly(Assembly assembly, params Assembly[] moreAssemblies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ public TRuleTypeShouldConjunction ImplementInterface(string pattern, bool useReg
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction ImplementInterface(Interface intf)
{
_ruleCreator.ContinueComplexCondition(
TypePredicatesDefinition<TReferenceType>.ImplementInterface(intf));
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction ImplementInterface(Type intf)
{
_ruleCreator.ContinueComplexCondition(
TypePredicatesDefinition<TReferenceType>.ImplementInterface(intf));
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}


public TRuleTypeShouldConjunction ResideInNamespace(string pattern, bool useRegularExpressions = false)
{
Expand Down Expand Up @@ -215,6 +229,20 @@ public TRuleTypeShouldConjunction DoNotImplementInterface(string pattern, bool u
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction DoNotImplementInterface(Interface intf)
{
_ruleCreator.ContinueComplexCondition(
TypePredicatesDefinition<TReferenceType>.DoNotImplementInterface(intf));
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction DoNotImplementInterface(Type intf)
{
_ruleCreator.ContinueComplexCondition(
TypePredicatesDefinition<TReferenceType>.DoNotImplementInterface(intf));
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction DoNotResideInNamespace(string pattern, bool useRegularExpressions = false)
{
_ruleCreator.ContinueComplexCondition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,37 @@ public static ICondition<TRuleType> ImplementInterface(string pattern, bool useR
" \"" + pattern + "\"");
}

public static ICondition<TRuleType> ImplementInterface(Interface intf)
{
return new SimpleCondition<TRuleType>(
type => type.ImplementsInterface(intf),
"implement interface \"" + intf.FullName + "\"",
"does not implement interface \"" + intf.FullName + "\"");
}

public static ICondition<TRuleType> ImplementInterface(Type intf)
{
IEnumerable<ConditionResult> Condition(IEnumerable<TRuleType> ruleTypes, Architecture architecture)
{
var ruleTypeList = ruleTypes.ToList();
var interf = architecture.GetInterfaceOfType(intf);
var passedObjects = ruleTypeList.Where(type => type.ImplementsInterface(interf)).ToList();

foreach (var failedObject in ruleTypeList.Except(passedObjects))
{
yield return new ConditionResult(failedObject, false,
"does not implement interface \"" + intf.FullName + "\"");
}

foreach (var passedObject in passedObjects)
{
yield return new ConditionResult(passedObject, true);
}
}

return new ArchitectureCondition<TRuleType>(Condition, "implement interface \"" + intf.FullName + "\"");
}

public static ICondition<TRuleType> ResideInNamespace(string pattern, bool useRegularExpressions = false)
{
return new SimpleCondition<TRuleType>(
Expand Down Expand Up @@ -590,6 +621,37 @@ public static ICondition<TRuleType> NotImplementInterface(string pattern, bool u
" \"" + pattern + "\"");
}

public static ICondition<TRuleType> NotImplementInterface(Interface intf)
{
return new SimpleCondition<TRuleType>(
type => !type.ImplementsInterface(intf),
"not implement interface \"" + intf.FullName + "\"",
"does implement interface \"" + intf.FullName + "\"");
}

public static ICondition<TRuleType> NotImplementInterface(Type intf)
{
IEnumerable<ConditionResult> Condition(IEnumerable<TRuleType> ruleTypes, Architecture architecture)
{
var ruleTypeList = ruleTypes.ToList();
var interf = architecture.GetInterfaceOfType(intf);
var passedObjects = ruleTypeList.Where(type => !type.ImplementsInterface(interf)).ToList();

foreach (var failedObject in ruleTypeList.Except(passedObjects))
{
yield return new ConditionResult(failedObject, false,
"does implement interface \"" + intf.FullName + "\"");
}

foreach (var passedObject in passedObjects)
{
yield return new ConditionResult(passedObject, true);
}
}

return new ArchitectureCondition<TRuleType>(Condition, "not implement interface \"" + intf.FullName + "\"");
}

public static ICondition<TRuleType> NotResideInNamespace(string pattern, bool useRegularExpressions = false)
{
return new SimpleCondition<TRuleType>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ public static IPredicate<T> ImplementInterface(string pattern, bool useRegularEx
pattern + "\"");
}

public static IPredicate<T> ImplementInterface(Interface intf)
{
return new SimplePredicate<T>(type => type.ImplementsInterface(intf),
"implement interface \"" + intf.FullName + "\"");
}

public static IPredicate<T> ImplementInterface(Type intf)
{
IEnumerable<T> Condition(IEnumerable<T> ruleTypes, Architecture architecture)
{
var interf = architecture.GetInterfaceOfType(intf);
return ruleTypes.Where(type => type.ImplementsInterface(interf));
}

return new ArchitecturePredicate<T>(Condition,
"implement interface \"" + intf.FullName + "\"");
}

public static IPredicate<T> ResideInNamespace(string pattern, bool useRegularExpressions = false)
{
return new SimplePredicate<T>(type => type.ResidesInNamespace(pattern, useRegularExpressions),
Expand Down Expand Up @@ -414,6 +432,24 @@ public static IPredicate<T> DoNotImplementInterface(string pattern, bool useRegu
" \"" + pattern + "\"");
}

public static IPredicate<T> DoNotImplementInterface(Interface intf)
{
return new SimplePredicate<T>(type => !type.ImplementsInterface(intf),
"do not implement interface \"" + intf.FullName + "\"");
}

public static IPredicate<T> DoNotImplementInterface(Type intf)
{
IEnumerable<T> Condition(IEnumerable<T> ruleTypes, Architecture architecture)
{
var interf = architecture.GetInterfaceOfType(intf);
return ruleTypes.Where(type => !type.ImplementsInterface(interf));
}

return new ArchitecturePredicate<T>(Condition,
"do not implement interface \"" + intf.FullName + "\"");
}

public static IPredicate<T> DoNotResideInNamespace(string pattern, bool useRegularExpressions = false)
{
return new SimplePredicate<T>(type => !type.ResidesInNamespace(pattern, useRegularExpressions),
Expand Down
28 changes: 28 additions & 0 deletions ArchUnitNET/Fluent/Syntax/Elements/Types/TypesShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ public TRuleTypeShouldConjunction ImplementInterface(string pattern, bool useReg
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction ImplementInterface(Interface intf)
{
_ruleCreator.AddCondition(
TypeConditionsDefinition<TRuleType>.ImplementInterface(intf));
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction ImplementInterface(Type intf)
{
_ruleCreator.AddCondition(
TypeConditionsDefinition<TRuleType>.ImplementInterface(intf));
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction ResideInNamespace(string pattern, bool useRegularExpressions = false)
{
_ruleCreator.AddCondition(
Expand Down Expand Up @@ -217,6 +231,20 @@ public TRuleTypeShouldConjunction NotImplementInterface(string pattern, bool use
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction NotImplementInterface(Interface intf)
{
_ruleCreator.AddCondition(
TypeConditionsDefinition<TRuleType>.NotImplementInterface(intf));
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction NotImplementInterface(Type intf)
{
_ruleCreator.AddCondition(
TypeConditionsDefinition<TRuleType>.NotImplementInterface(intf));
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction NotResideInNamespace(string pattern, bool useRegularExpressions = false)
{
_ruleCreator.AddCondition(
Expand Down

0 comments on commit 2efaab8

Please sign in to comment.