Files
PowerShell/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Csv.Tests.ps1
T

65 lines
2.1 KiB
PowerShell
Raw Normal View History

2016-03-04 14:48:30 -08:00
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
2016-07-25 14:01:41 -07:00
Describe "ConvertFrom-Csv" -Tags "CI" {
2016-03-04 14:48:30 -08:00
$testObject = "a", "1"
$testcsv = Join-Path -Path (Join-Path -Path $here -ChildPath assets) -ChildPath TestCsv2.csv
$testName = "Zaphod BeebleBrox"
$testColumns = @"
a,b,c
1,2,3
2016-03-04 14:48:30 -08:00
"@
It "Should be able to be called" {
{ ConvertFrom-Csv -InputObject $testObject } | Should Not Throw
2016-03-04 14:48:30 -08:00
}
It "Should be able to pipe" {
{ $testObject | ConvertFrom-Csv } | Should Not Throw
2016-03-04 14:48:30 -08:00
}
It "Should have expected results when using piped inputs" {
$csvContent = Get-Content $testcsv
$actualresult = $csvContent | ConvertFrom-Csv
2016-03-04 14:48:30 -08:00
$actualresult.GetType().BaseType.Name | Should Be "Array"
$actualresult[0].GetType().Name | Should Be "PSCustomObject"
2016-03-04 14:48:30 -08:00
#Should have a name property in the result
$actualresult[0].Name | Should Be $testName
2016-03-04 14:48:30 -08:00
}
It "Should be able to set a delimiter" {
{ $testcsv | ConvertFrom-Csv -Delimiter ";" } | Should Not Throw
2016-03-04 14:48:30 -08:00
}
It "Should actually delimit the output" {
$csvContent = Get-Content $testcsv
$actualresult = $csvContent | ConvertFrom-Csv -Delimiter ";"
2016-03-04 14:48:30 -08:00
$actualresult.GetType().BaseType.Name | Should Be "Array"
$actualresult[0].GetType().Name | Should Be "PSCustomObject"
2016-03-04 14:48:30 -08:00
# ConvertFrom-Csv takes the first line of the input as a header by default
$actualresult.Length | Should Be $($csvContent.Length - 1)
2016-03-04 14:48:30 -08:00
}
It "Should be able to have multiple columns" {
$actualData = $testColumns | ConvertFrom-Csv
2016-03-04 14:48:30 -08:00
$actualLength = $($( $actualData | gm) | Where-Object {$_.MemberType -eq "NoteProperty" }).Length
2016-03-04 14:48:30 -08:00
$actualLength | Should Be 3
2016-03-04 14:48:30 -08:00
}
}
2016-05-12 01:42:15 -07:00
2016-07-25 14:01:41 -07:00
Describe "ConvertFrom-Csv DRT Unit Tests" -Tags "CI" {
2016-05-12 01:42:15 -07:00
It "Test ConvertFrom-Csv with pipelined InputObject and Header" {
2016-05-12 21:46:34 -07:00
$inputObject = [pscustomobject]@{ First = 1; Second = 2 }
$res = $inputObject | ConvertTo-Csv
2016-05-12 01:42:15 -07:00
$result = $res | ConvertFrom-Csv -Header "Header1","Header2"
$result[0].Header1 | Should Be "First"
$result[0].Header2 | Should Be "Second"
$result[1].Header1 | Should Be "1"
$result[1].Header2 | Should Be "2"
}
2016-07-25 14:01:41 -07:00
}