Skip to content

Commit ca5a3ca

Browse files
committed
BenMorris/NetArchTest#120 - Add support for records and init-only properties
1 parent b84c1db commit ca5a3ca

File tree

7 files changed

+38
-5
lines changed

7 files changed

+38
-5
lines changed

src/NetArchTest.Rules/Extensions/Mono.Cecil/PropertyDefinitionExtensions.cs

+19-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,26 @@ static internal class PropertyDefinitionExtensions
1010
/// <returns>An indication of whether the property is readonly.</returns>
1111
public static bool IsReadonly(this PropertyDefinition propertyDefinition)
1212
{
13-
return propertyDefinition.SetMethod == null;
13+
if (propertyDefinition.SetMethod == null)
14+
{
15+
return true;
16+
}
17+
else
18+
{
19+
if (propertyDefinition.IsInitOnly())
20+
{
21+
return true;
22+
}
23+
}
24+
25+
return false;
1426
}
15-
27+
28+
public static bool IsInitOnly(this PropertyDefinition propertyDefinition)
29+
{
30+
return propertyDefinition.SetMethod?.ReturnType.FullName == "System.Void modreq(System.Runtime.CompilerServices.IsExternalInit)";
31+
}
32+
1633
/// <summary>
1734
/// Tests whether a property is nullable
1835
/// </summary>

src/NetArchTest.Rules/NetArchTest.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</PropertyGroup>
2222

2323
<ItemGroup>
24-
<PackageReference Include="Mono.Cecil" Version="0.11.4" />
24+
<PackageReference Include="Mono.Cecil" Version="0.11.5" />
2525
</ItemGroup>
2626

2727
<ItemGroup>

test/NetArchTest.Rules.UnitTests/ConditionTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ public void AreMutable_MatchesFound_ClassSelected()
624624
.That()
625625
.ResideInNamespace("NetArchTest.TestStructure.Mutability")
626626
.And()
627-
.DoNotHaveNameStartingWith("ImmutableClass")
627+
.DoNotHaveNameStartingWith("Immutable")
628628
.Should()
629629
.BeMutable().GetResult();
630630

test/NetArchTest.Rules.UnitTests/PredicateTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,11 @@ public void AreImmutable_MatchesFound_ClassSelected()
656656
.And()
657657
.AreImmutable().GetReflectionTypes();
658658

659-
Assert.Equal(3, result.Count()); // Three types found
659+
Assert.Equal(4, result.Count()); // Three types found
660660
Assert.Contains<Type>(typeof(ImmutableClass1), result);
661661
Assert.Contains<Type>(typeof(ImmutableClass2), result);
662662
Assert.Contains<Type>(typeof(ImmutableClass3), result);
663+
Assert.Contains<Type>(typeof(ImmutableRecord1), result);
663664
}
664665

665666
[Fact(DisplayName = "Types can be selected for being mutable.")]
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
using System.Runtime.CompilerServices;
22
[assembly: InternalsVisibleTo("NetArchTest.UnitTests")]
33

4+
namespace System.Runtime.CompilerServices
5+
{
6+
internal static class IsExternalInit { }
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace NetArchTest.TestStructure.Mutability
6+
{
7+
public record ImmutableRecord1(int Property)
8+
{
9+
}
10+
}

test/NetArchTest.TestStructure/NetArchTest.TestStructure.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
5+
<LangVersion>9.0</LangVersion>
56
</PropertyGroup>
67

78
<PropertyGroup>

0 commit comments

Comments
 (0)