-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathuse-continue-not-break.ps1
59 lines (47 loc) · 1.41 KB
/
use-continue-not-break.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
<#
.Synopsis
'continue', not 'break', should be used for skipping next cases.
.Description
The script contains three similar 'switch' tests. The idea is to process 1
and 2 and ignore other values. Tests use script blocks as switch conditions
in order to track their invocations.
#>
# results to be shown and tested by .test.ps1
$results = New-Object System.Collections.Specialized.OrderedDictionary
# adds a message to the current $log
function Write-Log($message) {
$null = $log.Add($message)
}
# gets true if $value is equal to $test and log the call
function Test-Condition($value, $test) {
Write-Log "Testing $value with $test"
$value -eq $test
}
### Test1 - correct but not effective
$log = [System.Collections.ArrayList]@()
$out = switch(1..3) {
{Test-Condition $_ 1} {'this is 1'}
{Test-Condition $_ 2} {'this is 2'}
}
$results.log1 = $log
$results.out1 = $out
### Test 2 - incorrect
$log = [System.Collections.ArrayList]@()
$out = switch(1..3) {
{Test-Condition $_ 1} {'this is 1'; break}
{Test-Condition $_ 2} {'this is 2'}
}
$results.log2 = $log
$results.out2 = $out
### Test 3 - correct and effective
$log = [System.Collections.ArrayList]@()
$out = switch(1..3) {
{Test-Condition $_ 1} {'this is 1'; continue}
{Test-Condition $_ 2} {'this is 2'}
}
$results.log3 = $log
$results.out3 = $out
# show results, points of interest:
# - log1 shows redundant 'Testing 1 with 2'
# - out2 shows incorrect result
$results