-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Skip null-element check for collections with a value-type element type #5432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
602472d
c303e9f
38364cd
5dfcb73
cdc06a9
f64c996
a73c7c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a parameter like:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A |
||
| ) | ||
|
|
||
| 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 | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is in the context isEmpty = !isElementValueType ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the check of element type doesn't involve inspecting the elements of the collection. You can see that the type of
argumentsis passed in instead of theargumentsitself.Please see the constructor of
ParameterCollectionTypeInformationfor details.