mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add Count and Length properties to [PSCustomobject] (#5745)
Related #3671 •Add Count and Length properties to [PSCustomobject]. Now following returns 1: ([pscustomobject] @{ foo = 'bar' }).Count ([pscustomobject] @{ foo = 'bar' }).Length •Add tests
This commit is contained in:
@@ -5690,6 +5690,11 @@ namespace System.Management.Automation.Language
|
||||
return memberInfo.Value;
|
||||
}
|
||||
|
||||
if (string.Equals(member, "Length", StringComparison.OrdinalIgnoreCase) || string.Equals(member, "Count", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (context != null && context.IsStrictVersion(2))
|
||||
{
|
||||
// If the member is undefined and we're in strict mode, throw an exception...
|
||||
|
||||
@@ -112,6 +112,33 @@ Describe "Adapter Tests" -tags "CI" {
|
||||
Remove-TypeData TestCodeMethodInvokationWithVoidReturn
|
||||
}
|
||||
}
|
||||
|
||||
It "Count and length property works for singletons" {
|
||||
# Return magic Count and Length property if it absent.
|
||||
$x = 5
|
||||
$x.Count | Should Be 1
|
||||
$x.Length | Should Be 1
|
||||
|
||||
$null.Count | Should Be 0
|
||||
$null.Length | Should Be 0
|
||||
|
||||
(10).Count | Should Be 1
|
||||
(10).Length | Should Be 1
|
||||
|
||||
("a").Count | Should Be 1
|
||||
# The Length property exists for strings, so we skip the test in the context.
|
||||
# ("a").Length | Should Be 1
|
||||
|
||||
([psobject] @{ foo = 'bar' }).Count | Should Be 1
|
||||
([psobject] @{ foo = 'bar' }).Length | Should Be 1
|
||||
|
||||
([pscustomobject] @{ foo = 'bar' }).Count | Should Be 1
|
||||
([pscustomobject] @{ foo = 'bar' }).Length | Should Be 1
|
||||
|
||||
# Return real Count and Length property if it present.
|
||||
([pscustomobject] @{ foo = 'bar'; count = 5 }).Count | Should Be 5
|
||||
([pscustomobject] @{ foo = 'bar'; length = 5 }).Length | Should Be 5
|
||||
}
|
||||
}
|
||||
|
||||
Describe "Adapter XML Tests" -tags "CI" {
|
||||
|
||||
Reference in New Issue
Block a user