Skip to content

Commit

Permalink
refactor: fix minor code issues (#73)
Browse files Browse the repository at this point in the history
Cleanup code, fix code issues and increase test coverage

---------

Co-authored-by: Valentin Breuß <[email protected]>
  • Loading branch information
vbreuss and vbtig authored May 31, 2023
1 parent e91b5b5 commit 57ce2ce
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 93 deletions.
1 change: 1 addition & 0 deletions Source/Testably.Architecture.Rules/ExclusionLists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class ExclusionLists
{
"mscorlib",
"System",
"Microsoft",
"xunit"
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,28 @@ public static bool HasAccessModifier(
this ConstructorInfo constructorInfo,
AccessModifiers accessModifiers)
{
if (constructorInfo.IsAssembly)
if (accessModifiers.HasFlag(AccessModifiers.Internal) &&
constructorInfo.IsAssembly)
{
return accessModifiers.HasFlag(AccessModifiers.Internal);
return true;
}

if (constructorInfo.IsFamily)
if (accessModifiers.HasFlag(AccessModifiers.Protected) &&
constructorInfo.IsFamily)
{
return accessModifiers.HasFlag(AccessModifiers.Protected);
return true;
}

if (constructorInfo.IsPrivate)
if (accessModifiers.HasFlag(AccessModifiers.Private) &&
constructorInfo.IsPrivate)
{
return accessModifiers.HasFlag(AccessModifiers.Private);
return true;
}

if (constructorInfo.IsPublic)
if (accessModifiers.HasFlag(AccessModifiers.Public) &&
constructorInfo.IsPublic)
{
return accessModifiers.HasFlag(AccessModifiers.Public);
return true;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,28 @@ public static bool HasAccessModifier(
this FieldInfo fieldInfo,
AccessModifiers accessModifiers)
{
if (fieldInfo.IsAssembly)
if (accessModifiers.HasFlag(AccessModifiers.Internal) &&
fieldInfo.IsAssembly)
{
return accessModifiers.HasFlag(AccessModifiers.Internal);
return true;
}

if (fieldInfo.IsFamily)
if (accessModifiers.HasFlag(AccessModifiers.Protected) &&
fieldInfo.IsFamily)
{
return accessModifiers.HasFlag(AccessModifiers.Protected);
return true;
}

if (fieldInfo.IsPrivate)
if (accessModifiers.HasFlag(AccessModifiers.Private) &&
fieldInfo.IsPrivate)
{
return accessModifiers.HasFlag(AccessModifiers.Private);
return true;
}

if (fieldInfo.IsPublic)
if (accessModifiers.HasFlag(AccessModifiers.Public) &&
fieldInfo.IsPublic)
{
return accessModifiers.HasFlag(AccessModifiers.Public);
return true;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,28 @@ public static bool HasAccessModifier(
this MethodInfo methodInfo,
AccessModifiers accessModifiers)
{
if (methodInfo.IsAssembly)
if (accessModifiers.HasFlag(AccessModifiers.Internal) &&
methodInfo.IsAssembly)
{
return accessModifiers.HasFlag(AccessModifiers.Internal);
return true;
}

if (methodInfo.IsFamily)
if (accessModifiers.HasFlag(AccessModifiers.Protected) &&
methodInfo.IsFamily)
{
return accessModifiers.HasFlag(AccessModifiers.Protected);
return true;
}

if (methodInfo.IsPrivate)
if (accessModifiers.HasFlag(AccessModifiers.Private) &&
methodInfo.IsPrivate)
{
return accessModifiers.HasFlag(AccessModifiers.Private);
return true;
}

if (methodInfo.IsPublic)
if (accessModifiers.HasFlag(AccessModifiers.Public) &&
methodInfo.IsPublic)
{
return accessModifiers.HasFlag(AccessModifiers.Public);
return true;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,6 @@ namespace Testably.Architecture.Rules;
/// </summary>
public static class PropertyInfoExtensions
{
///// <summary>
///// Checks if the <paramref name="propertyInfo" /> has the specified <paramref name="accessModifiers" />.
///// </summary>
///// <param name="propertyInfo">The <see cref="PropertyInfo" /> which is checked to have the attribute.</param>
///// <param name="accessModifiers">
///// The <see cref="AccessModifiers" />.
///// <para />
///// Supports specifying multiple <see cref="AccessModifiers" />.
///// </param>
//public static bool HasAccessModifier(
// this PropertyInfo propertyInfo,
// AccessModifiers accessModifiers)
//{
// if (propertyInfo.IsAssembly)
// {
// return accessModifiers.HasFlag(AccessModifiers.Internal);
// }

// if (propertyInfo.IsFamily)
// {
// return accessModifiers.HasFlag(AccessModifiers.Protected);
// }

// if (propertyInfo.IsPrivate)
// {
// return accessModifiers.HasFlag(AccessModifiers.Private);
// }

// if (propertyInfo.IsPublic)
// {
// return accessModifiers.HasFlag(AccessModifiers.Public);
// }

// return false;
//}

/// <summary>
/// Checks if the <paramref name="propertyInfo" /> has an attribute which satisfies the <paramref name="predicate" />.
/// </summary>
Expand Down
39 changes: 21 additions & 18 deletions Source/Testably.Architecture.Rules/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static MethodInfo[] GetDeclaredMethods(
.GetMethods(BindingFlags.DeclaredOnly |
BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Static |
BindingFlags.Instance)
.Where(m => !m.IsSpecialName)
.ToArray();
Expand Down Expand Up @@ -287,24 +288,28 @@ private static bool AreGenericTypesCompatible(Type type, Type other)

private static bool HasAccessModifierForNestedClass(Type type, AccessModifiers accessModifiers)
{
if (type.IsNestedAssembly)
if (accessModifiers.HasFlag(AccessModifiers.Internal) &&
type.IsNestedAssembly)
{
return accessModifiers.HasFlag(AccessModifiers.Internal);
return true;
}

if (type.IsNestedFamily)
if (accessModifiers.HasFlag(AccessModifiers.Protected) &&
type.IsNestedFamily)
{
return accessModifiers.HasFlag(AccessModifiers.Protected);
return true;
}

if (type.IsNestedPrivate)
if (accessModifiers.HasFlag(AccessModifiers.Private) &&
type.IsNestedPrivate)
{
return accessModifiers.HasFlag(AccessModifiers.Private);
return true;
}

if (type.IsNestedPublic)
if (accessModifiers.HasFlag(AccessModifiers.Public) &&
type.IsNestedPublic)
{
return accessModifiers.HasFlag(AccessModifiers.Public);
return true;
}

return false;
Expand All @@ -313,21 +318,19 @@ private static bool HasAccessModifierForNestedClass(Type type, AccessModifiers a
private static bool HasAccessModifierForNotNestedClass(Type type,
AccessModifiers accessModifiers)
{
if (!type.IsVisible)
{
return accessModifiers.HasFlag(AccessModifiers.Internal);
}

if (type.IsNotPublic)
if (accessModifiers.HasFlag(AccessModifiers.Internal) &&
!type.IsVisible)
{
return accessModifiers.HasFlag(AccessModifiers.Private);
return true;
}

if (type.IsPublic)
if (accessModifiers.HasFlag(AccessModifiers.Public) &&
type.IsPublic)
{
return accessModifiers.HasFlag(AccessModifiers.Public);
return true;
}

return false;
return accessModifiers.HasFlag(AccessModifiers.Private) &&
type.IsNotPublic;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface IOrderedParameterFilterResult
/// <summary>
/// Specifies an explicit position of the parameter.
/// <para />
/// Positive values are zero-based index from the start.<br />
/// Non-negative values are zero-based index from the start.<br />
/// Negative values count from the last parameter back (e.g. `-1` indicates the last parameter).
/// </summary>
IParameterFilter<IOrderedParameterFilterResult> At(int position);
Expand Down
15 changes: 13 additions & 2 deletions Source/Testably.Architecture.Rules/Parameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ public static IParameterFilter<IUnorderedParameterFilterResult> Any
/// <see cref="ParameterInfo" />s in the correct order.
/// </summary>
public static IParameterFilter<IOrderedParameterFilterResult> First
=> new ParameterInOrderFilter();
=> At(0);

/// <summary>
/// Specifies a decrementing series of parameter filters starting at the last parameter that must be satisfied by the
/// <see cref="ParameterInfo" />s in the correct order.
/// </summary>
public static IParameterFilter<IOrderedParameterFilterResult> Last
=> new ParameterInOrderFilter().At(-1);
=> At(-1);

/// <summary>
/// Specifies a series of parameter filters starting at the specified <paramref name="position" /> that must be
/// satisfied by the
/// <see cref="ParameterInfo" />s in the correct order.
/// <para />
/// Non-negative values start an incrementing series from a zero-based index from the start.<br />
/// Negative values start a decrementing series from the last parameter back (e.g. `-1` indicates the last parameter).
/// </summary>
public static IParameterFilter<IOrderedParameterFilterResult> At(int position)
=> new ParameterInOrderFilter().At(position);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private class TestClassWithProtectedField
private class TestClassWithPrivateField
{
// ReSharper disable once InconsistentNaming
private int PrivateTestField = 1;
private readonly int PrivateTestField = 1;
}
#pragma warning restore CS0414
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ public sealed partial class ParameterFilterExtensionsTests
{
public sealed class OfTypeTests
{
[Theory]
[InlineData(typeof(Foo), typeof(Bar), true)]
[InlineData(typeof(Bar), typeof(Foo), false)]
public void OfType_Ordered_At_ShouldGoToNextParameter(Type type1, Type type2,
bool expectedValue)
{
IParameterFilter<IOrderedParameterFilterResult> sut = Parameters.At(1);

bool result = Have.Method
.With(sut
.OfType(type2)
.At(0)
.OfType(type1))
.Applies(typeof(TestClassWithMultipleParameters).GetDeclaredMethods().First());

result.Should().Be(expectedValue);
}

[Theory]
[InlineData(true, true)]
[InlineData(false, false)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using FluentAssertions;
using System;
using Testably.Architecture.Rules.Tests.TestHelpers;
using Xunit;

Expand All @@ -16,18 +17,22 @@ public sealed class AreTests
[InlineData(typeof(PublicTestClass), AccessModifiers.Public)]
[InlineData(typeof(UnnestedPublicType), AccessModifiers.Public)]
[InlineData(typeof(UnnestedInternalType), AccessModifiers.Internal)]
public void With_AccessModifiers_Matching_ShouldBeChecked(
public void WhichAre_AccessModifiers_Matching_ShouldBeChecked(
Type type,
AccessModifiers accessModifiers)
{
ITestResult result = Expect.That.Types
ITypeFilterResult sut = Expect.That.Types
.WhichAre(type).And
.WhichAre(accessModifiers)
.ShouldAlwaysFail()
.AllowEmpty()
.Check.InAllLoadedAssemblies();
.WhichAre(accessModifiers);

ITestResult result =
sut.ShouldSatisfy(_ => false)
.AllowEmpty()
.Check.InAllLoadedAssemblies();

result.ShouldBeViolated();
sut.ToString().Should()
.Contain($"with {accessModifiers} access modifier");
}

[Theory]
Expand All @@ -46,7 +51,7 @@ public void With_AccessModifiers_Matching_ShouldBeChecked(
[InlineData(typeof(PublicTestClass),
AccessModifiers.Protected | AccessModifiers.Internal)]
[InlineData(typeof(PublicTestClass), AccessModifiers.Private)]
public void With_AccessModifiers_NotMatching_ShouldNotBeChecked(
public void WhichAre_AccessModifiers_NotMatching_ShouldNotBeChecked(
Type type,
AccessModifiers accessModifiers)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public sealed class ParameterInOrderFilterTests
public void Apply_First_Then_ShouldIncrementIndex()
{
ParameterInfo[] parameters = typeof(ParameterInOrderFilterTests)
.GetMethod(nameof(DummyMethod))!
.GetDeclaredMethods()
.First(m => m.Name == nameof(DummyMethod))
.GetParameters();
IParameterFilter<IOrderedParameterFilterResult> sut = Parameters.First;

Expand All @@ -35,7 +36,8 @@ public void Apply_First_Then_ShouldIncrementIndex()
public void Apply_Last_Then_ShouldDecrementIndex()
{
ParameterInfo[] parameters = typeof(ParameterInOrderFilterTests)
.GetMethod(nameof(DummyMethod))!
.GetDeclaredMethods()
.First(m => m.Name == nameof(DummyMethod))
.GetParameters();
IParameterFilter<IOrderedParameterFilterResult> sut = Parameters.Last;

Expand Down Expand Up @@ -162,7 +164,7 @@ public void FriendlyName_ShouldJoinAllFilters(string filter1, string filter2)

#region Helpers

public void DummyMethod(int v1, int v2, int v3, int v4, int v5)
private static void DummyMethod(int v1, int v2, int v3, int v4, int v5)
{
// Do nothing
}
Expand Down
Loading

0 comments on commit 57ce2ce

Please sign in to comment.