Improve ValidateCount attribute error message (#3596)

This commit is contained in:
Ilya 2017-04-22 05:01:04 +04:00 committed by Dongbo Wang
parent 63c20f15b1
commit e8ec40069b
3 changed files with 44 additions and 10 deletions

View File

@ -1309,18 +1309,25 @@ namespace System.Management.Automation
null, Metadata.ValidateCountNotInArray);
}
if (MinLength == MaxLength && len != MaxLength)
{
throw new ValidationMetadataException("ValidateCountNotExactlyEqual",
null, Metadata.ValidateCountFailure,
MinLength, MaxLength, len);
}
if (len < MinLength)
{
throw new ValidationMetadataException("ValidateCountSmallerThanMin",
null, Metadata.ValidateCountMinLengthFailure,
MinLength, len);
null, Metadata.ValidateCountFailure,
MinLength, MaxLength, len);
}
if (len > MaxLength)
{
throw new ValidationMetadataException("ValidateCountGreaterThanMax",
null, Metadata.ValidateCountMaxLengthFailure,
MaxLength, len);
null, Metadata.ValidateCountFailure,
MinLength, MaxLength, len);
}
}

View File

@ -144,11 +144,8 @@
<data name="ValidateCountNotInArray" xml:space="preserve">
<value>The ValidateCount attribute cannot be applied to a non-array parameter. Either remove the attribute from the parameter or make the parameter an array parameter.</value>
</data>
<data name="ValidateCountMinLengthFailure" xml:space="preserve">
<value>The number of provided arguments ({1}) is fewer than the minimum number of allowed arguments ({0}). Provide more than {0} arguments, and then try the command again.</value>
</data>
<data name="ValidateCountMaxLengthFailure" xml:space="preserve">
<value>The number of provided arguments, ({1}), exceeds the maximum number of allowed arguments ({0}). Provide fewer than {0} arguments, and then try the command again.</value>
<data name="ValidateCountFailure" xml:space="preserve">
<value>The parameter requires at least {0} value(s) and no more than {1} value(s) - {2} value(s) were provided.</value>
</data>
<data name="ValidateCountMaxLengthSmallerThanMinLength" xml:space="preserve">
<value>The specified maximum number of arguments for a parameter is fewer than the specified minimum number of arguments. Update the ValidateCount attribute for the parameter.</value>
@ -228,4 +225,4 @@
<data name="ValidateDrivePathNoRoot" xml:space="preserve">
<value>The path argument has no root drive. Supply a full path argument with a root drive.</value>
</data>
</root>
</root>

View File

@ -0,0 +1,30 @@
Import-Module $PSScriptRoot\..\Common\Test.Helpers.psm1
Describe 'Validate Attributes Tests' -Tags 'CI' {
Context "ValidateCount" {
BeforeAll {
$testCases = @(
@{ sb = { function Local:foo { param([ValidateCount(-1,2)] [string[]] $bar) }; foo }; FullyQualifiedErrorId = "ExceptionConstructingAttribute"; InnerErrorId = "" }
@{ sb = { function Local:foo { param([ValidateCount(1,-1)] [string[]] $bar) }; foo }; FullyQualifiedErrorId = "ExceptionConstructingAttribute"; InnerErrorId = "" }
@{ sb = { function Local:foo { param([ValidateCount(2, 1)] [string[]] $bar) }; foo }; FullyQualifiedErrorId = "ValidateRangeMaxLengthSmallerThanMinLength"; InnerErrorId = "" }
@{ sb = { function Local:foo { param([ValidateCount(2, 2)] [string[]] $bar) }; foo 1 }; FullyQualifiedErrorId = "ParameterArgumentValidationError,foo"; InnerErrorId = "ValidateCountNotExactlyEqual" }
@{ sb = { function Local:foo { param([ValidateCount(2, 3)] [string[]] $bar) }; foo 1 }; FullyQualifiedErrorId = "ParameterArgumentValidationError,foo"; InnerErrorId = "ValidateCountSmallerThanMin" }
@{ sb = { function Local:foo { param([ValidateCount(2, 3)] [string[]] $bar) }; foo 1,2,3,4 }; FullyQualifiedErrorId = "ParameterArgumentValidationError,foo"; InnerErrorId = "ValidateCountGreaterThanMax" }
)
}
It 'Exception: <FullyQualifiedErrorId>:<InnerErrorId>' -TestCases $testCases {
param($sb, $FullyQualifiedErrorId, $InnerErrorId)
$sb | ShouldBeErrorId $FullyQualifiedErrorId
if ($InnerErrorId) {
$error[0].exception.innerexception.errorrecord.FullyQualifiedErrorId | Should Be $InnerErrorId
}
}
It 'No Exception: valid argument count' {
{ function Local:foo { param([ValidateCount(2, 4)] [string[]] $bar) }; foo 1,2,3,4 } | Should Not Throw
}
}
}