From 7a88888916946a05b8bd7ee5a378631cc0068fb7 Mon Sep 17 00:00:00 2001 From: Zachary Folwick Date: Mon, 19 Oct 2015 12:23:40 -0700 Subject: [PATCH] Added pester tests for Export-Csv cmdlet --- src/pester-tests/Test-Export-Csv.Tests.ps1 | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/pester-tests/Test-Export-Csv.Tests.ps1 diff --git a/src/pester-tests/Test-Export-Csv.Tests.ps1 b/src/pester-tests/Test-Export-Csv.Tests.ps1 new file mode 100644 index 0000000000..d72fa3de14 --- /dev/null +++ b/src/pester-tests/Test-Export-Csv.Tests.ps1 @@ -0,0 +1,66 @@ +Describe "Test-Export-Csv" { + $testObject = @("test","object","array") + $testCsv = "output.csv" + + AfterEach { + Remove-Item $testCsv -Force -ErrorAction SilentlyContinue + } + + It "Should be able to be called without error" { + { $testObject | Export-Csv $testCsv } | Should Not Throw + } + + It "Should throw if an output file isn't specified" { + { $testObject | Export-Csv -ErrorAction SilentlyContinue } | Should Throw + } + + It "Should be a string when exporting via pipe" { + $testObject | Export-Csv $testCsv + + $piped = Get-Content $testCsv + + $piped[0] | Should Match ".String" + } + + It "Should be an object when exporting via the inputObject switch" { + Export-Csv -InputObject $testObject -Path $testCsv + + $switch = Get-Content $testCsv + + $switch[0] | Should Match ".Object" + } + + It "Should output a csv file containing a string of all the lengths of each element when piped input is used" { + $testObject | Export-Csv -Path $testCsv + + $first = "`"" + $testObject[0].Length.ToString() + "`"" + $second = "`"" + $testObject[1].Length.ToString() + "`"" + $third = "`"" + $testObject[2].Length.ToString() + "`"" + $expected = @("#TYPE System.String", "`"Length`"", $first , $second, $third) + + for ( $i = 0; $i -lt $testCsv.Length; $i++) + { + $(Get-Content $testCsv)[$i] | Should Be $expected[$i] + } + } + + It "Should be able to use the epcsv alias without error" { + { $testObject | Export-Csv -Path $testCsv } | Should Not Throw + } + + It "Should have the same information when using the alias vs the cmdlet" { + $testObject | Export-Csv -Path $testCsv + + $aliasObject = "alias.csv" + + $testObject | epcsv -Path $aliasObject + + for ( $i = 0; $i -lt $testCsv.Length; $i++) + { + $(Get-Content $testCsv)[$i] | Should Be $(Get-Content $aliasObject)[$i] + } + + # Clean up after yourself + Remove-Item $aliasObject -Force + } +}