Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,11 @@ internal class ConvertHTMLExpressionParameterDefinition : CommandParameterDefini
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) }));
}
}

Expand Down Expand Up @@ -362,7 +364,11 @@ private void InitializeResolvedNameMshParameters()
{
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?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not:

Suggested change
int? widthNum = p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as int?;
var widthEntryKey = p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey);
if (widthEntryKey is int widthNum)
{
width = widthNum.Value.ToString();
}
else
{
width = widthEntryKey as string;
}

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only integer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iSazonov: Good question: int is the type of the Alignment entry for Format-Table, and I simply amended that class to also be able to use it with ConvertTo-Html, with a string-typed width entry.

Since we're talking about UIs and column widths, my guess is that [int] is enough, but I'm not wedded to that, and if someone knows that a wider type is called for, I'm all ears.

$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'
}

}