tests/functions/Merge-Hashtables.Tests.ps1


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")
            }
        }
    }
}