Skip to content

Commit

Permalink
BenMorris/NetArchTest#120 - Add support for records and init-only pro…
Browse files Browse the repository at this point in the history
…perties
  • Loading branch information
NeVeSpl committed May 13, 2023
1 parent b84c1db commit ca5a3ca
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,26 @@ static internal class PropertyDefinitionExtensions
/// <returns>An indication of whether the property is readonly.</returns>
public static bool IsReadonly(this PropertyDefinition propertyDefinition)
{
return propertyDefinition.SetMethod == null;
if (propertyDefinition.SetMethod == null)
{
return true;
}
else
{
if (propertyDefinition.IsInitOnly())
{
return true;
}
}

return false;
}


public static bool IsInitOnly(this PropertyDefinition propertyDefinition)
{
return propertyDefinition.SetMethod?.ReturnType.FullName == "System.Void modreq(System.Runtime.CompilerServices.IsExternalInit)";
}

/// <summary>
/// Tests whether a property is nullable
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/NetArchTest.Rules/NetArchTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Mono.Cecil" Version="0.11.4" />
<PackageReference Include="Mono.Cecil" Version="0.11.5" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/NetArchTest.Rules.UnitTests/ConditionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ public void AreMutable_MatchesFound_ClassSelected()
.That()
.ResideInNamespace("NetArchTest.TestStructure.Mutability")
.And()
.DoNotHaveNameStartingWith("ImmutableClass")
.DoNotHaveNameStartingWith("Immutable")
.Should()
.BeMutable().GetResult();

Expand Down
3 changes: 2 additions & 1 deletion test/NetArchTest.Rules.UnitTests/PredicateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -656,10 +656,11 @@ public void AreImmutable_MatchesFound_ClassSelected()
.And()
.AreImmutable().GetReflectionTypes();

Assert.Equal(3, result.Count()); // Three types found
Assert.Equal(4, result.Count()); // Three types found

This comment has been minimized.

Copy link
@szyb

szyb May 31, 2023

It's not very constructive, but the comment should be also changed to "Four types found"

This comment has been minimized.

Copy link
@NeVeSpl

NeVeSpl May 31, 2023

Author Owner

This comment should not exist in the first place, because it does not provide any additional knowledge.

There are a lot of such small mistakes in the code base, but I do not see any business value in fixing them.

Assert.Contains<Type>(typeof(ImmutableClass1), result);
Assert.Contains<Type>(typeof(ImmutableClass2), result);
Assert.Contains<Type>(typeof(ImmutableClass3), result);
Assert.Contains<Type>(typeof(ImmutableRecord1), result);
}

[Fact(DisplayName = "Types can be selected for being mutable.")]
Expand Down
4 changes: 4 additions & 0 deletions test/NetArchTest.TestStructure/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("NetArchTest.UnitTests")]

namespace System.Runtime.CompilerServices
{
internal static class IsExternalInit { }
}
10 changes: 10 additions & 0 deletions test/NetArchTest.TestStructure/Mutability/ImmutableRecord1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NetArchTest.TestStructure.Mutability
{
public record ImmutableRecord1(int Property)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>9.0</LangVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit ca5a3ca

Please sign in to comment.