Skip to content

Commit

Permalink
Compare-DscParameterState: Add support for OrderedDictionary type (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju authored Jan 23, 2024
1 parent cbb838f commit df361a2
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Compare-DscParameterState`
- Add support for the type `[System.Collections.Specialized.OrderedDictionary]`
passed to parameters `CurrentValues` and `DesiredValues` ([issue #57](https://github.com/dsccommunity/DscResource.Common/issues/57)).
- Add support for `DesiredValues` (and `CurrentValues`) to pass a value,
e.g a hashtable, that includes a property with the type `[System.Collections.Specialized.OrderedDictionary]`
or an array of `[System.Collections.Specialized.OrderedDictionary]` ([issue #57](https://github.com/dsccommunity/DscResource.Common/issues/57)).

### Changed

Expand Down
7 changes: 3 additions & 4 deletions source/Public/Compare-DscParameterState.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ function Compare-DscParameterState
}
#endregion check same value
#region Check if the DesiredValuesClean has the key and if it doesn't have, it's not necessary to check his value
if ($desiredValuesClean.GetType().Name -in 'HashTable', 'PSBoundParametersDictionary')
if ($desiredValuesClean.GetType().Name -in 'HashTable', 'PSBoundParametersDictionary', 'OrderedDictionary')
{
$checkDesiredValue = $desiredValuesClean.ContainsKey($key)
}
Expand Down Expand Up @@ -459,7 +459,7 @@ function Compare-DscParameterState
}
}

if ($desiredType -eq [System.Collections.Hashtable] -and $currentType -eq [System.Collections.Hashtable])
if (($desiredType -eq [System.Collections.Hashtable] -or $desiredType -eq [System.Collections.Specialized.OrderedDictionary]) -and ($currentType -eq [System.Collections.Hashtable]-or $currentType -eq [System.Collections.Specialized.OrderedDictionary]))
{
$param = @{} + $PSBoundParameters
$param.CurrentValues = $currentArrayValues[$i]
Expand Down Expand Up @@ -495,10 +495,9 @@ function Compare-DscParameterState
continue
}
}

}
}
elseif ($desiredType -eq [System.Collections.Hashtable] -and $currentType -eq [System.Collections.Hashtable])
elseif (($desiredType -eq [System.Collections.Hashtable] -or $desiredType -eq [System.Collections.Specialized.OrderedDictionary]) -and ($currentType -eq [System.Collections.Hashtable]-or $currentType -eq [System.Collections.Specialized.OrderedDictionary]))
{
$param = @{} + $PSBoundParameters
$param.CurrentValues = $currentValue
Expand Down
230 changes: 228 additions & 2 deletions tests/Unit/Public/Compare-DscParameterState.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2689,12 +2689,12 @@ Describe 'ComputerManagementDsc.Common\Compare-DscParameterState' {

Context 'When all values match' {
BeforeAll {
$currentValues = [ordered]@{
$currentValues = [ordered] @{
String = 'This is a string'
Int = 99
Bool = $true
}
$desiredValues = [ordered]@{
$desiredValues = [ordered] @{
String = 'This is a string'
Int = 99
Bool = $true
Expand Down Expand Up @@ -2736,4 +2736,230 @@ Describe 'ComputerManagementDsc.Common\Compare-DscParameterState' {
}
}
}

Context 'When a property has an ordered dictionary' {
Context 'When the property with ordered property does not match' {
BeforeAll {
$currentValues = [ordered] @{
String = 'This is a string'
OrderedProperty = [ordered] @{
Int = 99
}
Bool = $true
}
$desiredValues = [ordered] @{
String = 'This is a string'
OrderedProperty = [ordered] @{
Int = 1
}
Bool = $false
}
}

BeforeEach {
$verbose = $true
}

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

It 'Should return non-null result' {
$script:result | Should -Not -BeNullOrEmpty
}

It 'Should return $true for String in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'String'
}).InDesiredState | Should -BeTrue
}

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

It 'Should return $true for Bool in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'Bool'
}).InDesiredState | Should -BeFalse
}
}

Context 'When all values match' {
BeforeAll {
$currentValues = [ordered] @{
String = 'This is a string'
OrderedProperty = [ordered] @{
Int = 99
}
Bool = $true
}
$desiredValues = [ordered] @{
String = 'This is a string'
OrderedProperty = [ordered] @{
Int = 99
}
Bool = $true
}
}

BeforeEach {
$verbose = $true
}

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

It 'Should return non-null result' {
$script:result | Should -Not -BeNullOrEmpty
}

It 'Should return $true for String in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'String'
}).InDesiredState | Should -BeTrue
}

It 'Should return $false for OrderedProperty in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'OrderedProperty'
}).InDesiredState | Should -BeTrue
}

It 'Should return $true for Bool in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'Bool'
}).InDesiredState | Should -BeTrue
}
}
}

Context 'When a property has an ordered dictionary array' {
Context 'When the property with ordered property does not match' {
BeforeAll {
$currentValues = [ordered] @{
String = 'This is a string'
OrderedProperty = @(
[ordered] @{
Int = 99
}
[ordered] @{
String = 'Yes'
}
)
Bool = $true
}
$desiredValues = [ordered] @{
String = 'This is a string'
OrderedProperty = @(
[ordered] @{
Int = 99
}
[ordered] @{
String = 'No'
}
)
Bool = $false
}
}

BeforeEach {
$verbose = $true
}

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

It 'Should return non-null result' {
$script:result | Should -Not -BeNullOrEmpty
}

It 'Should return $true for String in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'String'
}).InDesiredState | Should -BeTrue
}

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

It 'Should return $true for Bool in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'Bool'
}).InDesiredState | Should -BeFalse
}
}

Context 'When all values match' {
BeforeAll {
$currentValues = [ordered] @{
String = 'This is a string'
OrderedProperty = [ordered] @{
Int = 99
}
Bool = $true
}
$desiredValues = [ordered] @{
String = 'This is a string'
OrderedProperty = [ordered] @{
Int = 99
}
Bool = $true
}
}

BeforeEach {
$verbose = $true
}

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

It 'Should return non-null result' {
$script:result | Should -Not -BeNullOrEmpty
}

It 'Should return $true for String in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'String'
}).InDesiredState | Should -BeTrue
}

It 'Should return $false for OrderedProperty in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'OrderedProperty'
}).InDesiredState | Should -BeTrue
}

It 'Should return $true for Bool in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'Bool'
}).InDesiredState | Should -BeTrue
}
}
}
}

0 comments on commit df361a2

Please sign in to comment.