Functions/ConvertTo-Array.Tests.ps1

describe "BitTitan.Runbooks.Common/ConvertTo-Array" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\ConvertTo-Array.ps1"

    it "returns an empty array when given a null value" {
        # Set up the function input
        $inputValue = $null

        # Call the function
        $output = ConvertTo-Array $inputValue

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

    it "returns an empty array when given an empty Object[]" {
        # Set up the function input
        $inputValue = @()
        $inputValue.GetType().Name | Should Be "Object[]"
        $inputValue.length | Should Be 0

        # Call the function
        $output = ConvertTo-Array $inputValue

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

    it "returns an array containing that item when given an Object[] containing one item" {
        # Set up the function input
        $inputValue = @()
        $inputValue += 42
        $inputValue.GetType().Name | Should Be "Object[]"
        $inputValue.length | Should Be 1

        # 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
    }

    it "returns an array containing the items when given an Object[] containing multiple items" {
        # Set up the function input
        $inputValue = @()
        $inputValue += 42
        $inputValue += 43
        $inputValue += 44
        $inputValue.GetType().Name | Should Be "Object[]"
        $inputValue.length | Should Be 3

        # 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
    }

    it "returns an empty array when given an empty PSObject[]" {
        # Set up the function input
        $inputValue = New-Object PSObject[] 0
        $inputValue.GetType().Name | Should Be "PSObject[]"
        $inputValue.length | Should Be 0

        # Call the function
        $output = ConvertTo-Array $inputValue

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

    it "returns an array containing the item when given an PSObject[] containing one item" {
        # Set up the function input
        $inputValue = New-Object PSObject[] 1
        $inputValue[0] = 42
        $inputValue.GetType().Name | Should Be "PSObject[]"
        $inputValue.length | Should Be 1

        # 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
    }

    it "returns an array containing the items when given an PSObject[] containing multiple items" {
        # Set up the function input
        $inputValue = New-Object PSObject[] 3
        $inputValue[0] = 42
        $inputValue[1] = 43
        $inputValue[2] = 44
        $inputValue.GetType().Name | Should Be "PSObject[]"
        $inputValue.length | Should Be 3

        # 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
    }

    it "returns an empty array when given a empty List``1" {
        # Set up the function input
        $inputValue = New-Object "System.Collections.Generic.List[Int]"
        $inputValue.GetType().Name | Should Be "List``1"
        $inputValue.count | Should Be 0

        # Call the function
        $output = ConvertTo-Array $inputValue

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

    it "returns an array containing the item when given a List``1 containing one item" {
        # Set up the function input
        $inputValue = New-Object "System.Collections.Generic.List[Int]"
        $inputValue.Add(42)
        $inputValue.GetType().Name | Should Be "List``1"
        $inputValue.length | Should Be 1

        # 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
    }

    it "returns an array containing the items when given a List``1 containing multiple items" {
        # Set up the function input
        $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

        # 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
    }

    it "returns an array containing the String when given a single String" {
        # Set up the function input
        $inputValue = "stringValue"
        $inputValue.GetType().Name | Should Be "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"
    }

    it "returns an array containing the Int when given a single Int" {
        # Set up the function input
        $inputValue = 42
        $inputValue.GetType().Name | Should BeLike "*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
    }
}