Skip to content

Commit 7868fcb

Browse files
committed
Add more reliable asserts to DebugServiceTests
1 parent 847d2ee commit 7868fcb

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ await this.debugService.SetLineBreakpoints(
239239
BreakpointDetails.Create("", 10)
240240
});
241241

242-
Assert.Equal(2, breakpoints.Length);
242+
var confirmedBreakpoints = await this.GetConfirmedBreakpoints(this.debugScriptFile);
243+
244+
Assert.Equal(2, confirmedBreakpoints.Count());
243245
Assert.Equal(5, breakpoints[0].LineNumber);
244246
Assert.Equal(10, breakpoints[1].LineNumber);
245247

@@ -248,15 +250,20 @@ await this.debugService.SetLineBreakpoints(
248250
this.debugScriptFile,
249251
new[] { BreakpointDetails.Create("", 2) });
250252

251-
Assert.Equal(1, breakpoints.Length);
253+
confirmedBreakpoints = await this.GetConfirmedBreakpoints(this.debugScriptFile);
254+
255+
Assert.Equal(1, confirmedBreakpoints.Count());
252256
Assert.Equal(2, breakpoints[0].LineNumber);
253257

254-
breakpoints =
255-
await this.debugService.SetLineBreakpoints(
256-
this.debugScriptFile,
257-
new[] { BreakpointDetails.Create("", 0) });
258+
await this.debugService.SetLineBreakpoints(
259+
this.debugScriptFile,
260+
new[] { BreakpointDetails.Create("", 0) });
258261

259-
Assert.Equal(0, breakpoints.Length);
262+
var remainingBreakpoints = await this.GetConfirmedBreakpoints(this.debugScriptFile);
263+
264+
Assert.False(
265+
remainingBreakpoints.Any(),
266+
"Breakpoints in the script file were not cleared");
260267
}
261268

262269
[Fact]
@@ -458,15 +465,24 @@ await this.debugService.SetLineBreakpoints(
458465
[Fact]
459466
public async Task DebuggerBreaksWhenRequested()
460467
{
468+
var confirmedBreakpoints = await this.GetConfirmedBreakpoints(this.debugScriptFile);
469+
470+
await this.AssertStateChange(
471+
PowerShellContextState.Ready,
472+
PowerShellExecutionResult.Completed);
473+
474+
Assert.False(
475+
confirmedBreakpoints.Any(),
476+
"Unexpected breakpoint found in script file");
477+
461478
Task executeTask =
462479
this.powerShellContext.ExecuteScriptString(
463480
this.debugScriptFile.FilePath);
464481

465482
// Break execution and wait for the debugger to stop
466483
this.debugService.Break();
467484

468-
// File path is an empty string when paused while running
469-
await this.AssertDebuggerStopped(string.Empty);
485+
await this.AssertDebuggerPaused();
470486
await this.AssertStateChange(
471487
PowerShellContextState.Ready,
472488
PowerShellExecutionResult.Stopped);
@@ -885,6 +901,16 @@ await this.debugService.SetLineBreakpoints(
885901
this.powerShellContext.AbortExecution();
886902
}
887903

904+
public async Task AssertDebuggerPaused()
905+
{
906+
SynchronizationContext syncContext = SynchronizationContext.Current;
907+
908+
DebuggerStoppedEventArgs eventArgs =
909+
await this.debuggerStoppedQueue.DequeueAsync();
910+
911+
Assert.Equal(0, eventArgs.OriginalEvent.Breakpoints.Count);
912+
}
913+
888914
public async Task AssertDebuggerStopped(
889915
string scriptPath,
890916
int lineNumber = -1)
@@ -894,6 +920,8 @@ public async Task AssertDebuggerStopped(
894920
DebuggerStoppedEventArgs eventArgs =
895921
await this.debuggerStoppedQueue.DequeueAsync();
896922

923+
924+
897925
Assert.Equal(scriptPath, eventArgs.ScriptPath);
898926
if (lineNumber > -1)
899927
{
@@ -911,6 +939,15 @@ private async Task AssertStateChange(
911939
Assert.Equal(expectedState, newState.NewSessionState);
912940
Assert.Equal(expectedResult, newState.ExecutionResult);
913941
}
942+
943+
private async Task<IEnumerable<LineBreakpoint>> GetConfirmedBreakpoints(ScriptFile scriptFile)
944+
{
945+
return
946+
await this.powerShellContext.ExecuteCommand<LineBreakpoint>(
947+
new PSCommand()
948+
.AddCommand("Get-PSBreakpoint")
949+
.AddParameter("Script", scriptFile.FilePath));
950+
}
914951
}
915952
}
916953

0 commit comments

Comments
 (0)