Skip to content

Commit

Permalink
Adjust the test to check the test method and naming.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed May 20, 2024
1 parent 68eaff6 commit 4467102
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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<Class>().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();
Expand Down
6 changes: 6 additions & 0 deletions osu.Game.Rulesets.Karaoke.Architectures/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +68,11 @@ public static bool HasAttributeInSelfOrChild(this Class @class, Attribute attrib

#region Name

public static IEnumerable<IType> 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,
Expand Down

0 comments on commit 4467102

Please sign in to comment.