MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "ConvertTo-Csv DRT Unit Tests" -Tags "CI" {4 $inputObject = [pscustomobject]@{ First = 1; Second = 2 }5 6 It "Test convertto-csv with psobject pipelined" {7 $returnObject = $inputObject | ConvertTo-Csv -IncludeTypeInformation8 $returnObject.Count | Should -Be 39 $returnObject[0] | Should -BeExactly "#TYPE System.Management.Automation.PSCustomObject"10 $returnObject[1] | Should -BeExactly "`"First`",`"Second`""11 $returnObject[2] | Should -BeExactly "`"1`",`"2`""12 }13 14 It "Test convertto-csv with NoTypeInformation and psobject pipelined" {15 $returnObject = $inputObject | ConvertTo-Csv -NoTypeInformation16 $returnObject.Count | Should -Be 217 $returnObject[0] | Should -BeExactly "`"First`",`"Second`""18 $returnObject[1] | Should -BeExactly "`"1`",`"2`""19 }20 21 It "Test convertto-csv with a useculture flag" {22 $delimiter = [CultureInfo]::CurrentCulture.TextInfo.ListSeparator23 $returnObject = $inputObject | ConvertTo-Csv -UseCulture -IncludeTypeInformation24 $returnObject.Count | Should -Be 325 $returnObject[0] | Should -BeExactly "#TYPE System.Management.Automation.PSCustomObject"26 $returnObject[1] | Should -BeExactly "`"First`"$($delimiter)`"Second`""27 $returnObject[2] | Should -BeExactly "`"1`"$($delimiter)`"2`""28 }29 30 It "Test convertto-csv with Delimiter" {31 $returnObject = $inputObject | ConvertTo-Csv -Delimiter ";" -IncludeTypeInformation32 $returnObject.Count | Should -Be 333 $returnObject[0] | Should -BeExactly "#TYPE System.Management.Automation.PSCustomObject"34 $returnObject[1] | Should -BeExactly "`"First`";`"Second`""35 $returnObject[2] | Should -BeExactly "`"1`";`"2`""36 }37}38 39Describe "ConvertTo-Csv" -Tags "CI" {40 BeforeAll {41 $Name = "Hello"; $Data = "World";42 $testObject = [pscustomobject]@{ FirstColumn = $Name; SecondColumn = $Data }43 $testNullObject = [pscustomobject]@{ FirstColumn = $Name; SecondColumn = $null }44 }45 46 It "Should Be able to be called without error" {47 { $testObject | ConvertTo-Csv } | Should -Not -Throw48 }49 50 It "Should output an array of objects" {51 $result = $testObject | ConvertTo-Csv52 $result.GetType() | Should -Be ([object[]])53 }54 55 It "Should return the type of data in the first element of the output array" {56 $result = $testObject | ConvertTo-Csv -IncludeTypeInformation57 58 $result[0] | Should -BeExactly "#TYPE System.Management.Automation.PSCustomObject"59 }60 61 It "Should return the column info in the second element of the output array" {62 $result = $testObject | ConvertTo-Csv -IncludeTypeInformation63 64 $result[1] | Should -Match "`"FirstColumn`""65 $result[1] | Should -Match "`"SecondColumn`""66 }67 68 It "Should return the data as a comma-separated list in the third element of the output array" {69 $result = $testObject | ConvertTo-Csv -IncludeTypeInformation70 $result[2] | Should -Match "`"Hello`""71 $result[2] | Should -Match "`"World`""72 }73 74 It "Includes type information when -IncludeTypeInformation is supplied" {75 $result = $testObject | ConvertTo-Csv -IncludeTypeInformation76 77 ($result -split ([Environment]::NewLine))[0] | Should -BeExactly "#TYPE System.Management.Automation.PSCustomObject"78 }79 80 It "Does not include type information by default" {81 $result = $testObject | ConvertTo-Csv82 83 $result | Should -Not -Match ([regex]::Escape('System.Management.Automation.PSCustomObject'))84 $result | Should -Not -Match ([regex]::Escape('#TYPE'))85 }86 87 It "Does not include type information with -NoTypeInformation" {88 $result = $testObject | ConvertTo-Csv -NoTypeInformation89 90 $result | Should -Not -Match ([regex]::Escape('System.Management.Automation.PSCustomObject'))91 $result | Should -Not -Match ([regex]::Escape('#TYPE'))92 }93 94 It "Does not include headers with -NoHeader" {95 $result = $testObject | ConvertTo-Csv -NoHeader96 $result | Should -BeExactly '"Hello","World"'97 }98 99 It "Does not support -UseQuotes and -QuoteFields at the same time" {100 { $testObject | ConvertTo-Csv -UseQuotes Always -QuoteFields "TestFieldName" } |101 Should -Throw -ErrorId "CannotSpecifyQuoteFieldsAndUseQuotes,Microsoft.PowerShell.Commands.ConvertToCsvCommand"102 }103 104 Context "QuoteFields parameter" {105 It "QuoteFields" {106 # Use 'FiRstCoLumn' to test case insensitivity107 $result = $testObject | ConvertTo-Csv -QuoteFields FiRstCoLumn -Delimiter ','108 109 $result[0] | Should -BeExactly "`"FirstColumn`",SecondColumn"110 $result[1] | Should -BeExactly "`"Hello`",World"111 112 $result = $testObject | ConvertTo-Csv -QuoteFields FiRstCoLumn, SeCondCoLumn -Delimiter ','113 114 $result[0] | Should -BeExactly "`"FirstColumn`",`"SecondColumn`""115 $result[1] | Should -BeExactly "`"Hello`",`"World`""116 }117 }118 119 Context "UseQuotes parameter" {120 It "UseQuotes Always" {121 $result = $testObject | ConvertTo-Csv -UseQuotes Always -Delimiter ','122 123 $result[0] | Should -BeExactly "`"FirstColumn`",`"SecondColumn`""124 $result[1] | Should -BeExactly "`"Hello`",`"World`""125 126 $result = $testNullObject | ConvertTo-Csv -UseQuotes Always -Delimiter ','127 128 $result[0] | Should -BeExactly "`"FirstColumn`",`"SecondColumn`""129 $result[1] | Should -BeExactly "`"Hello`","130 }131 132 It "UseQuotes Always is default" {133 $result = $testObject | ConvertTo-Csv -UseQuotes Always -Delimiter ','134 $result2 = $testObject | ConvertTo-Csv -Delimiter ','135 136 $result | Should -BeExactly $result2137 }138 139 It "UseQuotes Never" {140 $result = $testObject | ConvertTo-Csv -UseQuotes Never -Delimiter ','141 142 $result[0] | Should -BeExactly "FirstColumn,SecondColumn"143 $result[1] | Should -BeExactly "Hello,World"144 145 $result = $testNullObject | ConvertTo-Csv -UseQuotes Never -Delimiter ','146 147 $result[0] | Should -BeExactly "FirstColumn,SecondColumn"148 $result[1] | Should -BeExactly "Hello,"149 }150 151 It "UseQuotes AsNeeded" {152 $result = $testObject | ConvertTo-Csv -UseQuotes AsNeeded -Delimiter 'r'153 154 $result[0] | Should -BeExactly "`"FirstColumn`"rSecondColumn"155 $result[1] | Should -BeExactly "Hellor`"World`""156 157 $result = $testNullObject | ConvertTo-Csv -UseQuotes AsNeeded -Delimiter 'r'158 159 $result[0] | Should -BeExactly "`"FirstColumn`"rSecondColumn"160 $result[1] | Should -BeExactly "Hellor"161 }162 163 It "UseQuotes AsNeeded Escapes Delimiters" {164 $testDelimitersObject = [pscustomobject]@{ "FirstColumn" = "Hello,"; "Second,Column" = "World" };165 166 $result = $testDelimitersObject | ConvertTo-Csv -UseQuotes AsNeeded -Delimiter ','167 168 $result[0] | Should -BeExactly "FirstColumn,`"Second,Column`""169 $result[1] | Should -BeExactly "`"Hello,`",World"170 171 $result = $testDelimitersObject | ConvertTo-Csv -UseQuotes AsNeeded -Delimiter "r"172 173 $result[0] | Should -BeExactly "`"FirstColumn`"rSecond,Column"174 $result[1] | Should -BeExactly "Hello,r`"World`""175 }176 177 It "UseQuotes AsNeeded Escapes Newlines" {178 $testCRLFObject = [pscustomobject]@{ "First`r`nColumn" = "Hello`r`nWorld" };179 $testLFObject = [pscustomobject]@{ "First`nColumn" = "Hello`nWorld" };180 181 $result = $testCRLFObject | ConvertTo-Csv -UseQuotes AsNeeded182 183 $result[0] | Should -BeExactly "`"First`r`nColumn`""184 $result[1] | Should -BeExactly "`"Hello`r`nWorld`""185 186 $result = $testLFObject | ConvertTo-Csv -UseQuotes AsNeeded187 188 $result[0] | Should -BeExactly "`"First`nColumn`""189 $result[1] | Should -BeExactly "`"Hello`nWorld`""190 }191 192 It "UseQuotes AsNeeded Escapes Quotes" {193 $testQuotesObject = [pscustomobject]@{ "First`"Column" = "`"Hello`" World" };194 195 $result = $testQuotesObject | ConvertTo-Csv -UseQuotes AsNeeded196 197 $result[0] | Should -BeExactly "`"First`"`"Column`""198 $result[1] | Should -BeExactly "`"`"`"Hello`"`" World`""199 }200 201 }202 203 Context 'Converting IDictionary Objects' {204 BeforeAll {205 $Letters = 'A', 'B', 'C', 'D', 'E', 'F'206 $Items = 0..5 | ForEach-Object {207 [ordered]@{ Number = $_; Letter = $Letters[$_] }208 }209 $CsvString = $Items | ConvertTo-Csv210 $TestHashTable = [ordered]@{ 'first' = 'value1'; 'second' = $null; 'third' = 'value3' }211 }212 213 It 'should treat dictionary entries as properties' {214 $CsvString[0] | Should -MatchExactly ($Items[0].Keys -join '","')215 216 for ($i = 0; $i -lt $Items.Count; $i++) {217 # Index in the CSV strings will be +1 due to header line218 $ValuesPattern = $Items[$i].Values -join '","'219 $CsvString[$i + 1] | Should -MatchExactly $ValuesPattern220 }221 }222 223 It 'should ignore regular object properties' {224 $PropertyPattern = $Items[0].PSObject.Properties.Name -join '|'225 $CsvString[0] | Should -Not -Match $PropertyPattern226 }227 228 It 'should account for extended properties added deliberately' {229 $Items | Add-Member -MemberType NoteProperty -Name 'Extra' -Value 'Surprise!'230 $NewCsvString = $Items | ConvertTo-Csv231 $NewCsvString[0] | Should -MatchExactly 'Extra'232 $NewCsvString | Select-Object -Skip 1 | Should -MatchExactly 'Surprise!'233 }234 235 It 'should properly convert hashtable with null and non-null values'{236 $CsvResult = $TestHashTable | ConvertTo-Csv237 238 $CsvResult[0] | Should -BeExactly "`"first`",`"second`",`"third`""239 $CsvResult[1] | Should -BeExactly "`"value1`",$null,`"value3`""240 }241 }242}243 