Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/System.Management.Automation/engine/CoreAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4911,14 +4911,8 @@ protected override object PropertyGet(PSProperty property)
/// <param name="convertIfPossible">instructs the adapter to convert before setting, if the adapter supports conversion</param>
protected override void PropertySet(PSProperty property, object setValue, bool convertIfPossible)
{
string valueString = setValue as string;
if (valueString == null)
{
throw new SetValueException("XmlNodeSetShouldBeAString",
null,
ExtendedTypeSystem.XmlNodeSetShouldBeAString,
property.Name);
}
// XML is always a string so implicitly convert to string
string valueString = LanguagePrimitives.ConvertTo<string>(setValue);
XmlNode[] nodes = (XmlNode[])property.adapterData;
Diagnostics.Assert(nodes.Length != 0, "DoGetProperty would not return an empty array, it would return null instead");
if (nodes.Length > 1)
Expand Down
28 changes: 28 additions & 0 deletions test/powershell/engine/ETS/Adapter.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,31 @@ Describe "Adapter Tests" -tags "CI" {
$val | Should Be "book"
}
}

Describe "Adapter XML Tests" -tags "CI" {
BeforeAll {
[xml]$x = "<root><data/></root>"
$testCases =
@{ rval = @{testprop = 1}; value = 'a hash (psobject)' },
@{ rval = $null; value = 'a null (codemethod)' },
@{ rval = 1; value = 'a int (codemethod)' },
@{ rval = "teststring"; value = 'a string (codemethod)' },
@{ rval = @("teststring1", "teststring2"); value = 'a string array (codemethod)' },
@{ rval = @(1,2); value = 'a int array (codemethod)' },
@{ rval = [PSObject]::AsPSObject(1); value = 'a int (psobject wrapping)' },
@{ rval = [PSObject]::AsPSObject("teststring"); value = 'a string (psobject wrapping)' },
@{ rval = [PSObject]::AsPSObject([psobject]@("teststring1", "teststring2")); value = 'a string array (psobject wrapping)' },
@{ rval = [PSObject]::AsPSObject(@(1,2)); value = 'int array (psobject wrapping)' }
}

Context "Can set XML node property to non-string object" {
It "rval is <value>" -TestCases $testCases {
# rval will be implicitly converted to 'string' type
param($rval)
{
{ $x.root.data = $rval } | Should Not Throw
$x.root.data | Should Be [System.Management.Automation.LanguagePrimitives]::ConvertTo($rval, [string])
}
}
}
}