Skip to content
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- New-*Exception
- Use `ThrowTerminatingError` instead of `throw`. Fixes [#177](https://github.com/dsccommunity/DscResource.Common/issues/177).
- Compare-DscParameterState
- Fixed a bug comparing single-valued arrays in the desired state when
TurnOffTypeChecking is used [#184](https://github.com/dsccommunity/DscResource.Common/issues/184).
- Fix typo in `Clear-ZeroedEnumPropertyValue` help text. Fixes [#181](https://github.com/dsccommunity/DscResource.Common/issues/181).

### Changed
Expand Down
4 changes: 2 additions & 2 deletions source/Public/Compare-DscParameterState.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ function Compare-DscParameterState
}
#endregion TestType
#region Check if the value of Current and desired state is the same but only if they are not an array
if ($currentValue -eq $desiredValue -and -not $desiredType.IsArray)
if ($currentValue -eq $desiredValue -and -not $desiredType.IsArray -and -not $currentType.IsArray)
{
Write-Debug -Message ($script:localizedData.MatchValueMessage -f $desiredType.FullName, $key, $currentValue, $desiredValue)
continue # pass to the next key
Expand Down Expand Up @@ -650,7 +650,7 @@ function Compare-DscParameterState
We use .foreach() method as we are sure that $returnValue is an array.
#>
[Array]$returnValue = @(
$returnValue.foreach(
$returnValue.ForEach(
{
if ($_ -is [System.Collections.Hashtable])
{
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Public/Compare-DscParameterState.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,7 @@ Describe 'DscResource.Common\Compare-DscParameterState' {
Bool = $true
Int = 99
Array = 'a', 'b', 'c', 1
SingleValueArray = 'v1', 'v2' #for testing the comparison of single value arrays in the desired state
Hashtable = @{
k1 = 'Test'
k2 = 123
Expand Down Expand Up @@ -1514,6 +1515,41 @@ Describe 'DscResource.Common\Compare-DscParameterState' {
}
}

Context 'When there is a single-valued array and TurnOffTypeChecking is used' {
BeforeAll {
$desiredValues = [PSObject] @{
String = 'a string'
Bool = $true
Int = 99
Array = 'a', 'b', 'c', 1
SingleValueArray = 'v1' #for testing the comparison of single value arrays in the desired state
Hashtable = @{
k1 = 'Test'
k2 = 123
k3 = 'v1', 'v2', 'v3'
}
}
}

It 'Should not throw exception' {
{ $script:result = Compare-DscParameterState `
-CurrentValues $currentValues `
-DesiredValues $desiredValues `
-TurnOffTypeChecking `
-Verbose:$verbose } | Should -Not -Throw
}

It 'Should not be null' {
$script:result | Should -Not -BeNullOrEmpty
}

It 'Should return $false for SingleValueArray InDesiredState' {
$script:result.Where({
$_.Property -eq 'SingleValueArray'
}).InDesiredState | Should -BeFalse
}
}

Context 'When both arrays are empty' {
BeforeAll {
$currentValues = @{
Expand Down
68 changes: 34 additions & 34 deletions tests/Unit/Public/Test-DscParameterState.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ Describe 'Test-DscParameterState' {
Bool = $true
Int = 99
Array = 'a', 'b', 'c', 1
SingleValueArray = 'v1', 'v2' #for testing the comparison of single value arrays in the desired state
Hashtable = @{
k1 = 'Test'
k2 = 123
Expand Down Expand Up @@ -540,7 +541,6 @@ Describe 'Test-DscParameterState' {
}
}


Context 'When array has a value with a different type' {
BeforeAll {
$desiredValues = [PSObject] @{
Expand Down Expand Up @@ -593,6 +593,39 @@ Describe 'Test-DscParameterState' {
}
}

Context 'When there is a single-valued array and TurnOffTypeChecking is used' {
BeforeAll {
$desiredValues = [PSObject] @{
String = 'a string'
Bool = $true
Int = 99
Array = 'a', 'b', 'c', 1
SingleValueArray = 'v1' #for testing the comparison of single value arrays in the desired state
Hashtable = @{
k1 = 'Test'
k2 = 123
k3 = 'v1', 'v2', 'v3'
}
}
}

It 'Should not throw exception' {
{ Test-DscParameterState `
-CurrentValues $currentValues `
-DesiredValues $desiredValues `
-TurnOffTypeChecking `
-Verbose:$verbose } | Should -Not -Throw
}

It 'Should return $false' {
Test-DscParameterState `
-CurrentValues $currentValues `
-DesiredValues $desiredValues `
-TurnOffTypeChecking `
-Verbose:$verbose | Should -BeFalse
}
}

Context 'When both arrays are empty' {
BeforeAll {
$currentValues = @{
Expand Down Expand Up @@ -849,39 +882,6 @@ Describe 'Test-DscParameterState' {
}
}

Context 'When an array in hashtable has different order but SortArrayValues is used' {
BeforeAll {
$desiredValues = [PSObject] @{
String = 'a string'
Bool = $true
Int = 99
Array = 'a', 'b', 'c'
Hashtable = @{
k1 = 'Test'
k2 = 123
k3 = 'v3', 'v2', 'v1', 99
}
}
}

It 'Should not throw exception' {
{ Test-DscParameterState `
-CurrentValues $currentValues `
-DesiredValues $desiredValues `
-SortArrayValues `
-Verbose:$verbose } | Should -Not -Throw
}

It 'Should return $true' {
Test-DscParameterState `
-CurrentValues $currentValues `
-DesiredValues $desiredValues `
-SortArrayValues `
-Verbose:$verbose | Should -BeTrue
}
}


Context 'When hashtable has a value with a different type' {
BeforeAll {
$desiredValues = [PSObject] @{
Expand Down