Public/Tests/Get-LeadingSpaceCount.Tests.ps1

BeforeAll {
    $env:TCS_CONFIG_ROOT = Join-Path -Path $TestDrive -ChildPath 'config'
    $env:TCS_SKIP_UPDATE_CHECK = '1'
    $env:TCS_TELEMETRY_OPTOUT = '1'
    $ModuleRoot = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent
    Import-Module -Name (Join-Path -Path $ModuleRoot -ChildPath 'tcs.utils.psd1') -Force
}

AfterAll {
    Remove-Module -Name tcs.utils -Force -ErrorAction SilentlyContinue
}

Describe 'Get-LeadingSpaceCount' {
    It 'Returns <Expected> for <Value>' -TestCases @(
        @{ Value = 'abc'; Expected = 0 }
        @{ Value = ' abc'; Expected = 3 }
        @{ Value = "`t`tabc"; Expected = 2 }
        @{ Value = " `t abc"; Expected = 3 }
        @{ Value = ' '; Expected = 4 }
        @{ Value = "`nabc"; Expected = 0 }
    ) {
        Get-LeadingSpaceCount -InputString $Value | Should -Be $Expected
    }

    It 'Returns 0 for an empty or null string' {
        Get-LeadingSpaceCount -InputString '' | Should -Be 0
        Get-LeadingSpaceCount -InputString $null | Should -Be 0
    }

    It 'Returns an integer' {
        Get-LeadingSpaceCount -InputString ' x' | Should -BeOfType [int]
    }

    It 'Accepts pipeline input' {
        ' two', ' four', 'none' | Get-LeadingSpaceCount | Should -Be @(2, 4, 0)
    }

    Context 'Telemetry' {
        BeforeAll {
            Mock -ModuleName tcs.utils Invoke-TelemetryCollection { }
        }

        It 'Sends one Start and one End event for a pipeline of several strings' {
            ' two', ' four', 'none' | Get-LeadingSpaceCount | Should -Be @(2, 4, 0)
            Should -Invoke -ModuleName tcs.utils Invoke-TelemetryCollection -Times 1 -Exactly -ParameterFilter { $Stage -eq 'Start' -and $CommandName -eq 'Get-LeadingSpaceCount' }
            Should -Invoke -ModuleName tcs.utils Invoke-TelemetryCollection -Times 1 -Exactly -ParameterFilter { $Stage -eq 'End' -and -not $Failed }
        }
    }
}