Variables.Tests.ps1

####################################################################################################
# Declarations
####################################################################################################

# Set InformationPreference
$InformationPreference = "Continue"

####################################################################################################
# Functions
####################################################################################################

# Import other BitTitan.Runbooks modules
Import-BT_Module BitTitan.Runbooks.Common -Quiet

####################################################################################################
# The tests
####################################################################################################

describe "Modules/BitTitan.Runbooks.Common/Variables/Set-DefaultValue" -Tags "module", "common", "unit" {

    context "when given a global variable containing null" {

        it "sets the variable to the default value" {
            $Global:variable = $null
            $variable | Set-DefaultValue 42
            $variable | Should Be 42

            $Global:variable = $null
            $variable | Set-DefaultValue "string"
            $variable | Should Be "string"

            $Global:variable = $null
            $variable | Set-DefaultValue $true
            $variable | Should Be $true

            $Global:variable = $null
            $variable | Set-DefaultValue @(1, 2, 3)
            $variable | Should Be @(1, 2, 3)
        }
    }

    context "when given a global variable already containing a value" {

        it "does not change the value of the variable" {
            $Global:variable = 5
            $variable | Set-DefaultValue 42
            $variable | Should Be 5

            $Global:variable = "foo"
            $variable | Set-DefaultValue "bar"
            $variable | Should Be "foo"

            $Global:variable = $false
            $variable | Set-DefaultValue $true
            $variable | Should Be $false

            $Global:variable = @(1, 2)
            $variable | Set-DefaultValue @(1, 2, 3)
            $variable | Should Be @(1, 2)
        }
    }

    context "when given a script variable containing null" {

        it "removes the script variable and sets the default for the global variable" {
            $Global:variable = 5
            $Script:variable = $null
            $variable | Set-DefaultValue 42
            $Script:Variable | Should Be $null
            $Global:Variable | Should Be 42
        }
    }

    context "when given a script variable already containing a value" {

        it "does not change the value of the global or script variable" {
            $Global:variable = 5
            $Script:variable = 10
            $variable | Set-DefaultValue 42
            $Global:variable | Should Be 5
            $Script:variable | Should Be 10
        }
    }
}