-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Add ScriptBlock substitution overload to -replace operator #6029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
daxian-dbw
merged 2 commits into
PowerShell:master
from
IISResetMe:overload-ReplaceOp-Lambda
Mar 14, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
test/powershell/Language/Operators/ReplaceOperator.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| Describe "Replace Operator" -Tags CI { | ||
| Context "Replace operator" { | ||
| It "Replace operator can replace string values using regular expressions" { | ||
| $res = "Get-Process" -replace "Get", "Stop" | ||
| $res | Should BeExactly "Stop-Process" | ||
|
|
||
| $res = "image.gif" -replace "\.gif$",".jpg" | ||
| $res | Should BeExactly "image.jpg" | ||
| } | ||
|
|
||
| It "Replace operator can be case-insensitive and case-sensitive" { | ||
| $res = "book" -replace "B","C" | ||
| $res | Should BeExactly "Cook" | ||
|
|
||
| $res = "book" -ireplace "B","C" | ||
| $res | Should BeExactly "Cook" | ||
|
|
||
| $res = "book" -creplace "B","C" | ||
| $res | Should BeExactly "book" | ||
| } | ||
|
|
||
| It "Replace operator can take 2 arguments, a mandatory pattern, and an optional substitution" { | ||
| $res = "PowerPoint" -replace "Point","Shell" | ||
| $res | Should BeExactly "PowerShell" | ||
|
|
||
| $res = "PowerPoint" -replace "Point" | ||
| $res | Should BeExactly "Power" | ||
| } | ||
| } | ||
|
|
||
| Context "Replace operator substitutions" { | ||
| It "Replace operator supports numbered substitution groups using ```$n" { | ||
| $res = "domain.example" -replace ".*\.(\w+)$","Tld of '`$0' is - '`$1'" | ||
| $res | Should BeExactly "Tld of 'domain.example' is - 'example'" | ||
| } | ||
|
|
||
| It "Replace operator supports named substitution groups using ```${name}" { | ||
| $res = "domain.example" -replace ".*\.(?<tld>\w+)$","`${tld}" | ||
| $res | Should BeExactly "example" | ||
| } | ||
|
|
||
| It "Replace operator can take a ScriptBlock in place of a substitution string" { | ||
| $res = "ID ABC123" -replace "\b[A-C]+", {return "X" * $_[0].Value.Length} | ||
| $res | Should BeExactly "ID XXX123" | ||
| } | ||
|
|
||
| It "Replace operator can take a MatchEvaluator in place of a substitution string" { | ||
| $matchEvaluator = {return "X" * $args[0].Value.Length} -as [System.Text.RegularExpressions.MatchEvaluator] | ||
| $res = "ID ABC123" -replace "\b[A-C]+", $matchEvaluator | ||
| $res | Should BeExactly "ID XXX123" | ||
| } | ||
|
|
||
| It "Replace operator can take a static PSMethod in place of a substitution string" { | ||
| class R { | ||
| static [string] Replace([System.Text.RegularExpressions.Match]$Match) { | ||
| return "X" * $Match.Value.Length | ||
| } | ||
| } | ||
| $substitutionMethod = [R]::Replace | ||
| $res = "ID 0000123" -replace "\b0+", $substitutionMethod | ||
| $res | Should BeExactly "ID XXXX123" | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see tests for
PSObject.ToStringParser- why we use this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used in the original code, to convert an object to a string:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What PowerShell magic it give us? If it is useful I'd prefer to see related tests.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It converts an object to a string in a powershell way and is used extensively in powershell. You can see the code yourself for more information.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an internal method at https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/MshObject.cs#L1206
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@daxian-dbw Thanks! I thought that using the method add some magic in the PR but no - it is only to get string.