Skip to content

Commit 8da498a

Browse files
Merge pull request #414 from TNG/refactor/object-syntax-elements
Refactor Object Syntax Elements
2 parents 659e039 + ad21b7b commit 8da498a

File tree

39 files changed

+3506
-5772
lines changed

39 files changed

+3506
-5772
lines changed

ArchUnitNET/Domain/Extensions/EnumerableExtensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,26 @@ public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
2121
{
2222
return source == null || !source.Any();
2323
}
24+
25+
public static string FormatDescription<T>(
26+
this IEnumerable<T> source,
27+
string emptyDescription,
28+
string singleDescription,
29+
string multipleDescription,
30+
Func<T, string> elementDescription = null
31+
)
32+
{
33+
var list = source as IList<T> ?? source.ToList();
34+
elementDescription = elementDescription ?? (element => $"\"{element}\"");
35+
switch (list.Count)
36+
{
37+
case 0:
38+
return emptyDescription;
39+
case 1:
40+
return $"{singleDescription} {string.Join(" and ", list.Select(elementDescription))}";
41+
default:
42+
return $"{multipleDescription} {string.Join(" and ", list.Select(elementDescription))}";
43+
}
44+
}
2445
}
2546
}

ArchUnitNET/Domain/Extensions/NamingExtensions.cs

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ namespace ArchUnitNET.Domain.Extensions
99
{
1010
public static class NamingExtensions
1111
{
12+
public static bool NameEquals(this IHasName cls, string name)
13+
{
14+
return string.Equals(cls.Name, name, StringComparison.OrdinalIgnoreCase);
15+
}
16+
17+
public static bool NameMatches(this IHasName cls, string pattern)
18+
{
19+
return pattern != null && Regex.IsMatch(cls.Name, pattern);
20+
}
21+
1222
public static bool NameEndsWith(
1323
this IHasName cls,
1424
string pattern,
@@ -36,31 +46,79 @@ public static bool NameContains(
3646
return cls.Name.IndexOf(pattern, stringComparison) >= 0;
3747
}
3848

39-
public static bool NameEquals(this IHasName cls, string name)
49+
public static bool FullNameEquals(this IHasName cls, string fullName)
4050
{
41-
return string.Equals(cls.Name, name, StringComparison.OrdinalIgnoreCase);
51+
return string.Equals(cls.FullName, fullName, StringComparison.OrdinalIgnoreCase);
4252
}
4353

44-
public static bool NameMatches(this IHasName cls, string pattern)
54+
public static bool FullNameMatches(this IHasName cls, string pattern)
4555
{
46-
return pattern != null && Regex.IsMatch(cls.Name, pattern);
56+
return pattern != null && Regex.IsMatch(cls.FullName, pattern);
4757
}
4858

49-
public static bool FullNameEquals(this IHasName cls, string fullName)
59+
public static bool FullNameEndsWith(this IHasName cls, string pattern)
5060
{
51-
return string.Equals(cls.FullName, fullName, StringComparison.OrdinalIgnoreCase);
61+
return cls.FullName.EndsWith(pattern, StringComparison.OrdinalIgnoreCase);
5262
}
5363

54-
public static bool FullNameMatches(this IHasName cls, string pattern)
64+
public static bool FullNameStartsWith(this IHasName cls, string pattern)
5565
{
56-
return pattern != null && Regex.IsMatch(cls.FullName, pattern);
66+
return cls.FullName.StartsWith(pattern, StringComparison.OrdinalIgnoreCase);
5767
}
5868

5969
public static bool FullNameContains(this IHasName cls, string pattern)
6070
{
6171
return pattern != null && cls.FullName.ToLower().Contains(pattern.ToLower());
6272
}
6373

74+
public static bool AssemblyQualifiedNameEquals(
75+
this IHasAssemblyQualifiedName cls,
76+
string assemblyQualifiedName
77+
)
78+
{
79+
return string.Equals(
80+
cls.AssemblyQualifiedName,
81+
assemblyQualifiedName,
82+
StringComparison.OrdinalIgnoreCase
83+
);
84+
}
85+
86+
public static bool AssemblyQualifiedNameMatches(
87+
this IHasAssemblyQualifiedName cls,
88+
string pattern
89+
)
90+
{
91+
return pattern != null && Regex.IsMatch(cls.AssemblyQualifiedName, pattern);
92+
}
93+
94+
public static bool AssemblyQualifiedNameEndsWith(
95+
this IHasAssemblyQualifiedName cls,
96+
string pattern
97+
)
98+
{
99+
return cls.AssemblyQualifiedName.EndsWith(pattern, StringComparison.OrdinalIgnoreCase);
100+
}
101+
102+
public static bool AssemblyQualifiedNameStartsWith(
103+
this IHasAssemblyQualifiedName cls,
104+
string pattern
105+
)
106+
{
107+
return cls.AssemblyQualifiedName.StartsWith(
108+
pattern,
109+
StringComparison.OrdinalIgnoreCase
110+
);
111+
}
112+
113+
public static bool AssemblyQualifiedNameContains(
114+
this IHasAssemblyQualifiedName cls,
115+
string pattern
116+
)
117+
{
118+
return pattern != null
119+
&& cls.AssemblyQualifiedName.ToLower().Contains(pattern.ToLower());
120+
}
121+
64122
[NotNull]
65123
public static IEnumerable<TType> WhereNameIs<TType>(
66124
this IEnumerable<TType> source,

ArchUnitNET/Domain/IObjectProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@ namespace ArchUnitNET.Domain
55
public interface IObjectProvider<out T> : IHasDescription
66
{
77
IEnumerable<T> GetObjects(Architecture architecture);
8+
9+
string FormatDescription(
10+
string emptyDescription,
11+
string singleDescription,
12+
string multipleDescription
13+
);
814
}
915
}

ArchUnitNET/Fluent/ObjectProvider.cs renamed to ArchUnitNET/Domain/ObjectProvider.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace ArchUnitNET.Fluent
66
{
7-
public class ObjectProvider<T> : ISizedObjectProvider<T>
7+
internal class ObjectProvider<T> : ISizedObjectProvider<T>
88
where T : ICanBeAnalyzed
99
{
1010
private readonly List<T> _objects;
@@ -20,16 +20,32 @@ public ObjectProvider(IEnumerable<T> objects)
2020

2121
public string Description { get; }
2222

23-
public int Count => _objects.Count();
23+
public int Count => _objects.Count;
2424

2525
public IEnumerable<T> GetObjects(Architecture architecture)
2626
{
2727
return _objects;
2828
}
2929

30+
public string FormatDescription(
31+
string emptyDescription,
32+
string singleDescription,
33+
string multipleDescription
34+
)
35+
{
36+
switch (Count)
37+
{
38+
case 0:
39+
return emptyDescription;
40+
case 1:
41+
return $"{singleDescription} {Description}";
42+
}
43+
return $"{multipleDescription} {Description}";
44+
}
45+
3046
private bool Equals(ObjectProvider<T> other)
3147
{
32-
return string.Equals(Description, other.Description);
48+
return _objects.SequenceEqual(other._objects);
3349
}
3450

3551
public override bool Equals(object obj)
@@ -49,7 +65,12 @@ public override bool Equals(object obj)
4965

5066
public override int GetHashCode()
5167
{
52-
return Description != null ? Description.GetHashCode() : 0;
68+
return _objects != null
69+
? _objects.Aggregate(
70+
0,
71+
(current, obj) => (current * 397) ^ (obj?.GetHashCode() ?? 0)
72+
)
73+
: 0;
5374
}
5475
}
5576
}

ArchUnitNET/Fluent/SystemTypeObjectProvider.cs renamed to ArchUnitNET/Domain/SystemTypeObjectProvider.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace ArchUnitNET.Fluent
88
{
9-
public class SystemTypeObjectProvider<T> : ISizedObjectProvider<T>
9+
internal class SystemTypeObjectProvider<T> : ISizedObjectProvider<T>
1010
where T : IType
1111
{
1212
private readonly List<Type> _types;
@@ -37,9 +37,25 @@ public IEnumerable<T> GetObjects(Architecture architecture)
3737
);
3838
}
3939

40+
public string FormatDescription(
41+
string emptyDescription,
42+
string singleDescription,
43+
string multipleDescription
44+
)
45+
{
46+
switch (Count)
47+
{
48+
case 0:
49+
return emptyDescription;
50+
case 1:
51+
return $"{singleDescription} {Description}";
52+
}
53+
return $"{multipleDescription} {Description}";
54+
}
55+
4056
private bool Equals(SystemTypeObjectProvider<T> other)
4157
{
42-
return string.Equals(Description, other.Description);
58+
return _types.SequenceEqual(other._types);
4359
}
4460

4561
public override bool Equals(object obj)
@@ -59,7 +75,12 @@ public override bool Equals(object obj)
5975

6076
public override int GetHashCode()
6177
{
62-
return Description != null ? Description.GetHashCode() : 0;
78+
return _types != null
79+
? _types.Aggregate(
80+
0,
81+
(current, type) => (current * 397) ^ (type?.GetHashCode() ?? 0)
82+
)
83+
: 0;
6384
}
6485
}
6586
}

ArchUnitNET/Fluent/BasicObjectProvider.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ public IEnumerable<T> GetObjects(Architecture architecture)
2222
return architecture.GetOrCreateObjects(this, _objects);
2323
}
2424

25+
public string FormatDescription(
26+
string emptyDescription,
27+
string singleDescription,
28+
string multipleDescription
29+
)
30+
{
31+
return $"{multipleDescription} {Description}";
32+
}
33+
2534
public override string ToString()
2635
{
2736
return Description;

ArchUnitNET/Fluent/Slices/GivenSlices.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,14 @@ public IEnumerable<Slice> GetObjects(Architecture architecture)
2424
{
2525
return _ruleCreator.GetSlices(architecture);
2626
}
27+
28+
public string FormatDescription(
29+
string emptyDescription,
30+
string singleDescription,
31+
string multipleDescription
32+
)
33+
{
34+
return $"{multipleDescription} {Description}";
35+
}
2736
}
2837
}

ArchUnitNET/Fluent/Syntax/DescriptionHelpers.cs

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

ArchUnitNET/Fluent/Syntax/Elements/GivenObjects.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ public IEnumerable<TRuleType> GetObjects(Architecture architecture)
3030
}
3131
}
3232

33+
public string FormatDescription(
34+
string emptyDescription,
35+
string singleDescription,
36+
string multipleDescription
37+
)
38+
{
39+
return $"{multipleDescription} {Description}";
40+
}
41+
3342
public TRuleTypeThat That()
3443
{
3544
return Create<TRuleTypeThat, TRuleType>(_ruleCreator);

ArchUnitNET/Fluent/Syntax/Elements/GivenObjectsConjunctionWithDescription.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ public IEnumerable<TRuleType> GetObjects(Architecture architecture)
3232
}
3333
}
3434

35+
public string FormatDescription(
36+
string emptyDescription,
37+
string singleDescription,
38+
string multipleDescription
39+
)
40+
{
41+
return $"{multipleDescription} {Description}";
42+
}
43+
3544
public TGivenRuleTypeThat And()
3645
{
3746
_ruleCreator.AddPredicateConjunction(LogicalConjunctionDefinition.And);

0 commit comments

Comments
 (0)