Arrays.Tests.ps1

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

# Set InformationPreference
$InformationPreference = "Continue"

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

# Add modules folder to path if not already added
if (!$env:PSModulePath.EndsWith(";$(Get-Location)\Modules")) {
    $env:PSModulePath += ";$(Get-Location)\Modules"
}

# Import functions from BitTitan.Runbooks.Modules
Import-Module BitTitan.Runbooks.Common -Force

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

describe "Modules/BitTitan.Runbooks.Common/Arrays/ConvertTo-Array" -Tags "module", "common", "unit" {

    context "when given a null value" {
        $inputValue = $null

        it "returns an empty array" {
            # Call the function
            $output = ConvertTo-Array $inputValue

            # Verify the output
            $output.GetType().Name | Should Be "Object[]"
            $output.length | Should Be 0
        }
    }

    context "when given an empty Object[]" {
        $inputValue = @()
        $inputValue.GetType().Name | Should Be "Object[]"
        $inputValue.length | Should Be 0

        it "returns an empty array" {
            # Call the function
            $output = ConvertTo-Array $inputValue

            # Verify the output
            $output.GetType().Name | Should Be "Object[]"
            $output.length | Should Be 0
        }
    }

    context "when given an Object[] containing one item" {
        $inputValue = @()
        $inputValue += 42
        $inputValue.GetType().Name | Should Be "Object[]"
        $inputValue.length | Should Be 1

        it "returns an array containing that item" {
            # Call the function
            $output = ConvertTo-Array $inputValue

            # Verify the output
            $output.GetType().Name | Should Be "Object[]"
            $output.length | Should Be 1
            $output[0] | Should Be 42
        }
    }

    context "when given an Object[] containing multiple items" {
        $inputValue = @()
        $inputValue += 42
        $inputValue += 43
        $inputValue += 44
        $inputValue.GetType().Name | Should Be "Object[]"
        $inputValue.length | Should Be 3

        it "returns an array containing the items" {
            # Call the function
            $output = ConvertTo-Array $inputValue

            # Verify the output
            $output.GetType().Name | Should Be "Object[]"
            $output.length | Should Be 3
            $output[0] | Should Be 42
            $output[1] | Should Be 43
            $output[2] | Should Be 44
        }
    }

    context "when given an empty PSObject[]" {
        $inputValue = New-Object PSObject[] 0
        $inputValue.GetType().Name | Should Be "PSObject[]"
        $inputValue.length | Should Be 0

        it "returns an empty array" {
            # Call the function
            $output = ConvertTo-Array $inputValue

            # Verify the output
            $output.GetType().Name | Should Be "Object[]"
            $output.length | Should Be 0
        }
    }

    context "when given an PSObject[] containing one item" {
        $inputValue = New-Object PSObject[] 0
        $inputValue += 42
        $inputValue.GetType().Name | Should Be "PSObject[]"
        $inputValue.length | Should Be 1

        it "returns an array containing that item" {
            # Call the function
            $output = ConvertTo-Array $inputValue

            # Verify the output
            $output.GetType().Name | Should Be "Object[]"
            $output.length | Should Be 1
            $output[0] | Should Be 42
        }
    }

    context "when given an PSObject[] containing multiple items" {
        $inputValue = New-Object PSObject[] 0
        $inputValue += 42
        $inputValue += 43
        $inputValue += 44
        $inputValue.GetType().Name | Should Be "PSObject[]"
        $inputValue.length | Should Be 3

        it "returns an array containing the items" {
            # Call the function
            $output = ConvertTo-Array $inputValue

            # Verify the output
            $output.GetType().Name | Should Be "Object[]"
            $output.length | Should Be 3
            $output[0] | Should Be 42
            $output[1] | Should Be 43
            $output[2] | Should Be 44
        }
    }

    context "when given a empty List``1" {
        $inputValue = New-Object "System.Collections.Generic.List[Int]"
        $inputValue.GetType().Name | Should Be "List``1"
        $inputValue.count | Should Be 0

        it "returns an empty array" {
            # Call the function
            $output = ConvertTo-Array $inputValue

            # Verify the output
            $output.GetType().Name | Should Be "Object[]"
            $output.length | Should Be 0
        }
    }

    context "when given a List``1 containing one item" {
        $inputValue = New-Object "System.Collections.Generic.List[Int]"
        $inputValue.Add(42)
        $inputValue.GetType().Name | Should Be "List``1"
        $inputValue.length | Should Be 1

        it "returns an array containing that item" {
            # Call the function
            $output = ConvertTo-Array $inputValue

            # Verify the output
            $output.GetType().Name | Should Be "Object[]"
            $output.length | Should Be 1
            $output[0] | Should Be 42
        }
    }

    context "when given a List``1 containing multiple items" {
        $inputValue = New-Object "System.Collections.Generic.List[Int]"
        $inputValue.Add(42)
        $inputValue.Add(43)
        $inputValue.Add(44)
        $inputValue.GetType().Name | Should Be "List``1"
        $inputValue.count | Should Be 3

        it "returns an array containing the items" {
            # Call the function
            $output = ConvertTo-Array $inputValue

            # Verify the output
            $output.GetType().Name | Should Be "Object[]"
            $output.length | Should Be 3
            $output[0] | Should Be 42
            $output[1] | Should Be 43
            $output[2] | Should Be 44
        }
    }

    context "when given a single String" {
        $inputValue = "stringValue"
        $inputValue.GetType().Name | Should Be "String"

        it "returns an array containing the String" {
            # Call the function
            $output = ConvertTo-Array $inputValue

            # Verify the output
            $output.GetType().Name | Should Be "Object[]"
            $output.length | Should Be 1
            $output[0] | Should Be "stringValue"
        }
    }

    context "when given a single Int" {
        $inputValue = 42
        $inputValue.GetType().Name | Should BeLike "*Int*"

        it "returns an array containing the Int" {
            # Call the function
            $output = ConvertTo-Array $inputValue

            # Verify the output
            $output.GetType().Name | Should Be "Object[]"
            $output.length | Should Be 1
            $output[0] | Should Be 42
        }
    }
}