Skip to content

Commit 1f94ca6

Browse files
committed
correct implementation of : BeAbstract, BeSealed
1 parent 1c260fb commit 1f94ca6

File tree

17 files changed

+200
-325
lines changed

17 files changed

+200
-325
lines changed

src/NetArchTest.Rules/Functions/FunctionDelegates.Traits.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,20 @@ internal static IEnumerable<TypeSpec> BeRecord(IEnumerable<TypeSpec> input, bool
8686
}
8787
}
8888

89-
// Modifiers
89+
// Modifiers & Generic
9090

9191
internal static IEnumerable<TypeSpec> BeAbstract(IEnumerable<TypeSpec> input, bool condition)
9292
{
9393
if (condition)
9494
{
95-
return input.Where(c => c.Definition.IsAbstract);
95+
return input.Where(c => ClassIsAbstract(c.Definition));
9696
}
9797
else
9898
{
99-
return input.Where(c => !c.Definition.IsAbstract);
99+
return input.Where(c => !ClassIsAbstract(c.Definition));
100100
}
101+
102+
bool ClassIsAbstract(TypeDefinition c) => c.IsAbstract && !c.IsSealed;
101103
}
102104

103105
internal static IEnumerable<TypeSpec> BeStatic(IEnumerable<TypeSpec> input, bool condition)
@@ -118,15 +120,15 @@ internal static IEnumerable<TypeSpec> BeSealed(IEnumerable<TypeSpec> input, bool
118120
{
119121
if (condition)
120122
{
121-
return input.Where(c => c.Definition.IsSealed);
123+
return input.Where(c => ClassIsSealed(c.Definition));
122124
}
123125
else
124126
{
125-
return input.Where(c => !c.Definition.IsSealed);
127+
return input.Where(c => !ClassIsSealed(c.Definition));
126128
}
127-
}
128129

129-
// Generic
130+
bool ClassIsSealed(TypeDefinition c) => !c.IsAbstract && c.IsSealed;
131+
}
130132

131133
internal static IEnumerable<TypeSpec> BeGeneric(IEnumerable<TypeSpec> input, bool condition)
132134
{

test/NetArchTest.Rules.UnitTests/ConditionTests.cs

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -297,101 +297,7 @@ public void NotImplementInterface_MatchesFound_ClassesSelected()
297297
Assert.True(result.IsSuccessful);
298298
}
299299

300-
[Fact(DisplayName = "Types can be selected if they are abstract.")]
301-
public void AreAbstract_MatchesFound_ClassesSelected()
302-
{
303-
var result = Types
304-
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
305-
.That()
306-
.ResideInNamespace("NetArchTest.TestStructure.Abstract")
307-
.And()
308-
.HaveNameStartingWith("Abstract")
309-
.Should()
310-
.BeAbstract().GetResult();
311-
312-
Assert.True(result.IsSuccessful);
313-
}
314-
315-
[Fact(DisplayName = "Types can be selected if they are not abstract.")]
316-
public void AreNotAbstract_MatchesFound_ClassesSelected()
317-
{
318-
var result = Types
319-
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
320-
.That()
321-
.ResideInNamespace("NetArchTest.TestStructure.Abstract")
322-
.And()
323-
.DoNotHaveNameStartingWith("Abstract")
324-
.Should()
325-
.NotBeAbstract().GetResult();
326-
327-
Assert.True(result.IsSuccessful);
328-
}
329-
330-
331-
332-
[Fact(DisplayName = "Types can be selected if they have generic parameters.")]
333-
public void AreGeneric_MatchesFound_ClassesSelected()
334-
{
335-
var result = Types
336-
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
337-
.That()
338-
.ResideInNamespace("NetArchTest.TestStructure.Generic")
339-
.And()
340-
.HaveNameStartingWith("Generic")
341-
.Should()
342-
.BeGeneric().GetResult();
343-
344-
Assert.True(result.IsSuccessful);
345-
}
346-
347-
[Fact(DisplayName = "Types can be selected if they do not have generic parameters.")]
348-
public void AreNotGeneric_MatchesFound_ClassesSelected()
349-
{
350-
var result = Types
351-
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
352-
.That()
353-
.ResideInNamespace("NetArchTest.TestStructure.Generic")
354-
.And()
355-
.HaveNameStartingWith("NonGeneric")
356-
.Should()
357-
.NotBeGeneric().GetResult();
358-
359-
Assert.True(result.IsSuccessful);
360-
}
361-
362-
363-
364-
365-
366-
[Fact(DisplayName = "Types can be selected for being declared as sealed.")]
367-
public void AreSealed_MatchesFound_ClassSelected()
368-
{
369-
var result = Types
370-
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
371-
.That()
372-
.ResideInNamespace("NetArchTest.TestStructure.Scope")
373-
.And()
374-
.HaveName("SealedClass")
375-
.Should()
376-
.BeSealed().GetResult();
377-
378-
Assert.True(result.IsSuccessful);
379-
}
380-
381-
[Fact(DisplayName = "Types can be selected for not being declared as sealed.")]
382-
public void AreNotSealed_MatchesFound_ClassSelected()
383-
{
384-
var result = Types
385-
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
386-
.That()
387-
.ResideInNamespace("NetArchTest.TestStructure.Scope")
388-
.And()
389-
.DoNotHaveName("SealedClass")
390-
.Should()
391-
.NotBeSealed().GetResult();
392-
393-
Assert.True(result.IsSuccessful);
394-
}
300+
395301

396302
[Fact(DisplayName = "Types can be selected for being immutable.")]
397303
public void AreImmutable_MatchesFound_ClassSelected()

test/NetArchTest.Rules.UnitTests/ConditionTests_AccessModifiers.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void NotBePrivate()
8686
Assert.True(result.IsSuccessful);
8787
}
8888

89-
[Fact(DisplayName = "BePrivate")]
89+
[Fact(DisplayName = "BePrivateProtected")]
9090
public void BePrivateProtected()
9191
{
9292

@@ -154,5 +154,29 @@ public void NotBeProtectedInternal()
154154

155155
Assert.True(result.IsSuccessful);
156156
}
157+
158+
159+
[Fact(DisplayName = "BePublic")]
160+
public void BePublic()
161+
{
162+
163+
var result = GetTypesThat()
164+
.HaveNameStartingWith("Public")
165+
.Should()
166+
.BePublic().GetResult();
167+
168+
Assert.True(result.IsSuccessful);
169+
}
170+
171+
[Fact(DisplayName = "NotBePublic")]
172+
public void NotBePublic()
173+
{
174+
var result = GetTypesThat()
175+
.DoNotHaveNameStartingWith("Public")
176+
.Should()
177+
.NotBePublic().GetResult();
178+
179+
Assert.True(result.IsSuccessful);
180+
}
157181
}
158182
}

test/NetArchTest.Rules.UnitTests/ConditionTests_Traits.cs

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Reflection;
22
using NetArchTest.Rules;
3+
using NetArchTest.TestStructure.NameMatching.Namespace1;
34
using NetArchTest.TestStructure.Traits;
45
using Xunit;
56

@@ -11,7 +12,7 @@ public class ConditionTests_Traits
1112
private Predicate GetTypesThat()
1213
{
1314
return Types
14-
.InAssembly(Assembly.GetAssembly(typeof(ExampleStaticClass)))
15+
.InAssembly(Assembly.GetAssembly(typeof(StaticClass)))
1516
.That()
1617
.ResideInNamespace("NetArchTest.TestStructure.Traits")
1718
.And();
@@ -22,7 +23,7 @@ private Predicate GetTypesThat()
2223
public void BeStatic()
2324
{
2425
var result = GetTypesThat()
25-
.HaveNameMatching("Static")
26+
.HaveNameStartingWith("Static")
2627
.Should()
2728
.BeStatic().GetResult();
2829

@@ -33,11 +34,86 @@ public void BeStatic()
3334
public void NotBeStatic()
3435
{
3536
var result = GetTypesThat()
36-
.DoNotHaveNameMatching("Static")
37+
.DoNotHaveNameStartingWith("Static")
3738
.Should()
3839
.NotBeStatic().GetResult();
3940

4041
Assert.True(result.IsSuccessful);
4142
}
43+
44+
[Fact(DisplayName = "BeAbstract")]
45+
public void BeAbstract()
46+
{
47+
var result = GetTypesThat()
48+
.HaveNameStartingWith("Abstract")
49+
.Should()
50+
.BeAbstract().GetResult();
51+
52+
Assert.True(result.IsSuccessful);
53+
}
54+
55+
[Fact(DisplayName = "NotBeAbstract")]
56+
public void NotBeAbstract()
57+
{
58+
var result = GetTypesThat()
59+
.DoNotHaveNameStartingWith("Abstract")
60+
.Should()
61+
.NotBeAbstract().GetResult();
62+
63+
Assert.True(result.IsSuccessful);
64+
}
65+
66+
67+
68+
[Fact(DisplayName = "BeGeneric")]
69+
public void BeGeneric()
70+
{
71+
var result = GetTypesThat()
72+
.HaveNameStartingWith("Generic")
73+
.Should()
74+
.BeGeneric().GetResult();
75+
76+
Assert.True(result.IsSuccessful);
77+
}
78+
79+
[Fact(DisplayName = "NotBeGeneric")]
80+
public void NotBeGeneric()
81+
{
82+
var result = GetTypesThat()
83+
.DoNotHaveNameStartingWith("Generic")
84+
.Should()
85+
.NotBeGeneric().GetResult();
86+
87+
Assert.True(result.IsSuccessful);
88+
}
89+
90+
91+
[Fact(DisplayName = "BeSealed")]
92+
public void BeSealed()
93+
{
94+
var result = GetTypesThat()
95+
.HaveNameStartingWith("Sealed")
96+
.Should()
97+
.BeSealed().GetResult();
98+
99+
Assert.True(result.IsSuccessful);
100+
}
101+
102+
[Fact(DisplayName = "NotBeSealed")]
103+
public void NotBeSealed()
104+
{
105+
var result = GetTypesThat()
106+
.DoNotHaveNameStartingWith("Sealed")
107+
.Should()
108+
.NotBeSealed().GetResult();
109+
110+
Assert.True(result.IsSuccessful);
111+
}
112+
113+
114+
115+
116+
117+
42118
}
43119
}

test/NetArchTest.Rules.UnitTests/FunctionSequenceTests.cs

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)