Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 13 additions & 4 deletions src/MSTest.Analyzer/MT1001CodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,18 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
private static SyntaxNode AddTestUsingDirective(SyntaxNode root)
{
var compilationUnit = root.AncestorsAndSelf().OfType<CompilationUnitSyntax>().First();
var updated = compilationUnit.AddUsings(
SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(MSTestConstants.Namespace)));
var updatedCompilationUnit = compilationUnit;

return root.ReplaceNode(compilationUnit, updated);
var existing = compilationUnit.Usings
.FirstOrDefault(x => x.GetText().ToString().Contains(MSTestConstants.Namespace));

if (existing == null)
{
updatedCompilationUnit = compilationUnit.AddUsings(
SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(MSTestConstants.Namespace)));
}

return root.ReplaceNode(compilationUnit, updatedCompilationUnit);
}

private static async Task<Document> AddTestClassAttributeAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
Expand All @@ -72,13 +80,14 @@ private static async Task<Document> AddTestClassAttributeAsync(Document document
.OfType<ClassDeclarationSyntax>()
.First();

var leadingTrivia = node.GetLeadingTrivia();
var attributes = node.AttributeLists.Add(
SyntaxFactory.AttributeList(
SyntaxFactory.SingletonSeparatedList<AttributeSyntax>(
SyntaxFactory.Attribute(
SyntaxFactory.IdentifierName(MSTestConstants.TestClass.Replace("Attribute", string.Empty)))))
.WithTrailingTrivia(SyntaxFactory.Whitespace("\r\n")));
root = root.ReplaceNode(node, node.WithAttributeLists(attributes));
root = root.ReplaceNode(node, node.WithoutLeadingTrivia().WithAttributeLists(attributes).WithLeadingTrivia(leadingTrivia));

// Add the using directive
root = AddTestUsingDirective(root);
Expand Down
17 changes: 13 additions & 4 deletions src/MSTest.Analyzer/MT1003CodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,18 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
private static SyntaxNode AddTestUsingDirective(SyntaxNode root)
{
var compilationUnit = root.AncestorsAndSelf().OfType<CompilationUnitSyntax>().First();
var updated = compilationUnit.AddUsings(
SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(MSTestConstants.Namespace)));
var updatedCompilationUnit = compilationUnit;

return root.ReplaceNode(compilationUnit, updated);
var existing = compilationUnit.Usings
.FirstOrDefault(x => x.GetText().ToString().Contains(MSTestConstants.Namespace));

if (existing == null)
{
updatedCompilationUnit = compilationUnit.AddUsings(
SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(MSTestConstants.Namespace)));
}

return root.ReplaceNode(compilationUnit, updatedCompilationUnit);
}

private static async Task<Document> AddTestMethodAttributeAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
Expand All @@ -72,13 +80,14 @@ private static async Task<Document> AddTestMethodAttributeAsync(Document documen
.OfType<MethodDeclarationSyntax>()
.First();

var leadingTrivia = node.GetLeadingTrivia();
var attributes = node.AttributeLists.Add(
SyntaxFactory.AttributeList(
SyntaxFactory.SingletonSeparatedList<AttributeSyntax>(
SyntaxFactory.Attribute(
SyntaxFactory.IdentifierName(MSTestConstants.TestMethod.Replace("Attribute", string.Empty)))))
.WithTrailingTrivia(SyntaxFactory.Whitespace("\r\n")));
root = root.ReplaceNode(node, node.WithAttributeLists(attributes));
root = root.ReplaceNode(node, node.WithoutLeadingTrivia().WithAttributeLists(attributes).WithLeadingTrivia(leadingTrivia));

// Add the using directive
root = AddTestUsingDirective(root);
Expand Down
46 changes: 46 additions & 0 deletions test/MSTest.Analyzer.Tests/MT1001UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,52 @@ public class TypeName
this.VerifyCSharpFix(test, fixtest);
}

/// <summary>
/// Tests reformatting is correct with documentation blocks
/// </summary>
[TestMethod]
public void ReformatsCorrectlyWithDocumentation()
{
var test = @"
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestPackage
{
/// <summary>
/// Tests the <see cref=""TypeName""/> class.
/// </summary>
public class TypeName
{
}
}";

var expected = new DiagnosticResult
{
Id = "MT1001",
Message = "The class 'TypeName' should be marked with the [TestClass] attribute",
Severity = DiagnosticSeverity.Warning,
Location = new DiagnosticResultLocation("Test0.cs", 9, 18)
};

this.VerifyCSharpDiagnostic(test, expected);

var fixtest = @"
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestPackage
{
/// <summary>
/// Tests the <see cref=""TypeName""/> class.
/// </summary>
[TestClass]
public class TypeName
{
}
}";

this.VerifyCSharpFix(test, fixtest);
}

/// <summary>
/// Tests with an invalid structure.
/// </summary>
Expand Down
60 changes: 60 additions & 0 deletions test/MSTest.Analyzer.Tests/MT1003UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,66 @@ public void MethodName()
this.VerifyCSharpFix(test, fixtest);
}

/// <summary>
/// Tests reformatting is correct with documentation blocks
/// </summary>
[TestMethod]
public void ReformatsCorrectlyWithDocumentation()
{
var test = @"
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyTest
{
/// <summary>
/// Tests the <see cref=""MyClass""/> class.
/// </summary>
[TestClass]
public class MyClassTest
{
/// <summary>
/// Tests the <see cref=""MyClass.Constructor""/> constructor.
/// </summary>
public void Constructor()
{
}
}
}";

var expected = new DiagnosticResult
{
Id = "MT1003",
Message = "The method 'MyClassTest.Constructor' is public and should be marked with one of the test attribute",
Severity = DiagnosticSeverity.Warning,
Location = new DiagnosticResultLocation("Test0.cs", 15, 21)
};

this.VerifyCSharpDiagnostic(test, expected);

var fixtest = @"
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyTest
{
/// <summary>
/// Tests the <see cref=""MyClass""/> class.
/// </summary>
[TestClass]
public class MyClassTest
{
/// <summary>
/// Tests the <see cref=""MyClass.Constructor""/> constructor.
/// </summary>
[TestMethod]
public void Constructor()
{
}
}
}";

this.VerifyCSharpFix(test, fixtest);
}

/// <summary>
/// Creates a new instance of the CSharp diagnostic analyzer begin tested.
/// </summary>
Expand Down