tests/functions/Get-OrElse.Tests.ps1
InModuleScope PSDataKit { Describe 'Get-OrElse' { $mockedEmptyArray = @() $mockedSampleArray = @(1, 2, 3, 2, 1) $mockedEmptyHashTable = @{ } $mockedSampleHashTable = @{a = 1; b = "2" } $mockedEmptyObject = New-Object -TypeName psobject $mockedSampleObject = New-Object -TypeName psobject $mockedSampleObject | Add-Member -MemberType NoteProperty -Name "UserName" -Value "SampleName" Context 'with pipelined value' { It 'returns default if value is null' { Get-OrElse $Null "default" | Should Be "default" } It 'returns default if value is empty' { Get-OrElse "" "default" | Should Be "default" } It 'returns value if value is neither null or empty' { Get-OrElse "a" "default" | Should Be "a" Get-OrElse 1 "default" | Should Be 1 Get-OrElse " " "default" | Should Be " " } } Context 'with pipeline value' { It 'returns default if value is null' { $Null | Get-OrElse -Default "default" | Should Be "default" } It 'returns default if value is empty' { "" | Get-OrElse -Default "default" | Should Be "default" } It 'returns value if value is neither null or empty' { "a" | Get-OrElse -Default "default" | Should Be "a" 1 | Get-OrElse -Default "default" | Should Be 1 " " | Get-OrElse -Default "default" | Should Be " " } } Context 'with pipeline objects' { It 'should return default if array is empty' { $mockedEmptyArray | Get-OrElse -Default "default" | Should Be "default" } It 'should return default if hashtable is empty' { $mockedEmptyHashTable | Get-OrElse -Default "default" | Should Be "default" } It 'should return object if object not empty' { $mockedSampleObject | Get-OrElse -Default "default" | Should Be $mockedSampleObject } It 'should return object if new object PSObject initialized but without members' { $mockedEmptyObject | Get-OrElse -Default "default" | Should Be $mockedEmptyObject } } Context 'with parametrized value' { It 'should return provided hashmap' { Get-OrElse -Value $mockedSampleHashTable -Default "default" | Should Be $mockedSampleHashTable } It 'should return provided array' { Get-OrElse -Value $mockedSampleArray -Default "default" | Should Be $mockedSampleArray } } } } |