diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c5fae95 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,21 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "PowerShell Interactive Session", + "type": "PowerShell", + "request": "launch", + "cwd": "" + }, + { + "name": "PowerShell: Launch Current File", + "type": "PowerShell", + "request": "launch", + "script": "${file}", + "cwd": "${file}" + } + ] +} \ No newline at end of file diff --git a/ConvertTo-Expression.ps1 b/ConvertTo-Expression.ps1 index 7f32eb3..4718dc8 100644 --- a/ConvertTo-Expression.ps1 +++ b/ConvertTo-Expression.ps1 @@ -171,6 +171,8 @@ Function ConvertTo-Expression { ) Begin { If (!$PSCmdlet.MyInvocation.ExpectingInput) {If ($Concatenate) {Write-Warning 'The concatenate switch only applies to pipeline input'} Else {$Concatenate = $True}} + + $allObjects = New-Object System.Collections.ArrayList $ListItem = $Null $Tab = $IndentChar * $Indentation Function Serialize($Object, $Iteration, $Indent) { @@ -265,7 +267,27 @@ Function ConvertTo-Expression { } } Process { - $Expression = (Serialize $Object).TrimEnd() - Try {[ScriptBlock]::Create($Expression)} Catch {$PSCmdlet.WriteError($_); $Expression} + [void]$allObjects.Add($Object) } -}; Set-Alias ctex ConvertTo-Expression + End { + if ($allObjects.Count -eq 1) + { + $expression = (Serialize -Object $allObjects[0]).TrimEnd() + } + else + { + $expression = (Serialize -Object $allObjects.ToArray()).TrimEnd() + } + try + { + [scriptblock]::Create($expression) + } + catch + { + $PSCmdlet.WriteError($_) + $expression + } + } +} + +Set-Alias ctex ConvertTo-Expression diff --git a/Tests/ConvertTo-Expression.Tests.ps1 b/Tests/ConvertTo-Expression.Tests.ps1 index 37f6803..edb3bf7 100644 --- a/Tests/ConvertTo-Expression.Tests.ps1 +++ b/Tests/ConvertTo-Expression.Tests.ps1 @@ -1,15 +1,19 @@ -$here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") -. "$here\$sut" +. "$PSScriptRoot\..\$sut" Function Should-BeEqualTo ($Value2, [Parameter(ValueFromPipeLine = $True)]$Value1) { $Value1 | Should -Be $Value2 $Value1 | Should -BeOfType $Value2.GetType().Name } -Function Test-Format ([String]$Expression, [Switch]$Strong, [Int]$Expand = 9) { +Function Test-Format ([String]$Expression, [Switch]$Strong, [Int]$Expand = 9, [switch]$UsePipeline) { $Object = &([ScriptBlock]::Create("$Expression")) - $Actual = ConvertTo-Expression $Object -Strong:$Strong -Expand $Expand + $Actual = if ($UsePipeline) { + $Object | ConvertTo-Expression -Strong:$Strong -Expand $Expand + } + else { + ConvertTo-Expression $Object -Strong:$Strong -Expand $Expand + } It "$Expression" {"$Actual" | Should -Be "$Expression"} } @@ -524,6 +528,180 @@ World "@ } + Context 'default formatting with pipeline support' { + + Test-Format "1" -UsePipeline + + Test-Format "'One'" -UsePipeline + +# Test-Format ",'One'" -UsePipeline + + Test-Format @" +1, +2, +3 +"@ -UsePipeline + + Test-Format @" +'One', +'Two', +'Three' +"@ -UsePipeline + + Test-Format @" +'One', +'Two', +'Three', +'Four' +"@ -UsePipeline + + Test-Format @" +'One', +(,'Two'), +'Three', +'Four' +"@ -UsePipeline + + Test-Format @" +'One', +( + 'Two', + 'Three' +), +'Four' +"@ -UsePipeline + + Test-Format @" +'One', +@{'Two' = 2}, +'Three', +'Four' +"@ -UsePipeline + + Test-Format @" +[pscustomobject]@{'value' = 1}, +[pscustomobject]@{'value' = 2}, +[pscustomobject]@{'value' = 3} +"@ -UsePipeline + + Test-Format @" +[pscustomobject]@{ + 'One' = 1 + 'Two' = 2 + 'Three' = 3 + 'Four' = 4 +} +"@ -UsePipeline + + Test-Format @" +'One', +[ordered]@{ + 'Two' = 2 + 'Three' = 3 +}, +'Four' +"@ -UsePipeline + + Test-Format "@{'One' = 1}" + + Test-Format @" +[ordered]@{ + 'One' = 1 + 'Two' = 2 + 'Three' = 3 + 'Four' = 4 +} +"@ -UsePipeline + + Test-Format @" +[ordered]@{ + 'One' = 1 + 'Two' = 2 + 'Three.1' = ,3.1 + 'Four' = 4 +} +"@ -UsePipeline + + Test-Format @" +[ordered]@{ + 'One' = 1 + 'Two' = 2 + 'Three.12' = + 3.1, + 3.2 + 'Four' = 4 +} +"@ -UsePipeline + + Test-Format @" +[ordered]@{ + 'One' = 1 + 'Two' = 2 + 'Three' = @{'One' = 3.1} + 'Four' = 4 +} +"@ -UsePipeline + + Test-Format @" +[ordered]@{ + 'One' = 1 + 'Two' = 2 + 'Three' = [ordered]@{ + 'One' = 3.1 + 'Two' = 3.2 + } + 'Four' = 4 +} +"@ -UsePipeline + + Test-Format @" +[ordered]@{ + 'String' = 'String' + 'HereString' = @' +Hello +World +'@ + 'Int' = 67 + 'Double' = 1.2 + 'Long' = 1234567890123456 + 'DateTime' = [datetime]'1963-10-07T17:56:53.8139055+02:00' + 'Version' = [version]'1.2.34567.890' + 'Guid' = [guid]'5f167621-6abe-4153-a26c-f643e1716720' + 'Script' = {2 * 3} + 'Array' = + 'One', + 'Two', + 'Three', + 'Four' + 'ByteArray' = + 1, + 2, + 3 + 'StringArray' = + 'One', + 'Two', + 'Three' + 'EmptyArray' = @() + 'SingleValueArray' = ,'one' + 'SubArray' = + 'One', + ( + 'Two', + 'Three' + ), + 'Four' + 'HashTable' = @{'Name' = 'Value'} + 'Ordered' = [ordered]@{ + 'One' = 1 + 'Two' = 2 + 'Three' = 3 + 'Four' = 4 + } + 'Object' = [pscustomobject]@{'Name' = 'Value'} +} +"@ -UsePipeline + } + Context 'strong formatting' { Test-Format -Strong "[int]1"