Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Assert assertions to Pester #2428

Merged
merged 63 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
567549c
Copy paste assert
nohwnd Apr 1, 2024
28761e6
Before adding format nicely2
nohwnd Apr 1, 2024
079d374
Few more tests
nohwnd Apr 1, 2024
cfaa965
Add code, did not fix help yet
nohwnd Apr 2, 2024
911a465
Fix $error
nohwnd Apr 6, 2024
0bf4752
Fix warnings
nohwnd Apr 6, 2024
b54b003
Fix collecting from pipeline
nohwnd Apr 6, 2024
6575ca4
Merge branch 'main' into add-assert
nohwnd Apr 7, 2024
50182d9
wip
nohwnd Apr 7, 2024
333417c
Fix input collecting
nohwnd Apr 7, 2024
0230d8c
Remove Compatibility.ps1
nohwnd Apr 7, 2024
3d1d896
Add Should-* aliases
nohwnd Apr 7, 2024
b0ffc38
Rename internal Should-* functions to Should-*Assertion to avoid conf…
nohwnd Apr 7, 2024
d60f535
Fix raw get-variable
nohwnd Apr 12, 2024
1815b55
Merge branch 'dev/6.x.x' into add-assert
nohwnd Apr 12, 2024
36f36c9
No SDK pre-release
nohwnd Apr 12, 2024
9829ae1
Fixes
nohwnd Apr 19, 2024
9143030
Add Should-BeEmptyString and fix formatting
nohwnd Apr 19, 2024
6e79b45
Build on dev/*
nohwnd Apr 20, 2024
e5a4446
Add not be null or empty, and not be null or whitespace string assert…
nohwnd Apr 20, 2024
0560d77
formatting almost fixed
nohwnd Apr 20, 2024
5b1ebd4
Change formats, fix all tests
nohwnd Apr 21, 2024
5b54c22
Update azure-pipelines.yml
nohwnd Apr 21, 2024
60c3a12
Add faster and before date time
nohwnd Apr 21, 2024
67e886d
Merge branch 'add-assert' of https://github.com/pester/Pester into ad…
nohwnd Apr 21, 2024
9e43582
Faster, slower, before, after
nohwnd Apr 23, 2024
0e5c073
before, after, faster, slower
nohwnd Apr 23, 2024
1565699
Strict mode
nohwnd Apr 23, 2024
d80c7ef
Fix running tests
nohwnd Apr 23, 2024
caaffae
all and any
nohwnd Apr 23, 2024
cc03bf4
all and any
nohwnd Apr 23, 2024
bdc7fcf
Why did pass thru failing now? :/
nohwnd Apr 23, 2024
28ae51f
Rename assertion files
nohwnd Apr 25, 2024
c70bdd2
rename some files
nohwnd Apr 25, 2024
6dd68d6
Continue on renaming and using Should-* in tests
nohwnd Apr 26, 2024
432e02d
Rename assertions and tests, and use assertion functions so we are su…
nohwnd Apr 26, 2024
b5f8da4
Fix tests
nohwnd May 1, 2024
2f262f3
Change messages,tests are broken
nohwnd May 1, 2024
13307ca
Fix messages
nohwnd May 3, 2024
ab36078
Fix more messages
nohwnd May 3, 2024
a1f283c
Little bit of help
nohwnd May 3, 2024
8509a5c
Add help
nohwnd May 7, 2024
4c8bae2
info
nohwnd May 7, 2024
a6affe3
unroll
nohwnd May 7, 2024
3c74309
Add Help
nohwnd May 14, 2024
46f46ac
Add help and put back help tests
nohwnd May 14, 2024
81ac71d
Merge branch 'main' into add-assert
nohwnd May 18, 2024
90a887d
assert
nohwnd May 18, 2024
7a971c5
Update src/functions/assert/General/Should-NotHaveType.ps1
nohwnd May 20, 2024
a18e746
Update src/functions/assert/General/Should-HaveType.ps1
nohwnd May 20, 2024
1b3f0b5
Update src/functions/assert/Equivalence/Should-BeEquivalent.ps1
nohwnd May 20, 2024
29f5cb8
Revert test, fix safe commands etc.
nohwnd May 20, 2024
7931820
Merge branch 'add-assert' of https://github.com/pester/Pester into ad…
nohwnd May 20, 2024
03e28d0
Revert launch.json
nohwnd May 20, 2024
bf19cb3
Remove TimeSpanOrStringWithTimeUnit.cs
nohwnd May 20, 2024
f1a3810
Apply suggestions from code review
nohwnd May 20, 2024
40595dc
Merge branch 'add-assert' of https://github.com/pester/Pester into ad…
nohwnd May 20, 2024
4e67f2f
Apply suggestions from code review
nohwnd May 20, 2024
0e17209
Fixes
nohwnd May 20, 2024
548e373
Merge branch 'add-assert' of https://github.com/pester/Pester into ad…
nohwnd May 20, 2024
162fab7
Get-equivalencyoption help
nohwnd May 20, 2024
6fa12ce
Get-equivalencyoption help
nohwnd May 20, 2024
c7886fb
Merge branch 'main' into add-assert
nohwnd May 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,14 @@ $script = @(
"$PSScriptRoot/src/Pester.Runtime.ps1"
"$PSScriptRoot/src/TypeClass.ps1"
"$PSScriptRoot/src/Format.ps1"
# TODO: the original version of Format2 from assert, because it does not format strings and other stuff in Pester specific way. I've used this regex (Format-Collection|Format-Object|Format-Null|Format-Boolean|Format-ScriptBlock|Format-Number|Format-Hashtable|Format-Dictionary|Format-Nicely|Get-DisplayProperty|Get-ShortType|Format-Type), '$12' to replace in VS Code.
"$PSScriptRoot/src/Format2.ps1"
"$PSScriptRoot/src/Pester.RSpec.ps1"
"$PSScriptRoot/src/Main.ps1"

# TODO: Imports Compatibility.ps1, remove the code that is there, because it is for PowerShell 2.0 compatibility!
"$PSScriptRoot/src/functions/assert/*"
"$PSScriptRoot/src/functions/assert/*/*"
"$PSScriptRoot/src/functions/assertions/*"
"$PSScriptRoot/src/functions/*"

Expand Down
156 changes: 156 additions & 0 deletions src/Format2.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
function Format-Collection2 ($Value, [switch]$Pretty) {
$separator = ', '
if ($Pretty) {
$separator = ",`n"
}
($Value | ForEach-Object { Format-Nicely2 -Value $_ -Pretty:$Pretty }) -join $separator

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.

Check warning

Code scanning / PSScriptAnalyzer

Unsafe call to 'ForEach-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1. Warning

Unsafe call to 'ForEach-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1.
}

function Format-Object2 ($Value, $Property, [switch]$Pretty) {
if ($null -eq $Property) {
$Property = $Value.PSObject.Properties | Select-Object -ExpandProperty Name

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.

Check warning

Code scanning / PSScriptAnalyzer

Unsafe call to 'Select-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1. Warning

Unsafe call to 'Select-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1.
}
$orderedProperty = $Property |
Sort-Object |

Check warning

Code scanning / PSScriptAnalyzer

Unsafe call to 'Sort-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1. Warning

Unsafe call to 'Sort-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1.
# force the values to be strings for powershell v2
ForEach-Object { "$_" }

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.

Check warning

Code scanning / PSScriptAnalyzer

Unsafe call to 'ForEach-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1. Warning

Unsafe call to 'ForEach-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1.

$valueType = Get-ShortType $Value
$valueFormatted = [string]([PSObject]$Value | Select-Object -Property $orderedProperty)

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.

Check warning

Code scanning / PSScriptAnalyzer

Unsafe call to 'Select-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1. Warning

Unsafe call to 'Select-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1.

if ($Pretty) {
$margin = " "
$valueFormatted = $valueFormatted `
-replace '^@{', "@{`n$margin" `
-replace '; ', ";`n$margin" `
-replace '}$', "`n}" `

}

$valueFormatted -replace "^@", $valueType
}

function Format-Null2 {
'$null'
}

function Format-Boolean2 ($Value) {
'$' + $Value.ToString().ToLower()
}

function Format-ScriptBlock2 ($Value) {
'{' + $Value + '}'
}

function Format-Number2 ($Value) {
[string]$Value
}

function Format-Hashtable2 ($Value) {
$head = '@{'
$tail = '}'

$entries = $Value.Keys | Sort-Object | ForEach-Object {

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.

Check warning

Code scanning / PSScriptAnalyzer

Unsafe call to 'ForEach-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1. Warning

Unsafe call to 'ForEach-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1.

Check warning

Code scanning / PSScriptAnalyzer

Unsafe call to 'Sort-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1. Warning

Unsafe call to 'Sort-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1.
$formattedValue = Format-Nicely2 $Value.$_
"$_=$formattedValue" }

$head + ( $entries -join '; ') + $tail
}

function Format-Dictionary2 ($Value) {
$head = 'Dictionary{'
$tail = '}'

$entries = $Value.Keys | Sort-Object | ForEach-Object {

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.

Check warning

Code scanning / PSScriptAnalyzer

Unsafe call to 'ForEach-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1. Warning

Unsafe call to 'ForEach-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1.

Check warning

Code scanning / PSScriptAnalyzer

Unsafe call to 'Sort-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1. Warning

Unsafe call to 'Sort-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1.
$formattedValue = Format-Nicely2 $Value.$_
"$_=$formattedValue" }

$head + ( $entries -join '; ') + $tail
}

function Format-Nicely2 ($Value, [switch]$Pretty) {
if ($null -eq $Value) {
return Format-Null2 -Value $Value
}

if ($Value -is [bool]) {
return Format-Boolean2 -Value $Value
}

if ($value -is [type]) {
return Format-Type2 -Value $Value
}

if (Is-DecimalNumber -Value $Value) {
return Format-Number2 -Value $Value
}

if (Is-ScriptBlock -Value $Value) {
return Format-ScriptBlock2 -Value $Value
}

if (Is-Value -Value $Value) {
return $Value
}

if (Is-Hashtable -Value $Value) {
return Format-Hashtable2 -Value $Value
}

if (Is-Dictionary -Value $Value) {
return Format-Dictionary2 -Value $Value
}

if (Is-Collection -Value $Value) {
return Format-Collection2 -Value $Value -Pretty:$Pretty
}

Format-Object2 -Value $Value -Property (Get-DisplayProperty2 (Get-Type $Value)) -Pretty:$Pretty
}

function Get-DisplayProperty2 ([Type]$Type) {
# rename to Get-DisplayProperty?

<# some objects are simply too big to show all of their properties,
so we can create a list of properties to show from an object
maybe the default info from Get-FormatData could be utilized here somehow
so we show only stuff that would normally show in format-table view
leveraging the work PS team already did #>
nohwnd marked this conversation as resolved.
Show resolved Hide resolved

# this will become more advanced, basically something along the lines of:
# foreach type, try constructing the type, and if it exists then check if the
# incoming type is assignable to the current type, if so then return the properties,
# this way I can specify the map from the most concrete type to the least concrete type
# and for types that do not exist

$propertyMap = @{
'System.Diagnostics.Process' = 'Id', 'Name'
}

$propertyMap[$Type.FullName]
}

function Get-ShortType2 ($Value) {
if ($null -ne $value) {
Format-Type2 (Get-Type $Value)
}
else {
Format-Type2 $null
}
}

function Format-Type2 ([Type]$Value) {
if ($null -eq $Value) {
return '<null>'
}

$type = [string]$Value

$type `
-replace "^System\." `
-replace "^Management\.Automation\.PSCustomObject$", "PSObject" `
-replace "^PSCustomObject$", "PSObject" `
-replace "^Object\[\]$", "collection" `

}

26 changes: 26 additions & 0 deletions src/Module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ $script:SafeCommands['Set-DynamicParameterVariable'] = $ExecutionContext.Session
'New-PesterContainer'
'New-PesterConfiguration'

# assert
'Assert-False'
'Assert-True'
'Assert-All'
'Assert-Any'
'Assert-Contain'
'Assert-NotContain'
'Assert-Equivalent'
'Assert-Throw'
'Assert-Equal'
'Assert-GreaterThan'
'Assert-GreaterThanOrEqual'
'Assert-LessThan'
'Assert-LessThanOrEqual'
'Assert-NotEqual'
'Assert-NotNull'
'Assert-NotSame'
'Assert-NotType'
'Assert-Null'
'Assert-Same'
'Assert-Type'
'Assert-Like'
'Assert-NotLike'
'Assert-StringEqual'
'Assert-StringNotEqual'

# export
'Export-NUnitReport'
'ConvertTo-NUnitReport'
Expand Down
27 changes: 27 additions & 0 deletions src/Pester.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,33 @@
'New-PesterContainer'
'New-PesterConfiguration'

# assert
'Assert-False'
'Assert-True'
'Assert-All'
'Assert-Any'
'Assert-Contain'
'Assert-NotContain'
'Assert-Equivalent'
'Assert-Throw'
'Assert-Equal'
'Assert-GreaterThan'
'Assert-GreaterThanOrEqual'
'Assert-LessThan'
'Assert-LessThanOrEqual'
'Assert-NotEqual'
'Assert-NotNull'
'Assert-NotSame'
'Assert-NotType'
'Assert-Null'
'Assert-Same'
'Assert-Type'
'Assert-Like'
'Assert-NotLike'
'Assert-StringEqual'
'Assert-StringNotEqual'


# legacy
'Assert-VerifiableMock'
'Assert-MockCalled'
Expand Down
8 changes: 8 additions & 0 deletions src/TypeClass.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ function Is-Object ($Value) {

-not ($null -eq $Value -or (Is-Value -Value $Value) -or (Is-Collection -Value $Value))
}

function Is-DataRow ($Value) {
$Value -is [Data.DataRow] -or $Value.Psobject.TypeNames[0] -like '*System.Data.DataRow'
}

function Is-DataTable ($Value) {
$Value -is [Data.DataTable] -or $Value.Psobject.TypeNames[0] -like '*System.Data.DataTable'
}
16 changes: 16 additions & 0 deletions src/functions/assert/Boolean/Assert-False.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function Assert-False {
param (
[Parameter(ValueFromPipeline=$true)]
$Actual,

Check warning

Code scanning / PSScriptAnalyzer

Command accepts pipeline input but has not defined a process block. Warning

Command accepts pipeline input but has not defined a process block.
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input
if ($Actual)
{
$Message = Get-AssertionMessage -Expected $false -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected <actualType> '<actual>' to be <expectedType> '<expected>' or falsy value 0, """", `$null, @()."
throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
}

$Actual
}
16 changes: 16 additions & 0 deletions src/functions/assert/Boolean/Assert-True.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function Assert-True{
param (
[Parameter(ValueFromPipeline=$true)]
$Actual,

Check warning

Code scanning / PSScriptAnalyzer

Command accepts pipeline input but has not defined a process block. Warning

Command accepts pipeline input but has not defined a process block.
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input
if (-not $Actual)
{
$Message = Get-AssertionMessage -Expected $true -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected <actualType> '<actual>' to be <expectedType> '<expected>' or truthy value."
throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
}

$Actual
}
40 changes: 40 additions & 0 deletions src/functions/assert/Collection/Assert-All.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function Assert-All {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true, Position = 1)]
$Actual,

Check warning

Code scanning / PSScriptAnalyzer

Command accepts pipeline input but has not defined a process block. Warning

Command accepts pipeline input but has not defined a process block.
[Parameter(Position = 0, Mandatory = $true)]
[scriptblock]$FilterScript,
[String]$CustomMessage
)


$Expected = $FilterScript
$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input
# we are jumping between modules so I need to explicitly pass the _ variable
# simply using '&' won't work
# see: https://blogs.msdn.microsoft.com/sergey_babkins_blog/2014/10/30/calling-the-script-blocks-in-powershell/
$actualFiltered = $Actual | ForEach-Object {

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.

Check warning

Code scanning / PSScriptAnalyzer

Unsafe call to 'Get-Variable' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1. Warning

Unsafe call to 'Get-Variable' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1.
# powershell v4 code where we have InvokeWithContext available
# $underscore = Get-Variable _
# $pass = $FilterScript.InvokeWithContext($null, $underscore, $null)

# polyfill for PowerShell v2
$PSCmdlet.SessionState.PSVariable.Set("_", $_)
$pass = & $FilterScript


if (-not $pass) { $_ }
}

if ($actualFiltered) {
$data = @{
actualFiltered = $actualFiltered
actualFilteredCount = @($actualFiltered).Count
}
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -Data $data -CustomMessage $CustomMessage -DefaultMessage "Expected all items in collection '<actual>' to pass filter '<expected>', but <actualFilteredCount> of them '<actualFiltered>' did not pass the filter."
throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
}

$Actual
}
19 changes: 19 additions & 0 deletions src/functions/assert/Collection/Assert-Any.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function Assert-Any {
param (
[Parameter(ValueFromPipeline=$true, Position=1)]
$Actual,

Check warning

Code scanning / PSScriptAnalyzer

Command accepts pipeline input but has not defined a process block. Warning

Command accepts pipeline input but has not defined a process block.
[Parameter(Position=0, Mandatory=$true)]
[scriptblock]$FilterScript,
[String]$CustomMessage
)

$Expected = $FilterScript
$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input
if (-not ($Actual | Where-Object -FilterScript $FilterScript))

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.

Check warning

Code scanning / PSScriptAnalyzer

Unsafe call to 'Where-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1. Warning

Unsafe call to 'Where-Object' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1.
{
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected at least one item in collection '<actual>' to pass filter '<expected>', but none of the items passed the filter."
throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
}

$Actual
}
19 changes: 19 additions & 0 deletions src/functions/assert/Collection/Assert-Contain.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function Assert-Contain {
param (
[Parameter(Position=1, ValueFromPipeline=$true)]
$Actual,

Check warning

Code scanning / PSScriptAnalyzer

Command accepts pipeline input but has not defined a process block. Warning

Command accepts pipeline input but has not defined a process block.
[Parameter(Position=0)]
$Expected,
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input
if ($Actual -notcontains $Expected)
{
$type = [string]$Expected

Check warning

Code scanning / PSScriptAnalyzer

The variable 'type' is assigned but never used. Warning

The variable 'type' is assigned but never used.
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected <expectedType> '<expected>' to be present in collection '<actual>', but it was not there."
throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
}

$Actual
}
19 changes: 19 additions & 0 deletions src/functions/assert/Collection/Assert-NotContain.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function Assert-NotContain {
param (
[Parameter(Position=1, ValueFromPipeline=$true)]
$Actual,

Check warning

Code scanning / PSScriptAnalyzer

Command accepts pipeline input but has not defined a process block. Warning

Command accepts pipeline input but has not defined a process block.
[Parameter(Position=0)]
$Expected,
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input
if ($Actual -contains $Expected)
{
$type = [string]$Expected

Check warning

Code scanning / PSScriptAnalyzer

The variable 'type' is assigned but never used. Warning

The variable 'type' is assigned but never used.
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected <expectedType> '<expected>' to not be present in collection '<actual>', but it was there."
throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
}

$Actual
}
Loading
Loading