Add ForEach and Where methods to [PSCustomobject]#5756
Add ForEach and Where methods to [PSCustomobject]#5756iSazonov merged 5 commits intoPowerShell:masterfrom
Conversation
| (WhereOperatorSelectionMode)Enum.Parse(typeof(WhereOperatorSelectionMode), args[1].ToString()), 0); | ||
| case 3: | ||
| return EnumerableOps.Where(enumerator, args[0] as ScriptBlock, | ||
| (WhereOperatorSelectionMode)Enum.Parse(typeof(WhereOperatorSelectionMode), args[1].ToString()), int.Parse(args[2].ToString())); |
There was a problem hiding this comment.
You should use LanguagePrimitives.ConvertTo<int>(args[2]).
It will:
- Not invoke
ToStringif the type is alreadyint. - Support additional conversions, e.g. "0x42"
- Provide a consistent error message
There was a problem hiding this comment.
You should also use LanguagePrimitives.ConvertTo<WhereOperatorSelectionMode>(args[1] as well for similar reasons.
There was a problem hiding this comment.
@lzybkr Thanks for help! I thought about it but didn't know how to use it properly.
| } | ||
|
|
||
| // The object haven't Where and ForEach methods. | ||
| // As last resort, we invoking Where and ForEach operators on singletions like |
SteveL-MSFT
left a comment
There was a problem hiding this comment.
I realize a bunch of the changes were simply reformatting existing tests, but there's a few small changes to improve the tests since you're already touching them :)
| return methodInfo.Invoke(args); | ||
| } | ||
|
|
||
| // The object haven't Where and ForEach methods. |
There was a problem hiding this comment.
I think grammatically, this should be:
// The object doesn't have `Where` and `ForEach` methods.| } | ||
|
|
||
| // The object haven't Where and ForEach methods. | ||
| // As last resort, we invoking Where and ForEach operators on singletons like |
There was a problem hiding this comment.
Change to:
// As a last resort, we invoke `Where` and `ForEach` operators on singlestons like| } | ||
| } | ||
|
|
||
| $psmemberset = new-object System.Management.Automation.PSMemberSet 'setname1' |
| $document = new-object System.Xml.XmlDocument | ||
| $document.LoadXml("<book ISBN='12345'><title>Pride And Prejudice</title><price>19.95</price></book>") | ||
| $doc = $document.DocumentElement | ||
| } |
There was a problem hiding this comment.
add newline after closing brace
| $document.LoadXml("<book ISBN='12345'><title>Pride And Prejudice</title><price>19.95</price></book>") | ||
| $doc = $document.DocumentElement | ||
| } | ||
| It "can get a Dotnet parameterized property" { |
There was a problem hiding this comment.
Capitalize can to Can. Also, I think it's DotNet
| It "Can access all properties" { | ||
| $props = $pso.psobject.Properties.Match("*") | ||
| $props | should Not BeNullOrEmpty | ||
| $props["ProcessName"].Value |Should be $processName |
There was a problem hiding this comment.
Since the test is accessing all properties, it seems like we should validate the property count.
There was a problem hiding this comment.
I don't know that is expected count.
There was a problem hiding this comment.
It's probably safe to maybe check GreaterThan 1?
| } | ||
| It "Can invoke a method" { | ||
| $method = $pso.psobject.Methods["ToString"] | ||
| $method.Invoke() | should be ($pso.ToString()) |
| class TestCodeMethodClass { | ||
| static [int] TestCodeMethod([PSObject] $target, [int] $i) | ||
| { | ||
| return 1; |
There was a problem hiding this comment.
Wouldn't it be better to return $i instead of 1?
There was a problem hiding this comment.
I don't know. It is used only in one test and the test works well.
|
|
||
| ("a").Count | Should Be 1 | ||
| # The Length property exists for strings, so we skip the test in the context. | ||
| # ("a").Length | Should Be 1 |
There was a problem hiding this comment.
Maybe better to have the test but change the comment that Length should use the member for [string] type. In which case, it would be better to test with "aa".
There was a problem hiding this comment.
Added test and changed the comment.
| $x = 5 | ||
| $x.ForEach({$_}) | Should Be 5 | ||
| (5).ForEach({$_}) | Should Be 5 | ||
| ("a").ForEach({$_}) | Should Be "a" |
There was a problem hiding this comment.
With text, we can use BeExactly
lzybkr
left a comment
There was a problem hiding this comment.
I sign off on the code change.
|
Reopen to restart CIs. |
PR Summary
Close #3671
Add ForEach and Where methods to [PSCustomobject]
Now following works with singletons like:
Add tests
PR Checklist
Note: Please mark anything not applicable to this PR
NA.[feature]if the change is significant or affectes feature testsWIP:to the beginning of the title and remove the prefix when the PR is ready.