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

Tests: Enable analysis of multiple test markers in a test file #953

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,29 @@ public void NoDiagnosticAtMarker(string markup, string diagnosticId)
NoDiagnostic(document, diagnosticId, codeMarkup.Locator);
}

public void NoDiagnosticAtAllMarkers(string markup, string diagnosticId)
{
var codeMarkup = new CodeMarkup(markup);
var document = CreateDocumentFromCode(codeMarkup.Code);
NoDiagnostic(document, [diagnosticId], codeMarkup.AllLocators);
}

public void NoDiagnostic(Document document, string diagnosticId, IDiagnosticLocator locator = null)
{
NoDiagnostic(document, new[] { diagnosticId }, locator);
}

public void NoDiagnostic(Document document, string[] diagnosticIds, IDiagnosticLocator locator = null)
{
NoDiagnostic(document, diagnosticIds, [locator]);
}

public void NoDiagnostic(Document document, string[] diagnosticIds, List<IDiagnosticLocator> locators)
{
var diagnostics = GetDiagnostics(document);
if (locator != null)
if (locators != null)
{
diagnostics = diagnostics.Where(x => locator.Match(x.Location)).ToImmutableArray();
diagnostics = diagnostics.Where(d => locators.Any(locator => locator.Match(d.Location))).ToImmutableArray();
}

var unexpectedDiagnostics = diagnostics.Where(d => diagnosticIds.Contains(d.Id)).ToList();
Expand All @@ -75,6 +87,13 @@ public void HasDiagnostic(string markupCode, string diagnosticId)
HasDiagnostic(document, diagnosticId, markup.Locator);
}

public void HasDiagnosticAtAllMarkers(string markupCode, string diagnosticId)
{
var markup = new CodeMarkup(markupCode);
var document = CreateDocumentFromCode(markup.Code);
HasDiagnostic(document, diagnosticId, markup.AllLocators);
}

public void HasDiagnosticAtLine(string code, string diagnosticId, int lineNumber)
{
var document = CreateDocumentFromCode(code);
Expand All @@ -96,12 +115,19 @@ public void HasDiagnostic(Document document, string diagnosticId, TextSpan span)

private void HasDiagnostic(Document document, string diagnosticId, IDiagnosticLocator locator)
{
var reporteddiagnostics = GetDiagnostics(document).Where(d => locator.Match(d.Location)).ToArray();
var matchedDiagnostics = reporteddiagnostics.Count(d => d.Id == diagnosticId);
HasDiagnostic(document, diagnosticId, [locator]);
}

if (matchedDiagnostics == 0)
private void HasDiagnostic(Document document, string diagnosticId, List<IDiagnosticLocator> locators)
{
var allDiagnostics = GetDiagnostics(document);
foreach (var locator in locators)
{
throw RoslynTestKitException.DiagnosticNotFound(diagnosticId, locator, reporteddiagnostics);
var reportedDiagnostics = allDiagnostics.Where(d => locator.Match(d.Location)).ToArray();
if (!reportedDiagnostics.Any(d => d.Id == diagnosticId))
{
throw RoslynTestKitException.DiagnosticNotFound(diagnosticId, locator, reportedDiagnostics);
}
}
}

Expand Down
39 changes: 39 additions & 0 deletions BusinessCentral.LinterCop.Test/RoslynTestKit/CodeMarkup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public CodeMarkup(string markup)
{
Code = markup.Replace("[|", "").Replace("|]", "");
Locator = GetLocator(markup);
AllLocators = GetAllLocators(markup);
}

private static IDiagnosticLocator GetLocator(string markupCode)
Expand All @@ -21,6 +22,21 @@ private static IDiagnosticLocator GetLocator(string markupCode)
throw new Exception("Cannot find any position marked with [||]");
}

private static List<IDiagnosticLocator> GetAllLocators(string markupCode)
{
List<IDiagnosticLocator> locators = new List<IDiagnosticLocator>();
int startIndex = 0, markers = 0;
while (TryFindMarkedTimeSpan(markupCode, ref startIndex, ref markers, out var textSpan))
{
locators.Add(new TextSpanLocator(textSpan));
}

if (markers == 0)
throw new Exception("Cannot find any position marked with [||]");

return locators;
}

private static bool TryFindMarkedTimeSpan(string markupCode, out TextSpan textSpan)
{
textSpan = default;
Expand All @@ -40,8 +56,31 @@ private static bool TryFindMarkedTimeSpan(string markupCode, out TextSpan textSp
return true;
}

private static bool TryFindMarkedTimeSpan(string markupCode, ref int startIndex, ref int markers, out TextSpan textSpan)
{
textSpan = default;
var start = markupCode.IndexOf("[|", startIndex, StringComparison.InvariantCulture);
if (start < 0)
{
return false;
}

var end = markupCode.IndexOf("|]", startIndex, StringComparison.InvariantCulture);
if (end < 0)
{
return false;
}

// textspans for code without the [| and |] markers
textSpan = TextSpan.FromBounds(start - (4 * markers), end - (4 * markers) - 2);
markers++;
startIndex = end + 2;
return true;
}

public IDiagnosticLocator Locator { get; }

public List<IDiagnosticLocator> AllLocators { get; }

public string Code { get; }
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0001.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task HasDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0001FlowFieldsShouldNotBeEditable>();
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0001FlowFieldsShouldNotBeEditable.Id);
fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0001FlowFieldsShouldNotBeEditable.Id);
}

[Test]
Expand All @@ -34,6 +34,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0001FlowFieldsShouldNotBeEditable>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0001FlowFieldsShouldNotBeEditable.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0001FlowFieldsShouldNotBeEditable.Id);
}
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0002.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task HasDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0002CommitMustBeExplainedByComment>();
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0002CommitMustBeExplainedByComment.Id);
fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0002CommitMustBeExplainedByComment.Id);
}

[Test]
Expand All @@ -33,6 +33,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0002CommitMustBeExplainedByComment>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0002CommitMustBeExplainedByComment.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0002CommitMustBeExplainedByComment.Id);
}
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0003.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task HasDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0003DoNotUseObjectIDsInVariablesOrProperties>();
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties.Id);
fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties.Id);
}

[Test]
Expand All @@ -30,6 +30,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0003DoNotUseObjectIDsInVariablesOrProperties>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties.Id);
}
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0004.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task HasDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0004LookupPageIdAndDrillDownPageId>();
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0004LookupPageIdAndDrillDownPageId.Id);
fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0004LookupPageIdAndDrillDownPageId.Id);
}

[Test]
Expand All @@ -31,6 +31,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0004LookupPageIdAndDrillDownPageId>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0004LookupPageIdAndDrillDownPageId.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0004LookupPageIdAndDrillDownPageId.Id);
}
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0005.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task HasDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0005VariableCasingShouldNotDifferFromDeclaration>();
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration.Id);
fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration.Id);
}

[Test]
Expand All @@ -33,6 +33,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0005VariableCasingShouldNotDifferFromDeclaration>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration.Id);
}
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0006.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task HasDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0006FieldNotAutoIncrementInTemporaryTable>();
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0006FieldNotAutoIncrementInTemporaryTable.Id);
fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0006FieldNotAutoIncrementInTemporaryTable.Id);
}

[Test]
Expand All @@ -30,6 +30,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0006FieldNotAutoIncrementInTemporaryTable>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0006FieldNotAutoIncrementInTemporaryTable.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0006FieldNotAutoIncrementInTemporaryTable.Id);
}
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0007.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void Setup()
// .ConfigureAwait(false);

// var fixture = RoslynFixtureFactory.Create<Rule0007DataPerCompanyShouldAlwaysBeSet>();
// fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0007DataPerCompanyShouldAlwaysBeSet.Id);
// fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0007DataPerCompanyShouldAlwaysBeSet.Id);
// }

[Test]
Expand All @@ -31,6 +31,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0007DataPerCompanyShouldAlwaysBeSet>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0007DataPerCompanyShouldAlwaysBeSet.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0007DataPerCompanyShouldAlwaysBeSet.Id);
}
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0008.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task HasDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0008NoFilterOperatorsInSetRange>();
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0008NoFilterOperatorsInSetRange.Id);
fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0008NoFilterOperatorsInSetRange.Id);
}

[Test]
Expand All @@ -31,6 +31,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0008NoFilterOperatorsInSetRange>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0008NoFilterOperatorsInSetRange.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0008NoFilterOperatorsInSetRange.Id);
}
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0011.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void Setup()
// .ConfigureAwait(false);

// var fixture = RoslynFixtureFactory.Create<Rule0011DataPerCompanyShouldAlwaysBeSet>();
// fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0011AccessPropertyShouldAlwaysBeSet.Id);
// fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0011AccessPropertyShouldAlwaysBeSet.Id);
// }

[Test]
Expand All @@ -31,6 +31,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0011DataPerCompanyShouldAlwaysBeSet>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0011AccessPropertyShouldAlwaysBeSet.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0011AccessPropertyShouldAlwaysBeSet.Id);
}
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0012.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task HasDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0012DoNotUseObjectIdInSystemFunctions>();
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0012DoNotUseObjectIdInSystemFunctions.Id);
fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0012DoNotUseObjectIdInSystemFunctions.Id);
}

[Test]
Expand All @@ -30,6 +30,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0012DoNotUseObjectIdInSystemFunctions>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0012DoNotUseObjectIdInSystemFunctions.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0012DoNotUseObjectIdInSystemFunctions.Id);
}
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0013.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task HasDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0013CheckForNotBlankOnSingleFieldPrimaryKeys>();
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0013CheckForNotBlankOnSingleFieldPrimaryKeys.Id);
fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0013CheckForNotBlankOnSingleFieldPrimaryKeys.Id);
}

[Test]
Expand All @@ -33,6 +33,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0013CheckForNotBlankOnSingleFieldPrimaryKeys>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0013CheckForNotBlankOnSingleFieldPrimaryKeys.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0013CheckForNotBlankOnSingleFieldPrimaryKeys.Id);
}
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0014.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task HasDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0014PermissionSetCaptionLength>();
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0014PermissionSetCaptionLength.Id);
fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0014PermissionSetCaptionLength.Id);
}

[Test]
Expand All @@ -33,6 +33,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0014PermissionSetCaptionLength>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0014PermissionSetCaptionLength.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0014PermissionSetCaptionLength.Id);
}
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0015.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void Setup()
// .ConfigureAwait(false);

// var fixture = RoslynFixtureFactory.Create<Rule0015PermissionSetCoverage>();
// fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0015PermissionSetCoverage.Id);
// fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0015PermissionSetCoverage.Id);
// }

[Test]
Expand All @@ -31,6 +31,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0015PermissionSetCoverage>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0015PermissionSetCoverage.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0015PermissionSetCoverage.Id);
}
}
4 changes: 2 additions & 2 deletions BusinessCentral.LinterCop.Test/Rule0016.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task HasDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0016CheckForMissingCaptions>();
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0016CheckForMissingCaptions.Id);
fixture.HasDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0016CheckForMissingCaptions.Id);
}

[Test]
Expand All @@ -46,6 +46,6 @@ public async Task NoDiagnostic(string testCase)
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0016CheckForMissingCaptions>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0016CheckForMissingCaptions.Id);
fixture.NoDiagnosticAtAllMarkers(code, DiagnosticDescriptors.Rule0016CheckForMissingCaptions.Id);
}
}
Loading