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

MSTEST0032: Always pass assertions #3238

Merged
merged 40 commits into from
Jul 11, 2024
Merged
Changes from 6 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
3d76129
add notnull to 0025
engyebrahim Jul 9, 2024
9db6a2b
fix
engyebrahim Jul 9, 2024
902207e
Merge branch 'main' into Enji/fix-0025
engyebrahim Jul 9, 2024
0891a57
fix
engyebrahim Jul 9, 2024
76f453a
add analyzer
engyebrahim Jul 9, 2024
6b17618
fix
engyebrahim Jul 9, 2024
2cb4f5e
add to api
engyebrahim Jul 9, 2024
4d52546
Merge branch 'main' into Enji/fix-0025
engyebrahim Jul 10, 2024
769723b
update helpers
engyebrahim Jul 10, 2024
42b73b2
refactor
engyebrahim Jul 10, 2024
f8e1a57
fix
engyebrahim Jul 10, 2024
d7f7cda
Merge branch 'Enji/fix-0025' of https://github.com/microsoft/testfx i…
engyebrahim Jul 10, 2024
d63a499
add field tests
engyebrahim Jul 10, 2024
41ef2b5
fix build
engyebrahim Jul 10, 2024
17e57d9
add annotatin tests
engyebrahim Jul 10, 2024
5ac5a01
testbase class
engyebrahim Jul 10, 2024
05a82d1
make var local
engyebrahim Jul 10, 2024
b6e2514
Merge branch 'main' into Enji/fix-0025
engyebrahim Jul 10, 2024
5f4e875
Merge branch 'Enji/fix-0025' into Enji/always-pass-0032
engyebrahim Jul 10, 2024
d6e9643
remove c ode
engyebrahim Jul 10, 2024
38ad546
fix it
engyebrahim Jul 10, 2024
1484e41
spaces
engyebrahim Jul 10, 2024
fd00d00
uupdate code
engyebrahim Jul 10, 2024
cc852b6
use nyllable context
engyebrahim Jul 10, 2024
e5ba818
fix
engyebrahim Jul 11, 2024
3ca1df5
add more test
engyebrahim Jul 11, 2024
aae4e66
Merge branch 'main' into Enji/fix-0025
engyebrahim Jul 11, 2024
8ff813b
Merge branch 'Enji/fix-0025' into Enji/always-pass-0032
engyebrahim Jul 11, 2024
0fc59b9
fix analyzer
engyebrahim Jul 11, 2024
53fac98
Update src/Analyzers/MSTest.Analyzers/RoslynAnalyzerHelpers/ISymbolEx…
Evangelink Jul 11, 2024
98abc89
Merge branch 'Enji/fix-0025' into Enji/always-pass-0032
engyebrahim Jul 11, 2024
cb51736
added tests
engyebrahim Jul 11, 2024
6fecdf4
Merge branch 'Enji/always-pass-0032' of https://github.com/microsoft/…
engyebrahim Jul 11, 2024
6ca9459
add link
engyebrahim Jul 11, 2024
df3f22f
fix naming
engyebrahim Jul 11, 2024
08ba886
Merge branch 'main' into Enji/always-pass-0032
engyebrahim Jul 11, 2024
fea46f3
rename
engyebrahim Jul 11, 2024
d1c39a0
update naming
engyebrahim Jul 11, 2024
16a311e
update messages
engyebrahim Jul 11, 2024
a878825
Merge branch 'main' into Enji/always-pass-0032
engyebrahim Jul 11, 2024
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
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ private static void AnalyzeOperation(OperationAnalysisContext context, INamedTyp
var operation = (IInvocationOperation)context.Operation;

if (assertSymbol.Equals(operation.TargetMethod.ContainingType, SymbolEqualityComparer.Default) &&
IsAlwaysFalse(operation, nullableSymbol))
IsAlwaysFalse(operation, nullableSymbol))
{
context.ReportDiagnostic(operation.CreateDiagnostic(Rule, operation.TargetMethod.Name));
}
@@ -84,22 +84,16 @@ private static bool IsAlwaysFalse(IInvocationOperation operation, INamedTypeSymb
"AreEqual" => GetEqualityStatus(operation, ExpectedParameterName) == EqualityStatus.NotEqual,
"AreNotEqual" => GetEqualityStatus(operation, NotExpectedParameterName) == EqualityStatus.Equal,
"IsNotNull" => GetValueArgument(operation) is { Value.ConstantValue: { HasValue: true, Value: null } },
"IsNull" => CheckIsNull(operation, nullableSymbol),
"IsNull" => GetValueArgument(operation) is { } valueArgumentOperation && IsNotNullableType(valueArgumentOperation, nullableSymbol),
_ => false,
};

private static bool CheckIsNull(IInvocationOperation operation, INamedTypeSymbol? nullableSymbol)
private static bool IsNotNullableType(IArgumentOperation valueArgumentOperation, INamedTypeSymbol? nullableSymbol)
{
if (nullableSymbol is null)
{
return false;
}

IArgumentOperation? valueArg = GetValueArgument(operation);
ITypeSymbol? valueArgType = valueArg?.Value.GetReferencedMemberOrLocalOrParameter().GetReferencedMemberOrLocalOrParameter();

return !SymbolEqualityComparer.IncludeNullability.Equals(valueArgType?.OriginalDefinition, nullableSymbol)
&& valueArgType?.NullableAnnotation != NullableAnnotation.Annotated;
ITypeSymbol? valueArgType = valueArgumentOperation.Value.GetReferencedMemberOrLocalOrParameter().GetReferencedMemberOrLocalOrParameter();
return valueArgType is not null
&& valueArgType.NullableAnnotation == NullableAnnotation.NotAnnotated
&& !SymbolEqualityComparer.IncludeNullability.Equals(valueArgType.OriginalDefinition, nullableSymbol);
}

private static IArgumentOperation? GetArgumentWithName(IInvocationOperation operation, string name)
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ public async Task WhenIsNullAssertion_ValueParameterIsNullable_NoDiagnostic()
{
string code = """
using Microsoft.VisualStudio.TestTools.UnitTesting;
#nullable enable
[TestClass]
public class MyTestClass
{
@@ -34,7 +34,7 @@ public async Task WhenIsNullAssertion_ValueParameterIsNotNullable_Diagnostic()
{
string code = """
using Microsoft.VisualStudio.TestTools.UnitTesting;
#nullable enable
[TestClass]
public class MyTestClass
{
@@ -54,7 +54,7 @@ public async Task WhenIsNullAssertion_ValueParameterAsPropertySymbolIsNotNullabl
{
string code = """
using Microsoft.VisualStudio.TestTools.UnitTesting;
#nullable enable
[TestClass]
public class MyTestClass
{
@@ -75,7 +75,7 @@ public async Task WhenIsNullAssertion_ValueParameterAsPropertySymbolIsNullable_N
{
string code = """
using Microsoft.VisualStudio.TestTools.UnitTesting;
#nullable enable
[TestClass]
public class MyTestClass
{
@@ -96,7 +96,7 @@ public async Task WhenIsNullAssertion_ValueParameterAsReferenceObjectIsNotNullab
{
string code = """
using Microsoft.VisualStudio.TestTools.UnitTesting;
#nullable enable
[TestClass]
public class TestClass
{
@@ -117,6 +117,30 @@ public class ObjectClass
await VerifyCS.VerifyAnalyzerAsync(code);
}

public async Task WhenIsNullAssertion_ValueParameterAsReferenceObjectIsNotNullable_WithoutEnableNullable_NoDiagnostic()
{
string code = """
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class TestClass
{
[TestMethod]
public void Test()
{
ObjectClass obj = new ObjectClass();
Assert.IsNull(obj);
}
}
public class ObjectClass
{
}
""";

await VerifyCS.VerifyAnalyzerAsync(code);
}

public async Task WhenIsNullAssertion_ValueParameterAsReferenceObjectIsNullable_NoDiagnostic()
{
string code = """
Original file line number Diff line number Diff line change
@@ -153,6 +153,7 @@ MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.PreferAssertFailOverAlwaysFalse
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.PreferAssertFailOverAlwaysFalseConditionsAnalyzerTests.WhenIsNullAssertion_ValueParameterAsReferenceObjectIsNullable_NoDiagnostic()
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.PreferAssertFailOverAlwaysFalseConditionsAnalyzerTests.WhenIsNullAssertion_ValueParameterIsNotNullable_Diagnostic()
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.PreferAssertFailOverAlwaysFalseConditionsAnalyzerTests.WhenIsNullAssertion_ValueParameterIsNullable_NoDiagnostic()
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.PreferAssertFailOverAlwaysFalseConditionsAnalyzerTests.WhenIsNullAssertion_ValueParameterAsReferenceObjectIsNotNullable_WithoutEnableNullable_NoDiagnostic()
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.PreferConstructorOverTestInitializeAnalyzerTests.WhenTestClassHasCtor_NoDiagnostic()
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.PreferConstructorOverTestInitializeAnalyzerTests.WhenTestClassHasTestInitialize_Diagnostic()
MSTest.Analyzers.UnitTests.MSTest.Analyzers.Test.PreferConstructorOverTestInitializeAnalyzerTests.WhenTestClassHasTestInitializeAndCtor_Diagnostic()