Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the MIT License.

using System;
using System.Management.Automation;
using System.Collections;
using System.Management.Automation;
using System.Text.RegularExpressions;

namespace Microsoft.PowerShell.Commands
Expand All @@ -30,19 +30,26 @@ public string StringData
{
return _stringData;
}

set
{
_stringData = value;
}
}

/// <summary>
/// Gets or sets the delimiter.
/// </summary>
[Parameter(Position = 1)]
public char Delimiter { get; set; } = '=';

/// <summary>
/// </summary>
protected override void ProcessRecord()
{
Hashtable result = new Hashtable(StringComparer.OrdinalIgnoreCase);

if (String.IsNullOrEmpty(_stringData))
if (string.IsNullOrEmpty(_stringData))
{
WriteObject(result);
return;
Expand All @@ -54,10 +61,10 @@ protected override void ProcessRecord()
{
string s = line.Trim();

if (String.IsNullOrEmpty(s) || s[0] == '#')
if (string.IsNullOrEmpty(s) || s[0] == '#')
continue;

int index = s.IndexOf('=');
int index = s.IndexOf(Delimiter);
Comment thread
iSazonov marked this conversation as resolved.
if (index <= 0)
{
throw PSTraceSource.NewInvalidOperationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,38 @@ bazz = 2
$(ConvertFrom-StringData -StringData $sampleData).Values | Should -BeIn @("0","1","2")
}
}

Describe "Delimiter parameter tests" -Tags "CI" {
BeforeAll {
$TestCases = @(
@{ Delimiter = ':'; StringData = 'value:10'; ExpectedResult = @{ Values = 10 } }
@{ Delimiter = '-'; StringData = 'a-b' ; ExpectedResult = @{ Values = 'b' } }
@{ Delimiter = ','; StringData = 'c,d' ; ExpectedResult = @{ Values = 'd' } }
)
}

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
}

It 'is able to parse <StringData> with delimiter "<Delimiter>"' -TestCases $TestCases {
param($Delimiter, $StringData, $ExpectedResult)

$Result = ConvertFrom-StringData -StringData $StringData -Delimiter $Delimiter

foreach ($Key in $ExpectedResult.Keys) {
$Key | Should -BeIn $ExpectedResult.Keys
$Result.$Key | Should -Be $ExpectedResult.$Key
}
}
}