Tests/PSTemplate.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
Import-Module "$PSScriptRoot\..\PSTemplate" -Force -DisableNameChecking

#region----------TEST DATA

    $MultilineStringTemplate = @"
line 1: {{Token1}}
line 2: {{Token2}}
"@


    $MultilineStringWithBlanksTemplate = @"
line 1: {{Token1}}
 
line 2: {{Token2}}
"@


    $HashTableWithAllKeys = @{
        Token1 = "Value1"
        Token2 = "Value2"
    }

    $MultilineStringFullyFilled = @"
line 1: Value1
line 2: Value2
"@


    $MultilineStringWithBlanksFullyFilled = @"
line 1: Value1
 
line 2: Value2
"@


    $HashTableWithLessKeys = @{
        Token1 = "Value1"
    }

    $MultilineStringLessFilled = @"
line 1: Value1
line 2: {{Token2}}
"@


    $SingleLineStringTemplate = "Token 1: {{Token1}}; Token 2: {{Token2}}"
    $SingleLineStringFullyFilled = "Token 1: Value1; Token 2: Value2"
    $SingleLineStringLessFilled = "Token 1: Value1; Token 2: {{Token2}}"

    $MultiLineTextFileTemplate = Get-Content "$PSScriptRoot\MultilineTextFile.template"
    $MultiLineTextFileFullyFilled = Get-Content "$PSScriptRoot\MultilineTextFile.FullyFilled"
    $MultiLineTextFileWithBlanksTemplate = Get-Content "$PSScriptRoot\MultilineTextFileWithBlanks.template"
    $MultiLineTextFileWithBlanksFullyFilled = Get-Content "$PSScriptRoot\MultilineTextFileWithBlanks.FullyFilled"
    $MultiLineTextFileLessFilled = Get-Content "$PSScriptRoot\MultilineTextFile.LessFilled"
        
#endregion-------

#region----------TESTS

Describe "Invoke-PSTemplate" {

    Context "Multi-line string" {

        It "replaces all {{<>}} tokens in a single multi-line string with values in a hash table that contains all tokens as keys" {
            Invoke-PSTemplate -Template $MultilineStringTemplate -Variables $HashTableWithAllKeys | Should Be $MultilineStringFullyFilled
        }

        It "replaces all {{<>}} tokens in a single multi-line string, including blank lines, with values in a hash table that contains all tokens as keys" {
            Invoke-PSTemplate -Template $MultilineStringWithBlanksTemplate -Variables $HashTableWithAllKeys | Should Be $MultilineStringWithBlanksFullyFilled
            {Invoke-PSTemplate -Template $MultilineStringWithBlanksTemplate -Variables $HashTableWithAllKeys} | Should Not Throw
        }
    
        It "replaces some {{<>}} tokens in a single multi-line string with values in a hash table that contains some tokens as keys" {
            Invoke-PSTemplate -Template $MultilineStringTemplate -Variables $HashTableWithLessKeys | Should Be $MultilineStringLessFilled
        }

        It "returns an array of the same size as the input" {
            (Invoke-PSTemplate -Template $MultilineStringTemplate -Variables $HashTableWithAllKeys).Count | Should Be $MultilineStringTemplate.Count
        }
    }

    Context "Single-line string"{

        It "replaces all {{<>}} tokens in a single single-line string with values in a hash table that contains all tokens as keys" {
            Invoke-PSTemplate -Template $SingleLineStringTemplate -Variables $HashTableWithAllKeys | Should Be $SingleLineStringFullyFilled
        }

        It "replaces some {{<>}} tokens in a single single-line string with values in a hash table that contains some tokens as keys" {
            Invoke-PSTemplate -Template $SingleLineStringTemplate -Variables $HashTableWithLessKeys | Should Be $SingleLineStringLessFilled
        }

        It "returns an array of the same size as the input" {
            (Invoke-PSTemplate -Template $SingleLineStringTemplate -Variables $HashTableWithAllKeys).Count | Should Be $SingleLineStringFullyFilled.Count
        }
    }

    Context "Multi-line text file" {

        It "replaces all {{<>}} tokens in a single multi-line text file with values in a hash table that contains all tokens as keys" {
            Invoke-PSTemplate -Template $MultiLineTextFileTemplate -Variables $HashTableWithAllKeys | Should Be $MultiLineTextFileFullyFilled
        }

        It "replaces all {{<>}} tokens in a single multi-line text file, including blank lines, with values in a hash table that contains all tokens as keys" {
            # pester 3 is funky with arrays, blank entries and pipes. As we're explictly testing blank lines in an array here, we need to deal with it
            # instead of comparing the arrays, join the arrays in the same way. They should then be the same if the original arrays were the same
            $Filled = Invoke-PSTemplate -Template $MultiLineTextFileWithBlanksTemplate -Variables $HashTableWithAllKeys
            $FilledJoined = $Filled -join "`n"
            $TargetJoined = $MultiLineTextFileWithBlanksFullyFilled -join "`n"
            $FilledJoined | Should Be $TargetJoined

            $Filled.Count | Should Be $MultiLineTextFileWithBlanksFullyFilled.Count

            {Invoke-PSTemplate -Template $MultiLineTextFileWithBlanksTemplate -Variables $HashTableWithAllKeys} | Should Not Throw
        }

        It "replaces some {{<>}} tokens in a single multi-line text file with values in a hash table that contains some tokens as keys" {
            Invoke-PSTemplate -Template $MultiLineTextFileTemplate -Variables $HashTableWithLessKeys | Should Be $MultiLineTextFileLessFilled
        }
        
        It "returns an array of the same size as the input" {
            (Invoke-PSTemplate -Template $MultiLineTextFileTemplate -Variables $HashTableWithAllKeys).Count | Should Be $MultiLineTextFileTemplate.Count
        }
    }

} 

Describe "Fill-Template" {

    It "is an alias of Invoke-PSTemplate" {
        (get-alias -Name "Fill-Template").ReferencedCommand.Name | Should Be "Invoke-PSTemplate"
    }

    It "has the same functionality as Invoke-PSTemplate (replaces all {{<>}} tokens in a single multi-line string with values in a hash table that contains all tokens as keys)" {
        Fill-Template -Template $MultilineStringTemplate -Variables $HashTableWithAllKeys | Should Be $MultilineStringFullyFilled
    }

}

#endregion-------