Skip to content

Commit

Permalink
chore: Update Assert-BlockString to allow passing string array and si…
Browse files Browse the repository at this point in the history
…ngle string to Expected parameter
  • Loading branch information
johlju committed Aug 22, 2024
1 parent d60eba6 commit c44d344
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
17 changes: 11 additions & 6 deletions source/Public/Assert-BlockString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ function Assert-BlockString
$Actual,

[Parameter(Position = 0, Mandatory = $true)]
[System.String[]]
$Expected,

[Parameter()]
Expand All @@ -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)
{
Expand Down
64 changes: 64 additions & 0 deletions tests/Unit/Public/Assert-BlockString.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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`[`]'
}
}

0 comments on commit c44d344

Please sign in to comment.