Merge pull request #989 from PowerShell/CsvCommandTest

Add Unit Test for Export-Csv
This commit is contained in:
James Truher [MSFT] 2016-06-09 16:37:57 -07:00 committed by GitHub
commit 5b17b603fb

View file

@ -64,3 +64,51 @@ Describe "Export-Csv" {
Remove-Item $aliasObject -Force
}
}
Describe "Export-Csv DRT Unit Tests" -Tags DRT{
$filePath = Join-Path $TestDrive -ChildPath "test.csv"
$newLine = [environment]::NewLine
It "Test basic function works well" {
$input = [pscustomobject]@{ "P1" = "V11"; "P2" = "V12"; "P3" = "V13" }
$input | Export-Csv -Path $filePath -NoTypeInformation
$results = Import-Csv $filePath
$results.P1 | Should Be "V11"
$results.P2 | Should Be "V12"
$results.P3 | Should Be "V13"
}
It "Test if it works with special character" {
$v3 = "abc" + $newLine + "foo"
$input = [pscustomobject]@{ "P1" = " "; "P2" = "abc,foo"; "P3" = $v3}
$input | Export-Csv -Path $filePath -NoTypeInformation
$results = Import-Csv $filePath
$results.P1 | Should Be " "
$results.P2 | Should Be "abc,foo"
$results.P3 | Should Be $v3
}
It "Test force switch works well" {
$input = [pscustomobject]@{ "P1" = "first" }
$input | Export-Csv -Path $filePath
$input = [pscustomobject]@{ "P2" = "second" }
$input | Export-Csv -Path $filePath -Force
$results = Import-Csv $filePath
$results.P2 | Should be "second"
$property = $results | Get-Member | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name }
$property | should not be P1
}
It "Test export-csv with a useculture flag" {
$outputFilesDir = Join-Path $TestDrive -ChildPath "Monad"
$fileToGenerate = Join-Path $outputFilesDir -ChildPath "CSVTests.csv"
$delimiter = (Get-Culture).TextInfo.ListSeparator
New-Item -Path $outputFilesDir -ItemType Directory -Force
Get-Item -Path $outputFilesDir| Export-Csv -Path $fileToGenerate -UseCulture -NoTypeInformation
$contents = Get-Content -Path $fileToGenerate
$contents.Count | Should Be 2
$contents[0].Contains($delimiter) | Should Be $true
$contents[1].Contains($delimiter) | Should Be $true
}
}