Public/ConvertFrom-ConnectorXmlToPowerShellObject.tests.ps1

BeforeAll {
    . "$($PSScriptRoot)/ConvertFrom-ConnectorXmlToPowerShellObject.ps1"
}

Describe "ConvertFrom-ConnectorXmlToPowerShellObject" {
    It "Should throw when input is null" {
        {
            ConvertFrom-ConnectorXmlToPowerShellObject -Node $null
        } | Should -Throw
    }

    It "Should return the InnerText when the node is a text node" {
        $doc = [System.Xml.XmlDocument]::new()
        $textNode = $doc.CreateTextNode("hello")

        $result = $textNode | ConvertFrom-ConnectorXmlToPowerShellObject

        $result | Should -BeExactly "hello"
    }

    It "Should return the text value directly for a node with a single text child and no attributes" {
        [xml]$xml = "<Root>hello</Root>"

        $result = $xml.DocumentElement | ConvertFrom-ConnectorXmlToPowerShellObject

        $result | Should -BeExactly "hello"
    }

    It "Should return an object with a '#text' property when IncludeChildTextNodes is set" {
        [xml]$xml = "<Root>hello</Root>"

        $result = $xml.DocumentElement | ConvertFrom-ConnectorXmlToPowerShellObject -IncludeChildTextNodes

        $result.'#text' | Should -BeExactly "hello"
    }

    It "Should return an object with attributes as properties" {
        [xml]$xml = "<Root id='123' name='test' />"

        $result = $xml.DocumentElement | ConvertFrom-ConnectorXmlToPowerShellObject

        $result.id | Should -BeExactly "123"
        $result.name | Should -BeExactly "test"
    }

    It "Should return an object with child elements as properties" {
        [xml]$xml = "<Root><FirstName>John</FirstName><LastName>Doe</LastName></Root>"

        $result = $xml.DocumentElement | ConvertFrom-ConnectorXmlToPowerShellObject

        $result.FirstName | Should -BeExactly "John"
        $result.LastName | Should -BeExactly "Doe"
    }

    It "Should return a list when there are duplicate child element names" {
        [xml]$xml = "<Root><Entry>A</Entry><Entry>B</Entry><Entry>C</Entry></Root>"

        $result = $xml.DocumentElement | ConvertFrom-ConnectorXmlToPowerShellObject

        $entries = $result.PSObject.Properties['Entry'].Value
        $entries.Count | Should -Be 3
        $entries[0] | Should -BeExactly "A"
        $entries[1] | Should -BeExactly "B"
        $entries[2] | Should -BeExactly "C"
    }

    It "Should handle deeply nested XML structures" {
        [xml]$xml = "<Root><Person><Name>Jane</Name><Address><City>Oslo</City></Address></Person></Root>"

        $result = $xml.DocumentElement | ConvertFrom-ConnectorXmlToPowerShellObject

        $result.Person.Name | Should -BeExactly "Jane"
        $result.Person.Address.City | Should -BeExactly "Oslo"
    }

    It "Should handle an element that has both attributes and child elements" {
        [xml]$xml = "<Root id='42'><Name>Test</Name></Root>"

        $result = $xml.DocumentElement | ConvertFrom-ConnectorXmlToPowerShellObject

        $result.id | Should -BeExactly "42"
        $result.Name | Should -BeExactly "Test"
    }
}