Skip to content

Commit 7e03a8a

Browse files
authored
Merge pull request #439 from TNG/438-donotdependonany-does-verify-inheritance
DependOnAny and DoNotDependOnAny should check dependencies on referenced types
2 parents 04b8593 + 78bea4c commit 7e03a8a

10 files changed

+203
-105
lines changed

ArchUnitNET/Fluent/Syntax/Elements/ObjectPredicatesDefinition.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,7 @@ public static IPredicate<T> DoNotDependOnAny(IObjectProvider<IType> objectProvid
440440
IEnumerable<T> Filter(IEnumerable<T> objects, Architecture architecture)
441441
{
442442
var types = objectProvider.GetObjects(architecture);
443-
return objects.Where(obj =>
444-
!obj.GetTypeDependencies(architecture).Intersect(types).Any()
445-
);
443+
return objects.Where(obj => !obj.GetTypeDependencies().Intersect(types).Any());
446444
}
447445

448446
var description = objectProvider.FormatDescription(

ArchUnitNETTests/AssemblyTestHelper/DependencyAssemblyTestHelpers.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using ArchUnitNET.Domain;
45
using ArchUnitNET.Domain.Extensions;
@@ -57,6 +58,12 @@ public class DependencyAssemblyTestHelper : AssemblyTestHelper
5758
public Class OtherClassWithoutDependencies;
5859
public Type OtherClassWithoutDependenciesSystemType = typeof(OtherClassWithoutDependencies);
5960

61+
public Class ClassWithReferencedTypeDependency;
62+
public Type ClassWithReferencedTypeDependencySystemType =
63+
typeof(ClassWithReferencedTypeDependency);
64+
65+
public Type ReferencedType = typeof(List<>);
66+
6067
public MethodMember MethodWithSingleDependency;
6168

6269
public MethodMember CalledMethod;
@@ -96,6 +103,9 @@ public DependencyAssemblyTestHelper()
96103
OtherClassWithoutDependencies = Architecture.GetClassOfType(
97104
typeof(OtherClassWithoutDependencies)
98105
);
106+
ClassWithReferencedTypeDependency = Architecture.GetClassOfType(
107+
typeof(ClassWithReferencedTypeDependency)
108+
);
99109
MethodWithSingleDependency = Architecture
100110
.MethodMembers.WhereNameIs("MethodWithSingleDependency()")
101111
.First();

ArchUnitNETTests/Fluent/Syntax/Elements/ObjectSyntaxElementsTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,18 @@ public async Task DependOnAnyTest()
414414
helper.AddSnapshotSubHeader("Predicates as conditions");
415415
should.BeTypesThat().DependOnAny(typeof(AttributeNamespace.ClassWithoutAttributes)).AssertException<TypeDoesNotExistInArchitecture>(helper);
416416

417+
helper.AddSnapshotHeader("Referenced type");
418+
should = Types().That().Are(helper.ClassWithReferencedTypeDependency).Should();
419+
420+
helper.AddSnapshotSubHeader("Conditions");
421+
should.DependOnAny(helper.ReferencedType).AssertNoViolations(helper);
422+
423+
helper.AddSnapshotSubHeader("Predicates");
424+
should.Be(Types().That().DependOnAny(helper.ReferencedType)).AssertNoViolations(helper);
425+
426+
helper.AddSnapshotSubHeader("Predicates as conditions");
427+
should.BeTypesThat().DependOnAny(helper.ReferencedType).AssertNoViolations(helper);
428+
417429
helper.AddSnapshotHeader("Empty arguments");
418430
should = Types().That().Are(helper.ClassWithMultipleDependencies).Should();
419431

@@ -1692,6 +1704,18 @@ public async Task NotDependOnAnyTest()
16921704
helper.AddSnapshotSubHeader("Predicates as conditions");
16931705
should.BeTypesThat().DoNotDependOnAny(typeof(AttributeNamespace.ClassWithoutAttributes)).AssertException<TypeDoesNotExistInArchitecture>(helper);
16941706

1707+
helper.AddSnapshotHeader("Referenced type");
1708+
should = Types().That().Are(helper.ClassWithReferencedTypeDependency).Should();
1709+
1710+
helper.AddSnapshotSubHeader("Conditions");
1711+
should.NotDependOnAny(helper.ReferencedType).AssertOnlyViolations(helper);
1712+
1713+
helper.AddSnapshotHeader("Predicates");
1714+
should.Be(Types().That().DoNotDependOnAny(helper.ReferencedType)).AssertOnlyViolations(helper);
1715+
1716+
helper.AddSnapshotSubHeader("Predicates as conditions");
1717+
should.BeTypesThat().DoNotDependOnAny(helper.ReferencedType).AssertOnlyViolations(helper);
1718+
16951719
helper.AddSnapshotHeader("Empty arguments");
16961720
should = Types().That().Are(helper.ChildClass).Should();
16971721

ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/ObjectSyntaxElementsTests.DependOnAnyTest.verified.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,32 @@ Exception: Type AttributeNamespace.ClassWithoutAttributes does not exist in prov
256256
Query: Types that are "TypeDependencyNamespace.ClassWithMultipleDependencies" should be types that depend on "AttributeNamespace.ClassWithoutAttributes"
257257
Exception: Type AttributeNamespace.ClassWithoutAttributes does not exist in provided architecture or is no class.
258258

259+
===== Referenced type =====
260+
261+
----- Conditions -----
262+
263+
Query: Types that are "TypeDependencyNamespace.ClassWithReferencedTypeDependency" should depend on "System.Collections.Generic.List`1"
264+
Result: True
265+
Description: TypeDependencyNamespace.ClassWithReferencedTypeDependency passed
266+
Message:
267+
All Evaluations passed
268+
269+
----- Predicates -----
270+
271+
Query: Types that are "TypeDependencyNamespace.ClassWithReferencedTypeDependency" should be Types that depend on "System.Collections.Generic.List`1"
272+
Result: True
273+
Description: TypeDependencyNamespace.ClassWithReferencedTypeDependency passed
274+
Message:
275+
All Evaluations passed
276+
277+
----- Predicates as conditions -----
278+
279+
Query: Types that are "TypeDependencyNamespace.ClassWithReferencedTypeDependency" should be types that depend on "System.Collections.Generic.List`1"
280+
Result: True
281+
Description: TypeDependencyNamespace.ClassWithReferencedTypeDependency passed
282+
Message:
283+
All Evaluations passed
284+
259285
===== Empty arguments =====
260286

261287
----- Conditions -----

ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/ObjectSyntaxElementsTests.NotBeTest.verified.txt

Lines changed: 10 additions & 10 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)