Skip to content

Add regression tests for parse error DiagnosticMarkers #1872

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

Merged
merged 2 commits into from
Aug 4, 2022
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 @@ -76,5 +76,29 @@ await analysisService
.ConfigureAwait(true);
Assert.Single(scriptFile.DiagnosticMarkers);
}

[Fact]
public async Task DoesNotClearParseErrorsAsync()
{
// Causing a missing closing } parser error
ScriptFile scriptFile = workspaceService.GetFileBuffer("untitled:Untitled-2", script.TrimEnd('}'));
ScriptFile[] scriptFiles = { scriptFile };

await analysisService
.DelayThenInvokeDiagnosticsAsync(scriptFiles, CancellationToken.None)
.ConfigureAwait(true);

Assert.Collection(scriptFile.DiagnosticMarkers,
(actual) =>
{
Assert.Equal("Missing closing '}' in statement block or type definition.", actual.Message);
Assert.Equal("PowerShell", actual.Source);
},
(actual) =>
{
Assert.Equal("PSUseSingularNouns", actual.RuleName);
Assert.Equal("PSScriptAnalyzer", actual.Source);
});
}
}
}
42 changes: 42 additions & 0 deletions test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,48 @@ public void CanDeleteFromEndOfFile()
);
}

[Trait("Category", "ScriptFile")]
[Fact]
public void UpdatesParseErrorDiagnosticMarkers()
{
ScriptFile myScript = CreateScriptFile(TestUtilities.NormalizeNewlines("{\n{"));

// Verify parse errors were detected on file open
Assert.Collection(myScript.DiagnosticMarkers.OrderBy(dm => dm.ScriptRegion.StartLineNumber),
(actual) =>
{
Assert.Equal(1, actual.ScriptRegion.StartLineNumber);
Assert.Equal("Missing closing '}' in statement block or type definition.", actual.Message);
Assert.Equal("PowerShell", actual.Source);
},
(actual) =>
{
Assert.Equal(2, actual.ScriptRegion.StartLineNumber);
Assert.Equal("Missing closing '}' in statement block or type definition.", actual.Message);
Assert.Equal("PowerShell", actual.Source);
});

// Remove second {
myScript.ApplyChange(
new FileChange
{
Line = 2,
EndLine = 2,
Offset = 1,
EndOffset = 2,
InsertString = ""
});

// Verify parse errors were updated on file change
Assert.Collection(myScript.DiagnosticMarkers,
(actual) =>
{
Assert.Equal(1, actual.ScriptRegion.StartLineNumber);
Assert.Equal("Missing closing '}' in statement block or type definition.", actual.Message);
Assert.Equal("PowerShell", actual.Source);
});
}

internal static ScriptFile CreateScriptFile(string initialString)
{
using StringReader stringReader = new(initialString);
Expand Down