Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5690,6 +5690,11 @@ internal static object GetAdaptedValue(object obj, string member)
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...
Expand Down
27 changes: 27 additions & 0 deletions test/powershell/engine/ETS/Adapter.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also test when a pscustomobject has a count or length property to make sure we return the correct value.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


# 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" {
Expand Down