From d3e65d10e8078a95d7bc1e3e2afcf22db281448c Mon Sep 17 00:00:00 2001 From: Marcin Celej Date: Fri, 18 Feb 2022 16:57:02 +0100 Subject: [PATCH] #16: I added some sample convention rules for convention #testing --- .../Rules/Deficit.cs | 42 +++++++++++++++++++ .../Rules/SealedRule.cs | 23 ++++++++++ 2 files changed, 65 insertions(+) create mode 100644 Contracts/Synergy.Convention.Testing/Rules/Deficit.cs create mode 100644 Contracts/Synergy.Convention.Testing/Rules/SealedRule.cs diff --git a/Contracts/Synergy.Convention.Testing/Rules/Deficit.cs b/Contracts/Synergy.Convention.Testing/Rules/Deficit.cs new file mode 100644 index 0000000..090079d --- /dev/null +++ b/Contracts/Synergy.Convention.Testing/Rules/Deficit.cs @@ -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}()"; + } + } +} \ No newline at end of file diff --git a/Contracts/Synergy.Convention.Testing/Rules/SealedRule.cs b/Contracts/Synergy.Convention.Testing/Rules/SealedRule.cs new file mode 100644 index 0000000..061e7ef --- /dev/null +++ b/Contracts/Synergy.Convention.Testing/Rules/SealedRule.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; + +namespace Synergy.Convention.Testing.Rules +{ + public static class SealedRule + { + public static IEnumerable MustBeSealed(this Type type) + { + if (type.IsSealed == false) + yield return new Deficit(type, "sealed"); + } + + public static IEnumerable MustBeSealedOrAbstract(this Type type) + { + if (type.IsAbstract) + yield break; + + foreach (var deficit in type.MustBeSealed()) + yield return deficit; + } + } +} \ No newline at end of file