Files
PowerShell/test/powershell/engine/ValidateAttributes.Tests.ps1
T
Ilya 3d7ef43498 Improve ValidateCount attribute error message (#3656)
Leave only two message for ValidateCount attribute error:
1. Count is not in allowed range.
2. Count is not exactly equal allowed value.
2017-05-02 10:04:52 -07:00

31 lines
2.0 KiB
PowerShell

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 = "ValidateCountExactFailure" }
@{ sb = { function Local:foo { param([ValidateCount(2, 3)] [string[]] $bar) }; foo 1 }; FullyQualifiedErrorId = "ParameterArgumentValidationError,foo"; InnerErrorId = "ValidateCountMinMaxFailure" }
@{ sb = { function Local:foo { param([ValidateCount(2, 3)] [string[]] $bar) }; foo 1,2,3,4 }; FullyQualifiedErrorId = "ParameterArgumentValidationError,foo"; InnerErrorId = "ValidateCountMinMaxFailure" }
)
}
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
}
}
}