Skip to content

Commit

Permalink
#16: I added some sample convention rules for convention #testing
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 18, 2022
1 parent 675ed61 commit d3e65d1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Contracts/Synergy.Convention.Testing/Rules/Deficit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Reflection;
using JetBrains.Annotations;
using Synergy.Contracts;

namespace Synergy.Convention.Testing.Rules
{
public struct Deficit
{
public MemberInfo Member { get; }
public string Description { get; }

private Type DeclaringType => Member.DeclaringType.OrFail(nameof(Member.DeclaringType))!;
public Assembly Assembly => DeclaringType.Assembly;
public string MemberName => FullNameOfMember();

public Deficit(MemberInfo member, string description)
{
this.Member = member.OrFail(nameof(member));
this.Description = description.OrFail(nameof(description));
}

public override string ToString()
{
return $"{MemberName}{Environment.NewLine}- {this.Description}";
}

[NotNull, Pure]
private string FullNameOfMember()
{
if (Member is Type type)
return type.FullName.OrFailIfWhiteSpace(nameof(type.FullName));

var fullName = DeclaringType.FullName;

if (Member is PropertyInfo)
return $"{fullName}.{Member.Name}";

return $"{fullName}.{Member.Name}()";
}
}
}
23 changes: 23 additions & 0 deletions Contracts/Synergy.Convention.Testing/Rules/SealedRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;

namespace Synergy.Convention.Testing.Rules
{
public static class SealedRule
{
public static IEnumerable<Deficit> MustBeSealed(this Type type)
{
if (type.IsSealed == false)
yield return new Deficit(type, "sealed");
}

public static IEnumerable<Deficit> MustBeSealedOrAbstract(this Type type)
{
if (type.IsAbstract)
yield break;

foreach (var deficit in type.MustBeSealed())
yield return deficit;
}
}
}

0 comments on commit d3e65d1

Please sign in to comment.