tests/functions/Copy-Object.Tests.ps1

InModuleScope PSDataKit {

    Describe 'PSDataKit deep copy capabilities (Copy-Object)' {

        Context "null or empty" {

            $nullObject = $null
            $emptyHashtable = @{}
            $emptyArray = @()
            $emptyString = ""

            # variable needs to exist to apply [ref]
            $result = $null

            It 'Should return empty hashtable' {
                Copy-Object -Original $emptyHashtable -DeepClone ([ref]$result)
                $result | Should -BeOfType [hashtable]
                $result | Should -BeNullOrEmpty
            }
            It 'Should return empty array' {
                Copy-Object -Original $emptyArray -DeepClone ([ref]$result)
                $result.GetType() | Should -be @().GetType()
                $result | Should -BeNullOrEmpty
            }
            It 'Should return empty string' {
                Copy-Object -Original $emptyString -DeepClone ([ref]$result)
                $result | Should -BeOfType [string]
                $result | Should -BeNullOrEmpty
            }
            It 'Should return null' {
                Copy-Object -Original $nullObject -DeepClone ([ref]$result)
                $result | Should -BeExactly $null
            }
        }

        Context "complex object" {

            $original = @{
                nullObject = $null
                emptyHashtable = @{}
                emptyArray = @()
                emptyString = ""
                nonEmptyString = "nonEmptyStirng"
                singleObjectArray = @("single")
                multipleObjectArray = @(1, 2)
                singleObjectHastable = @{ foo = "single" }
                multipleObjectHashtable = @{foo1 = 1; foo2 = "multiple"; foo3 = @("1","2","3")}
            }

            # variable needs to exist to apply [ref]
            $result = $null

            It 'Should make a deep copy' {
                Copy-Object -Original $original -DeepClone ([ref]$result)
                $result.nullObject | Should -BeExactly $null
                $result.emptyHashtable | Should -BeOfType [hashtable]
                $result.emptyHashtable | Should -BeNullOrEmpty
                $result.emptyArray | Should -BeExactly @()
                $result.emptyString | Should -BeExactly ""
                $result.nonEmptyString | Should -BeExactly "nonEmptyStirng"
                $result.singleObjectArray | Should -BeExactly @("single")
                $result.multipleObjectArray | Should -BeExactly @(1, 2)
                $result.singleObjectHastable | Should -BeOfType [hashtable]
                $result.singleObjectHastable.foo | Should -BeExactly "single"
                $result.multipleObjectHashtable | Should -BeOfType [hashtable]
                $result.multipleObjectHashtable.Count | Should -BeExactly 3
                $result.multipleObjectHashtable.foo1 | Should -BeExactly 1
                $result.multipleObjectHashtable.foo2 | Should -BeExactly "multiple"
                $result.multipleObjectHashtable.foo3 | Should -BeExactly @("1","2","3")
            }
        }
    }
}