Skip to content

Commit f853185

Browse files
bergmeisterrjmholt
authored andcommitted
Fix XUnit warnings that better assertion operators should be used. (#858)
* fix XUnit warnings that better assertion operators should be used. This also cleans up the build log
1 parent 2c5f8cf commit f853185

File tree

7 files changed

+42
-48
lines changed

7 files changed

+42
-48
lines changed

test/PowerShellEditorServices.Test.Host/LanguageServerTests.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ await this.WaitForEvent(
8282
PublishDiagnosticsNotification.Type);
8383

8484
// Was there a syntax error?
85-
Assert.NotEqual(0, diagnostics.Diagnostics.Length);
85+
Assert.NotEmpty(diagnostics.Diagnostics);
8686
Assert.False(
8787
string.IsNullOrEmpty(diagnostics.Diagnostics[0].Message));
8888
}
@@ -99,7 +99,7 @@ await this.WaitForEvent(
9999
PublishDiagnosticsNotification.Type);
100100

101101
// Was there a semantic error?
102-
Assert.NotEqual(0, diagnostics.Diagnostics.Length);
102+
Assert.NotEmpty(diagnostics.Diagnostics);
103103
Assert.Contains("unapproved", diagnostics.Diagnostics[0].Message);
104104
}
105105

@@ -115,7 +115,7 @@ await this.WaitForEvent(
115115
PublishDiagnosticsNotification.Type);
116116

117117
// Was there a syntax error?
118-
Assert.Equal(0, diagnostics.Diagnostics.Length);
118+
Assert.Empty(diagnostics.Diagnostics);
119119
}
120120

121121
[Fact]
@@ -140,7 +140,7 @@ await this.SendRequest(
140140
});
141141

142142
Assert.NotNull(completions);
143-
Assert.NotEqual(completions.Length, 0);
143+
Assert.NotEmpty(completions);
144144

145145
// TODO: Add more asserts
146146
}
@@ -279,7 +279,7 @@ await this.SendRequest(
279279
});
280280

281281
Assert.NotNull(locations);
282-
Assert.Equal(locations.Length, 3);
282+
Assert.Equal(3, locations.Length);
283283

284284
Assert.Equal(5, locations[0].Range.Start.Line);
285285
Assert.Equal(0, locations[0].Range.Start.Character);
@@ -311,7 +311,7 @@ await this.SendRequest(
311311
});
312312

313313
Assert.NotNull(locations);
314-
Assert.Equal(0, locations.Length);
314+
Assert.Empty(locations);
315315
}
316316

317317
[Fact]
@@ -400,7 +400,7 @@ await this.SendRequest(
400400
});
401401

402402
Assert.NotNull(locations);
403-
Assert.Equal(1, locations.Length);
403+
Assert.Single(locations);
404404
Assert.Equal(0, locations[0].Range.Start.Line);
405405
Assert.Equal(9, locations[0].Range.Start.Character);
406406
}
@@ -427,7 +427,7 @@ await this.SendRequest(
427427
});
428428

429429
Assert.NotNull(locations);
430-
Assert.Equal(0, locations.Length);
430+
Assert.Empty(locations);
431431
}
432432

433433
[Fact]
@@ -452,7 +452,7 @@ await this.SendRequest(
452452
});
453453

454454
Assert.NotNull(locations);
455-
Assert.Equal(1, locations.Length);
455+
Assert.Single(locations);
456456
Assert.Equal(5, locations[0].Range.Start.Line);
457457
Assert.Equal(0, locations[0].Range.Start.Character);
458458
Assert.Equal(5, locations[0].Range.End.Line);
@@ -481,7 +481,7 @@ await this.SendRequest(
481481
});
482482

483483
Assert.NotNull(locations);
484-
Assert.Equal(1, locations.Length);
484+
Assert.Single(locations);
485485
Assert.EndsWith("VariableDefinition.ps1", locations[0].Uri);
486486
Assert.Equal(0, locations[0].Range.Start.Line);
487487
Assert.Equal(0, locations[0].Range.Start.Character);
@@ -511,7 +511,7 @@ await this.SendRequest(
511511
});
512512

513513
Assert.NotNull(locations);
514-
Assert.Equal(1, locations.Length);
514+
Assert.Single(locations);
515515
Assert.EndsWith("FindReferences.ps1", locations[0].Uri);
516516
Assert.Equal(17, locations[0].Range.Start.Line);
517517
Assert.Equal(0, locations[0].Range.Start.Character);
@@ -567,7 +567,7 @@ await this.SendRequest(
567567
});
568568

569569
Assert.NotNull(signatureHelp);
570-
Assert.Equal(1, signatureHelp.Signatures.Length);
570+
Assert.Single(signatureHelp.Signatures);
571571
Assert.Equal(2, signatureHelp.Signatures[0].Parameters.Length);
572572
Assert.Equal(
573573
"Write-Output [-InputObject] <psobject[]> [-NoEnumerate] [<CommonParameters>]",

test/PowerShellEditorServices.Test.Protocol/Server/OutputDebouncerTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task OutputDebouncerAggregatesOutputEvents()
3838
await SendOutput(debouncer, "for great justice");
3939

4040
// Assert that there's only one event with the expected string
41-
Assert.Equal(1, messageSender.OutputEvents.Count);
41+
Assert.Single(messageSender.OutputEvents);
4242
Assert.Equal(
4343
TestUtilities.NormalizeNewlines("This is a test\nAnother line"),
4444
messageSender.OutputEvents[0].Output);

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ await this.debugService.SetCommandBreakpointsAsync(
178178
await this.debugService.SetCommandBreakpointsAsync(
179179
new[] { CommandBreakpointDetails.Create("Get-Host") });
180180

181-
Assert.Equal(1, breakpoints.Length);
181+
Assert.Single(breakpoints);
182182
Assert.Equal("Get-Host", breakpoints[0].Name);
183183

184184
breakpoints =
185185
await this.debugService.SetCommandBreakpointsAsync(
186186
new CommandBreakpointDetails[] {});
187187

188-
Assert.Equal(0, breakpoints.Length);
188+
Assert.Empty(breakpoints);
189189
}
190190

191191
[Fact]
@@ -258,7 +258,7 @@ await this.debugService.SetLineBreakpointsAsync(
258258

259259
confirmedBreakpoints = await this.GetConfirmedBreakpoints(this.debugScriptFile);
260260

261-
Assert.Equal(1, confirmedBreakpoints.Count());
261+
Assert.Single(confirmedBreakpoints);
262262
Assert.Equal(2, breakpoints[0].LineNumber);
263263

264264
await this.debugService.SetLineBreakpointsAsync(
@@ -843,8 +843,8 @@ await this.debugService.SetLineBreakpointsAsync(
843843
Assert.Equal(2, childVars.Count);
844844
Assert.Contains("Age", childVars.Keys);
845845
Assert.Contains("Name", childVars.Keys);
846-
Assert.Equal(childVars["Age"], "75");
847-
Assert.Equal(childVars["Name"], "\"John\"");
846+
Assert.Equal("75", childVars["Age"]);
847+
Assert.Equal("\"John\"", childVars["Name"]);
848848

849849
// Abort execution of the script
850850
this.powerShellContext.AbortExecution();
@@ -931,7 +931,7 @@ public async Task AssertDebuggerPaused()
931931
DebuggerStoppedEventArgs eventArgs =
932932
await this.debuggerStoppedQueue.DequeueAsync(new CancellationTokenSource(5000).Token);
933933

934-
Assert.Equal(0, eventArgs.OriginalEvent.Breakpoints.Count);
934+
Assert.Empty(eventArgs.OriginalEvent.Breakpoints);
935935
}
936936

937937
public async Task AssertDebuggerStopped(

test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,8 @@ await extensionService.PowerShellContext.ExecuteScriptStringAsync(
138138
await this.AssertExtensionEvent(EventType.Remove, "test.scriptblock");
139139

140140
// Ensure that the command has been unregistered
141-
await Assert.ThrowsAsync(
142-
typeof(KeyNotFoundException),
143-
() => extensionService.InvokeCommandAsync("test.scriptblock", this.commandContext));
141+
await Assert.ThrowsAsync<KeyNotFoundException>(() =>
142+
extensionService.InvokeCommandAsync("test.scriptblock", this.commandContext));
144143
}
145144

146145
private async Task<EditorCommand> AssertExtensionEvent(EventType expectedEventType, string expectedExtensionName)

test/PowerShellEditorServices.Test/Language/LanguageServiceTests.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async Task LanguageServiceCompletesCommandInFile()
4949
await this.GetCompletionResults(
5050
CompleteCommandInFile.SourceDetails);
5151

52-
Assert.NotEqual(0, completionResults.Completions.Length);
52+
Assert.NotEmpty(completionResults.Completions);
5353
Assert.Equal(
5454
CompleteCommandInFile.ExpectedCompletion,
5555
completionResults.Completions[0]);
@@ -62,7 +62,7 @@ public async Task LanguageServiceCompletesCommandFromModule()
6262
await this.GetCompletionResults(
6363
CompleteCommandFromModule.SourceDetails);
6464

65-
Assert.NotEqual(0, completionResults.Completions.Length);
65+
Assert.NotEmpty(completionResults.Completions);
6666
Assert.Equal(
6767
CompleteCommandFromModule.ExpectedCompletion,
6868
completionResults.Completions[0]);
@@ -75,7 +75,7 @@ public async Task LanguageServiceCompletesVariableInFile()
7575
await this.GetCompletionResults(
7676
CompleteVariableInFile.SourceDetails);
7777

78-
Assert.Equal(1, completionResults.Completions.Length);
78+
Assert.Single(completionResults.Completions);
7979
Assert.Equal(
8080
CompleteVariableInFile.ExpectedCompletion,
8181
completionResults.Completions[0]);
@@ -88,7 +88,7 @@ public async Task LanguageServiceCompletesAttributeValue()
8888
await this.GetCompletionResults(
8989
CompleteAttributeValue.SourceDetails);
9090

91-
Assert.NotEqual(0, completionResults.Completions.Length);
91+
Assert.NotEmpty(completionResults.Completions);
9292
Assert.Equal(
9393
CompleteAttributeValue.ExpectedRange,
9494
completionResults.ReplacedRange);
@@ -101,7 +101,7 @@ public async Task LanguageServiceCompletesFilePath()
101101
await this.GetCompletionResults(
102102
CompleteFilePath.SourceDetails);
103103

104-
Assert.NotEqual(0, completionResults.Completions.Length);
104+
Assert.NotEmpty(completionResults.Completions);
105105
// TODO: Since this is a path completion, this test will need to be
106106
// platform specific. Probably something like:
107107
// - Windows: C:\Program
@@ -133,7 +133,7 @@ await this.GetParamSetSignatures(
133133

134134
Assert.NotNull(paramSignatures);
135135
Assert.Equal("Write-Host", paramSignatures.CommandName);
136-
Assert.Equal(1, paramSignatures.Signatures.Count());
136+
Assert.Single(paramSignatures.Signatures);
137137
}
138138

139139
[Fact]
@@ -299,9 +299,9 @@ public void LanguageServiceFindsSymbolsInFile()
299299
this.FindSymbolsInFile(
300300
FindSymbolsInMultiSymbolFile.SourceDetails);
301301

302-
Assert.Equal(4, symbolsResult.FoundOccurrences.Where(r => r.SymbolType == SymbolType.Function).Count());
303-
Assert.Equal(3, symbolsResult.FoundOccurrences.Where(r => r.SymbolType == SymbolType.Variable).Count());
304-
Assert.Equal(1, symbolsResult.FoundOccurrences.Where(r => r.SymbolType == SymbolType.Workflow).Count());
302+
Assert.Equal(4, symbolsResult.FoundOccurrences.Where(symbolReference => symbolReference.SymbolType == SymbolType.Function).Count());
303+
Assert.Equal(3, symbolsResult.FoundOccurrences.Where(symbolReference => symbolReference.SymbolType == SymbolType.Variable).Count());
304+
Assert.Single(symbolsResult.FoundOccurrences.Where(symbolReference => symbolReference.SymbolType == SymbolType.Workflow));
305305

306306
SymbolReference firstFunctionSymbol = symbolsResult.FoundOccurrences.Where(r => r.SymbolType == SymbolType.Function).First();
307307
Assert.Equal("AFunction", firstFunctionSymbol.SymbolName);
@@ -347,7 +347,7 @@ public void LanguageServiceFindsSymbolsInNoSymbolsFile()
347347
this.FindSymbolsInFile(
348348
FindSymbolsInNoSymbolsFile.SourceDetails);
349349

350-
Assert.Equal(0, symbolsResult.FoundOccurrences.Count());
350+
Assert.Empty(symbolsResult.FoundOccurrences);
351351
}
352352

353353
private ScriptFile GetScriptFile(ScriptRegion scriptRegion)

test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs

+10-15
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ public void FindsDotSourcedFiles()
194194
[Fact]
195195
public void ThrowsExceptionWithEditOutsideOfRange()
196196
{
197-
Assert.Throws(
198-
typeof(ArgumentOutOfRangeException),
197+
Assert.Throws<ArgumentOutOfRangeException>(
199198
() =>
200199
{
201200
this.AssertFileChange(
@@ -293,7 +292,7 @@ public void CanGetWholeLine()
293292
_scriptFile_noTrailingNewline.GetLinesInRange(
294293
new BufferRange(5, 1, 5, 10));
295294

296-
Assert.Equal(1, lines.Length);
295+
Assert.Single(lines);
297296
Assert.Equal("Line Five", lines[0]);
298297
}
299298

@@ -314,7 +313,7 @@ public void CanGetSubstringInSingleLine()
314313
_scriptFile_noTrailingNewline.GetLinesInRange(
315314
new BufferRange(4, 3, 4, 8));
316315

317-
Assert.Equal(1, lines.Length);
316+
Assert.Single(lines);
318317
Assert.Equal("ne Fo", lines[0]);
319318
}
320319

@@ -325,7 +324,7 @@ public void CanGetEmptySubstringRange()
325324
_scriptFile_noTrailingNewline.GetLinesInRange(
326325
new BufferRange(4, 3, 4, 3));
327326

328-
Assert.Equal(1, lines.Length);
327+
Assert.Single(lines);
329328
Assert.Equal("", lines[0]);
330329
}
331330

@@ -386,15 +385,15 @@ public void CanGetSameLinesWithUnixLineBreaks()
386385
public void CanGetLineForEmptyString()
387386
{
388387
var emptyFile = ScriptFileChangeTests.CreateScriptFile(string.Empty);
389-
Assert.Equal(1, emptyFile.FileLines.Count);
388+
Assert.Single(emptyFile.FileLines);
390389
Assert.Equal(string.Empty, emptyFile.FileLines[0]);
391390
}
392391

393392
[Fact]
394393
public void CanGetLineForSpace()
395394
{
396395
var spaceFile = ScriptFileChangeTests.CreateScriptFile(" ");
397-
Assert.Equal(1, spaceFile.FileLines.Count);
396+
Assert.Single(spaceFile.FileLines);
398397
Assert.Equal(" ", spaceFile.FileLines[0]);
399398
}
400399
}
@@ -445,8 +444,7 @@ public void CanOffsetByColumn()
445444
public void ThrowsWhenPositionOutOfRange()
446445
{
447446
// Less than line range
448-
Assert.Throws(
449-
typeof(ArgumentOutOfRangeException),
447+
Assert.Throws<ArgumentOutOfRangeException>(
450448
() =>
451449
{
452450
scriptFile.CalculatePosition(
@@ -455,8 +453,7 @@ public void ThrowsWhenPositionOutOfRange()
455453
});
456454

457455
// Greater than line range
458-
Assert.Throws(
459-
typeof(ArgumentOutOfRangeException),
456+
Assert.Throws<ArgumentOutOfRangeException>(
460457
() =>
461458
{
462459
scriptFile.CalculatePosition(
@@ -465,8 +462,7 @@ public void ThrowsWhenPositionOutOfRange()
465462
});
466463

467464
// Less than column range
468-
Assert.Throws(
469-
typeof(ArgumentOutOfRangeException),
465+
Assert.Throws<ArgumentOutOfRangeException>(
470466
() =>
471467
{
472468
scriptFile.CalculatePosition(
@@ -475,8 +471,7 @@ public void ThrowsWhenPositionOutOfRange()
475471
});
476472

477473
// Greater than column range
478-
Assert.Throws(
479-
typeof(ArgumentOutOfRangeException),
474+
Assert.Throws<ArgumentOutOfRangeException>(
480475
() =>
481476
{
482477
scriptFile.CalculatePosition(

test/PowerShellEditorServices.Test/Utility/AsyncQueueTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ await Task.WhenAll(
5353

5454
// At this point, numbers 0 through 299 should be in the outputItems
5555
IEnumerable<int> expectedItems = Enumerable.Range(0, 300);
56-
Assert.Equal(0, expectedItems.Except(outputItems).Count());
56+
Assert.Empty(expectedItems.Except(outputItems));
5757
}
5858

5959
[Fact]

0 commit comments

Comments
 (0)