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

67 lines
2.7 KiB
PowerShell
Raw Normal View History

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
2016-07-25 14:01:41 -07:00
Describe "ConvertFrom-StringData DRT Unit Tests" -Tags "CI" {
2016-05-06 04:09:57 -07:00
It "Should able to throw error when convert invalid line" {
$str =@"
#comments here
abc
#comments here
def=content of def
"@
{ ConvertFrom-StringData $str } | Should -Throw -ErrorId "InvalidOperation,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand"
2016-05-06 04:09:57 -07:00
}
}
2016-07-25 14:01:41 -07:00
Describe "ConvertFrom-StringData" -Tags "CI" {
2016-03-04 14:48:30 -08:00
$sampleData = @'
foo = 0
bar = 1
bazz = 2
2016-03-04 14:48:30 -08:00
'@
It "Should not throw when called with just the stringdata switch" {
{ ConvertFrom-StringData -StringData 'a=b' } | Should -Not -Throw
2016-03-04 14:48:30 -08:00
}
It "Should return a hashtable" {
$result = ConvertFrom-StringData -StringData 'a=b'
$result | Should -BeOfType Hashtable
2016-03-04 14:48:30 -08:00
}
It "Should throw if not in x=y format" {
{ ConvertFrom-StringData -StringData 'ab' } | Should -Throw -ErrorId "InvalidOperation,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand"
{ ConvertFrom-StringData -StringData 'a,b' } | Should -Throw -ErrorId "InvalidOperation,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand"
{ ConvertFrom-StringData -StringData 'a b' } | Should -Throw -ErrorId "InvalidOperation,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand"
{ ConvertFrom-StringData -StringData 'a\tb' } | Should -Throw -ErrorId "InvalidOperation,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand"
{ ConvertFrom-StringData -StringData 'a:b' } | Should -Throw -ErrorId "InvalidOperation,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand"
2016-03-04 14:48:30 -08:00
}
It "Should return the data on the left side in the key" {
$actualValue = ConvertFrom-StringData -StringData 'a=b'
2016-03-04 14:48:30 -08:00
$actualValue.Keys | Should -BeExactly "a"
2016-03-04 14:48:30 -08:00
}
It "Should return the data on the right side in the value" {
$actualValue = ConvertFrom-StringData -StringData 'a=b'
2016-03-04 14:48:30 -08:00
$actualValue.Values | Should -BeExactly "b"
2016-03-04 14:48:30 -08:00
}
It "Should return a keycollection for the keys" {
$(ConvertFrom-StringData -StringData 'a=b').Keys.PSObject.TypeNames[0] | Should -BeExactly "System.Collections.Hashtable+KeyCollection"
2016-03-04 14:48:30 -08:00
}
It "Should return a valuecollection for the values" {
$(ConvertFrom-StringData -StringData 'a=b').Values.PSObject.TypeNames[0] | Should -BeExactly "System.Collections.Hashtable+ValueCollection"
2016-03-04 14:48:30 -08:00
}
It "Should work for multiple lines" {
{ ConvertFrom-StringData -StringData $sampleData } | Should -Not -Throw
2016-03-04 14:48:30 -08:00
# keys are not order guaranteed
$(ConvertFrom-StringData -StringData $sampleData).Keys | Should -BeIn @("foo", "bar", "bazz")
2016-03-04 14:48:30 -08:00
$(ConvertFrom-StringData -StringData $sampleData).Values | Should -BeIn @("0","1","2")
2016-03-04 14:48:30 -08:00
}
}