File size: 7,366 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
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 | Should -BeOfType System.Object[]
$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>
<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 meta"{
$returnString = ($customObject | ConvertTo-Html -Meta @{"author"="John Doe"}) -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>
<meta name="author" content="John Doe">
<title>HTML TABLE</title>
</head><body>
<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>
</body></html>
"@
$returnString | Should -Be $expectedValue
}
It "Test ConvertTo-HTML meta with invalid properties should throw warning" {
$parms = @{"authors"="John Doe";"keywords"="PowerShell,PSv6"}
# make this a string, rather than an array of string so match will behave
[string]$observedProperties = $customObject | ConvertTo-Html -Meta $parms 3>&1
$observedProperties | Should -Match $parms["authors"]
}
It "Test ConvertTo-HTML charset"{
$returnString = ($customObject | ConvertTo-Html -Charset "utf-8") -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>
<meta charset="UTF-8">
<title>HTML TABLE</title>
</head><body>
<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>
</body></html>
"@
$returnString | Should -Be $expectedValue
}
It "Test ConvertTo-HTML transitional"{
$returnString = $customObject | ConvertTo-Html -Transitional | Select-Object -First 1
$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
$returnString | Should -Match '\b43\b'
}
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'
}
}
|