Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -283,24 +283,12 @@ private void InitializeResolvedNameMshParameters()
string width = p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as string;
MshExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression;
List<MshExpression> 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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Copy link
Collaborator

@JamesWTruher JamesWTruher May 18, 2017

Choose a reason for hiding this comment

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

$text -replace "${newLine}?|`n","${newLine}"

might be better, or could the line endings be removed entirely? Is that part of what you're looking to validate?

}

It "Test ConvertTo-Html with no parameters" {
$returnObject = $customObject | ConvertTo-Html
$returnObject -is [System.Array] | Should Be $true
Copy link
Collaborator

Choose a reason for hiding this comment

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

this test isn't really needed, right? isn't the test on line 30 enough?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the test is appropriate; however, it would be easier to diagnose if Type.FullName is compared instead of a soft-cast.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should Be $true is an anti-pattern - the logs aren't very useful so try to avoid that.

In this case, you can use 'Should BeOfType`. See the list of Pester assertions here.

And I'll be honest - I'm surprised it's an array - I would have expected a single string, so from that point of view, it might be worth a test to at least make it obvious if someone were to implement my expected behavior.

$returnString = $returnObject -join $newLine
$expectedValue = normalizeLineEnds @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/><col/><col/></colgroup>
Copy link
Collaborator

Choose a reason for hiding this comment

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

i sure wish we could replicate less code - create variables and reuse them?

<tr><th>Name</th><th>Age</th><th>Friends</th></tr>
<tr><td>John Doe</td><td>42</td><td>System.Object[]</td></tr>
</table>
</body></html>
"@
$returnString | Should Be $expectedValue
}

It "Test ConvertTo-Html Fragment parameter" {
$returnString = ($customObject | ConvertTo-Html -Fragment) -join $newLine
$expectedValue = normalizeLineEnds @"
<table>
<colgroup><col/><col/><col/></colgroup>
<tr><th>Name</th><th>Age</th><th>Friends</th></tr>
<tr><td>John Doe</td><td>42</td><td>System.Object[]</td></tr>
</table>
"@
$returnString | Should Be $expectedValue
}

It "Test ConvertTo-Html as List" {
$returnString = ($customObject | ConvertTo-Html -As List) -join $newLine
$expectedValue = normalizeLineEnds @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<tr><td>Name:</td><td>John Doe</td></tr>
<tr><td>Age:</td><td>42</td></tr>
<tr><td>Friends:</td><td>System.Object[]</td></tr>
</table>
</body></html>
"@
$returnString | Should Be $expectedValue
}

It "Test ConvertTo-Html specified properties" {
$returnString = ($customObject | ConvertTo-Html -Property Name, Friends -As List) -join $newLine
$expectedValue = normalizeLineEnds @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<tr><td>Name:</td><td>John Doe</td></tr>
<tr><td>Friends:</td><td>System.Object[]</td></tr>
</table>
</body></html>
"@
$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 @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Custom Object</title>
<link rel="stylesheet" type="text/css" href="page.css" />
</head><body>
Body Text
<table>
<tr><td>Name:</td><td>John Doe</td></tr>
<tr><td>Age:</td><td>42</td></tr>
<tr><td>Friends:</td><td>System.Object[]</td></tr>
</table>
</body></html>
"@
$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 @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
Before the object
<table>
<colgroup><col/><col/><col/></colgroup>
<tr><th>Name</th><th>Age</th><th>Friends</th></tr>
<tr><td>John Doe</td><td>42</td><td>System.Object[]</td></tr>
</table>
After the object
</body></html>
"@
$returnString | Should Be $expectedValue
}
It "Test ConvertTo-Html header with default single property" {
$returnString = ($customObject | Select-Object Name | ConvertTo-Html) -join $newLine
$expectedValue = normalizeLineEnds @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/></colgroup>
<tr><th>Name</th></tr>
<tr><td>John Doe</td></tr>
</table>
</body></html>
"@
$returnString | Should Be $expectedValue
}
It "Test ConvertTo-Html header with matched single property" {
$returnString = ($customObject | ConvertTo-Html -Property Name*) -join $newLine
$expectedValue = normalizeLineEnds @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/></colgroup>
<tr><th>Name</th></tr>
<tr><td>John Doe</td></tr>
</table>
</body></html>
"@
$returnString | Should Be $expectedValue
}
}