Skip to content

Commit

Permalink
Fix input collecting
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd committed Apr 7, 2024
1 parent 50182d9 commit 333417c
Show file tree
Hide file tree
Showing 25 changed files with 142 additions and 69 deletions.
8 changes: 4 additions & 4 deletions src/functions/assert/Boolean/Assert-False.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
function Assert-False {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')]
param (
[Parameter(ValueFromPipeline=$true)]
[Parameter(ValueFromPipeline = $true)]
$Actual,
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
if ($Actual)
{
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
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)
}
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/Boolean/Assert-True.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
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)
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/Collection/Assert-All.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@


$Expected = $FilterScript
$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
# 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/
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/Collection/Assert-Any.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
)

$Expected = $FilterScript
$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
if (-not ($Actual | & $SafeCommands['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.`.
$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)
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/Collection/Assert-Contain.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
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.
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/Collection/Assert-NotContain.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
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.
Expand Down
25 changes: 15 additions & 10 deletions src/functions/assert/Common/Collect-Input.ps1
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
function Collect-Input ($ParameterInput, $PipelineInput, $IsInPipeline) {
if ($IsInPipeline) {
# We are called like this: 1 | Assert-Equal -Expected 1, we will get $local:Input in $PipelineInput and $true in $IsInPipeline (coming from $MyInvocation.ExpectingInput).
function Collect-Input ($ParameterInput, $PipelineInput, $IsPipelineInput) {
if ($IsPipelineInput) {
# We are called like this: 1 | Assert-Equal -Expected 1, we will get $local:Input in $PipelineInput and $true in $IsPipelineInput (coming from $MyInvocation.ExpectingInput).

if ($PipelineInput.Count -eq 0) {
# When calling @() | Assert-Equal -Expected 1, the engine will special case it, and we will get empty array $local:Input, fix that
# by returning empty array wrapped in array.
, @()
# When calling @() | Assert-Equal -Expected 1, the engine will special case it, and we will get empty array in $local:Input
$collectedInput = @()
}
else {
# This is array of all the input, when we output it, the function will unwrap it. So we get the raw input on the output.
$PipelineInput
# This is array of all the input, unwrap it.
$collectedInput = foreach ($item in $PipelineInput) { $item }
}
}
else {
# This is exactly what was provided to the ActualParmeter, wrap it in array so the function return can unwrap it.
, $ParameterInput
# This is exactly what was provided to the ActualParmeter.
$collectedInput = $ParameterInput
}

@{
Actual = $collectedInput
# We can use this to determine if collections are comparable. Pipeline input will unwind the collection, so pipeline input collection type is not comparable.
IsPipelineInput = $IsPipelineInput
}
}
3 changes: 2 additions & 1 deletion src/functions/assert/Exception/Assert-Throw.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ function Assert-Throw {
[String]$CustomMessage
)

$ScriptBlock = Collect-Input -ParameterInput $ScriptBlock -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $ScriptBlock -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$ScriptBlock = $collectedInput.Actual

$errorThrown = $false
$err = $null
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/General/Assert-Equal.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual

if ((Ensure-ExpectedIsNotCollection $Expected) -ne $Actual) {
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected <expectedType> '<expected>', but got <actualType> '<actual>'."
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/General/Assert-GreaterThan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
if ((Ensure-ExpectedIsNotCollection $Expected) -ge $Actual)
{
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected <actualType> '<actual>' to be greater than <expectedType> '<expected>', but it was not."
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/General/Assert-GreaterThanOrEqual.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
if ((Ensure-ExpectedIsNotCollection $Expected) -gt $Actual)
{
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected <actualType> '<actual>' to be greater than or equal to <expectedType> '<expected>', but it was not."
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/General/Assert-LessThan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
if ((Ensure-ExpectedIsNotCollection $Expected) -le $Actual)
{
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected <actualType> '<actual>' to be less than <expectedType> '<expected>', but it was not."
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/General/Assert-LessThanOrEqual.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
if ((Ensure-ExpectedIsNotCollection $Expected) -lt $Actual)
{
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected <actualType> '<actual>' to be less than or equal to <expectedType> '<expected>', but it was not."
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/General/Assert-NotEqual.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
if ((Ensure-ExpectedIsNotCollection $Expected) -eq $Actual)
{
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected <expectedType> '<expected>', to be different than the actual value, but they were the same."
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/General/Assert-NotNull.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
if ($null -eq $Actual)
{
$Message = Get-AssertionMessage -Expected $null -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected not `$null, but got `$null."
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/General/Assert-NotSame.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
if ([object]::ReferenceEquals($Expected, $Actual)) {
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected <expectedType> '<expected>', to not be the same instance."
throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/General/Assert-NotType.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
if ($Actual -is $Expected) {
$type = [string]$Expected
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected value to be of different type than '$type', but got '<actual>' of type '<actualType>'."
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/General/Assert-Null.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
if ($null -ne $Actual) {
$Message = Get-AssertionMessage -Expected $null -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected `$null, but got <actualType> '<actual>'."
throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/General/Assert-Same.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
throw [ArgumentException]"Assert-Same compares objects by reference. You provided a value type or a string, those are not reference types and you most likely don't need to compare them by reference, see https://github.com/nohwnd/Assert/issues/6.`n`nAre you trying to compare two values to see if they are equal? Use Assert-Equal instead."
}

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
if (-not ([object]::ReferenceEquals($Expected, $Actual))) {
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected <expectedType> '<expected>', to be the same instance but it was not."
throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/General/Assert-Type.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual
if ($Actual -isnot $Expected) {
$type = [string]$Expected
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected value to be of type '$type', but got '<actual>' of type '<actualType>'."
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/String/Assert-Like.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function Assert-Like {
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual

if ($Actual -isnot [string]) {
throw [ArgumentException]"Actual is expected to be string, to avoid confusing behavior that -like operator exhibits with collections. To assert on collections use Assert-Any, Assert-All or some other collection assertion."
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/String/Assert-NotLike.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function Assert-NotLike {
[String]$CustomMessage
)

$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual

if ($Actual -isnot [string]) {
throw [ArgumentException]"Actual is expected to be string, to avoid confusing behavior that -like operator exhibits with collections. To assert on collections use Assert-Any, Assert-All or some other collection assertion."
Expand Down
9 changes: 5 additions & 4 deletions src/functions/assert/String/Assert-StringEqual.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ function Assert-StringEqual {
[switch]$IgnoreWhitespace
)

$_actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput
$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput
$Actual = $collectedInput.Actual

if (-not $CustomMessage) {
$formattedMessage = Get-StringEqualDefaultFailureMessage -Expected $Expected -Actual $_actual
$formattedMessage = Get-StringEqualDefaultFailureMessage -Expected $Expected -Actual $Actual
}
else {
$formattedMessage = Get-CustomFailureMessage -Expected $Expected -Actual $_actual -CustomMessage $CustomMessage
$formattedMessage = Get-CustomFailureMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage
}

$stringsAreEqual = Test-StringEqual -Expected $Expected -Actual $_actual -CaseSensitive:$CaseSensitive -IgnoreWhitespace:$IgnoreWhiteSpace
$stringsAreEqual = Test-StringEqual -Expected $Expected -Actual $Actual -CaseSensitive:$CaseSensitive -IgnoreWhitespace:$IgnoreWhiteSpace
if (-not ($stringsAreEqual)) {
throw [Pester.Factory]::CreateShouldErrorRecord($formattedMessage, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
}
Expand Down
Loading

0 comments on commit 333417c

Please sign in to comment.