Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
ConvertTo-Html.Tests.ps1198 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "ConvertTo-Html Tests" -Tags "CI" {4 5    BeforeAll {6        $customObject = [pscustomobject]@{"Name" = "John Doe"; "Age" = 42; "Friends" = ("Jack", "Jill")}7        $newLine = "`r`n"8    }9 10    function normalizeLineEnds([string]$text)11    {12        $text -replace "`r`n?|`n", "`r`n"13    }14 15    It "Test ConvertTo-Html with no parameters" {16        $returnObject = $customObject | ConvertTo-Html17        ,$returnObject | Should -BeOfType System.Object[]18        $returnString = $returnObject -join $newLine19        $expectedValue = normalizeLineEnds @"20<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">21<html xmlns="http://www.w3.org/1999/xhtml">22<head>23<title>HTML TABLE</title>24</head><body>25<table>26<colgroup><col/><col/><col/></colgroup>27<tr><th>Name</th><th>Age</th><th>Friends</th></tr>28<tr><td>John Doe</td><td>42</td><td>System.Object[]</td></tr>29</table>30</body></html>31"@32        $returnString | Should -Be $expectedValue33    }34 35    It "Test ConvertTo-Html Fragment parameter" {36        $returnString = ($customObject | ConvertTo-Html -Fragment) -join $newLine37        $expectedValue = normalizeLineEnds @"38<table>39<colgroup><col/><col/><col/></colgroup>40<tr><th>Name</th><th>Age</th><th>Friends</th></tr>41<tr><td>John Doe</td><td>42</td><td>System.Object[]</td></tr>42</table>43"@44        $returnString | Should -Be $expectedValue45    }46 47    It "Test ConvertTo-Html as List" {48        $returnString = ($customObject | ConvertTo-Html -As List) -join $newLine49        $expectedValue = normalizeLineEnds @"50<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">51<html xmlns="http://www.w3.org/1999/xhtml">52<head>53<title>HTML TABLE</title>54</head><body>55<table>56<tr><td>Name:</td><td>John Doe</td></tr>57<tr><td>Age:</td><td>42</td></tr>58<tr><td>Friends:</td><td>System.Object[]</td></tr>59</table>60</body></html>61"@62        $returnString | Should -Be $expectedValue63    }64 65    It "Test ConvertTo-Html specified properties" {66        $returnString = ($customObject | ConvertTo-Html -Property Name, Friends -As List) -join $newLine67        $expectedValue = normalizeLineEnds @"68<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">69<html xmlns="http://www.w3.org/1999/xhtml">70<head>71<title>HTML TABLE</title>72</head><body>73<table>74<tr><td>Name:</td><td>John Doe</td></tr>75<tr><td>Friends:</td><td>System.Object[]</td></tr>76</table>77</body></html>78"@79        $returnString | Should -Be $expectedValue80    }81 82    It "Test ConvertTo-Html using page parameters" {83        $returnString = ($customObject | ConvertTo-Html -Title "Custom Object" -Body "Body Text" -CssUri "page.css" -As List) -join $newLine84        $expectedValue = normalizeLineEnds @"85<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">86<html xmlns="http://www.w3.org/1999/xhtml">87<head>88<title>Custom Object</title>89<link rel="stylesheet" type="text/css" href="page.css" />90</head><body>91Body Text92<table>93<tr><td>Name:</td><td>John Doe</td></tr>94<tr><td>Age:</td><td>42</td></tr>95<tr><td>Friends:</td><td>System.Object[]</td></tr>96</table>97</body></html>98"@99        $returnString | Should -Be $expectedValue100    }101 102    It "Test ConvertTo-Html pre and post" {103        $returnString = ($customObject | ConvertTo-Html -PreContent "Before the object" -PostContent "After the object") -join $newLine104        $expectedValue = normalizeLineEnds @"105<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">106<html xmlns="http://www.w3.org/1999/xhtml">107<head>108<title>HTML TABLE</title>109</head><body>110Before the object111<table>112<colgroup><col/><col/><col/></colgroup>113<tr><th>Name</th><th>Age</th><th>Friends</th></tr>114<tr><td>John Doe</td><td>42</td><td>System.Object[]</td></tr>115</table>116After the object117</body></html>118"@119        $returnString | Should -Be $expectedValue120    }121 122    It "Test ConvertTo-HTML meta"{123        $returnString = ($customObject | ConvertTo-Html -Meta @{"author"="John Doe"}) -join $newLine124        $expectedValue = normalizeLineEnds @"125<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">126<html xmlns="http://www.w3.org/1999/xhtml">127<head>128<meta name="author" content="John Doe">129<title>HTML TABLE</title>130</head><body>131<table>132<colgroup><col/><col/><col/></colgroup>133<tr><th>Name</th><th>Age</th><th>Friends</th></tr>134<tr><td>John Doe</td><td>42</td><td>System.Object[]</td></tr>135</table>136</body></html>137"@138        $returnString | Should -Be $expectedValue139    }140 141    It "Test ConvertTo-HTML meta with invalid properties should throw warning" {142        $parms = @{"authors"="John Doe";"keywords"="PowerShell,PSv6"}143        # make this a string, rather than an array of string so match will behave144        [string]$observedProperties = $customObject | ConvertTo-Html -Meta $parms 3>&1145        $observedProperties | Should -Match $parms["authors"]146    }147 148    It "Test ConvertTo-HTML charset"{149        $returnString = ($customObject | ConvertTo-Html -Charset "utf-8") -join $newLine150        $expectedValue = normalizeLineEnds @"151<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">152<html xmlns="http://www.w3.org/1999/xhtml">153<head>154<meta charset="UTF-8">155<title>HTML TABLE</title>156</head><body>157<table>158<colgroup><col/><col/><col/></colgroup>159<tr><th>Name</th><th>Age</th><th>Friends</th></tr>160<tr><td>John Doe</td><td>42</td><td>System.Object[]</td></tr>161</table>162</body></html>163"@164        $returnString | Should -Be $expectedValue165    }166 167    It "Test ConvertTo-HTML transitional"{168        $returnString = $customObject | ConvertTo-Html -Transitional | Select-Object -First 1169        $returnString | Should -Be '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'170    }171 172    It "Test ConvertTo-HTML supports scriptblock-based calculated properties: by hashtable" {173        $returnString = ($customObject | ConvertTo-Html @{ l = 'NewAge'; e = { $_.Age + 1 } }) -join $newLine174        $returnString | Should -Match '\b43\b'175    }176 177    It "Test ConvertTo-HTML supports scriptblock-based calculated properties: directly" {178        $returnString = ($customObject | ConvertTo-Html { $_.Age + 1 }) -join $newLine179        $returnString | Should -Match '\b43\b'180    }181 182    It "Test ConvertTo-HTML calculated property supports 'name' key as alias of 'label'" {183        $returnString = ($customObject | ConvertTo-Html @{ name = 'AgeRenamed'; e = 'Age'}) -join $newLine184        $returnString | Should -Match 'AgeRenamed'185    }186 187    It "Test ConvertTo-HTML calculated property supports integer 'width' entry" {188        $returnString = ($customObject | ConvertTo-Html @{ e = 'Age'; width = 10 }) -join $newLine189        $returnString | Should -Match '\swidth\s*=\s*(["''])10\1'190    }191 192    It "Test ConvertTo-HTML calculated property supports string 'width' entry" {193        $returnString = ($customObject | ConvertTo-Html @{ e = 'Age'; width = '10' }) -join $newLine194        $returnString | Should -Match '\swidth\s*=\s*(["''])10\1'195    }196 197}198