mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Skip null-element check for collections with a value-type element type (#5432)
* Fix NotNullOrEmpty check logic * Fix a test issue on Unix
This commit is contained in:
committed by
Aditya Patwardhan
parent
52df947080
commit
b5f84c2b39
@@ -260,4 +260,124 @@ Describe 'Validate Attributes Tests' -Tags 'CI' {
|
||||
$ScriptBlock | Should Not Throw
|
||||
}
|
||||
}
|
||||
|
||||
Context "ValidateNotNull, ValidateNotNullOrEmpty and Not-Null-Or-Empty check for Mandatory parameter" {
|
||||
|
||||
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 ","
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$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
|
||||
$baseline = (Measure-Command { MandatoryFunc -Value $byteArray }).Milliseconds
|
||||
## Running time should be less than 'expected'
|
||||
$expected = $baseline + 20
|
||||
|
||||
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 } }
|
||||
)
|
||||
}
|
||||
|
||||
It "Validate running time '<ScriptBlock>'" -TestCases $testCases {
|
||||
param ($ScriptBlock)
|
||||
(Measure-Command $ScriptBlock).Milliseconds | Should BeLessThan $expected
|
||||
}
|
||||
|
||||
It "COM enumerable argument should work with 'ValidateNotNull' and 'ValidateNotNullOrEmpty'" -Skip:(!$IsWindows) {
|
||||
$shell = New-Object -ComObject "Shell.Application"
|
||||
$folder = $shell.Namespace("$TESTDRIVE")
|
||||
$items = $folder.Items()
|
||||
|
||||
NotNullFunc -Value $items -TestType "COM-Enumerable" | Should Be "file1"
|
||||
NotNullOrEmptyFunc -Value $items -TestType "COM-Enumerable" | Should Be "file1"
|
||||
}
|
||||
|
||||
It "Enumerator argument should work with 'ValidateNotNull' and 'ValidateNotNullOrEmpty'" {
|
||||
$data = @(1,2,3)
|
||||
NotNullFunc -Value $data.GetEnumerator() -TestType "Enumerator" | Should Be "1,2,3"
|
||||
NotNullOrEmptyFunc -Value $data.GetEnumerator() -TestType "Enumerator" | Should Be "1,2,3"
|
||||
}
|
||||
|
||||
It "'ValidateNotNull' should throw on null element of a collection argument" {
|
||||
## Should throw on null element
|
||||
{ NotNullFunc -Value @("string", $null, 2) } | Should Throw
|
||||
## Should not throw on empty string element
|
||||
{ NotNullFunc -Value @("string", "", 2) } | Should Not Throw
|
||||
## Should not throw on an empty collection
|
||||
{ NotNullFunc -Value @() } | Should Not Throw
|
||||
}
|
||||
|
||||
It "'ValidateNotNullOrEmpty' should throw on null element of a collection argument or empty collection/dictionary" {
|
||||
{ NotNullOrEmptyFunc -Value @("string", $null, 2) } | Should Throw
|
||||
{ NotNullOrEmptyFunc -Value @("string", "", 2) } | Should Throw
|
||||
{ NotNullOrEmptyFunc -Value @() } | Should Throw
|
||||
{ NotNullOrEmptyFunc -Value @{} } | Should Throw
|
||||
}
|
||||
|
||||
It "Mandatory parameter should throw on empty collection" {
|
||||
{ MandatoryFunc -ByteArray ([byte[]]@()) } | Should Throw
|
||||
{ MandatoryFunc -ByteList ([System.Collections.Generic.List[byte]]@()) } | Should Throw
|
||||
{ MandatoryFunc -ByteList ([System.Collections.ObjectModel.Collection[byte]]@()) } | Should Throw
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user