Implicitly convert value assigned to XML property to string (#2678)

Using LanguagePrimitives.ConvertTo (to emulate what would happen if the property were a regular .Net property typed as string), we now convert the value assigned to an XML property to string.
This commit is contained in:
iSazonov
2016-12-02 15:43:10 -08:00
committed by Jason Shirk
parent fbb5c16eb9
commit 8abb6c3a94
2 changed files with 30 additions and 8 deletions
@@ -4887,14 +4887,8 @@ namespace System.Management.Automation
/// <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)
@@ -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])
}
}
}
}