From 7f7f39ba66eadad7909676f5ef8a5713133088ff Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Thu, 22 Aug 2024 21:07:35 +0200 Subject: [PATCH] `Assert-BlockString`: Allow string array and single string for `Expected` (#3) --- CHANGELOG.md | 2 + source/Public/Assert-BlockString.ps1 | 17 ++++-- .../Unit/Public/Assert-BlockString.tests.ps1 | 60 +++++++++++++++++++ 3 files changed, 73 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..c0afef8 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,24 @@ 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.' + } }