Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.
Closed
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
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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}"
}
]
}
28 changes: 25 additions & 3 deletions ConvertTo-Expression.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
186 changes: 182 additions & 4 deletions Tests/ConvertTo-Expression.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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"}
}

Expand Down Expand Up @@ -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"
Expand Down