Skip to content

Commit 2c4d776

Browse files
authored
Merge pull request #90 from ecofreshdatatechnologies/ClassesAreStatic
add AreStatic, AreNotStatic, BeStatic, NotBeStatic methods
2 parents 746c201 + 412a60b commit 2c4d776

7 files changed

Lines changed: 163 additions & 4 deletions

File tree

src/NetArchTest.Rules/Conditions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,26 @@ public ConditionList NotBeInterfaces()
344344
return new ConditionList(_types, _should, _sequence);
345345
}
346346

347+
/// <summary>
348+
/// Selects types that are static.
349+
/// </summary>
350+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
351+
public ConditionList BeStatic()
352+
{
353+
_sequence.AddFunctionCall(FunctionDelegates.BeStatic, true, true);
354+
return new ConditionList(_types, _should, _sequence);
355+
}
356+
357+
/// <summary>
358+
/// Selects types that are not static.
359+
/// </summary>
360+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
361+
public ConditionList NotBeStatic()
362+
{
363+
_sequence.AddFunctionCall(FunctionDelegates.BeStatic, true, false);
364+
return new ConditionList(_types, _should, _sequence);
365+
}
366+
347367
/// <summary>
348368
/// Selects types that are nested.
349369
/// </summary>

src/NetArchTest.Rules/FunctionDelegates.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace NetArchTest.Rules
1+
using Mono.Cecil.Rocks;
2+
3+
namespace NetArchTest.Rules
24
{
35
using System;
46
using System.Collections.Generic;
@@ -194,6 +196,21 @@ internal static FunctionDelegate<string> MakeFunctionDelegateUsingStringComparer
194196
}
195197
};
196198

199+
/// <summary> Function for finding static classes. </summary>
200+
internal static FunctionDelegate<bool> BeStatic = delegate(IEnumerable<TypeDefinition> input, bool dummy, bool condition)
201+
{
202+
if (condition)
203+
{
204+
return input.Where(ClassIsStatic);
205+
}
206+
else
207+
{
208+
return input.Where(c => !ClassIsStatic(c));
209+
}
210+
211+
bool ClassIsStatic(TypeDefinition c) => c.IsAbstract && c.IsSealed && !c.IsInterface && !c.GetConstructors().Any(m => m.IsPublic);
212+
};
213+
197214
/// <summary> Function for finding types with generic parameters. </summary>
198215
internal static FunctionDelegate<bool> BeGeneric = delegate (IEnumerable<TypeDefinition> input, bool dummy, bool condition)
199216
{

src/NetArchTest.Rules/NetArchTest.Rules.xml

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/NetArchTest.Rules/Predicates.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,26 @@ public PredicateList AreNotInterfaces()
351351
return new PredicateList(_types, _sequence);
352352
}
353353

354+
/// <summary>
355+
/// Selects types that are static.
356+
/// </summary>
357+
/// <returns>An updated set of predicates that can be applied to a list of types.</returns>
358+
public PredicateList AreStatic()
359+
{
360+
_sequence.AddFunctionCall(FunctionDelegates.BeStatic, true, true);
361+
return new PredicateList(_types, _sequence);
362+
}
363+
364+
/// <summary>
365+
/// Selects types that are not static.
366+
/// </summary>
367+
/// <returns>An updated set of predicates that can be applied to a list of types.</returns>
368+
public PredicateList AreNotStatic()
369+
{
370+
_sequence.AddFunctionCall(FunctionDelegates.BeStatic, true, false);
371+
return new PredicateList(_types, _sequence);
372+
}
373+
354374
/// <summary>
355375
/// Selects types that are nested.
356376
/// </summary>

test/NetArchTest.Rules.UnitTests/ConditionTests.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace NetArchTest.Rules.UnitTests
1+
using NetArchTest.TestStructure.Classes;
2+
3+
namespace NetArchTest.Rules.UnitTests
24
{
35
using System;
46
using System.Linq;
@@ -416,6 +418,36 @@ public void AreNotInterfaces_MatchesFound_ClassesSelected()
416418
Assert.True(result.IsSuccessful);
417419
}
418420

421+
[Fact(DisplayName = "Types can be selected if they are static.")]
422+
public void AreStatic_MatchesFound_ClassesSelected()
423+
{
424+
var result = Types
425+
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
426+
.That()
427+
.ResideInNamespace("NetArchTest.TestStructure.Classes")
428+
.And()
429+
.HaveNameEndingWith("StaticClass")
430+
.Should()
431+
.BeStatic().GetResult();
432+
433+
Assert.True(result.IsSuccessful);
434+
}
435+
436+
[Fact(DisplayName = "Types can be selected if they are not static.")]
437+
public void AreNotStatic_MatchesFound_ClassesSelected()
438+
{
439+
var result = Types
440+
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
441+
.That()
442+
.ResideInNamespace("NetArchTest.TestStructure.Classes")
443+
.And()
444+
.HaveName(nameof(ExampleClass))
445+
.Should()
446+
.NotBeStatic().GetResult();
447+
448+
Assert.True(result.IsSuccessful);
449+
}
450+
419451
[Fact(DisplayName = "Types can be selected if they are nested.")]
420452
public void AreNested_MatchesFound_ClassesSelected()
421453
{

test/NetArchTest.Rules.UnitTests/PredicateTests.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,9 @@ public void AreClasses_MatchesFound_ClassesSelected()
392392
.And()
393393
.AreClasses().GetTypes();
394394

395-
Assert.Single(result); // One type found
395+
Assert.Equal(2, result.Count()); // Two types found
396396
Assert.Contains<Type>(typeof(ExampleClass), result);
397+
Assert.Contains<Type>(typeof(ExampleStaticClass), result);
397398
}
398399

399400
[Fact(DisplayName = "Types can be selected if they are not classes.")]
@@ -462,8 +463,38 @@ public void AreNotInterfaces_MatchesFound_ClassesSelected ()
462463
.And()
463464
.AreNotInterfaces().GetTypes();
464465

465-
Assert.Single(result); // One type found
466+
Assert.Equal(2, result.Count()); // Two types found
467+
Assert.Contains<Type>(typeof(ExampleClass), result);
468+
Assert.Contains<Type>(typeof(ExampleStaticClass), result);
469+
}
470+
471+
[Fact(DisplayName = "Types can be selected if they are static.")]
472+
public void AreStatic_MatchesFound_ClassesSelected()
473+
{
474+
var result = Types
475+
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
476+
.That()
477+
.ResideInNamespace("NetArchTest.TestStructure.Classes")
478+
.And()
479+
.AreStatic().GetTypes();
480+
481+
Assert.Single(result); // One type found
482+
Assert.Contains<Type>(typeof(ExampleStaticClass), result);
483+
}
484+
485+
[Fact(DisplayName = "Types can be selected if they are not static.")]
486+
public void AreNotStatic_MatchesFound_ClassesSelected()
487+
{
488+
var result = Types
489+
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
490+
.That()
491+
.ResideInNamespace("NetArchTest.TestStructure.Classes")
492+
.And()
493+
.AreNotStatic().GetTypes();
494+
495+
Assert.Equal(2, result.Count()); // Two types found
466496
Assert.Contains<Type>(typeof(ExampleClass), result);
497+
Assert.Contains<Type>(typeof(IExampleInterface), result);
467498
}
468499

469500
[Fact(DisplayName = "Types can be selected if they are nested.")]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace NetArchTest.TestStructure.Classes
2+
{
3+
public static class ExampleStaticClass
4+
{
5+
static ExampleStaticClass()
6+
{
7+
8+
}
9+
10+
public static bool SampleMethod() => true;
11+
}
12+
}

0 commit comments

Comments
 (0)