-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix array expression to not return null or throw error #4296
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
e987ef1
be0a266
50b55cd
bdbe68d
33b5461
88a68ab
237c2bb
ce4d2d9
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 |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| Describe "ArrayExpression Tests" -Tags "CI" { | ||
| It "@([object[]](1,2,3)) should return a 3-element array of object[]" { | ||
| $result = @([object[]](1,2,3)) | ||
| $result.GetType().FullName | Should Be "System.Object[]" | ||
| $result.Length | Should Be 3 | ||
| } | ||
|
|
||
| It "@([int[]](1,2,3)) should return a 3-element array of object[]" { | ||
| $result = @([int[]](1,2,3)) | ||
|
Collaborator
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. Should we test complex types? PSCustomObject?
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. For any array type other than
Collaborator
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. Sorry, I mean member types.
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. Oh, the member type doesn't matter for the scenario of this fix. It's OK as long as the conversion works. |
||
| $result.GetType().FullName | Should Be "System.Object[]" | ||
| $result.Length | Should Be 3 | ||
| } | ||
|
|
||
| It "@([object[]]`$null) should return a 1-element(`$null) array of object[]" { | ||
| $result = @([object[]]$null) | ||
| $result.GetType().FullName | Should Be "System.Object[]" | ||
| $result.Length | Should Be 1 | ||
| $result[0] | Should Be $null | ||
| } | ||
|
|
||
| It "@([int[]]`$null) should return a 1-element(`$null) array of object[]" { | ||
| $result = @([int[]]$null) | ||
| $result.GetType().FullName | Should Be "System.Object[]" | ||
| $result.Length | Should Be 1 | ||
| $result[0] | Should Be $null | ||
| } | ||
|
|
||
| It "@([object[]][System.Management.Automation.Internal.AutomationNull]::Value) should return a 1-element(`$null) array of object[]" { | ||
| $result = @([object[]][System.Management.Automation.Internal.AutomationNull]::Value) | ||
| $result.GetType().FullName | Should Be "System.Object[]" | ||
| $result.Length | Should Be 1 | ||
| $result[0] | Should Be $null | ||
| } | ||
|
|
||
| It "@([int[]][System.Management.Automation.Internal.AutomationNull]::Value) should return a 1-element(`$null) array of object[]" { | ||
| $result = @([int[]][System.Management.Automation.Internal.AutomationNull]::Value) | ||
| $result.GetType().FullName | Should Be "System.Object[]" | ||
| $result.Length | Should Be 1 | ||
| $result[0] | Should Be $null | ||
| } | ||
|
|
||
| It "@(`$null) should return a 1-element(`$null) array of object[]" { | ||
| $result = @($null) | ||
| $result.GetType().FullName | Should Be "System.Object[]" | ||
| $result.Length | Should Be 1 | ||
| $result[0] | Should Be $null | ||
| } | ||
|
|
||
| It "@([System.Management.Automation.Internal.AutomationNull]::Value) should return an empty array of object[]" { | ||
| $result = @([System.Management.Automation.Internal.AutomationNull]::Value) | ||
| $result.GetType().FullName | Should Be "System.Object[]" | ||
| $result.Length | Should Be 0 | ||
| } | ||
|
|
||
| It "@([object[]]`$a) should return a new array" { | ||
| $a = 1,2,3 | ||
| $result = @([object[]]$a) | ||
| $result.GetType().FullName | Should Be "System.Object[]" | ||
| $result.Length | Should Be 3 | ||
| } | ||
|
|
||
| It "@([int[]]`$a) should return a new array" { | ||
| $a = 1,2,3 | ||
| $result = @([int[]]$a) | ||
| $result.GetType().FullName | Should Be "System.Object[]" | ||
| $result.Length | Should Be 3 | ||
| } | ||
|
|
||
| It "@([System.Collections.Generic.List[object]]`$null) should return a 1-element(`$null) array of object[]" { | ||
| $result = @([System.Collections.Generic.List[object]]$null) | ||
| $result.GetType().FullName | Should Be "System.Object[]" | ||
| $result.Length | Should Be 1 | ||
| $result[0] | Should Be $null | ||
| } | ||
| } | ||
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.
I believe we should check $result.Length before $result.GetType() in all test here. This is not important in these tests, but it is a bad pattern for other tests in which the $result are calculated. (I mean that somebody can copy-paste the bad pattern).
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.
What do you mean by
the $result are calculated? Can you please elaborate a bit and give an example?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.
From Get-Content.Tests.ps1:
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.
Can we use Should BeOfType?
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.
To use
Should BeOfTypeI need to wrap$resultin a new array because it will be unraveled otherwise. I feel it's clearer to just useGetType().FullNamein this case.As for the bad pattern, I'm still confused. Why do you think putting
$result.Length | Should Be 3after the type check would be a bad pattern? More technically speaking, the length check should happen after we know$resultis an array, otherwise we might (actually impossible) end up checking theLengthproperty of some random type :)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.
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.
Ah, I see your point. A
Should Not BeNullOrEmptycheck should be done in that case.