-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#16: I added some sample convention rules for convention #testing
- Loading branch information
1 parent
675ed61
commit d3e65d1
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}()"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |