Functions/ConvertTo-CsvObject.Tests.ps1

describe "BitTitan.Modules.Common/ConvertTo-CsvObject" -Tag "module", "unit" {

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

    # Prepare the input
    $csvString = @"
sep=,
"name1","name2","name3"
"1 ",2,"-1"
True,"False","TRUE "
"ゟ","ᆐ","⺝"
"@


    it "converts a CSV string to a CSV object containing unicode characters with no additional processing" {
        # Call the function
        $csvObject = ConvertTo-CsvObject $csvString -ConvertBooleans:$false

        # Verify the output
        $csvObject.GetType().Name | Should Be "Object[]"
        $csvObject.length | Should Be 3
        $csvObject[0].name1 | Should Be "1 "
        $csvObject[0].name2 | Should Be 2
        $csvObject[0].name3 | Should Be "-1"
        $csvObject[1].name1 | Should Be "True"
        $csvObject[1].name2 | Should Be "False"
        $csvObject[1].name3 | Should Be "TRUE "
        $csvObject[2].name1 | Should Be "ゟ"
        $csvObject[2].name2 | Should Be "ᆐ"
        $csvObject[2].name3 | Should Be "⺝"
    }

    # Prepare the input
    $csvString = @"
sep=,
"name1","name2","name3"
"1 ",2,"-1"
True,"False","TRUE "
"@


    it "converts a CSV string to a CSV object converting the booleans" {
        # Call the function
        $csvObject = ConvertTo-CsvObject $csvString

        # Verify the output
        $csvObject.GetType().Name | Should Be "Object[]"
        $csvObject.length | Should Be 2
        $csvObject[0].name1 | Should Be "1 "
        $csvObject[0].name2 | Should Be 2
        $csvObject[0].name3 | Should Be "-1"
        $csvObject[1].name1 | Should Be $true
        $csvObject[1].name2 | Should Be $false
        $csvObject[1].name3 | Should Be $true
    }

    it "converts a CSV string to a CSV object converting the booleans and trimming the strings" {
        # Call the function
        $csvObject = ConvertTo-CsvObject $csvString -TrimStrings

        # Verify the output
        $csvObject.GetType().Name | Should Be "Object[]"
        $csvObject.length | Should Be 2
        $csvObject[0].name1 | Should Be "1"
        $csvObject[0].name2 | Should Be 2
        $csvObject[0].name3 | Should Be "-1"
        $csvObject[1].name1 | Should Be $true
        $csvObject[1].name2 | Should Be $false
        $csvObject[1].name3 | Should Be $true
    }

    it "converts a CSV string to a CSV object converting the booleans and trimming the strings and converting the numbers" {
        # Call the function
        $csvObject = ConvertTo-CsvObject $csvString -TrimStrings -ConvertIntegersMaxLength 2

        # Verify the output
        $csvObject.GetType().Name | Should Be "Object[]"
        $csvObject.length | Should Be 2
        $csvObject[0].name1 | Should Be 1
        $csvObject[0].name2 | Should Be 2
        $csvObject[0].name3 | Should Be -1
        $csvObject[1].name1 | Should Be $true
        $csvObject[1].name2 | Should Be $false
        $csvObject[1].name3 | Should Be $true
    }

    # Declare external functions to fail one at a time
    $externalFunctionsToFail = @(
        "New-TemporaryFile",
        "Set-Content",
        "Import-Csv",
        "ConvertTo-Array"
    )
    foreach ($function in $externalFunctionsToFail) {
        context "when $($function) has an issue" {
            # Mock the function to return null
            mock $function {}

            it "returns null when $($function) fails" {
                # Call the function
                $csvObject = ConvertTo-CsvObject $csvString -ErrorAction SilentlyContinue

                # Verify the output
                $csvObject | Should Be $null
            }

            # Mock the function to throw an exception
            mock $function { throw "mocking function to throw an exception" }

            it "returns null when $($function) throws an exception" {
                # Call the function
                $csvObject = ConvertTo-CsvObject $csvString -ErrorAction SilentlyContinue

                # Verify the output
                $csvObject | Should Be $null
            }
        }
    }
}