Functions/Convert-CsvStringToObject.Tests.ps1

describe "BitTitan.Modules.Csv/Convert-CsvStringToObject" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Convert-CsvStringToObject.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 = Convert-CsvStringToObject -CsvString $csvString -TrimHeaders:$false -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 trimming the headers" {
        # Call the function
        $csvObject = Convert-CsvStringToObject -CsvString $csvString -ConvertBooleans:$false

        # 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" {
        # Call the function
        $csvObject = Convert-CsvStringToObject $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 = Convert-CsvStringToObject $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 = Convert-CsvStringToObject $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, with an error message" {
                # Call the function
                $csvObject = Convert-CsvStringToObject $csvString -ErrorAction SilentlyContinue -ErrorVariable errorVariable

                # Verify the output
                $errorVariable | Should Not BeNullOrEmpty
                $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, with an error message" {
                # Call the function
                $csvObject = Convert-CsvStringToObject $csvString -ErrorAction SilentlyContinue -ErrorVariable errorVariable

                # Verify the output
                $errorVariable | Should Not BeNullOrEmpty
                $csvObject | Should Be $null
            }

            it "returns null when $($function) throws an exception, with a warning message" {
                # Call the function
                $csvObject = Convert-CsvStringToObject $csvString -OutputStream Warning -WarningAction SilentlyContinue -WarningVariable warningVariable

                # Verify the output
                $warningVariable | Should Not BeNullOrEmpty
                $csvObject | Should Be $null
            }
        }
    }
}