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:
Ilya
2017-12-28 10:15:06 +04:00
committed by GitHub
parent c17a975683
commit 7257404929
2 changed files with 32 additions and 0 deletions
@@ -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" {