Allow 'name' as an alias key for 'label' in ConvertTo-Html, allow the 'width' entry to be an integer (#8426)

This commit is contained in:
Michael Klement 2019-01-18 14:02:22 -05:00 committed by Aditya Patwardhan
parent f1218bd3d9
commit 20919ee793
2 changed files with 28 additions and 8 deletions

View file

@ -326,9 +326,11 @@ namespace Microsoft.PowerShell.Commands
protected override void SetEntries()
{
this.hashEntries.Add(new ExpressionEntryDefinition());
this.hashEntries.Add(new HashtableEntryDefinition(ConvertHTMLParameterDefinitionKeys.LabelEntryKey, new Type[] { typeof(string) }));
this.hashEntries.Add(new HashtableEntryDefinition(ConvertHTMLParameterDefinitionKeys.AlignmentEntryKey, new Type[] { typeof(string) }));
this.hashEntries.Add(new HashtableEntryDefinition(ConvertHTMLParameterDefinitionKeys.WidthEntryKey, new Type[] { typeof(string) }));
this.hashEntries.Add(new LabelEntryDefinition());
this.hashEntries.Add(new HashtableEntryDefinition(ConvertHTMLParameterDefinitionKeys.AlignmentEntryKey, new[] { typeof(string) }));
// Note: We accept "width" as either string or int.
this.hashEntries.Add(new HashtableEntryDefinition(ConvertHTMLParameterDefinitionKeys.WidthEntryKey, new[] { typeof(string), typeof(int) }));
}
}
@ -362,7 +364,11 @@ namespace Microsoft.PowerShell.Commands
{
string label = p.GetEntry(ConvertHTMLParameterDefinitionKeys.LabelEntryKey) as string;
string alignment = p.GetEntry(ConvertHTMLParameterDefinitionKeys.AlignmentEntryKey) as string;
string width = p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as string;
// Accept the width both as a string and as an int.
string width;
int? widthNum = p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as int?;
width = widthNum != null ? widthNum.Value.ToString() : p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as string;
PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
List<PSPropertyExpression> resolvedNames = ex.ResolveNames(_inputObject);
foreach (PSPropertyExpression resolvedName in resolvedNames)

View file

@ -169,15 +169,29 @@ After the object
$returnString | Should -Be '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
}
It "Test ConvertTo-HTML supports scriptblock-based calculated properties: by hashtable"{
$returnString = ($customObject | ConvertTo-HTML @{ l='NewAge'; e={ $_.Age + 1 } }) -join $newLine
It "Test ConvertTo-HTML supports scriptblock-based calculated properties: by hashtable" {
$returnString = ($customObject | ConvertTo-HTML @{ l = 'NewAge'; e = { $_.Age + 1 } }) -join $newLine
$returnString | Should -Match '\b43\b'
}
It "Test ConvertTo-HTML supports scriptblock-based calculated properties: directly"{
It "Test ConvertTo-HTML supports scriptblock-based calculated properties: directly" {
$returnString = ($customObject | ConvertTo-HTML { $_.Age + 1 }) -join $newLine
$returnString | Should -Match '\b43\b'
}
}
It "Test ConvertTo-HTML calculated property supports 'name' key as alias of 'label'" {
$returnString = ($customObject | ConvertTo-Html @{ name = 'AgeRenamed'; e = 'Age'}) -join $newLine
$returnString | Should -Match 'AgeRenamed'
}
It "Test ConvertTo-HTML calculated property supports integer 'width' entry" {
$returnString = ($customObject | ConvertTo-Html @{ e = 'Age'; width = 10 }) -join $newLine
$returnString | Should -Match '\swidth\s*=\s*(["''])10\1'
}
It "Test ConvertTo-HTML calculated property supports string 'width' entry" {
$returnString = ($customObject | ConvertTo-Html @{ e = 'Age'; width = '10' }) -join $newLine
$returnString | Should -Match '\swidth\s*=\s*(["''])10\1'
}
}