Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some handy warnings about non-static coyote test methods. #239

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions Source/Test/SystematicTesting/TestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Coyote.Actors;
using Microsoft.Coyote.IO;
Expand Down Expand Up @@ -156,8 +157,30 @@ private static (Delegate testMethod, string testName) GetTestMethod(Assembly ass
}
else if (testMethods.Count is 0)
{
throw new InvalidOperationException("Cannot detect a Coyote test method declared with the " +
$"'[{typeof(TestAttribute).FullName}]' attribute.");
// see if user forgot to make it static!
flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
testMethods = FindTestMethodsWithAttribute(typeof(TestAttribute), flags, assembly);
StringBuilder sb = new StringBuilder();
if (testMethods.Count > 0)
{
foreach (var method in testMethods)
{
string wrong = "non-static";
if (!method.IsPublic)
{
wrong += " and non-public";
}

sb.AppendLine($"Ignoring Coyote test method '{method.DeclaringType.Name + "." + method.Name}' because it is {wrong}");
}

throw new InvalidOperationException($"The following Coyote test methods cannot be used :\n{sb}");
}
else
{
throw new InvalidOperationException($"{sb}Cannot detect a static Coyote test method declared with the " +
$"'[{typeof(TestAttribute).FullName}]' attribute.");
}
}

MethodInfo testMethod = testMethods[0];
Expand Down Expand Up @@ -235,6 +258,13 @@ private static MethodInfo GetTestSetupMethod(Assembly assembly, Type attribute)

if (testMethods.Count is 0)
{
flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
testMethods = FindTestMethodsWithAttribute(attribute, flags, assembly);
if (testMethods.Count > 0)
{
throw new InvalidOperationException($"Test setup methods must be static and public: '{testMethods[0].Name}'.");
lovettchris marked this conversation as resolved.
Show resolved Hide resolved
}

return null;
}
else if (testMethods.Count > 1)
Expand Down Expand Up @@ -294,8 +324,9 @@ private static List<MethodInfo> FindTestMethodsWithAttribute(Type attribute, Bin

try
{
var name = attribute.FullName; // so we can test an assembly built against a different version of Coyote.exe.
lovettchris marked this conversation as resolved.
Show resolved Hide resolved
testMethods = assembly.GetTypes().SelectMany(t => t.GetMethods(bindingFlags)).
Where(m => m.GetCustomAttributes(attribute, false).Length > 0).ToList();
Where(m => m.GetCustomAttributes(attribute, false).Any()).ToList();
}
catch (ReflectionTypeLoadException ex)
{
Expand Down