Write-ProgressEx.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# mazzy@mazzy.ru, 2017-10-14
# https://github.com/mazzy-ax/Write-progressEx

$me = Split-Path -Leaf $PSCommandPath
$path = Split-Path -Parent $PSCommandPath
$module = Join-Path $path "Write-ProgressEx.psd1"
Import-Module -Force $module

Describe "Write-ProgressEx" -Tag "Run", "UnitTest", "UT" {
    $outerLevel = 1..3
    $innerLevel = 1..2

    Context "Work" {
        It "work" {
            { Write-ProgressEx } | Should not throw
        }

        It "double trouble work" {
            { Write-ProgressEx; Write-ProgressEx } | Should not throw
        }

        It "parameters: outer and inner loops" {
            {
                Write-ProgressEx "outer" -Total $outerLevel.Count
                $outerLevel | ForEach-Object {
                    Write-ProgressEx "inner" -Total $innerLevel.Count -id 1
                    $innerLevel | ForEach-Object {
                        #start-sleep 1
                        Write-ProgressEx -id 1 -increment
                    }
                    Write-ProgressEx -increment
                }
                Write-ProgressEx -Completed
            } | Should not throw
        }

        It "pipes: outer and inner loops" {
            {
                $outerLevel | Write-ProgressEx "outer" -Total $outerLevel.Count | ForEach-Object {
                    $innerLevel | Write-ProgressEx "inner" -Total $innerLevel.Count -id 1 | ForEach-Object {
                        # ...
                    }
                }
            } | Should not throw
        }

        It "progress info: splatting" {
            $pInfo = Get-ProgressEx -Force
            $pInfo.Activity = 'splating'
            $pInfo.Status = 'another status'
            $pInfo.Total = $outerLevel.Count

            { Write-ProgressEx @pInfo } | Should not throw

            Write-ProgressEx
        }
    }

    Context "Functional" {
        AfterEach {
            Write-ProgressEx # complete all
        }

        It "store paramters in module variable" {
            Write-ProgressEx -Total $outerLevel.Count
            (Get-ProgressEx).Total | Should be $outerLevel.Count
        }

        It "increment counter after the increment" {
            Write-ProgressEx -Total $outerLevel.Count
            (Get-ProgressEx).current | Should be 0

            Write-ProgressEx -increment
            (Get-ProgressEx).current | Should be 1
        }

        It "autocomplete with pipe" {
            $outerLevel | Write-ProgressEx "outer" -Total $outerLevel.Count | ForEach-Object {
                $innerLevel | Write-ProgressEx "inner" -Total $innerLevel.Count -id 1 | ForEach-Object {
                    # ...
                    (Get-ProgressEx -id 0).Total | Should be $outerLevel.Count
                    (Get-ProgressEx -id 1).Total | Should be $innerLevel.Count
                    break
                }
                (Get-ProgressEx -id 0).Total | Should be $outerLevel.Count
                (Get-ProgressEx -id 1).Total | Should be $null
                break
            }
            (Get-ProgressEx -id 0) | Should be $null
        }

        It "complete children progress when 'previous' id used" {
            Write-ProgressEx -Total $outerLevel.Count
            Write-ProgressEx -Total $innerLevel.Count -id 1
            (Get-ProgressEx -id 0).current | Should be 0
            (Get-ProgressEx -id 1).current | Should be 0

            Write-ProgressEx -id 1 -increment
            (Get-ProgressEx -id 0).current | Should be 0
            (Get-ProgressEx -id 1).current | Should be 1

            Write-ProgressEx -id 0 -increment
            (Get-ProgressEx -id 0).current | Should be 1
            (Get-ProgressEx -id 1) | Should be $null

            Write-ProgressEx
            (Get-ProgressEx -id 0) | Should be $null
        }

        It "without parameters complete all children progresses" {
            Write-ProgressEx "outerLevel" -Total $outerLevel.Count
            Write-ProgressEx "innerLevel" -Total $innerLevel.Count -id 1

            (Get-ProgressEx -id 0).Total | Should be $outerLevel.Count
            (Get-ProgressEx -id 1).Total | Should be $innerLevel.Count

            Write-ProgressEx
            (Get-ProgressEx) | Should be $null
            (Get-ProgressEx -id 1) | Should be $null
        }
    }
}

Describe "Get-ProgressEx" {

    Context "work" {
        It "get" {
            { Get-ProgressEx } | Should not throw
        }

        It "get -force" {
            { Get-ProgressEx -force } | Should not throw
        }
    }

    Context "functional" {
        It "name parameter" {
            $pInfo = Get-ProgressEx -id 1
            $pInfo | Should be $null
        }

        It "name parameter -force" {
            $pInfo = Get-ProgressEx -id 1 -force
            $pInfo.Id | Should be 1
        }

        It "position parameter" {
            $pInfo = Get-ProgressEx 1
            $pInfo | Should be $null
        }

        It "position parameter -force" {
            $pInfo = Get-ProgressEx 1 -force
            $pInfo.Id | Should be 1
        }

        It "pipe parameter" {
            $pInfo = 1 | Get-ProgressEx
            $pInfo | Should be $null
        }

        It "pipe parameter -force" {
            $pInfo = 1 | Get-ProgressEx -force
            $pInfo.Id | Should be 1
        }

        It "pipe array parameter" {
            $pInfo = 1, 30, 40 | Get-ProgressEx
            $pInfo.Count | Should be 3
            $pInfo[0] | Should be $null
            $pInfo[1] | Should be $null
            $pInfo[2] | Should be $null
        }

        It "pipe array parameter -force" {
            $pInfo = 1, 30, 40 | Get-ProgressEx -force
            $pInfo.Count | Should be 3
            $pInfo[0].Id | Should be 1
            $pInfo[1].Id | Should be 30
            $pInfo[2].Id | Should be 40
        }
    }
}

Describe "Set-ProgressEx" {

    Context "work" {
        It "work" {
            { Set-ProgressEx } | Should not throw
        }

        It "double trouble work" {
            { Set-ProgressEx; Set-ProgressEx } | Should not throw
        }
    }
}

<#
#remove-module 'Write-ProgressEx'
 
Describe "Write-ProgressExMesage" {
 
    InModuleScope {
        Context "work" {
            $Message = {"Test"}
            $pInfo = @{
                id = 1
                ShowMessageOnBegin = $true
                ShowMessageOnNewActivity = $true
                ShowMessageOnNewStatus = $true
                ShowMessageOnEnd = $true
                MessageOnBegin = {"$($pInfo.id):onBegin"}
                MessageOnNewActivity = {"$($pInfo.id):onNewActivity"}
                MessageOnNewStatus = {"$($pInfo.id):onNewStatus"}
                MessageOnEnd = {"$($pInfo.id):onEnd"}
            }
            $Expected = @{
                Message = Invoke-Command $Message
                MessageOnBegin = Invoke-Command $pInfo.MessageOnBegin
                MessageOnNewActivity = Invoke-Command $pInfo.MessageOnNewActivity
                MessageOnNewStatus = Invoke-Command $pInfo.MessageOnNewStatus
                MessageOnEnd = Invoke-Command $pInfo.MessageOnEnd
            }
 
            It "message" {
                Write-ProgressExMessage $pInfo -Message $Message | Should be $Expected.Message
            }
 
            It "onBegin" {
                Write-ProgressExMessage $pInfo -ShowMessagesOnBegin | Should be $Expected.MessageOnBegin
            }
 
            It "onNewActivity" {
                Write-ProgressExMessage $pInfo -ShowMessagesOnNewActivity | Should be $Expected.MessageOnNewActivity
            }
 
            It "onNewStatus" {
                Write-ProgressExMessage $pInfo -ShowMessagesOnNewStatus | Should be $Expected.MessageOnNewStatus
            }
 
            It "onEnd" {
                Write-ProgressExMessage $pInfo -ShowMessagesOnEnd | Should be $Expected.MessageOnEnd
            }
        }
    }
}
#>


<#
Describe "Write-ProgressExStd" {
 
    Context "work" {
        It "not exported" {
            { get-command Write-ProgressExStd -Module Write-ProgressEx } | Should throw
        }
 
        InModuleScope Write-ProgressEx {
            It "by pInfo" {
                $pInfo = Get-ProgressEx -id 0
                { Write-ProgressExStd -pInfo $pInfo } | Should not throw
            }
        }
    }
}
#>