PowerShell/test/powershell/ConvertFrom-Csv.Tests.ps1

53 lines
1.8 KiB
PowerShell
Raw Normal View History

2015-11-03 00:26:14 +01:00
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
Describe "ConvertFrom-Csv" {
2015-10-29 22:55:09 +01:00
$testObject = "a", "1"
2016-02-05 17:53:44 +01:00
$testcsv = Join-Path -Path (Join-Path -Path $here -ChildPath assets) -ChildPath TestCsv2.csv
2015-11-02 22:45:39 +01:00
$testName = "Zaphod BeebleBrox"
2015-10-29 22:55:09 +01:00
$testColumns = @"
a,b,c
1,2,3
"@
It "Should be able to be called" {
{ ConvertFrom-Csv -InputObject $testObject } | Should Not Throw
}
It "Should be able to pipe" {
{ $testObject | ConvertFrom-Csv } | Should Not Throw
}
2015-11-02 22:45:39 +01:00
It "Should have expected results when using piped inputs" {
$csvContent = Get-Content $testcsv
$actualresult = $csvContent | ConvertFrom-Csv
$actualresult.GetType().BaseType.Name | Should Be "Array"
$actualresult[0].GetType().Name | Should Be "PSCustomObject"
#Should have a name property in the result
$actualresult[0].Name | Should Be $testName
}
2015-10-29 22:55:09 +01:00
It "Should be able to set a delimiter" {
2015-11-02 22:45:39 +01:00
{ $testcsv | ConvertFrom-Csv -Delimiter ";" } | Should Not Throw
}
It "Should actually delimit the output" {
$csvContent = Get-Content $testcsv
$actualresult = $csvContent | ConvertFrom-Csv -Delimiter ";"
$actualresult.GetType().BaseType.Name | Should Be "Array"
$actualresult[0].GetType().Name | Should Be "PSCustomObject"
# ConvertFrom-Csv takes the first line of the input as a header by default
$actualresult.Length | Should Be $($csvContent.Length - 1)
2015-10-29 22:55:09 +01:00
}
It "Should be able to have multiple columns" {
$actualData = $testColumns | ConvertFrom-Csv
2015-11-02 22:45:39 +01:00
2015-10-29 22:55:09 +01:00
$actualLength = $($( $actualData | gm) | Where-Object {$_.MemberType -eq "NoteProperty" }).Length
$actualLength | Should Be 3
}
2015-11-02 22:45:39 +01:00
}