Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added regex matching to slices. #276

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ArchUnitNET/Fluent/Slices/GivenSlices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// SPDX-License-Identifier: Apache-2.0
//

using System;
using System.Collections.Generic;
using ArchUnitNET.Domain;

Expand All @@ -31,5 +32,11 @@ public IEnumerable<Slice> GetObjects(Architecture architecture)
{
return _ruleCreator.GetSlices(architecture);
}

public GivenSlices Where(Func<Slice, bool> filter)
{
_ruleCreator.AddFilter(filter);
return this;
}
}
}
13 changes: 12 additions & 1 deletion ArchUnitNET/Fluent/Slices/SliceRuleCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ private Func<
> _evaluationFunc;
private SliceAssignment _sliceAssignment;

private List<Func<Slice, bool>> filters = new List<Func<Slice, bool>>();

public void AddFilter(Func<Slice, bool> filter)
{
filters.Add(filter);
}

public SliceRuleCreator()
{
Description = "Slices";
Expand Down Expand Up @@ -71,9 +78,13 @@ public IEnumerable<Slice> GetSlices(Architecture architecture)
);
}

return _sliceAssignment
var slices = _sliceAssignment
.Apply(architecture.Types)
.Where(slice => !slice.Identifier.Ignored);

filters.ForEach(filter => slices = slices.Where(filter));

return slices;
}
}
}
53 changes: 53 additions & 0 deletions ArchUnitNET/Fluent/Slices/SliceRuleInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

using System;
using System.Text.RegularExpressions;
using ArchUnitNET.Domain;

namespace ArchUnitNET.Fluent.Slices
Expand All @@ -19,6 +20,58 @@ public SliceRuleInitializer(SliceRuleCreator ruleCreator)
_ruleCreator = ruleCreator;
}

/// <summary></summary>
/// <param name="pattern">
/// Fullname is matched with regex.
/// Non matching Types are Ignored
/// Others are grouped by capture groups (append all captures to identifier string)
/// </param>
/// <param name="match_namespace">
/// If true namespace fullname is matched.
/// Otherwise fullname of Type/Object is matched.
/// </param>
/// <returns></returns>

public GivenSlices MatchingRegex(
string pattern,
bool match_namespace = false
)
{
_ruleCreator.SetSliceAssignment(
new SliceAssignment(
t =>
{
var match = Regex.Match(match_namespace ? t.Namespace.FullName : t.FullName, pattern);
if (!match.Success) return SliceIdentifier.Ignore();

string sliceName = "";
if (match.Groups.Count == 1)
{
sliceName += match.Value;
}
for (int i = 1; i < match.Groups.Count; i++)
{
if(match.Groups[i].Captures.Count == 1)
{
sliceName += match.Groups[i];
}
else
{
for(int j = 0; j < match.Groups[i].Captures.Count; j++)
{
sliceName += match.Groups[i].Captures[j];
}
}
}

return SliceIdentifier.Of(sliceName, null);
},
"matching \"" + pattern + "\""
)
);
return new GivenSlices(_ruleCreator);
}

/// <summary>
/// </summary>
/// <param name="pattern">
Expand Down
66 changes: 66 additions & 0 deletions ArchUnitNETTests/Fluent/Slices/SlicesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,72 @@ public void MatchingTest()
);
}

[Fact]
public void MatchingRegexTest()
{
Assert.True(
SliceRuleDefinition
.Slices()
.MatchingRegex(@"(?:[^\.]*\.){2}([^\.]+).*", true)
.GetObjects(StaticTestArchitectures.ArchUnitNETTestAssemblyArchitecture)
.ToList().Count > 5
);

Assert.False(
SliceRuleDefinition
.Slices()
.MatchingRegex(@"TestAssembly\.Slices\.([^\.]+).*", true)
.GetObjects(StaticTestArchitectures.ArchUnitNETTestAssemblyArchitecture)
.ToList()
.Select(slice => slice.Identifier.Identifier)
.Select(identifier => identifier.Contains("."))
.Aggregate(false, (a, b) => a||b)
);

Assert.DoesNotContain("TestAssembly.Slices.Slice1",
SliceRuleDefinition
.Slices()
.MatchingRegex(@"TestAssembly\.Slices\.(?!Slice1)([^\.]+).*")
.GetObjects(StaticTestArchitectures.ArchUnitNETTestAssemblyArchitecture)
.ToList()
.Select(slice => slice.Identifier.Identifier)
);
}

[Fact]
public void WhereTest()
{
Assert.True(
SliceRuleDefinition
.Slices()
.Matching("TestAssembly.Slices.Slice3.(*)")
.Where(slice => slice.Identifier.Identifier.Contains("Group1"))
.Where(slice => true)
.GetObjects(StaticTestArchitectures.ArchUnitNETTestAssemblyArchitecture)
.Count() == 1
);

SliceRuleDefinition
.Slices()
.Matching("TestAssembly.Slices.(**)")
.Where(slice => !slice.Identifier.Identifier.Contains("Slice3"))
.Where(slice => !slice.Identifier.Identifier.Contains("Slice2"))
.Should()
.NotDependOnEachOther()
.Check(StaticTestArchitectures.ArchUnitNETTestAssemblyArchitecture);

Assert.True(
SliceRuleDefinition
.Slices()
.Matching("TestAssembly.Slices.(**)")
.Where(slice => !slice.Identifier.Identifier.Contains("Slice3"))
.Where(slice => !slice.Identifier.Identifier.Contains("Slice2"))
.Should()
.NotDependOnEachOther()
.HasNoViolations(StaticTestArchitectures.ArchUnitNETTestAssemblyArchitecture)
);
}

[Fact]
public void NotDependOnEachOtherTest()
{
Expand Down