Skip to content

Commit

Permalink
correct implementation of : BeAbstract, BeSealed
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed Nov 21, 2023
1 parent 1c260fb commit 1f94ca6
Show file tree
Hide file tree
Showing 17 changed files with 200 additions and 325 deletions.
16 changes: 9 additions & 7 deletions src/NetArchTest.Rules/Functions/FunctionDelegates.Traits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,20 @@ internal static IEnumerable<TypeSpec> BeRecord(IEnumerable<TypeSpec> input, bool
}
}

// Modifiers
// Modifiers & Generic

internal static IEnumerable<TypeSpec> BeAbstract(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
{
return input.Where(c => c.Definition.IsAbstract);
return input.Where(c => ClassIsAbstract(c.Definition));
}
else
{
return input.Where(c => !c.Definition.IsAbstract);
return input.Where(c => !ClassIsAbstract(c.Definition));
}

bool ClassIsAbstract(TypeDefinition c) => c.IsAbstract && !c.IsSealed;
}

internal static IEnumerable<TypeSpec> BeStatic(IEnumerable<TypeSpec> input, bool condition)
Expand All @@ -118,15 +120,15 @@ internal static IEnumerable<TypeSpec> BeSealed(IEnumerable<TypeSpec> input, bool
{
if (condition)
{
return input.Where(c => c.Definition.IsSealed);
return input.Where(c => ClassIsSealed(c.Definition));
}
else
{
return input.Where(c => !c.Definition.IsSealed);
return input.Where(c => !ClassIsSealed(c.Definition));
}
}

// Generic
bool ClassIsSealed(TypeDefinition c) => !c.IsAbstract && c.IsSealed;
}

internal static IEnumerable<TypeSpec> BeGeneric(IEnumerable<TypeSpec> input, bool condition)
{
Expand Down
96 changes: 1 addition & 95 deletions test/NetArchTest.Rules.UnitTests/ConditionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,101 +297,7 @@ public void NotImplementInterface_MatchesFound_ClassesSelected()
Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they are abstract.")]
public void AreAbstract_MatchesFound_ClassesSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace("NetArchTest.TestStructure.Abstract")
.And()
.HaveNameStartingWith("Abstract")
.Should()
.BeAbstract().GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they are not abstract.")]
public void AreNotAbstract_MatchesFound_ClassesSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace("NetArchTest.TestStructure.Abstract")
.And()
.DoNotHaveNameStartingWith("Abstract")
.Should()
.NotBeAbstract().GetResult();

Assert.True(result.IsSuccessful);
}



[Fact(DisplayName = "Types can be selected if they have generic parameters.")]
public void AreGeneric_MatchesFound_ClassesSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace("NetArchTest.TestStructure.Generic")
.And()
.HaveNameStartingWith("Generic")
.Should()
.BeGeneric().GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they do not have generic parameters.")]
public void AreNotGeneric_MatchesFound_ClassesSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace("NetArchTest.TestStructure.Generic")
.And()
.HaveNameStartingWith("NonGeneric")
.Should()
.NotBeGeneric().GetResult();

Assert.True(result.IsSuccessful);
}





[Fact(DisplayName = "Types can be selected for being declared as sealed.")]
public void AreSealed_MatchesFound_ClassSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace("NetArchTest.TestStructure.Scope")
.And()
.HaveName("SealedClass")
.Should()
.BeSealed().GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected for not being declared as sealed.")]
public void AreNotSealed_MatchesFound_ClassSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace("NetArchTest.TestStructure.Scope")
.And()
.DoNotHaveName("SealedClass")
.Should()
.NotBeSealed().GetResult();

Assert.True(result.IsSuccessful);
}


[Fact(DisplayName = "Types can be selected for being immutable.")]
public void AreImmutable_MatchesFound_ClassSelected()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void NotBePrivate()
Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "BePrivate")]
[Fact(DisplayName = "BePrivateProtected")]
public void BePrivateProtected()
{

Expand Down Expand Up @@ -154,5 +154,29 @@ public void NotBeProtectedInternal()

Assert.True(result.IsSuccessful);
}


[Fact(DisplayName = "BePublic")]
public void BePublic()
{

var result = GetTypesThat()
.HaveNameStartingWith("Public")
.Should()
.BePublic().GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "NotBePublic")]
public void NotBePublic()
{
var result = GetTypesThat()
.DoNotHaveNameStartingWith("Public")
.Should()
.NotBePublic().GetResult();

Assert.True(result.IsSuccessful);
}
}
}
82 changes: 79 additions & 3 deletions test/NetArchTest.Rules.UnitTests/ConditionTests_Traits.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using NetArchTest.Rules;
using NetArchTest.TestStructure.NameMatching.Namespace1;
using NetArchTest.TestStructure.Traits;
using Xunit;

Expand All @@ -11,7 +12,7 @@ public class ConditionTests_Traits
private Predicate GetTypesThat()
{
return Types
.InAssembly(Assembly.GetAssembly(typeof(ExampleStaticClass)))
.InAssembly(Assembly.GetAssembly(typeof(StaticClass)))
.That()
.ResideInNamespace("NetArchTest.TestStructure.Traits")
.And();
Expand All @@ -22,7 +23,7 @@ private Predicate GetTypesThat()
public void BeStatic()
{
var result = GetTypesThat()
.HaveNameMatching("Static")
.HaveNameStartingWith("Static")
.Should()
.BeStatic().GetResult();

Expand All @@ -33,11 +34,86 @@ public void BeStatic()
public void NotBeStatic()
{
var result = GetTypesThat()
.DoNotHaveNameMatching("Static")
.DoNotHaveNameStartingWith("Static")
.Should()
.NotBeStatic().GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "BeAbstract")]
public void BeAbstract()
{
var result = GetTypesThat()
.HaveNameStartingWith("Abstract")
.Should()
.BeAbstract().GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "NotBeAbstract")]
public void NotBeAbstract()
{
var result = GetTypesThat()
.DoNotHaveNameStartingWith("Abstract")
.Should()
.NotBeAbstract().GetResult();

Assert.True(result.IsSuccessful);
}



[Fact(DisplayName = "BeGeneric")]
public void BeGeneric()
{
var result = GetTypesThat()
.HaveNameStartingWith("Generic")
.Should()
.BeGeneric().GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "NotBeGeneric")]
public void NotBeGeneric()
{
var result = GetTypesThat()
.DoNotHaveNameStartingWith("Generic")
.Should()
.NotBeGeneric().GetResult();

Assert.True(result.IsSuccessful);
}


[Fact(DisplayName = "BeSealed")]
public void BeSealed()
{
var result = GetTypesThat()
.HaveNameStartingWith("Sealed")
.Should()
.BeSealed().GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "NotBeSealed")]
public void NotBeSealed()
{
var result = GetTypesThat()
.DoNotHaveNameStartingWith("Sealed")
.Should()
.NotBeSealed().GetResult();

Assert.True(result.IsSuccessful);
}






}
}
75 changes: 0 additions & 75 deletions test/NetArchTest.Rules.UnitTests/FunctionSequenceTests.cs

This file was deleted.

Loading

0 comments on commit 1f94ca6

Please sign in to comment.