2020-03-24 11:08:37 -07:00
# Copyright (c) Microsoft Corporation.
2018-02-13 09:23:53 -08:00
# Licensed under the MIT License.
2017-04-22 05:01:04 +04:00
Describe 'Validate Attributes Tests' -Tags 'CI' {
Context "ValidateCount" {
BeforeAll {
$testCases = @ (
2017-08-02 11:24:06 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-ArrayCount { param ([ ValidateCount ( -1 , 2 )] [ string[] ] $Items ) }; Test-ArrayCount }
2017-08-02 11:24:06 -07:00
FullyQualifiedErrorId = "ExceptionConstructingAttribute"
2020-03-24 11:08:37 -07:00
InnerErrorId = ""
2017-08-02 11:24:06 -07:00
}
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-ArrayCount { param ([ ValidateCount ( 1 , -1 )] [ string[] ] $Items ) }; Test-ArrayCount }
2020-03-24 11:08:37 -07:00
FullyQualifiedErrorId = "ExceptionConstructingAttribute"
InnerErrorId = ""
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-ArrayCount { param ([ ValidateCount ( 2 , 1 )] [ string[] ] $Items ) }; Test-ArrayCount }
2017-08-02 11:24:06 -07:00
FullyQualifiedErrorId = "ValidateRangeMaxLengthSmallerThanMinLength"
2020-03-24 11:08:37 -07:00
InnerErrorId = ""
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-ArrayCount { param ([ ValidateCount ( 2 , 2 )] [ string[] ] $Items ) }; Test-ArrayCount 1 }
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-ArrayCount"
2020-03-24 11:08:37 -07:00
InnerErrorId = "ValidateCountExactFailure"
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-ArrayCount { param ([ ValidateCount ( 2 , 3 )] [ string[] ] $Items ) }; Test-ArrayCount 1 }
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-ArrayCount"
2020-03-24 11:08:37 -07:00
InnerErrorId = "ValidateCountMinMaxFailure"
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-ArrayCount { param ([ ValidateCount ( 2 , 3 )] [ string[] ] $Items ) }; Test-ArrayCount 1 , 2 , 3 , 4 }
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-ArrayCount"
2017-08-02 11:24:06 -07:00
InnerErrorId = "ValidateCountMinMaxFailure"
}
2017-04-22 05:01:04 +04:00
)
}
It 'Exception: <FullyQualifiedErrorId>:<InnerErrorId>' -TestCases $testCases {
2017-08-02 11:24:06 -07:00
param ( $ScriptBlock , $FullyQualifiedErrorId , $InnerErrorId )
2017-04-22 05:01:04 +04:00
2025-10-12 23:55:20 -07:00
$err = $ScriptBlock | Should -Throw -ErrorId $FullyQualifiedErrorId -PassThru
2017-04-22 05:01:04 +04:00
if ( $InnerErrorId ) {
2025-10-12 23:55:20 -07:00
$err . exception . innerexception . errorrecord . FullyQualifiedErrorId | Should -Be $InnerErrorId
2017-04-22 05:01:04 +04:00
}
}
It 'No Exception: valid argument count' {
2025-10-12 23:55:20 -07:00
{ function Test-ArrayCount { param ([ ValidateCount ( 2 , 4 )] [ string[] ] $Items ) }; Test-ArrayCount 1 , 2 , 3 , 4 } | Should -Not -Throw
2017-08-02 11:24:06 -07:00
}
}
2023-02-22 11:09:04 -06:00
Context "ValidateRange - ParameterConstructors" {
2017-08-02 11:24:06 -07:00
BeforeAll {
$testCases = @ (
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'xPositive' )] $Number ) }; Test-NumericRange }
2017-08-02 11:24:06 -07:00
FullyQualifiedErrorId = "ExceptionConstructingAttribute"
2020-03-24 11:08:37 -07:00
InnerErrorId = "SubstringDisambiguationEnumParseThrewAnException"
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 2 , 1 )] [ int ] $Number ) }; Test-NumericRange }
2017-08-02 11:24:06 -07:00
FullyQualifiedErrorId = "MaxRangeSmallerThanMinRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = ""
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( "one" , 10 )] $Number ) }; Test-NumericRange }
2017-08-02 11:24:06 -07:00
FullyQualifiedErrorId = "MinRangeNotTheSameTypeOfMaxRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = ""
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 1 , "two" )] $Number ) }; Test-NumericRange }
2017-08-02 11:24:06 -07:00
FullyQualifiedErrorId = "MinRangeNotTheSameTypeOfMaxRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = ""
2017-08-02 11:24:06 -07:00
}
)
}
It 'Exception: <FullyQualifiedErrorId>:<InnerErrorId>' -TestCases $testCases {
param ( $ScriptBlock , $FullyQualifiedErrorId , $InnerErrorId )
2025-10-12 23:55:20 -07:00
$err = $ScriptBlock | Should -Throw -ErrorId $FullyQualifiedErrorId -PassThru
2017-08-02 11:24:06 -07:00
if ( $InnerErrorId ) {
2025-10-12 23:55:20 -07:00
$err . exception . innerexception . errorrecord . FullyQualifiedErrorId | Should -Be $InnerErrorId
2017-08-02 11:24:06 -07:00
}
}
}
Context "ValidateRange - User Defined Range" {
BeforeAll {
$testCases = @ (
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 1 , 10 )] [ int ] $Number ) }; Test-NumericRange -1 }
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-NumericRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = "ValidateRangeTooSmall"
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 1 , 10 )] [ int ] $Number ) }; Test-NumericRange 11 }
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-NumericRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = "ValidateRangeTooBig"
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 1 , 10 )] $Number ) }; Test-NumericRange "one" }
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-NumericRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = "ValidationRangeElementType"
2017-08-02 11:24:06 -07:00
}
)
$validTestCases = @ (
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 1 , 10 )] [ int ] $Number ) }; Test-NumericRange 5 }
2017-08-02 11:24:06 -07:00
}
)
}
It 'Exception: <FullyQualifiedErrorId>:<InnerErrorId>' -TestCases $testCases {
param ( $ScriptBlock , $FullyQualifiedErrorId , $InnerErrorId )
2025-10-12 23:55:20 -07:00
$err = $ScriptBlock | Should -Throw -ErrorId $FullyQualifiedErrorId -PassThru
2017-08-02 11:24:06 -07:00
if ( $InnerErrorId ) {
2025-10-12 23:55:20 -07:00
$err . exception . innerexception . errorrecord . FullyQualifiedErrorId | Should -Be $InnerErrorId
2017-08-02 11:24:06 -07:00
}
}
It 'No Exception: value within range' -TestCases $validTestCases {
param ( $ScriptBlock )
2018-03-14 12:13:32 -07:00
$ScriptBlock | Should -Not -Throw
2017-08-02 11:24:06 -07:00
}
}
Context "ValidateRange - Predefined Range" {
BeforeAll {
$testCases = @ (
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( "Positive" )] [ int ] $Number ) }; Test-NumericRange -1 }
2017-08-02 11:24:06 -07:00
RangeType = "Positive"
2025-10-12 23:55:20 -07:00
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-NumericRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = "ValidateRangePositiveFailure"
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( "Positive" )] [ int ] $Number ) }; Test-NumericRange 0 }
2017-08-02 11:24:06 -07:00
RangeType = "Positive"
2025-10-12 23:55:20 -07:00
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-NumericRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = "ValidateRangePositiveFailure"
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( "Positive" )] $Number ) }; Test-NumericRange "one" }
2017-08-02 11:24:06 -07:00
RangeType = "Positive"
2025-10-12 23:55:20 -07:00
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-NumericRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = ""
2017-08-02 11:24:06 -07:00
}
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'NonNegative' )] [ int ] $Number ) }; Test-NumericRange -1 }
2017-08-02 11:24:06 -07:00
RangeType = "NonNegative"
2025-10-12 23:55:20 -07:00
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-NumericRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = "ValidateRangeNonNegativeFailure"
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'NonNegative' )] $Number ) }; Test-NumericRange "one" }
2017-08-02 11:24:06 -07:00
RangeType = "NonNegative"
2025-10-12 23:55:20 -07:00
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-NumericRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = ""
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'Negative' )] [ int ] $Number ) }; Test-NumericRange 1 }
2017-08-02 11:24:06 -07:00
RangeType = "Negative"
2025-10-12 23:55:20 -07:00
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-NumericRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = "ValidateRangeNegativeFailure"
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'Negative' )] [ int ] $Number ) }; Test-NumericRange 0 }
2017-08-02 11:24:06 -07:00
RangeType = "Negative"
2025-10-12 23:55:20 -07:00
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-NumericRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = "ValidateRangeNegativeFailure"
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'Negative' )] $Number ) }; Test-NumericRange "one" }
2017-08-02 11:24:06 -07:00
RangeType = "Negative"
2025-10-12 23:55:20 -07:00
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-NumericRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = ""
2017-08-02 11:24:06 -07:00
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'NonPositive' )] $Number ) }; Test-NumericRange 1 }
2017-08-02 11:24:06 -07:00
RangeType = "NonPositive"
2025-10-12 23:55:20 -07:00
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-NumericRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = "ValidateRangeNonPositiveFailure"
2017-08-02 11:24:06 -07:00
}
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'NonPositive' )] $Number ) }; Test-NumericRange "one" }
2017-08-02 11:24:06 -07:00
RangeType = "NonPositive"
2025-10-12 23:55:20 -07:00
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-NumericRange"
2020-03-24 11:08:37 -07:00
InnerErrorId = ""
2017-08-02 11:24:06 -07:00
}
)
$validTestCases = @ (
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( "Positive" )] [ int ] $Number ) }; Test-NumericRange 15 }
2017-08-02 11:24:06 -07:00
RangeType = "Positive"
TestValue = 15
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( "Positive" )] [ double ] $Number ) }; Test-NumericRange ([ double ]:: MaxValue ) };
2017-08-02 11:24:06 -07:00
RangeType = "Positive"
TestValue = [ double ]:: MaxValue
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'NonNegative' )] [ int ] $Number ) }; Test-NumericRange 0 }
2017-08-02 11:24:06 -07:00
RangeType = "NonNegative"
TestValue = 0
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'NonNegative' )] [ int ] $Number ) }; Test-NumericRange 15 }
2017-08-02 11:24:06 -07:00
RangeType = "NonNegative"
TestValue = 15
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'NonNegative' )] [ double ] $Number ) }; Test-NumericRange ([ double ]:: MaxValue ) };
2017-08-02 11:24:06 -07:00
RangeType = "NonNegative"
TestValue = [ double ]:: MaxValue
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'Negative' )] [ int ] $Number ) }; Test-NumericRange -15 }
2017-08-02 11:24:06 -07:00
RangeType = "Negative"
TestValue = -15
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'Negative' )] [ double ] $Number ) }; Test-NumericRange ([ double ]:: MinValue ) };
2017-08-02 11:24:06 -07:00
TestValue = [ double ]:: MinValue
RangeType = "Negative"
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'NonPositive' )] [ int ] $Number ) }; Test-NumericRange 0 }
2017-08-02 11:24:06 -07:00
RangeType = "NonPositive"
TestValue = 0
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'NonPositive' )] [ int ] $Number ) }; Test-NumericRange -15 }
2017-08-02 11:24:06 -07:00
RangeType = "NonPositive"
TestValue = -15
}
2020-03-24 11:08:37 -07:00
@ {
2025-10-12 23:55:20 -07:00
ScriptBlock = { function Test-NumericRange { param ([ ValidateRange ( 'NonPositive' )] [ double ] $Number ) }; Test-NumericRange ([ double ]:: MinValue ) }
2017-08-02 11:24:06 -07:00
RangeType = "NonPositive"
TestValue = [ double ]:: MinValue
}
)
}
It 'Exception: <FullyQualifiedErrorId>:<InnerErrorId>, RangeType: <RangeType>' -TestCases $testCases {
param ( $ScriptBlock , $RangeType , $FullyQualifiedErrorId , $InnerErrorId )
2025-10-12 23:55:20 -07:00
$err = $ScriptBlock | Should -Throw -ErrorId $FullyQualifiedErrorId -PassThru
2017-08-02 11:24:06 -07:00
if ( $InnerErrorId ) {
2025-10-12 23:55:20 -07:00
$err . exception . innerexception . errorrecord . FullyQualifiedErrorId | Should -Be $InnerErrorId
2017-08-02 11:24:06 -07:00
}
}
It 'No Exception: RangeType: <RangeType> - argument "<TestValue>"' -TestCases $validTestCases {
param ( $ScriptBlock , $RangeType , $testValue )
2018-03-14 12:13:32 -07:00
$ScriptBlock | Should -Not -Throw
2017-04-22 05:01:04 +04:00
}
}
2017-11-30 14:46:09 -08:00
2025-10-12 23:55:20 -07:00
Context "ValidateLength" {
BeforeAll {
$testCases = @ (
@ {
ScriptBlock = { function Test-StringLength { param ([ ValidateLength ( 2 , 5 )] [ string ] $InputString ) }; Test-StringLength "a" }
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-StringLength"
InnerErrorId = "ValidateLengthMinLengthFailure"
}
@ {
ScriptBlock = { function Test-StringLength { param ([ ValidateLength ( 2 , 5 )] [ string ] $InputString ) }; Test-StringLength "abcdef" }
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-StringLength"
InnerErrorId = "ValidateLengthMaxLengthFailure"
}
@ {
ScriptBlock = { function Test-StringLength { param ([ ValidateLength ( 2 , 5 )] $InputString ) }; Test-StringLength 123 }
FullyQualifiedErrorId = "ParameterArgumentValidationError,Test-StringLength"
InnerErrorId = "ValidateLengthNotString"
}
)
$validTestCases = @ (
@ {
ScriptBlock = { function Test-StringLength { param ([ ValidateLength ( 2 , 5 )] [ string ] $InputString ) }; Test-StringLength "abc" }
}
@ {
ScriptBlock = { function Test-StringLength { param ([ ValidateLength ( 2 , 5 )] [ string ] $InputString ) }; Test-StringLength "ab" }
}
@ {
ScriptBlock = { function Test-StringLength { param ([ ValidateLength ( 2 , 5 )] [ string ] $InputString ) }; Test-StringLength "abcde" }
}
)
}
It 'Exception: <FullyQualifiedErrorId>:<InnerErrorId>' -TestCases $testCases {
param ( $ScriptBlock , $FullyQualifiedErrorId , $InnerErrorId )
$err = $ScriptBlock | Should -Throw -ErrorId $FullyQualifiedErrorId -PassThru
if ( $InnerErrorId ) {
$err . exception . innerexception . errorrecord . FullyQualifiedErrorId | Should -Be $InnerErrorId
}
}
It 'No Exception: valid string length' -TestCases $validTestCases {
param ( $ScriptBlock )
$ScriptBlock | Should -Not -Throw
}
It 'ValidateLength error message should be properly formatted' {
function Test-ValidateLengthMax { param ([ ValidateLength ( 0 , 2 )] [ string ] $Value ) $Value }
function Test-ValidateLengthMin { param ([ ValidateLength ( 5 , 10 )] [ string ] $Value ) $Value }
$TestStringTooLong = "11111"
$TestStringTooShort = "123"
$ExpectedMaxLength = 2
$ExpectedMinLength = 5
$err = { Test-ValidateLengthMax $TestStringTooLong } | Should -Throw -ErrorId "ParameterArgumentValidationError,Test-ValidateLengthMax" -PassThru
$err . Exception . InnerException . Message | Should -Match ".+\( $( $TestStringTooLong . Length ) \).+\ `" $ExpectedMaxLength \ `" "
$err = { Test-ValidateLengthMin $TestStringTooShort } | Should -Throw -ErrorId "ParameterArgumentValidationError,Test-ValidateLengthMin" -PassThru
$err . Exception . InnerException . Message | Should -Match ".+\( $( $TestStringTooShort . Length ) \).+\ `" $ExpectedMinLength \ `" "
}
}
2022-08-10 03:48:08 +10:00
Context "ValidateNotNull, ValidateNotNullOrEmpty, ValidateNotNullOrWhiteSpace and Not-Null-Or-Empty check for Mandatory parameter" {
2017-11-30 14:46:09 -08:00
BeforeAll {
function MandatoryFunc {
param (
[ Parameter ( Mandatory , ParameterSetName = "ByteArray" )]
[ byte[] ] $ByteArray ,
[ Parameter ( Mandatory , ParameterSetName = "ByteList" )]
[ System.Collections.Generic.List[byte] ] $ByteList ,
[ Parameter ( Mandatory , ParameterSetName = "ByteCollection" )]
[ System.Collections.ObjectModel.Collection[byte] ] $ByteCollection ,
[ Parameter ( ParameterSetName = "Default" )]
$Value
)
}
function NotNullFunc {
param (
[ ValidateNotNull ()]
$Value ,
[ string ] $TestType
)
switch ( $TestType ) {
"COM-Enumerable" { $Value | ForEach-Object Name }
"Enumerator" {
$items = foreach ( $i in $Value ) { $i }
$items -join ","
}
}
}
function NotNullOrEmptyFunc {
param (
[ ValidateNotNullOrEmpty ()]
$Value ,
[ string ] $TestType
)
switch ( $TestType ) {
"COM-Enumerable" { $Value | ForEach-Object Name }
"Enumerator" {
$items = foreach ( $i in $Value ) { $i }
$items -join ","
}
}
}
2022-08-10 03:48:08 +10:00
function NotNullOrWhiteSpaceFunc {
param (
[ ValidateNotNullOrWhiteSpace ()]
$Value ,
[ string ] $TestType
)
switch ( $TestType ) {
"COM-Enumerable" { $Value | ForEach-Object Name }
"Enumerator" {
$items = foreach ( $i in $Value ) { $i }
$items -join ","
}
}
}
2017-11-30 14:46:09 -08:00
$filePath = Join-Path -Path $PSHOME -ChildPath System . Management . Automation . dll
$byteArray = [ System.IO.File ]:: ReadAllBytes ( $filePath )
$byteList = [ System.Collections.Generic.List[byte] ] $byteArray
$byteCollection = [ System.Collections.ObjectModel.Collection[byte] ] $byteArray
## Use the running time of 'MandatoryFunc -Value $byteArray' as the baseline time
2017-12-06 12:46:53 -08:00
## because it does no check on the argument.
2017-11-30 14:46:09 -08:00
$baseline = ( Measure-Command { MandatoryFunc -Value $byteArray }). Milliseconds
2017-12-06 12:46:53 -08:00
## Running time should be less than 'UpperBoundTime'
## This is not really a performance test (perf test cannot run reliably in our CI), but a test
## to make sure we don't check the elements of a value-type collection.
## The crossgen'ed 'S.M.A.dll' is about 28mb in size, and it would take more than 2000ms if we
## check each byte of the array, list or collection. We use ($baseline + 200)ms as the upper
## bound value in tests to prove that we don't check each byte.
$UpperBoundTime = $baseline + 200
2017-11-30 14:46:09 -08:00
if ( $IsWindows ) {
$null = New-Item -Path $TESTDRIVE / file1
}
$testCases = @ (
@ { ScriptBlock = { MandatoryFunc -ByteArray $byteArray } }
@ { ScriptBlock = { MandatoryFunc -ByteList $byteList } }
@ { ScriptBlock = { MandatoryFunc -ByteCollection $byteCollection } }
@ { ScriptBlock = { NotNullFunc -Value $byteArray } }
@ { ScriptBlock = { NotNullFunc -Value $byteList } }
@ { ScriptBlock = { NotNullFunc -Value $byteCollection } }
@ { ScriptBlock = { NotNullOrEmptyFunc -Value $byteArray } }
@ { ScriptBlock = { NotNullOrEmptyFunc -Value $byteList } }
@ { ScriptBlock = { NotNullOrEmptyFunc -Value $byteCollection } }
2022-08-10 03:48:08 +10:00
@ { ScriptBlock = { NotNullOrWhiteSpaceFunc -Value $byteArray } }
@ { ScriptBlock = { NotNullOrWhiteSpaceFunc -Value $byteList } }
@ { ScriptBlock = { NotNullOrWhiteSpaceFunc -Value $byteCollection } }
2017-11-30 14:46:09 -08:00
)
}
It "Validate running time '<ScriptBlock>'" -TestCases $testCases {
param ( $ScriptBlock )
2018-03-14 12:13:32 -07:00
( Measure-Command $ScriptBlock ). Milliseconds | Should -BeLessThan $UpperBoundTime
2017-11-30 14:46:09 -08:00
}
2022-08-10 03:48:08 +10:00
It "COM enumerable argument should work with 'ValidateNotNull', 'ValidateNotNullOrEmpty' and 'ValidateNotNullOrWhiteSpace'" -Skip: (! $IsWindows ) {
2017-11-30 14:46:09 -08:00
$shell = New-Object -ComObject "Shell.Application"
$folder = $shell . Namespace ( " $TESTDRIVE " )
$items = $folder . Items ()
2018-03-14 12:13:32 -07:00
NotNullFunc -Value $items -TestType "COM-Enumerable" | Should -Be "file1"
NotNullOrEmptyFunc -Value $items -TestType "COM-Enumerable" | Should -Be "file1"
2022-08-10 03:48:08 +10:00
NotNullOrWhiteSpaceFunc -Value $items -TestType "COM-Enumerable" | Should -Be "file1"
2017-11-30 14:46:09 -08:00
}
2022-08-10 03:48:08 +10:00
It "Enumerator argument should work with 'ValidateNotNull', 'ValidateNotNullOrEmpty' and 'ValidateNotNullOrWhiteSpace'" {
2017-11-30 14:46:09 -08:00
$data = @ ( 1 , 2 , 3 )
2018-03-14 12:13:32 -07:00
NotNullFunc -Value $data . GetEnumerator () -TestType "Enumerator" | Should -Be "1,2,3"
NotNullOrEmptyFunc -Value $data . GetEnumerator () -TestType "Enumerator" | Should -Be "1,2,3"
2022-08-10 03:48:08 +10:00
NotNullOrWhiteSpaceFunc -Value $data . GetEnumerator () -TestType "Enumerator" | Should -Be "1,2,3"
2017-11-30 14:46:09 -08:00
}
It "'ValidateNotNull' should throw on null element of a collection argument" {
## Should throw on null element
2018-03-14 12:13:32 -07:00
{ NotNullFunc -Value @ ( "string" , $null , 2 ) } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullFunc"
2017-11-30 14:46:09 -08:00
## Should not throw on empty string element
2018-03-14 12:13:32 -07:00
{ NotNullFunc -Value @ ( "string" , "" , 2 ) } | Should -Not -Throw
2017-11-30 14:46:09 -08:00
## Should not throw on an empty collection
2018-03-14 12:13:32 -07:00
{ NotNullFunc -Value @ () } | Should -Not -Throw
2017-11-30 14:46:09 -08:00
}
It "'ValidateNotNullOrEmpty' should throw on null element of a collection argument or empty collection/dictionary" {
2018-03-14 12:13:32 -07:00
{ NotNullOrEmptyFunc -Value @ ( "string" , $null , 2 ) } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrEmptyFunc"
{ NotNullOrEmptyFunc -Value @ ( "string" , "" , 2 ) } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrEmptyFunc"
{ NotNullOrEmptyFunc -Value @ () } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrEmptyFunc"
{ NotNullOrEmptyFunc -Value @ {} } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrEmptyFunc"
2017-11-30 14:46:09 -08:00
}
2022-08-10 03:48:08 +10:00
It "'ValidateNotNullOrWhiteSpace' should throw on null element of a collection argument, white-space only string element of a collection argument or empty collection/dictionary" {
{ NotNullOrWhiteSpaceFunc -Value @ ( "string" , $null , 2 ) } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrWhiteSpaceFunc"
{ NotNullOrWhiteSpaceFunc -Value @ ( "string" , "" , 2 ) } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrWhiteSpaceFunc"
{ NotNullOrWhiteSpaceFunc -Value @ ( "string" , " " , 2 ) } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrWhiteSpaceFunc"
{ NotNullOrWhiteSpaceFunc -Value @ () } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrWhiteSpaceFunc"
{ NotNullOrWhiteSpaceFunc -Value @ {} } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrWhiteSpaceFunc"
}
It "'ValidateNotNull' should throw on a scalar null value" {
{ NotNullFunc -Value $null } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullFunc"
}
It "'ValidateNotNullOrEmpty' should throw on a scalar null value and scalar empty string" {
{ NotNullOrEmptyFunc -Value $null } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrEmptyFunc"
{ NotNullOrEmptyFunc -Value "" } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrEmptyFunc"
}
It "'ValidateNotNullOrWhiteSpace' should throw on a scalar null value, scalar empty string and scalar white-space string" {
{ NotNullOrWhiteSpaceFunc -Value $null } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrWhiteSpaceFunc"
{ NotNullOrWhiteSpaceFunc -Value "" } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrWhiteSpaceFunc"
{ NotNullOrWhiteSpaceFunc -Value " " } | Should -Throw -ErrorId "ParameterArgumentValidationError,NotNullOrWhiteSpaceFunc"
}
2017-11-30 14:46:09 -08:00
It "Mandatory parameter should throw on empty collection" {
2018-03-14 12:13:32 -07:00
{ MandatoryFunc -ByteArray ([ byte[] ] @ ()) } | Should -Throw -ErrorId "ParameterArgumentValidationErrorEmptyArrayNotAllowed,MandatoryFunc"
{ MandatoryFunc -ByteList ([ System.Collections.Generic.List[byte] ] @ ()) } | Should -Throw -ErrorId "ParameterArgumentValidationErrorEmptyCollectionNotAllowed,MandatoryFunc"
{ MandatoryFunc -ByteList ([ System.Collections.ObjectModel.Collection[byte] ] @ ()) } | Should -Throw -ErrorId "ParameterArgumentValidationErrorEmptyCollectionNotAllowed,MandatoryFunc"
2017-11-30 14:46:09 -08:00
}
}
2017-04-22 05:01:04 +04:00
}