tests/functions/Copy-Object.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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")
            }
        }
    }
}