Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
ConvertTo-Xml.Tests.ps1127 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "ConvertTo-Xml DRT Unit Tests" -Tags "CI" {5    BeforeAll {6        class fruit {7            [string] $name;8        }9 10        $customPSObject = [PSCustomObject]@{ "prop1" = "val1"; "prop2" = "val2" }11        $newLine = [System.Environment]::NewLine12    }13 14    It "Can convert to XML with parameter Depth" {15        $returnObject = $customPSObject | ConvertTo-Xml -Depth 116        $returnObject | Should -BeOfType System.Xml.XmlDocument17        $expectedValue = '<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.Management.Automation.PSCustomObject"><Property Name="prop1" Type="System.String">val1</Property><Property Name="prop2" Type="System.String">val2</Property></Object></Objects>'18        $returnObject.OuterXml | Should -Be $expectedValue19    }20 21    It "Can convert to XML with parameter NoTypeInformation" {22        $returnObject = $customPSObject | ConvertTo-Xml -NoTypeInformation23        $returnObject | Should -BeOfType System.Xml.XmlDocument24        $expectedValue = '<?xml version="1.0" encoding="utf-8"?><Objects><Object><Property Name="prop1">val1</Property><Property Name="prop2">val2</Property></Object></Objects>'25        $returnObject.OuterXml | Should -Be $expectedValue26    }27 28    It "Can convert to XML as String" {29        $returnObject = $customPSObject | ConvertTo-Xml -As String30        $returnObject | Should -BeOfType System.String31        $expectedValue = @"32<?xml version="1.0" encoding="utf-8"?>$newLine<Objects>$newLine  <Object Type="System.Management.Automation.PSCustomObject">$newLine    <Property Name="prop1" Type="System.String">val1</Property>$newLine    <Property Name="prop2" Type="System.String">val2</Property>$newLine  </Object>$newLine</Objects>33"@34        $returnObject | Should -Be $expectedValue35    }36 37    It "Can convert to XML as Stream" {38        $returnObject = $customPSObject | ConvertTo-Xml -As Stream39        $returnObject -is [System.Array] | Should -BeTrue # Cannot use -BeOfType syntax due to issue https://github.com/pester/Pester/issues/38640        $stream1 = '<?xml version="1.0" encoding="utf-8"?>'41        $stream2 = '<Objects>'42        $stream3 = @"43<Object Type="System.Management.Automation.PSCustomObject">$newLine  <Property Name="prop1" Type="System.String">val1</Property>$newLine  <Property Name="prop2" Type="System.String">val2</Property>$newLine</Object>44"@45        $stream4 = '</Objects>'46 47        $returnObject | Should -HaveCount 448        $returnObject[0] | Should -Be $stream149        $returnObject[1] | Should -Be $stream250        $returnObject[2] | Should -Be $stream351        $returnObject[3] | Should -Be $stream452    }53 54    It "Can convert to XML as Document" {55        $returnObject = $customPSObject | ConvertTo-Xml -As Document -NoTypeInformation56        $returnObject | Should -BeOfType System.Xml.XmlDocument57        $expectedValue = '<?xml version="1.0" encoding="utf-8"?><Objects><Object><Property Name="prop1">val1</Property><Property Name="prop2">val2</Property></Object></Objects>'58        $returnObject.OuterXml | Should -Be $expectedValue59    }60 61    It "Can be stopped with method StopProcessing" {62		$ps = [PowerShell]::Create()63		$ps.AddCommand("Get-Process")64		$ps.AddCommand("ConvertTo-Xml")65		$ps.AddParameter("Depth", 2)66		$ps.BeginInvoke()67		$ps.Stop()68		$ps.InvocationStateInfo.State | Should -BeExactly "Stopped"69    }70 71    # these tests just cover aspects that aren't normally exercised being used as a cmdlet72	It "Can read back switch and parameter values using API" {73        Add-Type -AssemblyName "${pshome}/Microsoft.PowerShell.Commands.Utility.dll"74 75		$cmd = [Microsoft.PowerShell.Commands.ConvertToXmlCommand]::new()76		$cmd.NoTypeInformation = $true77		$cmd.NoTypeInformation | Should -BeTrue78    }79 80    It "Can serialize integer primitive type" {81        [int] $i = 182        $x = $i | ConvertTo-Xml83        $x.Objects.Object.Type | Should -BeExactly $i.GetType().ToString()84        $x.Objects.Object."#text" | Should -BeExactly $i85    }86 87    It "Can serialize ContainerType.Dictionary type" {88        $a = @{foo="bar"}89        $x = $a | ConvertTo-Xml90        $x.Objects.Object.Type | Should -BeExactly $a.GetType().ToString()91        $x.Objects.Object.Property[0].Name | Should -BeExactly "Key"92        $x.Objects.Object.Property[0]."#text" | Should -BeExactly "foo"93        $x.Objects.Object.Property[1].Name | Should -BeExactly "Value"94        $x.Objects.Object.Property[1]."#text" | Should -BeExactly "bar"95    }96 97    It "Can serialize ContainerType.Enumerable type" {98        $fruit1 = [fruit]::new()99        $fruit1.name = "apple"100        $fruit2 = [fruit]::new()101        $fruit2.name = "banana"102        $x = $fruit1,$fruit2 | ConvertTo-Xml103        $x.Objects.Object | Should -HaveCount 2104        $x.Objects.Object[0].Type | Should -BeExactly $fruit1.GetType().FullName105        $x.Objects.Object[0].Property.Name | Should -BeExactly "name"106        $x.Objects.Object[0].Property."#text" | Should -BeExactly "apple"107        $x.Objects.Object[1].Type | Should -BeExactly $fruit2.GetType().FullName108        $x.Objects.Object[1].Property.Name | Should -BeExactly "name"109        $x.Objects.Object[1].Property."#text" | Should -BeExactly "banana"110    }111 112    It "Can serialize nested PSCustomObject properties" {113        $nestedObject = [PSCustomObject]@{114            Prop1 = [PSCustomObject]@{115                Prop1 = [PSCustomObject]@{116                    Prop1 = 111117                    Prop2 = 222118                }119                Prop2 = 22120            }121            Prop2 = 2122        }123        $x = $nestedObject | ConvertTo-Xml -Depth 1124        $x.OuterXml | Should -Be '<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.Management.Automation.PSCustomObject"><Property Name="Prop1" Type="System.Management.Automation.PSCustomObject"><Property Type="System.String">@{Prop1=; Prop2=22}</Property><Property Name="Prop1" Type="System.Management.Automation.PSNoteProperty">@{Prop1=111; Prop2=222}</Property><Property Name="Prop2" Type="System.Management.Automation.PSNoteProperty">22</Property></Property><Property Name="Prop2" Type="System.Int32">2</Property></Object></Objects>'125    }126}127