From c44d3441e14efb1d731dd523ba656080f8c18914 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Thu, 22 Aug 2024 20:50:54 +0200 Subject: [PATCH 1/2] chore: Update Assert-BlockString to allow passing string array and single string to Expected parameter --- CHANGELOG.md | 2 + source/Public/Assert-BlockString.ps1 | 17 +++-- .../Unit/Public/Assert-BlockString.tests.ps1 | 64 +++++++++++++++++++ 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1661d91..568dfdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,3 +13,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Updated README.md +- `Assert-BlockString` + - Allow it to pass string array and single string to `Expected` parameter. diff --git a/source/Public/Assert-BlockString.ps1 b/source/Public/Assert-BlockString.ps1 index 167e003..1a7c118 100644 --- a/source/Public/Assert-BlockString.ps1 +++ b/source/Public/Assert-BlockString.ps1 @@ -57,7 +57,6 @@ function Assert-BlockString $Actual, [Parameter(Position = 0, Mandatory = $true)] - [System.String[]] $Expected, [Parameter()] @@ -77,19 +76,25 @@ function Assert-BlockString } # Verify if $Actual is a string or string array - $isStringType = $Actual -is [System.String] -or ($Actual -is [System.Array] -and $Actual[0] -is [System.String]) + $isActualStringType = $Actual -is [System.String] -or ($Actual -is [System.Array] -and ($Actual.Count -eq 0 -or ($Actual.Count -gt 0 -and $Actual[0] -is [System.String]))) - $stringsAreEqual = $isStringType -and (-join $Actual) -ceq (-join $Expected) + $isExpectedStringType = $Expected -is [System.String] -or ($Expected -is [System.Array] -and ($Expected.Count -eq 0 -or ($Expected.Count -gt 0 -and $Expected[0] -is [System.String]))) + + $stringsAreEqual = $isActualStringType -and $isExpectedStringType -and (-join $Actual) -ceq (-join $Expected) if (-not $stringsAreEqual) { - if (-not $isStringType) + if (-not $isActualStringType) + { + $message = 'The Actual value must be of type string or string[], but it was not.' + } + elseif (-not $isExpectedStringType) { - $message = 'Expected to actual value to be of type string or string[], but it was not.' + $message = 'The Expected value must be of type string or string[], but it was not.' } else { - $message = 'Expect the strings to be equal' + $message = 'Expected the strings to be equal' if ($Because) { diff --git a/tests/Unit/Public/Assert-BlockString.tests.ps1 b/tests/Unit/Public/Assert-BlockString.tests.ps1 index bbcfe2b..0b2a5a4 100644 --- a/tests/Unit/Public/Assert-BlockString.tests.ps1 +++ b/tests/Unit/Public/Assert-BlockString.tests.ps1 @@ -91,6 +91,46 @@ Describe 'Assert-BlockString' { { & $scriptBlock } | Should -Not -Throw } + It 'Should be able to pass empty collection as Expected' { + $mockExpected = 'Test string' + + $scriptBlock = { + '' | Assert-BlockString -Expected @() + } + + { & $scriptBlock } | Should -Not -Throw + } + + It 'Should be able to pass empty string as Expected' { + $mockExpected = 'Test string' + + $scriptBlock = { + '' | Assert-BlockString -Expected '' + } + + { & $scriptBlock } | Should -Not -Throw + } + + It 'Should be able to pass empty collection as Actual' { + $mockExpected = 'Test string' + + $scriptBlock = { + Assert-BlockString -Actual @() -Expected @() + } + + { & $scriptBlock } | Should -Not -Throw + } + + It 'Should be able to pass empty string as Actual' { + $mockExpected = 'Test string' + + $scriptBlock = { + Assert-BlockString -Actual '' -Expected '' + } + + { & $scriptBlock } | Should -Not -Throw + } + It 'Should be able to be called using its alias' { $mockExpected = 'Test string' @@ -100,4 +140,28 @@ Describe 'Assert-BlockString' { { & $scriptBlock } | Should -Not -Throw } + + It 'Should throw the correct error message when Actual is not a string' { + $mockExpected = 'Test string' + + $scriptBlock = { + ([Int32] -1) | Should-BeBlockString -Expected $mockExpected + } + + { & $scriptBlock } | Should -Throw -ExpectedMessage 'The Actual value must be of type string or string`[`], but it was not.' + } + + It 'Should throw the correct error message when Expected is not a string' { + $mockActual = 'Test string' + + $scriptBlock = { + 'Test string' | Should-BeBlockString -Expected ([Int32] -1) + } + + { & $scriptBlock } | Should-Throw -ExceptionMessage 'The Expected value must be of type string or string`[`], but it was not.' + } + + It 'Should throw the correct error message when Expected is not a string' { + { throw 'int[]' } | Should-Throw -ExceptionMessage 'string`[`]' + } } From d441c24aff524c3918bf9f416887fb4630e88113 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Thu, 22 Aug 2024 21:02:55 +0200 Subject: [PATCH 2/2] Remove debug test --- tests/Unit/Public/Assert-BlockString.tests.ps1 | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/Unit/Public/Assert-BlockString.tests.ps1 b/tests/Unit/Public/Assert-BlockString.tests.ps1 index 0b2a5a4..c0afef8 100644 --- a/tests/Unit/Public/Assert-BlockString.tests.ps1 +++ b/tests/Unit/Public/Assert-BlockString.tests.ps1 @@ -160,8 +160,4 @@ Describe 'Assert-BlockString' { { & $scriptBlock } | Should-Throw -ExceptionMessage 'The Expected value must be of type string or string`[`], but it was not.' } - - It 'Should throw the correct error message when Expected is not a string' { - { throw 'int[]' } | Should-Throw -ExceptionMessage 'string`[`]' - } }