Skip to content

Commit

Permalink
Merge pull request #55 from brandhuf/feature-custom-rules
Browse files Browse the repository at this point in the history
Feature custom conditions and predicates
  • Loading branch information
fgather authored Feb 26, 2021
2 parents 68cf21f + 4b9d23d commit 57ac8c0
Show file tree
Hide file tree
Showing 49 changed files with 222 additions and 45 deletions.
16 changes: 15 additions & 1 deletion ArchUnitNET/Fluent/Syntax/Elements/GivenObjectsThat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
using System;
using System.Collections.Generic;
using ArchUnitNET.Domain;
using ArchUnitNET.Fluent.Predicates;
using static ArchUnitNET.Fluent.Syntax.ConjunctionFactory;
using Attribute = ArchUnitNET.Domain.Attribute;

namespace ArchUnitNET.Fluent.Syntax.Elements
{
public abstract class GivenObjectsThat<TGivenRuleTypeConjunction, TRuleType> : SyntaxElement<TRuleType>,
IObjectPredicates<TGivenRuleTypeConjunction> where TRuleType : ICanBeAnalyzed
IObjectPredicates<TGivenRuleTypeConjunction, TRuleType> where TRuleType : ICanBeAnalyzed
{
protected GivenObjectsThat(IArchRuleCreator<TRuleType> ruleCreator) : base(ruleCreator)
{
Expand Down Expand Up @@ -127,6 +128,19 @@ public TGivenRuleTypeConjunction DependOnAny(IEnumerable<Type> types)
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction FollowCustomPredicate(IPredicate<TRuleType> predicate)
{
_ruleCreator.AddPredicate(predicate);
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction FollowCustomPredicate(Func<TRuleType, bool> predicate, string description)
{
_ruleCreator.AddPredicate(
ObjectPredicatesDefinition<TRuleType>.FollowCustomPredicate(predicate, description));
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction OnlyDependOn(string pattern, bool useRegularExpressions = false)
{
_ruleCreator.AddPredicate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
namespace ArchUnitNET.Fluent.Syntax.Elements
{
public interface
IComplexObjectConditions<TRuleTypeShouldConjunction, TRuleType> : IObjectConditions<TRuleTypeShouldConjunction>
IComplexObjectConditions<TRuleTypeShouldConjunction, TRuleType> : IObjectConditions<TRuleTypeShouldConjunction,
TRuleType>
where TRuleType : ICanBeAnalyzed
where TRuleTypeShouldConjunction : SyntaxElement<TRuleType>
{
Expand Down
6 changes: 5 additions & 1 deletion ArchUnitNET/Fluent/Syntax/Elements/IObjectConditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
using System;
using System.Collections.Generic;
using ArchUnitNET.Domain;
using ArchUnitNET.Fluent.Conditions;
using Attribute = ArchUnitNET.Domain.Attribute;

namespace ArchUnitNET.Fluent.Syntax.Elements
{
public interface IObjectConditions<out TReturnType>
public interface IObjectConditions<out TReturnType, out TRuleType> where TRuleType : ICanBeAnalyzed
{
TReturnType Exist();
TReturnType Be(string pattern, bool useRegularExpressions = false);
Expand All @@ -31,6 +32,9 @@ public interface IObjectConditions<out TReturnType>
TReturnType DependOnAny(IObjectProvider<IType> types);
TReturnType DependOnAny(IEnumerable<IType> types);
TReturnType DependOnAny(IEnumerable<Type> types);
TReturnType FollowCustomCondition(ICondition<TRuleType> condition);
TReturnType FollowCustomCondition(Func<TRuleType, ConditionResult> condition, string description);
TReturnType FollowCustomCondition(Func<TRuleType, bool> condition, string description, string failDescription);
TReturnType OnlyDependOn(string pattern, bool useRegularExpressions = false);
TReturnType OnlyDependOn(IEnumerable<string> patterns, bool useRegularExpressions = false);
TReturnType OnlyDependOn(IType firstType, params IType[] moreTypes);
Expand Down
5 changes: 4 additions & 1 deletion ArchUnitNET/Fluent/Syntax/Elements/IObjectPredicates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
using System;
using System.Collections.Generic;
using ArchUnitNET.Domain;
using ArchUnitNET.Fluent.Predicates;
using Attribute = ArchUnitNET.Domain.Attribute;

namespace ArchUnitNET.Fluent.Syntax.Elements
{
public interface IObjectPredicates<out TReturnType>
public interface IObjectPredicates<out TReturnType, TRuleType> where TRuleType : ICanBeAnalyzed
{
TReturnType Are(string pattern, bool useRegularExpressions = false);
TReturnType Are(IEnumerable<string> patterns, bool useRegularExpressions = false);
Expand All @@ -30,6 +31,8 @@ public interface IObjectPredicates<out TReturnType>
TReturnType DependOnAny(IObjectProvider<IType> types);
TReturnType DependOnAny(IEnumerable<IType> types);
TReturnType DependOnAny(IEnumerable<Type> types);
TReturnType FollowCustomPredicate(IPredicate<TRuleType> predicate);
TReturnType FollowCustomPredicate(Func<TRuleType, bool> predicate, string description);
TReturnType OnlyDependOn(string pattern, bool useRegularExpressions = false);
TReturnType OnlyDependOn(IEnumerable<string> patterns, bool useRegularExpressions = false);
TReturnType OnlyDependOn(Type firstType, params Type[] moreTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace ArchUnitNET.Fluent.Syntax.Elements.Members.FieldMembers
{
public class GivenFieldMembersThat : GivenMembersThat<GivenFieldMembersConjunction, FieldMember>,
IFieldMemberPredicates<GivenFieldMembersConjunction>
IFieldMemberPredicates<GivenFieldMembersConjunction, FieldMember>
{
public GivenFieldMembersThat(IArchRuleCreator<FieldMember> ruleCreator) : base(ruleCreator)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ArchUnitNET.Fluent.Syntax.Elements.Members.FieldMembers
{
public interface IComplexFieldMemberConditions :
IComplexMemberConditions<FieldMembersShouldConjunction, FieldMember>,
IFieldMemberConditions<FieldMembersShouldConjunction>
IFieldMemberConditions<FieldMembersShouldConjunction, FieldMember>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
//
// SPDX-License-Identifier: Apache-2.0

using ArchUnitNET.Domain;

namespace ArchUnitNET.Fluent.Syntax.Elements.Members.FieldMembers
{
public interface IFieldMemberConditions<out TReturnType> : IMemberConditions<TReturnType>
public interface IFieldMemberConditions<out TReturnType, out TRuleType> : IMemberConditions<TReturnType, TRuleType>
where TRuleType : ICanBeAnalyzed
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
//
// SPDX-License-Identifier: Apache-2.0

using ArchUnitNET.Domain;

namespace ArchUnitNET.Fluent.Syntax.Elements.Members.FieldMembers
{
public interface IFieldMemberPredicates<out TRuleTypeConjunction> : IMemberPredicates<TRuleTypeConjunction>
public interface
IFieldMemberPredicates<out TRuleTypeConjunction, TRuleType> : IMemberPredicates<TRuleTypeConjunction, TRuleType>
where TRuleType : ICanBeAnalyzed
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ArchUnitNET.Fluent.Syntax.Elements.Members.FieldMembers
{
public class ShouldRelateToFieldMembersThat<TRuleTypeShouldConjunction, TRuleType> :
ShouldRelateToMembersThat<TRuleTypeShouldConjunction, FieldMember, TRuleType>,
IFieldMemberPredicates<TRuleTypeShouldConjunction>
IFieldMemberPredicates<TRuleTypeShouldConjunction, FieldMember>
where TRuleTypeShouldConjunction : SyntaxElement<TRuleType>
where TRuleType : ICanBeAnalyzed
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ArchUnitNET.Fluent.Syntax.Elements.Members
{
public class GivenMembersThat<TGivenRuleTypeConjunction, TRuleType> :
GivenObjectsThat<TGivenRuleTypeConjunction, TRuleType>,
IMemberPredicates<TGivenRuleTypeConjunction>
IMemberPredicates<TGivenRuleTypeConjunction, TRuleType>
where TRuleType : IMember
{
// ReSharper disable once MemberCanBeProtected.Global
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace ArchUnitNET.Fluent.Syntax.Elements.Members
public interface
IComplexMemberConditions<TRuleTypeShouldConjunction, TRuleType> :
IComplexObjectConditions<TRuleTypeShouldConjunction, TRuleType>,
IMemberConditions<TRuleTypeShouldConjunction>
IMemberConditions<TRuleTypeShouldConjunction, TRuleType>
where TRuleType : IMember
where TRuleTypeShouldConjunction : SyntaxElement<TRuleType>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

namespace ArchUnitNET.Fluent.Syntax.Elements.Members
{
public interface IMemberConditions<out TReturnType> : IObjectConditions<TReturnType>
public interface IMemberConditions<out TReturnType, out TRuleType> : IObjectConditions<TReturnType, TRuleType>
where TRuleType : ICanBeAnalyzed
{
TReturnType BeDeclaredIn(string pattern, bool useRegularExpressions = false);
TReturnType BeDeclaredIn(IEnumerable<string> patterns, bool useRegularExpressions = false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

namespace ArchUnitNET.Fluent.Syntax.Elements.Members
{
public interface IMemberPredicates<out TRuleTypeConjunction> : IObjectPredicates<TRuleTypeConjunction>
public interface
IMemberPredicates<out TRuleTypeConjunction, TRuleType> : IObjectPredicates<TRuleTypeConjunction, TRuleType>
where TRuleType : ICanBeAnalyzed
{
TRuleTypeConjunction AreDeclaredIn(string pattern, bool useRegularExpressions = false);
TRuleTypeConjunction AreDeclaredIn(IEnumerable<string> patterns, bool useRegularExpressions = false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace ArchUnitNET.Fluent.Syntax.Elements.Members.MethodMembers
{
public class GivenMethodMembersThat : GivenMembersThat<GivenMethodMembersConjunction, MethodMember>,
IMethodMemberPredicates<GivenMethodMembersConjunction>
IMethodMemberPredicates<GivenMethodMembersConjunction, MethodMember>
{
public GivenMethodMembersThat(IArchRuleCreator<MethodMember> ruleCreator) : base(ruleCreator)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ArchUnitNET.Fluent.Syntax.Elements.Members.MethodMembers
{
public interface IComplexMethodMemberConditions :
IComplexMemberConditions<MethodMembersShouldConjunction, MethodMember>,
IMethodMemberConditions<MethodMembersShouldConjunction>
IMethodMemberConditions<MethodMembersShouldConjunction, MethodMember>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

namespace ArchUnitNET.Fluent.Syntax.Elements.Members.MethodMembers
{
public interface IMethodMemberConditions<out TReturnType> : IMemberConditions<TReturnType>
public interface IMethodMemberConditions<out TReturnType, out TRuleType> : IMemberConditions<TReturnType, TRuleType>
where TRuleType : ICanBeAnalyzed
{
TReturnType BeConstructor();
TReturnType BeVirtual();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

namespace ArchUnitNET.Fluent.Syntax.Elements.Members.MethodMembers
{
public interface IMethodMemberPredicates<out TRuleTypeConjunction> : IMemberPredicates<TRuleTypeConjunction>
public interface
IMethodMemberPredicates<out TRuleTypeConjunction, TRuleType> : IMemberPredicates<TRuleTypeConjunction, TRuleType
> where TRuleType : ICanBeAnalyzed
{
TRuleTypeConjunction AreConstructors();
TRuleTypeConjunction AreVirtual();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ArchUnitNET.Fluent.Syntax.Elements.Members.MethodMembers
{
public class ShouldRelateToMethodMembersThat<TRuleTypeShouldConjunction, TRuleType> :
ShouldRelateToMembersThat<TRuleTypeShouldConjunction, MethodMember, TRuleType>,
IMethodMemberPredicates<TRuleTypeShouldConjunction>
IMethodMemberPredicates<TRuleTypeShouldConjunction, MethodMember>
where TRuleTypeShouldConjunction : SyntaxElement<TRuleType>
where TRuleType : ICanBeAnalyzed
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace ArchUnitNET.Fluent.Syntax.Elements.Members.PropertyMembers
{
public class GivenPropertyMembersThat : GivenMembersThat<GivenPropertyMembersConjunction, PropertyMember>,
IPropertyMemberPredicates<GivenPropertyMembersConjunction>
IPropertyMemberPredicates<GivenPropertyMembersConjunction, PropertyMember>
{
public GivenPropertyMembersThat(IArchRuleCreator<PropertyMember> ruleCreator) : base(ruleCreator)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ArchUnitNET.Fluent.Syntax.Elements.Members.PropertyMembers
{
public interface IComplexPropertyMemberConditions :
IComplexMemberConditions<PropertyMembersShouldConjunction, PropertyMember>,
IPropertyMemberConditions<PropertyMembersShouldConjunction>
IPropertyMemberConditions<PropertyMembersShouldConjunction, PropertyMember>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
//
// SPDX-License-Identifier: Apache-2.0

using ArchUnitNET.Domain;

namespace ArchUnitNET.Fluent.Syntax.Elements.Members.PropertyMembers
{
public interface IPropertyMemberConditions<out TReturnType> : IMemberConditions<TReturnType>
public interface
IPropertyMemberConditions<out TReturnType, out TRuleType> : IMemberConditions<TReturnType, TRuleType>
where TRuleType : ICanBeAnalyzed
{
TReturnType HaveGetter();
TReturnType HavePrivateGetter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
//
// SPDX-License-Identifier: Apache-2.0

using ArchUnitNET.Domain;

namespace ArchUnitNET.Fluent.Syntax.Elements.Members.PropertyMembers
{
public interface IPropertyMemberPredicates<out TRuleTypeConjunction> : IMemberPredicates<TRuleTypeConjunction>
public interface
IPropertyMemberPredicates<out TRuleTypeConjunction, TRuleType> : IMemberPredicates<TRuleTypeConjunction,
TRuleType> where TRuleType : ICanBeAnalyzed
{
TRuleTypeConjunction HaveGetter();
TRuleTypeConjunction HavePrivateGetter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ArchUnitNET.Fluent.Syntax.Elements.Members.PropertyMembers
{
public class ShouldRelateToPropertyMembersThat<TRuleTypeShouldConjunction, TRuleType> :
ShouldRelateToMembersThat<TRuleTypeShouldConjunction, PropertyMember, TRuleType>,
IPropertyMemberPredicates<TRuleTypeShouldConjunction>
IPropertyMemberPredicates<TRuleTypeShouldConjunction, PropertyMember>
where TRuleTypeShouldConjunction : SyntaxElement<TRuleType>
where TRuleType : ICanBeAnalyzed
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ArchUnitNET.Fluent.Syntax.Elements.Members
{
public class ShouldRelateToMembersThat<TRuleTypeShouldConjunction, TReferenceType, TRuleType> :
ShouldRelateToObjectsThat<TRuleTypeShouldConjunction, TReferenceType, TRuleType>,
IMemberPredicates<TRuleTypeShouldConjunction>
IMemberPredicates<TRuleTypeShouldConjunction, TReferenceType>
where TReferenceType : IMember
where TRuleType : ICanBeAnalyzed
{
Expand Down
12 changes: 12 additions & 0 deletions ArchUnitNET/Fluent/Syntax/Elements/ObjectConditionsDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,18 @@ IEnumerable<ConditionResult> Condition(IEnumerable<TRuleType> ruleTypes, Archite
return new ArchitectureCondition<TRuleType>(Condition, description);
}

public static ICondition<TRuleType> FollowCustomCondition(Func<TRuleType, ConditionResult> condition,
string description)
{
return new SimpleCondition<TRuleType>(condition, description);
}

public static ICondition<TRuleType> FollowCustomCondition(Func<TRuleType, bool> condition, string description,
string failDescription)
{
return new SimpleCondition<TRuleType>(condition, description, failDescription);
}

public static ICondition<TRuleType> OnlyDependOn(string pattern, bool useRegularExpressions = false)
{
ConditionResult Condition(TRuleType ruleType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ IEnumerable<T> Filter(IEnumerable<T> objects, Architecture architecture)
return new ArchitecturePredicate<T>(Filter, description);
}

public static IPredicate<T> FollowCustomPredicate(Func<T, bool> predicate, string description)
{
return new SimplePredicate<T>(predicate, description);
}

public static IPredicate<T> OnlyDependOn(string pattern, bool useRegularExpressions = false)
{
return new SimplePredicate<T>(obj => obj.OnlyDependsOn(pattern, useRegularExpressions),
Expand Down
23 changes: 23 additions & 0 deletions ArchUnitNET/Fluent/Syntax/Elements/ObjectsShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using ArchUnitNET.Domain;
using ArchUnitNET.Fluent.Conditions;
using ArchUnitNET.Fluent.Syntax.Elements.Members.MethodMembers;
using ArchUnitNET.Fluent.Syntax.Elements.Types;
using ArchUnitNET.Fluent.Syntax.Elements.Types.Attributes;
Expand Down Expand Up @@ -140,6 +141,28 @@ public TRuleTypeShouldConjunction DependOnAny(IEnumerable<Type> types)
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction FollowCustomCondition(ICondition<TRuleType> condition)
{
_ruleCreator.AddCondition(condition);
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction FollowCustomCondition(Func<TRuleType, ConditionResult> condition,
string description)
{
_ruleCreator.AddCondition(
ObjectConditionsDefinition<TRuleType>.FollowCustomCondition(condition, description));
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

public TRuleTypeShouldConjunction FollowCustomCondition(Func<TRuleType, bool> condition, string description,
string failDescription)
{
_ruleCreator.AddCondition(
ObjectConditionsDefinition<TRuleType>.FollowCustomCondition(condition, description, failDescription));
return Create<TRuleTypeShouldConjunction, TRuleType>(_ruleCreator);
}

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

0 comments on commit 57ac8c0

Please sign in to comment.