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

Added warning: Async test method must have non-void return type #69

Closed
wants to merge 1 commit into from

Conversation

304NotModified
Copy link
Contributor

Fixes #7

{
class TestCaseUsageAnalyzerTestsAnalyzeWhenReturnTypeIsAsyncVoid
{
[Test]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note, this isn't a TestCase, but a Test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we will - at some time - need a test for both cases. But for now I think it is fine

@@ -5,6 +5,7 @@ internal static class TestCaseUsageAnalyzerConstants
internal const string ExpectedResultTypeMismatchMessage = "The ExpectedResult value cannot be assigned to the return type, {0}";
internal const string NotEnoughArgumentsMessage = "There are not enough arguments provided from the TestCaseAttribute for the method";
internal const string SpecifiedExpectedResultForVoidMethodMessage = "Cannot specify ExpectedResult when the method returns void";
internal const string AsyncVoidMessage = "Async test method must have non-void return type";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this should be in TestCaseUsageAnalyzerConstants

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need another class as most of the async validations will be for Test and TestCase attributes.

if (testCaseType.ContainingAssembly.Identity == attributeSymbol?.ContainingAssembly.Identity &&
NunitFrameworkConstants.NameOfTestCaseAttribute == attributeSymbol?.ContainingType.Name)
(isTestCase = NunitFrameworkConstants.NameOfTestCaseAttribute == attributeSymbol?.ContainingType.Name)
|| NunitFrameworkConstants.NameOfTestAttribute == attributeSymbol?.ContainingType.Name)
Copy link
Contributor Author

@304NotModified 304NotModified Aug 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this could be simplified,. note this is now checking for TestCase + Test attributes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I don't like assignments within boolean expressions (can be that I've not programmed enough C/C++). The plan is to have another analyzer that will run for both Test and TestCase attributes.

}

AnalyzeReturnType(context, methodSymbol);
Copy link
Contributor Author

@304NotModified 304NotModified Aug 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this will run with Test+TestCase

context.CancellationToken.ThrowIfCancellationRequested();
TestCaseUsageAnalyzer.AnalyzeNamedArguments(context,
attributeNamedArguments, methodSymbol);
}
Copy link
Contributor Author

@304NotModified 304NotModified Aug 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: nothing changed in this block above (lines 66-98)

@304NotModified
Copy link
Contributor Author

any opinion on this one?

@@ -27,6 +27,7 @@ public static class NunitFrameworkConstants
public const string FullNameOfTypeTestCaseAttribute = "NUnit.Framework.TestCaseAttribute";

public const string NameOfTestCaseAttribute = "TestCaseAttribute";
public const string NameOfTestAttribute = "TestAttribute";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've exactly the same line in my local changes 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 nice!

@@ -5,6 +5,7 @@ internal static class TestCaseUsageAnalyzerConstants
internal const string ExpectedResultTypeMismatchMessage = "The ExpectedResult value cannot be assigned to the return type, {0}";
internal const string NotEnoughArgumentsMessage = "There are not enough arguments provided from the TestCaseAttribute for the method";
internal const string SpecifiedExpectedResultForVoidMethodMessage = "Cannot specify ExpectedResult when the method returns void";
internal const string AsyncVoidMessage = "Async test method must have non-void return type";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need another class as most of the async validations will be for Test and TestCase attributes.

if (testCaseType.ContainingAssembly.Identity == attributeSymbol?.ContainingAssembly.Identity &&
NunitFrameworkConstants.NameOfTestCaseAttribute == attributeSymbol?.ContainingType.Name)
(isTestCase = NunitFrameworkConstants.NameOfTestCaseAttribute == attributeSymbol?.ContainingType.Name)
|| NunitFrameworkConstants.NameOfTestAttribute == attributeSymbol?.ContainingType.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I don't like assignments within boolean expressions (can be that I've not programmed enough C/C++). The plan is to have another analyzer that will run for both Test and TestCase attributes.

@@ -128,6 +136,22 @@ private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
}
}
}
private static void AnalyzeReturnType(SyntaxNodeAnalysisContext context, IMethodSymbol methodSymbol)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major nit pick. Insert empty line before method and remove the empty line beginning the statements within the if statement below

{
context.ReportDiagnostic(Diagnostic.Create(
TestCaseUsageAnalyzer.CreateDescriptor(
TestCaseUsageAnalyzerConstants.AsyncVoidMessage),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this also be mentioned in SupportedDiagnostics?

{
class TestCaseUsageAnalyzerTestsAnalyzeWhenReturnTypeIsAsyncVoid
{
[Test]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we will - at some time - need a test for both cases. But for now I think it is fine

@mikkelbu
Copy link
Member

mikkelbu commented Sep 5, 2018

Hi @304NotModified
Thanks for the contribution, and again sorry about the delay, but I've only had time to very little the last copuple of weeks. Overall, I think it looks good, but I prefer that all the async functionality is placed in a separate analyzer (to keep the classes smaller). As I noted in the issue (#7) I've also started working on one of the other async tasks (#48), and I hoped to be done with it 10 days ago, but since then no much has happened 😞.

But my "plan" is as follows:

  • Create a new async analyzer for both Test and TestCase attributes (to begin with mostly a copy of the existing TestCase analyzer, then we can always improve on it - since some of the result type checking will be different from non-async methods to async methods)
  • Add your changes on top of the new analyzer (I think we can use the test and the body of the analyzer - AnalyzeReturnType - more or less directly).

I've also added some minor comments in the code, but please don't spend time on them before I've pushed the skeleton for #48

@304NotModified
Copy link
Contributor Author

Thanks for the contribution, and again sorry about the delay, but I've only had time to very little the last copuple of weeks.

No problem! It's the nature of open source IMO 😄

Create a new async analyzer for both Test and TestCase attributes (to begin with mostly a copy of the existing TestCase analyzer, then we can always improve on it - since some of the result type checking will be different from non-async methods to async methods)

Could we create subclasses etc? e.g. a base class for the sharing functionality and the a subclass for Test attribute and and for testcase attribute?

Add your changes on top of the new analyzer (I think we can use the test and the body of the analyzer - AnalyzeReturnType - more or less directly).

If I need to rebase stuff, no problem. Just let me know

@mikkelbu
Copy link
Member

mikkelbu commented Dec 3, 2018

@304NotModified Once more I've left this hanging for much too long, and I'm sorry about that.
I think that the easiest solution would be to extend the method AnalyzeExpectedResult to also handle that there is no ExpectedResultAttribute, but that the return type is not null.
I'm unsure how much can be reapplied from this PR, and I can fully understand if you think that your time is better spent somewhere else given the lack of feedback from my side, but I'll try to increase the activity on this project again.

@304NotModified
Copy link
Contributor Author

304NotModified commented Dec 4, 2018

When I found time (i'm also very busy these days), I will create a new PR with the changes!

@mikkelbu mikkelbu added this to the Closed Without Action milestone Apr 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Look for async void tests
2 participants