Skip to content

Commit 55ca43f

Browse files
committed
Add tests for new static analysis checks
1 parent 566bfff commit 55ca43f

File tree

2 files changed

+174
-2
lines changed

2 files changed

+174
-2
lines changed

tools/StaticAnalysis/StaticAnalysis.Test/SignatureVerifierTests.cs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,54 @@ public void CmdletWithPluralNoun()
208208
}
209209
#endregion
210210

211-
#region ParameterWithPluralNoun
211+
#region OutputChecks
212+
[Fact]
213+
[Trait(Category.AcceptanceType, Category.CheckIn)]
214+
public void CmdletWithNoOutput()
215+
{
216+
cmdletSignatureVerifier.Analyze(
217+
new List<string> { _testCmdletDirPath },
218+
((dirList) => { return new List<string> { _testCmdletDirPath }; }),
219+
(cmdletName) => cmdletName.Equals("Get-CmdletWithNoOutput", StringComparison.OrdinalIgnoreCase));
220+
221+
AnalysisReport testReport = cmdletSignatureVerifier.GetAnalysisReport();
222+
Assert.Equal(1, testReport.ProblemIdList.Count);
223+
Assert.True(testReport.ProblemIdList.Where<int>((problemId) => problemId.Equals(SignatureProblemId.CmdletWithNoOutputType)).SingleOrDefault<int>().Equals(SignatureProblemId.CmdletWithNoOutputType));
224+
}
225+
226+
#endregion
227+
228+
#region ParameterSetChecks
229+
[Fact]
230+
[Trait(Category.AcceptanceType, Category.CheckIn)]
231+
public void ParameterSetNameWithSpace()
232+
{
233+
cmdletSignatureVerifier.Analyze(
234+
new List<string> { _testCmdletDirPath },
235+
((dirList) => { return new List<string> { _testCmdletDirPath }; }),
236+
(cmdletName) => cmdletName.Equals("Get-ParameterSetNameWithSpace", StringComparison.OrdinalIgnoreCase));
237+
238+
AnalysisReport testReport = cmdletSignatureVerifier.GetAnalysisReport();
239+
Assert.Equal(1, testReport.ProblemIdList.Count);
240+
Assert.True(testReport.ProblemIdList.Where<int>((problemId) => problemId.Equals(SignatureProblemId.ParameterSetWithSpace)).SingleOrDefault<int>().Equals(SignatureProblemId.ParameterSetWithSpace));
241+
}
242+
243+
[Fact]
244+
[Trait(Category.AcceptanceType, Category.CheckIn)]
245+
public void MultipleParameterSetsWithNoDefault()
246+
{
247+
cmdletSignatureVerifier.Analyze(
248+
new List<string> { _testCmdletDirPath },
249+
((dirList) => { return new List<string> { _testCmdletDirPath }; }),
250+
(cmdletName) => cmdletName.Equals("Get-MultipleParameterSetsWithNoDefault", StringComparison.OrdinalIgnoreCase));
251+
252+
AnalysisReport testReport = cmdletSignatureVerifier.GetAnalysisReport();
253+
Assert.Equal(1, testReport.ProblemIdList.Count);
254+
Assert.True(testReport.ProblemIdList.Where<int>((problemId) => problemId.Equals(SignatureProblemId.MultipleParameterSetsWithNoDefault)).SingleOrDefault<int>().Equals(SignatureProblemId.MultipleParameterSetsWithNoDefault));
255+
}
256+
#endregion
257+
258+
#region ParameterChecks
212259
[Fact]
213260
[Trait(Category.AcceptanceType, Category.CheckIn)]
214261
public void ParameterWithSingularNoun()
@@ -248,6 +295,20 @@ public void CmdletAndParameterWithSingularNounInList()
248295
AnalysisReport testReport = cmdletSignatureVerifier.GetAnalysisReport();
249296
Assert.Equal(0, testReport.ProblemIdList.Count);
250297
}
298+
299+
[Fact]
300+
[Trait(Category.AcceptanceType, Category.CheckIn)]
301+
public void ParameterWithOutOfRangePosition()
302+
{
303+
cmdletSignatureVerifier.Analyze(
304+
new List<string> { _testCmdletDirPath },
305+
((dirList) => { return new List<string> { _testCmdletDirPath }; }),
306+
(cmdletName) => cmdletName.Equals("Get-ParameterWithOutOfRangePosition", StringComparison.OrdinalIgnoreCase));
307+
308+
AnalysisReport testReport = cmdletSignatureVerifier.GetAnalysisReport();
309+
Assert.Equal(1, testReport.ProblemIdList.Count);
310+
Assert.True(testReport.ProblemIdList.Where<int>((problemId) => problemId.Equals(SignatureProblemId.ParameterWithOutOfRangePosition)).SingleOrDefault<int>().Equals(SignatureProblemId.ParameterWithOutOfRangePosition));
311+
}
251312
#endregion
252313
}
253314
}

tools/StaticAnalysis/StaticAnalysis.Test/TestCmdlets/SignatureVerifier_Cmdlet.cs

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.AddVerbWithoutSupportsShouldP
1313
/// Check if a cmdlet that has Verbs that require ShouldProcess has ShouldProcess parameter
1414
/// </summary>
1515
[Cmdlet(VerbsCommon.Add, "AddVerbWithoutSupportsShouldProcessParameter")]
16+
[OutputType(typeof(bool))]
1617
public class AddVerbWithoutSupportsShouldProcessParameter : Cmdlet
1718
{
1819
/// <summary>
@@ -42,6 +43,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.AddVerbWithSupportsShouldProc
4243
/// Check if a cmdlet that has Verbs that require ShouldProcess has ShouldProcess parameter
4344
/// </summary>
4445
[Cmdlet(VerbsCommon.Add, "AddVerbWithSupportsShouldProcessParameter", SupportsShouldProcess = true)]
46+
[OutputType(typeof(bool))]
4547
public class AddVerbWithSupportsShouldProcessParameter : Cmdlet
4648
{
4749
/// <summary>
@@ -73,6 +75,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.ForceParameterWithoutSupports
7375
/// Verify if cmdlet that has Force parameter should also define SupportsShouldProcess parameter
7476
/// </summary>
7577
[Cmdlet(VerbsDiagnostic.Test, "ForceParameterWithoutSupportsShouldProcess")]
78+
[OutputType(typeof(bool))]
7679
public class ForceParameterWithoutSupportsShouldProcess : Cmdlet
7780
{
7881
/// <summary>
@@ -108,6 +111,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.ForceParameterWithSupportsSho
108111
/// Verify if cmdlet that has Force parameter should also define SupportsShouldProcess parameter
109112
/// </summary>
110113
[Cmdlet(VerbsDiagnostic.Test, "ForceParameterWithSupportsShouldProcess", SupportsShouldProcess = true)]
114+
[OutputType(typeof(bool))]
111115
public class ForceParameterWithSupportsShouldProcess : Cmdlet
112116
{
113117
/// <summary>
@@ -137,6 +141,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.ConfirmImpactWithoutSupportsS
137141
/// Verify if cmdlet that has Force parameter should also define SupportsShouldProcess parameter
138142
/// </summary>
139143
[Cmdlet(VerbsDiagnostic.Test, "ConfirmImpactWithoutSupportsShouldProcess", ConfirmImpact = ConfirmImpact.High)]
144+
[OutputType(typeof(bool))]
140145
public class ConfirmImpactWithoutSupportsShouldProcess : Cmdlet
141146
{
142147
/// <summary>
@@ -158,6 +163,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.ConfirmImpactWithSupportsShou
158163
/// Verify if cmdlet that has Force parameter should also define SupportsShouldProcess parameter
159164
/// </summary>
160165
[Cmdlet(VerbsDiagnostic.Test, "ConfirmImpactWithSupportsShouldProcess", ConfirmImpact = ConfirmImpact.Medium, SupportsShouldProcess = true)]
166+
[OutputType(typeof(bool))]
161167
public class ConfirmImpactWithSupportsShouldProcess : Cmdlet
162168
{
163169
/// <summary>
@@ -187,6 +193,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.ShouldContinueVerbWithForceSw
187193
/// Verify if cmdlet that has Force parameter should also define SupportsShouldProcess parameter
188194
/// </summary>
189195
[Cmdlet(VerbsCommon.Copy, "ShouldContinueVerbWithForceSwitch", SupportsShouldProcess = true)]
196+
[OutputType(typeof(bool))]
190197
public class ShouldContinueVerbWithForceSwitch : Cmdlet
191198
{
192199
/// <summary>
@@ -216,6 +223,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.CmdletWithApprovedVerb
216223
/// Verify if a cmdlet has an approved verb in its name.
217224
/// </summary>
218225
[Cmdlet(VerbsCommon.Get, "SampleCmdlet")]
226+
[OutputType(typeof(bool))]
219227
public class CmdletWithApprovedVerb : Cmdlet
220228
{
221229
/// <summary>
@@ -237,6 +245,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.CmdletWithUnapprovedVerb
237245
/// Verify if a cmdlet has an approved verb in its name.
238246
/// </summary>
239247
[Cmdlet("Prepare", "SampleCmdlet")]
248+
[OutputType(typeof(bool))]
240249
public class CmdletWithUnapprovedVerb : Cmdlet
241250
{
242251
/// <summary>
@@ -260,6 +269,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.CmdletWithSingularNoun
260269
/// Verify if a cmdlet has a singular noun in its name.
261270
/// </summary>
262271
[Cmdlet(VerbsCommon.Get, "SampleKey")]
272+
[OutputType(typeof(bool))]
263273
public class CmdletGetSampleKey : Cmdlet
264274
{
265275
/// <summary>
@@ -281,6 +291,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.CmdletWithPluralNoun
281291
/// Verify if a cmdlet has a plural noun in its name.
282292
/// </summary>
283293
[Cmdlet("Get", "SampleKeys")]
294+
[OutputType(typeof(bool))]
284295
public class CmdletGetSampleKeys : Cmdlet
285296
{
286297
/// <summary>
@@ -295,7 +306,72 @@ protected override void BeginProcessing()
295306
}
296307
#endregion
297308

298-
#region ParameterWithPluralNoun
309+
#region OutputChecks
310+
namespace StaticAnalysis.Test.CmdletTest.Signature.CmdletWithNoOutput
311+
{
312+
using System.Management.Automation;
313+
314+
[Cmdlet(VerbsCommon.Get, "CmdletWithNoOutput")]
315+
public class CmdletGetCmdletWithNoOutput : Cmdlet
316+
{
317+
protected override void BeginProcessing()
318+
{
319+
WriteObject("Get-CmdletWithNoOutput BeginProcessing()");
320+
WriteInformation("Info", null);
321+
}
322+
}
323+
}
324+
325+
#endregion
326+
327+
#region ParameterSetChecks
328+
namespace StaticAnalysis.Test.CmdletTest.Signature.ParameterSetNameWithSpace
329+
{
330+
using System.Management.Automation;
331+
332+
[Cmdlet(VerbsCommon.Get, "ParameterSetNameWithSpace", DefaultParameterSetName = "GetFoo")]
333+
[OutputType(typeof(bool))]
334+
public class CmdletGetParameterSetNameWithSpace : Cmdlet
335+
{
336+
[Parameter(ParameterSetName = "GetFoo")]
337+
public string Foo { get; set; }
338+
339+
[Parameter(ParameterSetName = "Get Bar")]
340+
public string Bar { get; set; }
341+
342+
protected override void BeginProcessing()
343+
{
344+
WriteObject("Get-ParameterSetNameWithSpace BeginProcessing()");
345+
WriteInformation("Info", null);
346+
}
347+
}
348+
}
349+
350+
namespace StaticAnalysis.Test.CmdletTest.Signature.MultipleParameterSetsWithNoDefault
351+
{
352+
using System.Management.Automation;
353+
354+
[Cmdlet(VerbsCommon.Get, "MultipleParameterSetsWithNoDefault")]
355+
[OutputType(typeof(bool))]
356+
public class CmdletGetMultipleParameterSetsWithNoDefault : Cmdlet
357+
{
358+
[Parameter(ParameterSetName = "GetFoo")]
359+
public string Foo { get; set; }
360+
361+
[Parameter(ParameterSetName = "GetBar")]
362+
public string Bar { get; set; }
363+
364+
protected override void BeginProcessing()
365+
{
366+
WriteObject("Get-MultipleParameterSetsWithNoDefault BeginProcessing()");
367+
WriteInformation("Info", null);
368+
}
369+
}
370+
}
371+
372+
#endregion
373+
374+
#region ParameterChecks
299375
namespace StaticAnalysis.Test.CmdletTest.Signature.ParameterWithSingularNoun
300376
{
301377
using System.Management.Automation;
@@ -304,6 +380,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.ParameterWithSingularNoun
304380
/// Verify if a parameter has a singular noun in its name.
305381
/// </summary>
306382
[Cmdlet(VerbsCommon.Get, "SampleFoo")]
383+
[OutputType(typeof(bool))]
307384
public class CmdletGetSampleFoo : Cmdlet
308385
{
309386
[Parameter(Mandatory = false)]
@@ -328,6 +405,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.ParameterWithPluralNoun
328405
/// Verify if a parameter has a plural noun in its name.
329406
/// </summary>
330407
[Cmdlet("Get", "SampleBar")]
408+
[OutputType(typeof(bool))]
331409
public class CmdletGetSampleBar : Cmdlet
332410
{
333411
[Parameter(Mandatory = false)]
@@ -353,6 +431,7 @@ namespace StaticAnalysis.Test.CmdletTest.Signature.CmdletAndParameterWithSingula
353431
/// accepted nouns ending with "s".
354432
/// </summary>
355433
[Cmdlet("Get", "SampleAddress")]
434+
[OutputType(typeof(bool))]
356435
public class CmdletGetSampleAddress : Cmdlet
357436
{
358437
[Parameter(Mandatory = false)]
@@ -368,4 +447,36 @@ protected override void BeginProcessing()
368447
}
369448
}
370449
}
450+
451+
namespace StaticAnalysis.Test.CmdletTest.Signature.ParameterWithOutOfRangePosition
452+
{
453+
using System.Management.Automation;
454+
455+
[Cmdlet(VerbsCommon.Get, "ParameterWithOutOfRangePosition")]
456+
[OutputType(typeof(bool))]
457+
public class CmdletGetParameterWithOutOfRangePosition : Cmdlet
458+
{
459+
[Parameter(Position = 0)]
460+
public string FirstParameter { get; set; }
461+
462+
[Parameter(Position = 1)]
463+
public string SecondParameter { get; set; }
464+
465+
[Parameter(Position = 2)]
466+
public string ThirdParameter { get; set; }
467+
468+
[Parameter(Position = 3)]
469+
public string FourthParameter { get; set; }
470+
471+
[Parameter(Position = 4)]
472+
public string FifthParameter { get; set; }
473+
474+
protected override void BeginProcessing()
475+
{
476+
WriteObject("Get-ParameterWithOutOfRangePosition BeginProcessing()");
477+
WriteInformation("Info", null);
478+
}
479+
}
480+
}
481+
371482
#endregion

0 commit comments

Comments
 (0)