From 446710295c0a3bbbac8ea26ff757e1a452f12a95 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 19 May 2024 13:23:16 +0800 Subject: [PATCH] Adjust the test to check the test method and naming. --- .../Edit/Checks/TestCheckTest.cs | 32 +++++++++++++++++-- .../Extensions.cs | 6 ++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/osu.Game.Rulesets.Karaoke.Architectures/Edit/Checks/TestCheckTest.cs b/osu.Game.Rulesets.Karaoke.Architectures/Edit/Checks/TestCheckTest.cs index c3f93d33e..5721fd9d7 100644 --- a/osu.Game.Rulesets.Karaoke.Architectures/Edit/Checks/TestCheckTest.cs +++ b/osu.Game.Rulesets.Karaoke.Architectures/Edit/Checks/TestCheckTest.cs @@ -5,6 +5,7 @@ using ArchUnitNET.Domain; using ArchUnitNET.Domain.Extensions; using NUnit.Framework; +using osu.Game.Rulesets.Edit.Checks.Components; using osu.Game.Rulesets.Karaoke.Tests.Editor.Checks; namespace osu.Game.Rulesets.Karaoke.Architectures.Edit.Checks; @@ -47,9 +48,10 @@ public void CheckShouldContainsTest() [Test] [Project.KaraokeTest(true)] - public void CheckTestMethod() + public void CheckTestClassAndMethod() { - var architecture = GetProjectArchitecture(); + var architecture = GetProjectArchitecture(new Project.KaraokeAttribute()); + var baseCheck = architecture.GetInterfaceOfType(typeof(ICheck)); var baseCheckTest = architecture.GetClassOfType(typeof(BaseCheckTest<>)); var assertOkMethod = baseCheckTest.GetMethodMembersContainsName("AssertOk").FirstOrDefault(); @@ -66,17 +68,41 @@ public void CheckTestMethod() foreach (var checkTest in allCheckTests) { + // check the class naming. + Assert.IsTrue(isTestClassValid(checkTest, baseCheck), $"Test class {checkTest} should have correct naming"); + + // check the test method naming in the test case. var testMethods = checkTest.GetAllTestMembers(architecture).ToArray(); - Assert.NotZero(testMethods.Length, $"No test method in the {checkTest}"); + Assert.NotZero(testMethods.Length, $"No test method in the {checkTest}"); foreach (var testMethod in testMethods) { + Assert.IsTrue(isTestNamingValid(testMethod), $"Test method {testMethod} should have correct naming"); Assert.IsTrue(isTestMethod(testMethod), $"Test method {testMethod} should call {assertOkMethod} or {assertNotOkMethod} method."); } } return; + static bool isTestClassValid(Class testClass, Interface baseCheck) + { + var testCheck = testClass.GetGenericTypes().OfType().First(x => x.ImplementsInterface(baseCheck)); + return testClass.NameStartsWith(testCheck.Name); + } + + static bool isTestNamingValid(IMember testMethod) + { + var calledMethods = testMethod.GetMethodCallDependencies().FirstOrDefault(x => x.TargetMember.NameStartsWith("AssertNotOk")); + + if (calledMethods != null) + { + // todo: should get the generic type from the AssertNotOk method. + return testMethod.NameStartsWith("IssueTemplate"); + } + + return true; + } + static bool isTestMethod(IMember testMethod) { var calledMethods = testMethod.GetCalledMethods().ToArray(); diff --git a/osu.Game.Rulesets.Karaoke.Architectures/Extensions.cs b/osu.Game.Rulesets.Karaoke.Architectures/Extensions.cs index 2b118dc3c..aa6eee30b 100644 --- a/osu.Game.Rulesets.Karaoke.Architectures/Extensions.cs +++ b/osu.Game.Rulesets.Karaoke.Architectures/Extensions.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Text.RegularExpressions; using ArchUnitNET.Domain; +using ArchUnitNET.Domain.Dependencies; using ArchUnitNET.Domain.Extensions; using NUnit.Framework; using osu.Game.Rulesets.Karaoke.Tests; @@ -67,6 +68,11 @@ public static bool HasAttributeInSelfOrChild(this Class @class, Attribute attrib #region Name + public static IEnumerable GetGenericTypes(this Class @class) + { + return @class.GetInheritsBaseClassDependencies().SelectMany(x => x.TargetGenericArguments.Select(arg => arg.Type)); + } + public static bool RelativeNameStartsWith( this IHasName cls, Project.ProjectAttribute project,