diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/convert-HTML.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/convert-HTML.cs index 8acbd47b444..e1349558ff6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/convert-HTML.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/convert-HTML.cs @@ -283,24 +283,12 @@ private void InitializeResolvedNameMshParameters() string width = p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as string; MshExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression; List resolvedNames = ex.ResolveNames(_inputObject); - if (resolvedNames.Count == 1) + foreach (MshExpression resolvedName in resolvedNames) { Hashtable ht = CreateAuxPropertyHT(label, alignment, width); - if (ex.Script != null) - ht.Add(FormatParameterDefinitionKeys.ExpressionEntryKey, ex.Script); - else - ht.Add(FormatParameterDefinitionKeys.ExpressionEntryKey, ex.ToString()); + ht.Add(FormatParameterDefinitionKeys.ExpressionEntryKey, resolvedName.ToString()); resolvedNameProperty.Add(ht); } - else - { - foreach (MshExpression resolvedName in resolvedNames) - { - Hashtable ht = CreateAuxPropertyHT(label, alignment, width); - ht.Add(FormatParameterDefinitionKeys.ExpressionEntryKey, resolvedName.ToString()); - resolvedNameProperty.Add(ht); - } - } } _resolvedNameMshParameters = ProcessParameter(resolvedNameProperty.ToArray()); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Html.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Html.Tests.ps1 new file mode 100644 index 00000000000..a77e8d04fe0 --- /dev/null +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Html.Tests.ps1 @@ -0,0 +1,153 @@ +Describe "ConvertTo-Html Tests" -Tags "CI" { + + BeforeAll { + $customObject = [pscustomobject]@{"Name" = "John Doe"; "Age" = 42; "Friends" = ("Jack", "Jill")} + $newLine = "`r`n" + } + + function normalizeLineEnds([string]$text) + { + $text -replace "`r`n?|`n", "`r`n" + } + + It "Test ConvertTo-Html with no parameters" { + $returnObject = $customObject | ConvertTo-Html + $returnObject -is [System.Array] | Should Be $true + $returnString = $returnObject -join $newLine + $expectedValue = normalizeLineEnds @" + + + +HTML TABLE + + + + + +
NameAgeFriends
John Doe42System.Object[]
+ +"@ + $returnString | Should Be $expectedValue + } + + It "Test ConvertTo-Html Fragment parameter" { + $returnString = ($customObject | ConvertTo-Html -Fragment) -join $newLine + $expectedValue = normalizeLineEnds @" + + + + +
NameAgeFriends
John Doe42System.Object[]
+"@ + $returnString | Should Be $expectedValue + } + + It "Test ConvertTo-Html as List" { + $returnString = ($customObject | ConvertTo-Html -As List) -join $newLine + $expectedValue = normalizeLineEnds @" + + + +HTML TABLE + + + + + +
Name:John Doe
Age:42
Friends:System.Object[]
+ +"@ + $returnString | Should Be $expectedValue + } + + It "Test ConvertTo-Html specified properties" { + $returnString = ($customObject | ConvertTo-Html -Property Name, Friends -As List) -join $newLine + $expectedValue = normalizeLineEnds @" + + + +HTML TABLE + + + + +
Name:John Doe
Friends:System.Object[]
+ +"@ + $returnString | Should Be $expectedValue + } + + It "Test ConvertTo-Html using page parameters" { + $returnString = ($customObject | ConvertTo-Html -Title "Custom Object" -Body "Body Text" -CssUri "page.css" -As List) -join $newLine + $expectedValue = normalizeLineEnds @" + + + +Custom Object + + +Body Text + + + + +
Name:John Doe
Age:42
Friends:System.Object[]
+ +"@ + $returnString | Should Be $expectedValue + } + + It "Test ConvertTo-Html pre and post" { + $returnString = ($customObject | ConvertTo-Html -PreContent "Before the object" -PostContent "After the object") -join $newLine + $expectedValue = normalizeLineEnds @" + + + +HTML TABLE + +Before the object + + + + +
NameAgeFriends
John Doe42System.Object[]
+After the object + +"@ + $returnString | Should Be $expectedValue + } + It "Test ConvertTo-Html header with default single property" { + $returnString = ($customObject | Select-Object Name | ConvertTo-Html) -join $newLine + $expectedValue = normalizeLineEnds @" + + + +HTML TABLE + + + + + +
Name
John Doe
+ +"@ + $returnString | Should Be $expectedValue + } + It "Test ConvertTo-Html header with matched single property" { + $returnString = ($customObject | ConvertTo-Html -Property Name*) -join $newLine + $expectedValue = normalizeLineEnds @" + + + +HTML TABLE + + + + + +
Name
John Doe
+ +"@ + $returnString | Should Be $expectedValue + } +}