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

Assert-BlockString: Allow string array and single string for Expected #3

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
60 changes: 60 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,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.'
}
}