Commit 7d1a4ee 1 parent 95a1a41 commit 7d1a4ee Copy full SHA for 7d1a4ee
File tree 3 files changed +26
-5
lines changed
src/functions/assert/Collection
tst/functions/assert/Collection
3 files changed +26
-5
lines changed Original file line number Diff line number Diff line change 76
76
$appendMore = $true
77
77
}
78
78
79
- $pass = $false
79
+ $pass = @ ($false )
80
+ }
81
+
82
+ # The API returns a collection and user can return anything from their script
83
+ # or there can be no output when assertion is used, so we are checking if the first item
84
+ # in the output is a boolean $false. The scriptblock should not fail in $null for example,
85
+ # hence the explicit type check
86
+ if (($pass.Count -ge 1 ) -and ($pass [0 ] -is [bool ]) -and ($false -eq $pass [0 ])) {
87
+ $item
80
88
}
81
- if (-not $pass ) { $item }
82
89
}
83
90
84
91
# Make sure are checking the count of the filtered items, not just truthiness of a single item.
Original file line number Diff line number Diff line change 55
55
56
56
$failReasons = $null
57
57
$appendMore = $false
58
- $pass = $false
59
58
foreach ($item in $Actual ) {
60
59
$underscore = [PSVariable ]::new(' _' , $item )
61
60
try {
72
71
$appendMore = $true
73
72
}
74
73
75
- $pass = $false
74
+ # InvokeWithContext returns collection. This makes it easier to check the value if we throw and don't assign the value.
75
+ $pass = @ ($false )
76
+ }
77
+
78
+ # The API returns a collection and user can return anything from their script
79
+ # or there can be no output when assertion is used, so we are checking if the first item
80
+ # in the output is a boolean $false. The scriptblock should not fail in $null for example,
81
+ # hence the explicit type check
82
+ if (-not (($pass.Count -ge 1 ) -and ($pass [0 ] -is [bool ]) -and ($false -eq $pass [0 ]))) {
83
+ $pass = $true
84
+ break
76
85
}
77
- if ($pass ) { break }
78
86
}
79
87
80
88
if (-not $pass ) {
Original file line number Diff line number Diff line change @@ -9,6 +9,12 @@ Describe "Should-Any" {
9
9
$Actual | Should- Any - FilterScript { $_ -eq 1 }
10
10
}
11
11
12
+ It " Passes when at least one item in the given collection passes the predicate with assertion" - TestCases @ (
13
+ @ { Actual = @ (1 , 2 , 3 ) }
14
+ ) {
15
+ $Actual | Should- Any - FilterScript { $_ | Should- Be 1 }
16
+ }
17
+
12
18
It " Fails when none of the items passes the predicate" - TestCases @ (
13
19
@ { Actual = @ (1 , 2 , 3 ) }
14
20
@ { Actual = @ (1 ) }
You can’t perform that action at this time.
0 commit comments