forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PSObject boxing support in PesterConfiguration (pester#1978)
* Add PSObject boxing support in PesterConfiguration * Removes out-of-scope changes * Adds PS 5 plus PesterConfiguration deserializer * Removes use of ::new in tests * Adds tests for serializer * Removes use of .Value on set operations * Rewrites deserializer in CS * Tweaks accessors * Use SafeCommands Co-authored-by: nohwnd <[email protected]>
- Loading branch information
1 parent
465544d
commit 8634da0
Showing
6 changed files
with
262 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Management.Automation; | ||
using System.Reflection; | ||
using Pester; | ||
|
||
public class PesterConfigurationDeserializer : PSTypeConverter | ||
{ | ||
public override bool CanConvertFrom(object sourceValue, Type destinationType) | ||
{ | ||
if (!(sourceValue is PSObject)) | ||
return false; | ||
|
||
return ((PSObject)sourceValue).TypeNames.Contains("Deserialized.PesterConfiguration"); | ||
} | ||
|
||
public override object ConvertFrom(object sourceValue, Type destinationType, IFormatProvider formatProvider, bool ignoreCase) | ||
{ | ||
return ConvertToPesterConfiguration(sourceValue); | ||
} | ||
|
||
public override bool CanConvertTo(object sourceValue, Type destinationType) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override object ConvertTo(object sourceValue, Type destinationType, IFormatProvider formatProvider, bool ignoreCase) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private PesterConfiguration ConvertToPesterConfiguration(object sourceValue) | ||
{ | ||
if (sourceValue is IDictionary) | ||
return new PesterConfiguration((IDictionary)sourceValue); | ||
|
||
return new PesterConfiguration(ConvertToConfigurationHashtable((PSObject)sourceValue)); | ||
} | ||
|
||
private Hashtable ConvertToConfigurationHashtable(PSObject sourceConfiguration) | ||
{ | ||
Hashtable configuration = new Hashtable(); | ||
|
||
foreach (var property in sourceConfiguration.Properties) | ||
{ | ||
configuration.Add( | ||
property.Name, | ||
ConvertToSectionHashtable( | ||
(PSObject)property.Value, | ||
property.Name | ||
) | ||
); | ||
} | ||
|
||
return configuration; | ||
} | ||
|
||
private Hashtable ConvertToSectionHashtable(PSObject sourceSection, string sectionName) | ||
{ | ||
Hashtable configurationSection = new Hashtable(); | ||
|
||
foreach (var property in sourceSection.Properties) | ||
{ | ||
configurationSection.Add( | ||
property.Name, | ||
GetPropertyValue( | ||
(PSObject)property.Value, | ||
sectionName, | ||
property.Name | ||
) | ||
); | ||
} | ||
|
||
return configurationSection; | ||
} | ||
|
||
private object GetPropertyValue(PSObject sourceItem, string sectionName, string propertyName) | ||
{ | ||
var value = sourceItem.Properties["Value"].Value; | ||
|
||
if (value is PSObject) | ||
value = ((PSObject)value).BaseObject; | ||
|
||
if (value == null) | ||
return null; | ||
|
||
var expectedType = GetExpectedType(sectionName, propertyName); | ||
|
||
if (expectedType == typeof(ScriptBlock[])) | ||
{ | ||
ArrayList scriptBlocks = new ArrayList(); | ||
foreach (string scriptBlock in (ArrayList)value) | ||
{ | ||
scriptBlocks.Add(ScriptBlock.Create(scriptBlock)); | ||
} | ||
value = scriptBlocks; | ||
} | ||
|
||
if (expectedType == typeof(ContainerInfo[])) | ||
{ | ||
ArrayList containers = new ArrayList(); | ||
foreach (PSObject container in (ArrayList)value) | ||
{ | ||
var containerInfo = Pester.ContainerInfo.Create(); | ||
containerInfo.Type = (string)container.Properties["Type"].Value; | ||
containerInfo.Item = container.Properties["Item"].Value; | ||
containerInfo.Data = container.Properties["Data"].Value; | ||
|
||
containers.Add(containerInfo); | ||
} | ||
value = containers; | ||
} | ||
|
||
if (value is ArrayList) | ||
value = ((ArrayList)value).ToArray(); | ||
|
||
return value; | ||
} | ||
|
||
private Type GetExpectedType(string sectionName, string propertyName) | ||
{ | ||
return typeof(PesterConfiguration). | ||
GetProperty(sectionName). | ||
PropertyType. | ||
GetProperty(propertyName). | ||
PropertyType. | ||
GetProperty("Value"). | ||
PropertyType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters