2020-03-24 11:08:37 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 09:23:53 -08:00
|
|
|
# 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
|
|
|
|
|
"@
|
2018-03-23 10:26:23 -07:00
|
|
|
{ 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 = @'
|
2016-03-04 14:52:27 -08:00
|
|
|
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" {
|
2018-03-23 10:26:23 -07:00
|
|
|
{ ConvertFrom-StringData -StringData 'a=b' } | Should -Not -Throw
|
2016-03-04 14:48:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should return a hashtable" {
|
2017-02-16 04:40:51 +04:00
|
|
|
$result = ConvertFrom-StringData -StringData 'a=b'
|
2018-03-23 10:26:23 -07:00
|
|
|
$result | Should -BeOfType Hashtable
|
2016-03-04 14:48:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should throw if not in x=y format" {
|
2018-03-23 10:26:23 -07:00
|
|
|
{ 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" {
|
2016-03-04 14:52:27 -08:00
|
|
|
$actualValue = ConvertFrom-StringData -StringData 'a=b'
|
2016-03-04 14:48:30 -08:00
|
|
|
|
2018-03-23 10:26:23 -07: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" {
|
2016-03-04 14:52:27 -08:00
|
|
|
$actualValue = ConvertFrom-StringData -StringData 'a=b'
|
2016-03-04 14:48:30 -08:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$actualValue.Values | Should -BeExactly "b"
|
2016-03-04 14:48:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should return a keycollection for the keys" {
|
2018-03-23 10:26:23 -07:00
|
|
|
$(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" {
|
2018-03-23 10:26:23 -07:00
|
|
|
$(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" {
|
2018-03-23 10:26:23 -07:00
|
|
|
{ ConvertFrom-StringData -StringData $sampleData } | Should -Not -Throw
|
2016-03-04 14:48:30 -08:00
|
|
|
|
2018-02-05 11:58:25 -08:00
|
|
|
# keys are not order guaranteed
|
2018-03-23 10:26:23 -07:00
|
|
|
$(ConvertFrom-StringData -StringData $sampleData).Keys | Should -BeIn @("foo", "bar", "bazz")
|
2016-03-04 14:48:30 -08:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$(ConvertFrom-StringData -StringData $sampleData).Values | Should -BeIn @("0","1","2")
|
2016-03-04 14:48:30 -08:00
|
|
|
}
|
2016-03-04 14:52:27 -08:00
|
|
|
}
|
2019-10-11 17:03:02 -04:00
|
|
|
|
|
|
|
|
Describe "Delimiter parameter tests" -Tags "CI" {
|
|
|
|
|
BeforeAll {
|
|
|
|
|
$TestCases = @(
|
2019-10-22 22:37:48 -07:00
|
|
|
@{ Delimiter = ':'; StringData = 'value:10'; ExpectedResult = @{ value = 10 } }
|
|
|
|
|
@{ Delimiter = '-'; StringData = 'a-b' ; ExpectedResult = @{ a = 'b' } }
|
|
|
|
|
@{ Delimiter = ','; StringData = 'c,d' ; ExpectedResult = @{ c = 'd' } }
|
2019-10-11 17:03:02 -04:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Default delimiter '=' works" {
|
|
|
|
|
$actualValue = ConvertFrom-StringData -StringData 'a=b'
|
|
|
|
|
|
|
|
|
|
$actualValue.Values | Should -BeExactly "b"
|
|
|
|
|
$actualValue.Keys | Should -BeExactly "a"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should not throw on given delimiter" {
|
|
|
|
|
$sampleData = @"
|
|
|
|
|
a:b
|
|
|
|
|
"@
|
|
|
|
|
{ $sampleData | ConvertFrom-StringData -Delimiter ':' } | Should -Not -Throw
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 22:37:48 -07:00
|
|
|
It 'is able to parse <StringData> with delimiter "<Delimiter> with <stringdata>"' -TestCases $TestCases {
|
2019-10-11 17:03:02 -04:00
|
|
|
param($Delimiter, $StringData, $ExpectedResult)
|
|
|
|
|
|
|
|
|
|
$Result = ConvertFrom-StringData -StringData $StringData -Delimiter $Delimiter
|
|
|
|
|
|
2019-10-22 22:37:48 -07:00
|
|
|
$key = $ExpectedResult.Keys
|
|
|
|
|
|
|
|
|
|
# validate the key in expected and result hashtables match
|
|
|
|
|
$Result.Keys | Should -BeExactly $ExpectedResult.Keys
|
|
|
|
|
|
|
|
|
|
# validate the values in expected and result hashtables match
|
|
|
|
|
$Result[$key] | Should -BeExactly $ExpectedResult.Values
|
2019-10-11 17:03:02 -04:00
|
|
|
}
|
|
|
|
|
}
|