Tests/Out-HashString.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#$here = Split-Path -Parent $MyInvocation.MyCommand.Path
#$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
#. "$here\$sut"

$modulePath = Split-Path $PSScriptRoot -Parent
$modulepath = Join-Path -Path $modulePath -ChildPath poshARM.psd1
Import-Module $modulePath

Describe "Out-HashString" {

    Context "Without Pipeline" {
        It "Should return an empty hashtable if input is not a hashtable" {
            $expected = "@{}"
            $actual = Out-HashString -InputObject "foo"
            $actual | Should Be $Expected
        }

        It "Should convert a hashtabel to a string representation" {
            $var = @{Test="keyValue"}
            $Expected = '@{\r\n Test = "keyValue"\r\n}' -replace ([regex]::Escape("\r\n")), [environment]::NewLine
            $actual = Out-HashString -InputObject $var
            $actual | Should Be $Expected
        }

        It "Should handle nested hashtables" {
            $var = @{Test="keyValue";Nested = @{Key1="Value1";Key2="Value2"}}
            $Expected = '@{\r\n Test = "keyValue"\r\n Nested = @{\r\n Key1 = "Value1"\r\n Key2 = "Value2"\r\n }\r\n}' -replace ([regex]::Escape("\r\n")), [environment]::NewLine
            $actual = Out-HashString -InputObject $var
            $actual | Should Be $Expected
        }
    }

    Context "With Pipeline" {
        It "Should return an empty hashtable if input is not a hashtable" {
            $expected = "@{}"
            $actual = "foo" | Out-HashString
            $actual | Should Be $Expected
        }

        It "Should convert a hashtabel to a string representation" {
            $var = @{Test="keyValue"}
            $Expected = '@{\r\n Test = "keyValue"\r\n}' -replace ([regex]::Escape("\r\n")), [environment]::NewLine
            $actual = $var | Out-HashString
            $actual | Should Be $Expected
        }

        It "Should handle nested hashtables" {
            $var = @{Test="keyValue";Nested = @{Key1="Value1";Key2="Value2"}}
            $Expected = '@{\r\n Test = "keyValue"\r\n Nested = @{\r\n Key1 = "Value1"\r\n Key2 = "Value2"\r\n }\r\n}' -replace ([regex]::Escape("\r\n")), [environment]::NewLine
            $actual = $var | Out-HashString
            $actual | Should Be $Expected
        }
    }    
}

Remove-Module -name posharm -ErrorAction SilentlyContinue