Files
PowerShell/test/powershell/Export-Csv.Tests.ps1
T

66 lines
1.8 KiB
PowerShell
Raw Normal View History

2016-03-04 14:48:30 -08:00
Describe "Export-Csv" {
2015-10-19 12:23:40 -07:00
$testObject = @("test","object","array")
$testCsv = "output.csv"
AfterEach {
Remove-Item $testCsv -Force -ErrorAction SilentlyContinue
2015-10-19 12:23:40 -07:00
}
It "Should be able to be called without error" {
{ $testObject | Export-Csv $testCsv } | Should Not Throw
2015-10-19 12:23:40 -07:00
}
It "Should throw if an output file isn't specified" {
{ $testObject | Export-Csv -ErrorAction SilentlyContinue } | Should Throw
2015-10-19 12:23:40 -07:00
}
It "Should be a string when exporting via pipe" {
$testObject | Export-Csv $testCsv
2015-10-19 12:23:40 -07:00
$piped = Get-Content $testCsv
2015-10-19 12:23:40 -07:00
$piped[0] | Should Match ".String"
2015-10-19 12:23:40 -07:00
}
It "Should be an object when exporting via the inputObject switch" {
Export-Csv -InputObject $testObject -Path $testCsv
2015-10-19 12:23:40 -07:00
$switch = Get-Content $testCsv
2015-10-19 12:23:40 -07:00
$switch[0] | Should Match ".Object"
2015-10-19 12:23:40 -07:00
}
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
2015-10-19 12:23:40 -07:00
$first = "`"" + $testObject[0].Length.ToString() + "`""
$second = "`"" + $testObject[1].Length.ToString() + "`""
$third = "`"" + $testObject[2].Length.ToString() + "`""
$expected = @("#TYPE System.String", "`"Length`"", $first , $second, $third)
2015-10-19 12:23:40 -07:00
for ( $i = 0; $i -lt $testCsv.Length; $i++)
{
$(Get-Content $testCsv)[$i] | Should Be $expected[$i]
}
2015-10-19 12:23:40 -07:00
}
It "Should be able to use the epcsv alias without error" {
{ $testObject | Export-Csv -Path $testCsv } | Should Not Throw
2015-10-19 12:23:40 -07:00
}
It "Should have the same information when using the alias vs the cmdlet" {
$testObject | Export-Csv -Path $testCsv
2015-10-19 12:23:40 -07:00
$aliasObject = "alias.csv"
2015-10-19 12:23:40 -07:00
$testObject | epcsv -Path $aliasObject
2015-10-19 12:23:40 -07:00
for ( $i = 0; $i -lt $testCsv.Length; $i++)
{
$(Get-Content $testCsv)[$i] | Should Be $(Get-Content $aliasObject)[$i]
}
2015-10-19 12:23:40 -07:00
# Clean up after yourself
Remove-Item $aliasObject -Force
2015-10-19 12:23:40 -07:00
}
}