Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add ForEach and Where methods to [PSCustomobject]#5756

Merged
iSazonov merged 5 commits intoPowerShell:masterfrom
iSazonov:pscustomobject-foreach-where
Jan 5, 2018
Merged

Add ForEach and Where methods to [PSCustomobject]#5756
iSazonov merged 5 commits intoPowerShell:masterfrom
iSazonov:pscustomobject-foreach-where

Conversation

@iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Dec 29, 2017

PR Summary

Close #3671

  • Add ForEach and Where methods to [PSCustomobject]
    Now following works with singletons like:

     ([pscustomobject]@{ foo = 'bar' }).ForEach({$_})
     ([pscustomobject]@{ foo = 'bar' }).Where({$_})
    
  • Add tests

PR Checklist

Note: Please mark anything not applicable to this PR NA.

@iSazonov iSazonov self-assigned this Dec 29, 2017
(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()));
Copy link
Contributor

Choose a reason for hiding this comment

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

You should use LanguagePrimitives.ConvertTo<int>(args[2]).

It will:

  • Not invoke ToString if the type is already int.
  • Support additional conversions, e.g. "0x42"
  • Provide a consistent error message

Copy link
Contributor

Choose a reason for hiding this comment

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

You should also use LanguagePrimitives.ConvertTo<WhereOperatorSelectionMode>(args[1] as well for similar reasons.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@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
Copy link
Contributor

Choose a reason for hiding this comment

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

Singletons

Copy link
Member

@SteveL-MSFT SteveL-MSFT left a comment

Choose a reason for hiding this comment

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

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.
Copy link
Member

Choose a reason for hiding this comment

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

I think grammatically, this should be:

// The object doesn't have `Where` and `ForEach` methods.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

}

// The object haven't Where and ForEach methods.
// As last resort, we invoking Where and ForEach operators on singletons like
Copy link
Member

Choose a reason for hiding this comment

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

Change to:

// As a last resort, we invoke `Where` and `ForEach` operators on singlestons like

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

}
}

$psmemberset = new-object System.Management.Automation.PSMemberSet 'setname1'
Copy link
Member

Choose a reason for hiding this comment

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

New-Object

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

$document = new-object System.Xml.XmlDocument
$document.LoadXml("<book ISBN='12345'><title>Pride And Prejudice</title><price>19.95</price></book>")
$doc = $document.DocumentElement
}
Copy link
Member

Choose a reason for hiding this comment

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

add newline after closing brace

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

$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" {
Copy link
Member

Choose a reason for hiding this comment

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

Capitalize can to Can. Also, I think it's DotNet

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

It "Can access all properties" {
$props = $pso.psobject.Properties.Match("*")
$props | should Not BeNullOrEmpty
$props["ProcessName"].Value |Should be $processName
Copy link
Member

Choose a reason for hiding this comment

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

Since the test is accessing all properties, it seems like we should validate the property count.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't know that is expected count.

Copy link
Member

Choose a reason for hiding this comment

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

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())
Copy link
Member

Choose a reason for hiding this comment

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

extra space after Invoke()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

class TestCodeMethodClass {
static [int] TestCodeMethod([PSObject] $target, [int] $i)
{
return 1;
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be better to return $i instead of 1?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

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
Copy link
Member

Choose a reason for hiding this comment

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

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".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added test and changed the comment.

$x = 5
$x.ForEach({$_}) | Should Be 5
(5).ForEach({$_}) | Should Be 5
("a").ForEach({$_}) | Should Be "a"
Copy link
Member

Choose a reason for hiding this comment

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

With text, we can use BeExactly

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Contributor

@lzybkr lzybkr left a comment

Choose a reason for hiding this comment

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

I sign off on the code change.

@iSazonov
Copy link
Collaborator Author

iSazonov commented Jan 4, 2018

Reopen to restart CIs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Treating scalars implicitly as collections doesn't fully work with custom objects ([pscustomobject]) - lacks a .Count property

3 participants