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

Simplify HasCorrectTimeout #4042

Merged
merged 7 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
14 changes: 7 additions & 7 deletions src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ private TestAssemblyInfo GetAssemblyInfo(Type type)
TimeoutAttribute? timeoutAttribute = _reflectionHelper.GetFirstNonDerivedAttributeOrDefault<TimeoutAttribute>(methodInfo, inherit: false);
if (timeoutAttribute != null)
{
if (!methodInfo.HasCorrectTimeout(timeoutAttribute))
if (!timeoutAttribute.HasCorrectTimeout)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorInvalidTimeout, methodInfo.DeclaringType!.FullName, methodInfo.Name);
throw new TypeInspectionException(message);
Expand All @@ -437,7 +437,7 @@ private TestAssemblyInfo GetAssemblyInfo(Type type)
TimeoutAttribute? timeoutAttribute = _reflectionHelper.GetFirstNonDerivedAttributeOrDefault<TimeoutAttribute>(methodInfo, inherit: false);
if (timeoutAttribute != null)
{
if (!methodInfo.HasCorrectTimeout(timeoutAttribute))
if (!timeoutAttribute.HasCorrectTimeout)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorInvalidTimeout, methodInfo.DeclaringType!.FullName, methodInfo.Name);
throw new TypeInspectionException(message);
Expand Down Expand Up @@ -578,7 +578,7 @@ private void UpdateInfoIfClassInitializeOrCleanupMethod(
TimeoutAttribute? timeoutAttribute = _reflectionHelper.GetFirstNonDerivedAttributeOrDefault<TimeoutAttribute>(methodInfo, inherit: false);
if (timeoutAttribute != null)
{
if (!methodInfo.HasCorrectTimeout(timeoutAttribute))
if (!timeoutAttribute.HasCorrectTimeout)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorInvalidTimeout, methodInfo.DeclaringType!.FullName, methodInfo.Name);
throw new TypeInspectionException(message);
Expand Down Expand Up @@ -611,7 +611,7 @@ private void UpdateInfoIfClassInitializeOrCleanupMethod(
TimeoutAttribute? timeoutAttribute = _reflectionHelper.GetFirstNonDerivedAttributeOrDefault<TimeoutAttribute>(methodInfo, inherit: false);
if (timeoutAttribute != null)
{
if (!methodInfo.HasCorrectTimeout(timeoutAttribute))
if (!timeoutAttribute.HasCorrectTimeout)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorInvalidTimeout, methodInfo.DeclaringType!.FullName, methodInfo.Name);
throw new TypeInspectionException(message);
Expand Down Expand Up @@ -677,7 +677,7 @@ private void UpdateInfoIfTestInitializeOrCleanupMethod(
TimeoutAttribute? timeoutAttribute = _reflectionHelper.GetFirstNonDerivedAttributeOrDefault<TimeoutAttribute>(methodInfo, inherit: false);
if (timeoutAttribute != null)
{
if (!methodInfo.HasCorrectTimeout(timeoutAttribute))
if (!timeoutAttribute.HasCorrectTimeout)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorInvalidTimeout, methodInfo.DeclaringType!.FullName, methodInfo.Name);
throw new TypeInspectionException(message);
Expand Down Expand Up @@ -708,7 +708,7 @@ private void UpdateInfoIfTestInitializeOrCleanupMethod(
TimeoutAttribute? timeoutAttribute = _reflectionHelper.GetFirstNonDerivedAttributeOrDefault<TimeoutAttribute>(methodInfo, inherit: false);
if (timeoutAttribute != null)
{
if (!methodInfo.HasCorrectTimeout(timeoutAttribute))
if (!timeoutAttribute.HasCorrectTimeout)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorInvalidTimeout, methodInfo.DeclaringType!.FullName, methodInfo.Name);
throw new TypeInspectionException(message);
Expand Down Expand Up @@ -875,7 +875,7 @@ private TimeoutInfo GetTestTimeout(MethodInfo methodInfo, TestMethod testMethod)

if (timeoutAttribute != null)
{
if (!methodInfo.HasCorrectTimeout(timeoutAttribute))
if (!timeoutAttribute.HasCorrectTimeout)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorInvalidTimeout, testMethod.FullClassName, testMethod.Name);
throw new TypeInspectionException(message);
Expand Down
16 changes: 0 additions & 16 deletions src/Adapter/MSTest.TestAdapter/Extensions/MethodInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,6 @@ internal static bool HasCorrectTestMethodSignature(this MethodInfo method, bool
method.IsValidReturnType(); // Match return type Task for async methods only. Else return type void.
}

/// <summary>
/// Checks whether test method has correct Timeout attribute.
/// </summary>
/// <param name="method">The method to verify.</param>
/// <param name="timeoutAttribute">The timeout attribute when we already have it.</param>
/// <returns>True if the method has the right test timeout signature.</returns>
internal static bool HasCorrectTimeout(this MethodInfo method, TimeoutAttribute? timeoutAttribute = null)
{
DebugEx.Assert(method != null, "method should not be null.");

// TODO: redesign this, probably change this to GetTimeout? so we don't have to do this weird dance?
timeoutAttribute ??= ReflectHelper.Instance.GetFirstNonDerivedAttributeOrDefault<TimeoutAttribute>(method, inherit: false);

return timeoutAttribute?.Timeout > 0;
}

/// <summary>
/// Check is return type is void for non async and Task for async methods.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ public sealed class TimeoutAttribute : Attribute
/// <param name="timeout">
/// The timeout in milliseconds.
/// </param>
public TimeoutAttribute(int timeout) => Timeout = timeout;
public TimeoutAttribute(int timeout)
{
Timeout = timeout;
}
nohwnd marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Initializes a new instance of the <see cref="TimeoutAttribute"/> class with a preset timeout.
/// </summary>
/// <param name="timeout">
/// The timeout.
/// </param>
public TimeoutAttribute(TestTimeout timeout) => Timeout = (int)timeout;
public TimeoutAttribute(TestTimeout timeout)
: this((int)timeout)
{
}
nohwnd marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets the timeout in milliseconds.
Expand All @@ -52,5 +58,10 @@ public bool CooperativeCancellation
}
}

/// <summary>
/// Gets a value indicating whether the instance has the correct test timeout signature.
/// </summary>
internal bool HasCorrectTimeout => Timeout > 0;

internal bool IsCooperativeCancellationSet { get; private set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,28 +244,6 @@ public void HasCorrectTestMethodSignatureShouldReturnFalseForAsyncTestMethodsWit

#endregion

#region HasCorrectTimeout tests

public void HasCorrectTimeoutShouldReturnFalseForMethodsWithoutTimeoutAttribute()
{
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("PublicMethod");
Verify(!methodInfo.HasCorrectTimeout());
}

public void HasCorrectTimeoutShouldReturnFalseForMethodsWithInvalidTimeoutAttribute()
{
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("PublicMethodWithInvalidTimeout");
Verify(!methodInfo.HasCorrectTimeout());
}

public void HasCorrectTimeoutShouldReturnTrueForMethodsWithTimeoutAttribute()
{
MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("PublicMethodWithTimeout");
Verify(methodInfo.HasCorrectTimeout());
}
nohwnd marked this conversation as resolved.
Show resolved Hide resolved

#endregion

#region IsVoidOrTaskReturnType tests

public void IsVoidOrTaskReturnTypeShouldReturnTrueForVoidMethods()
Expand Down
Loading