Skip to content

Commit

Permalink
refactoring : extract ConditionTests_Dependencies and PredicateTests_…
Browse files Browse the repository at this point in the history
…Dependencies to separate files
  • Loading branch information
NeVeSpl committed Nov 21, 2023
1 parent 1f94ca6 commit de5139a
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 281 deletions.
28 changes: 1 addition & 27 deletions src/NetArchTest.Rules/Functions/FunctionDelegates.Traits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,32 +229,6 @@ internal static IEnumerable<TypeSpec> BePrivateProtected(IEnumerable<TypeSpec> i
{
return input.Where(c => !c.Definition.IsNestedFamilyAndAssembly);
}
}

//

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

internal static IEnumerable<TypeSpec> HasNullableMembers(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
{
return input.Where(c => c.Definition.HasNullableMembers());
}
else
{
return input.Where(c => !c.Definition.HasNullableMembers());
}
}
}
}
}
27 changes: 27 additions & 0 deletions src/NetArchTest.Rules/Functions/FunctionDelegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,32 @@ internal static IEnumerable<TypeSpec> MeetCustomRule(IEnumerable<TypeSpec> input
return input.Where(t => !rule.MeetsRule(t.Definition));
}
}


//

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

internal static IEnumerable<TypeSpec> HasNullableMembers(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
{
return input.Where(c => c.Definition.HasNullableMembers());
}
else
{
return input.Where(c => !c.Definition.HasNullableMembers());
}
}
}
}
127 changes: 0 additions & 127 deletions test/NetArchTest.Rules.UnitTests/ConditionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,133 +516,6 @@ public void ResideInNamespace_Nested_AllClassReturned()
Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they have a dependency on a specific item.")]
public void HaveDependency_MatchesFound_ClassSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace(typeof(HasDependency).Namespace)
.And()
.HaveNameStartingWith("HasDepend")
.Should()
.HaveDependencyOnAny(typeof(ExampleDependency).FullName)
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they have a dependency on any item in a list.")]
public void HaveDependencyOnAny_MatchesFound_ClassSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace(typeof(HasDependency).Namespace)
.And()
.HaveNameStartingWith("Has")
.Should()
.HaveDependencyOnAny(new[] { typeof(ExampleDependency).FullName, typeof(AnotherExampleDependency).FullName })
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they have a dependency on all the items in a list.")]
public void HaveDependencyOnAll_MatchesFound_ClassSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace(typeof(HasDependency).Namespace)
.And()
.HaveNameStartingWith("HasDependencies")
.Should()
.HaveDependencyOnAll(new[] { typeof(ExampleDependency).FullName, typeof(AnotherExampleDependency).FullName })
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they only have a dependency on any item in a list.")]
public void OnlyHaveDependenciesOn_MatchesFound_ClassSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace(typeof(HasDependency).Namespace)
.And()
.HaveNameStartingWith("HasDependency")
.Should()
.OnlyHaveDependencyOn(new[] { typeof(ExampleDependency).FullName, "System" })
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they do not have a dependency on another type.")]
public void NotHaveDependency_MatchesFound_ClassSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace(typeof(HasDependency).Namespace)
.And()
.HaveNameStartingWith("NoDependency")
.Should()
.NotHaveDependencyOnAny(typeof(ExampleDependency).FullName)
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they do not have a dependency on any item in a list.")]
public void NotHaveDependencyOnAny_MatchesFound_ClassSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace(typeof(HasDependency).Namespace)
.And()
.HaveNameStartingWith("NoDependency")
.Should()
.NotHaveDependencyOnAny(new[] { typeof(ExampleDependency).FullName, typeof(AnotherExampleDependency).FullName })
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they do not have a dependency on all the items in a list.")]
public void NotHaveDependencyOnAll_MatchesFound_ClassSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace(typeof(HasDependency).Namespace)
.And()
.HaveNameStartingWith("NoDependency")
.Should()
.NotHaveDependencyOnAll(new[] { typeof(ExampleDependency).FullName, typeof(AnotherExampleDependency).FullName })
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they have a dependency that is not on the a list.")]
public void HaveDependenciesOtherThan_MatchesFound_ClassSelected()
{
var result = Types
.InAssembly(Assembly.GetAssembly(typeof(ClassA1)))
.That()
.ResideInNamespace(typeof(HasDependency).Namespace)
.And()
.HaveNameStartingWith("HasDependencies")
.Should()
.HaveDependencyOtherThan(new[] { typeof(ExampleDependency).FullName, "System" })
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types failing condition are reported when test fails.")]
public void MatchNotFound_ClassesReported()
Expand Down
118 changes: 118 additions & 0 deletions test/NetArchTest.Rules.UnitTests/ConditionTests_Dependencies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System.Reflection;
using NetArchTest.Rules;
using NetArchTest.TestStructure.Dependencies.Examples;
using NetArchTest.TestStructure.Dependencies.Implementation;
using Xunit;

namespace NetArchTest.UnitTests
{
public class ConditionTests_Dependencies
{
private Predicate GetTypesThat()
{
return Types
.InAssembly(Assembly.GetAssembly(typeof(HasDependency)))
.That()
.ResideInNamespace(typeof(HasDependency).Namespace)
.And();
}


[Fact(DisplayName = "Types can be selected if they have a dependency on a specific item.")]
public void HaveDependencyOnAny()
{
var result = GetTypesThat()
.HaveNameStartingWith("HasDepend")
.Should()
.HaveDependencyOnAny(typeof(ExampleDependency).FullName)
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they have a dependency on any item in a list.")]
public void HaveDependencyOnAny_Many()
{
var result = GetTypesThat()
.HaveNameStartingWith("Has")
.Should()
.HaveDependencyOnAny(new[] { typeof(ExampleDependency).FullName, typeof(AnotherExampleDependency).FullName })
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they have a dependency on all the items in a list.")]
public void HaveDependencyOnAll()
{
var result = GetTypesThat()
.HaveNameStartingWith("HasDependencies")
.Should()
.HaveDependencyOnAll(new[] { typeof(ExampleDependency).FullName, typeof(AnotherExampleDependency).FullName })
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they only have a dependency on any item in a list.")]
public void HaveNameStartingWith()
{
var result = GetTypesThat()
.HaveNameStartingWith("HasDependency")
.Should()
.OnlyHaveDependencyOn(new[] { typeof(ExampleDependency).FullName, "System" })
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they do not have a dependency on another type.")]
public void NotHaveDependencyOnAny()
{
var result = GetTypesThat()
.HaveNameStartingWith("NoDependency")
.Should()
.NotHaveDependencyOnAny(typeof(ExampleDependency).FullName)
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they do not have a dependency on any item in a list.")]
public void NotHaveDependencyOnAny_Many()
{
var result = GetTypesThat()
.HaveNameStartingWith("NoDependency")
.Should()
.NotHaveDependencyOnAny(new[] { typeof(ExampleDependency).FullName, typeof(AnotherExampleDependency).FullName })
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they do not have a dependency on all the items in a list.")]
public void NotHaveDependencyOnAll()
{
var result = GetTypesThat()
.HaveNameStartingWith("NoDependency")
.Should()
.NotHaveDependencyOnAll(new[] { typeof(ExampleDependency).FullName, typeof(AnotherExampleDependency).FullName })
.GetResult();

Assert.True(result.IsSuccessful);
}

[Fact(DisplayName = "Types can be selected if they have a dependency that is not on the a list.")]
public void HaveDependencyOtherThan()
{
var result = GetTypesThat()
.HaveNameStartingWith("HasDependencies")
.Should()
.HaveDependencyOtherThan(new[] { typeof(ExampleDependency).FullName, "System" })
.GetResult();

Assert.True(result.IsSuccessful);
}

}
}
Loading

0 comments on commit de5139a

Please sign in to comment.