tests/functions/Merge-Hashtables.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

InModuleScope PSDataKit {

    Describe 'PSDataKit merging capabilities (Merge-Hashtables)' {

        Context "secondary parameter" {
            It 'Merge with $null secondary parameter' {

                $primary = @{
                    foo = "foo"
                    bar = "bar"
                }
                $secondary = $null

                $result = Merge-Hashtables -primary $primary -secondary $secondary
                $result | Should -BeExactly $primary
            }
            It 'Merge with BeNullOrEmpty values inside secondary parameter' {

                $primary = @{
                    nullValue      = "nullValue"
                    emptyValue     = "emptyValue"
                    emptyArray     = "emptyArray"
                    emptyHashtable = "emptyHashtable"
                }
                $secondary = @{
                    emptyValue     = ""
                    nullValue      = $null
                    emptyArray     = @()
                    emptyHashtable = @{ }
                }

                $result = Merge-Hashtables -primary $primary -secondary $secondary
                $result.nullValue | Should -Be $primary.nullValue
                $result.emptyValue | Should -Be $primary.emptyValue
                $result.emptyArray | Should -Be $primary.emptyArray
                $result.emptyHashtable | Should -Be $primary.emptyHashtable
                $result.Test | Should -Be $primary.Test
            }

            It 'Merge with BeNullOrEmpty values inside secondary parameter when primary is hashtable' {

                $primary = @{
                    foo = @{
                        bar = "bar"
                    }
                }
                $secondary = @{
                    foo = $null
                }

                $result = Merge-Hashtables -primary $primary -secondary $secondary
                $result.foo.bar | Should -Be $primary.foo.bar
            }

            It "Merge and does not alter the input parameters" {

                $primary = @{
                    Key1 = @{
                        test = "IsThere"
                    }
                    Key2 = @{
                        test = "IsThere"
                    }
                }
                $primaryJson= ($primary | ConvertTo-Json)
                $secondary = @{
                    Key1 = @{
                        test   = "Will be lost"
                        remain = "Will remain"
                    }
                    Key3 = "Will be added"
                }
                $secondaryJson = ($secondary | ConvertTo-Json)

                $expectedResults = @{
                    Key1 = @{
                        test   = "IsThere"
                        remain = "Will remain"
                    }
                    Key2 = @{
                        test = "IsThere"
                    }
                    Key3 = "Will be added"
                }

                $result = Merge-Hashtables -primary $primary -secondary $secondary
                $result.Key1.test | Should -BeExactly $expectedResults.Key1.test
                $result.Key1.remain | Should -BeExactly $expectedResults.Key1.remain
                $result.Key2.test | Should -BeExactly $expectedResults.Key2.test
                $result.Key3 | Should -BeExactly $expectedResults.Key3
                ($primary | ConvertTo-Json) | Should -Be $primaryJson
                ($secondary | ConvertTo-Json) | Should -Be $secondaryJson
            }
        }
    }

    Describe 'PSDataKit shallow merging capabilities (Merge-Hashtables)' {

        Context "switch shallow with empty primary array" {

            $primary = @{
                foo = @()
            }
            $secondary = @{
                foo = @("secondary")
            }

            It 'Should take secondary array without shallow' {
                $result = Merge-Hashtables -primary $primary -secondary $secondary
                $result.foo | Should -BeExactly @("secondary")
            }
            It 'Should take primary array with shallow' {
                $result = Merge-Hashtables -primary $primary -secondary $secondary -shallow
                $result.foo | Should -BeExactly @()
            }
        }

        Context "switch shallow with single-object array" {

            $primary = @{
                foo = @("primary")
            }
            $secondary = @{
                foo = @("secondary")
            }

            It 'Should merge arrays without shallow' {
                $result = Merge-Hashtables -primary $primary -secondary $secondary
                $result.foo | Should -BeExactly @("primary","secondary")
            }
            It 'Should take primary array with shallow' {
                $result = Merge-Hashtables -primary $primary -secondary $secondary -shallow
                $result.foo | Should -BeExactly @("primary")
            }
        }

        Context "switch shallow with multiple-object array" {

            $primary = @{
                foo = @("primary","primo")
            }
            $secondary = @{
                foo = @("secondary","secundo")
            }

            It 'Should merge arrays without shallow' {
                $result = Merge-Hashtables -primary $primary -secondary $secondary
                $result.foo | Should -BeExactly @("primary","primo","secondary","secundo")
            }
            It 'Should take primary array with shallow' {
                $result = Merge-Hashtables -primary $primary -secondary $secondary -shallow
                $result.foo | Should -BeExactly @("primary","primo")
            }
        }

        Context "recursive mode" {

            $primary = @{
                foo = @{arr = @("primary")}
            }
            $secondary = @{
                foo = @{arr = @("secondary")}
            }

            It 'Should merge recursively in normal mode' {
                $result = Merge-Hashtables -primary $primary -secondary $secondary
                $result.foo.arr | Should -BeExactly @("primary","secondary")
            }
            It 'Should merge recursively in shallow mode' {
                $result = Merge-Hashtables -primary $primary -secondary $secondary -shallow
                $result.foo.arr | Should -BeExactly @("primary")
            }
        }
    }
}