CamelConverter - more fixes picked up in testing (#30601)

This commit is contained in:
Jordan Borean 2017-09-20 15:17:26 +10:00 committed by GitHub
parent 92426840d6
commit a940eb1e80
2 changed files with 27 additions and 17 deletions

View file

@ -28,15 +28,15 @@ Function Convert-ListToSnakeCase($list) {
foreach ($value in $list) { foreach ($value in $list) {
if ($value -is [Hashtable]) { if ($value -is [Hashtable]) {
$new_value = Convert-DictToSnakeCase -dict $value $new_value = Convert-DictToSnakeCase -dict $value
} elseif ($value -is [Array]) { } elseif ($value -is [Array] -or $value -is [System.Collections.ArrayList]) {
$new_value = Convert-ListToSnakeCase -list $value $new_value = Convert-ListToSnakeCase -list $value
} else { } else {
$new_value = $value $new_value = $value
} }
$snake_list.Add($new_value) | Out-Null [void]$snake_list.Add($new_value)
} }
return $snake_list return ,$snake_list
} }
# converts a dict/hashtable keys from camelCase to snake_case # converts a dict/hashtable keys from camelCase to snake_case
@ -51,14 +51,14 @@ Function Convert-DictToSnakeCase($dict) {
$value = $dict_entry.Value $value = $dict_entry.Value
if ($value -is [Hashtable]) { if ($value -is [Hashtable]) {
$snake_dict.$snake_key = Convert-DictToSnakeCase -dict $value $snake_dict.$snake_key = Convert-DictToSnakeCase -dict $value
} elseif ($value -is [Array]) { } elseif ($value -is [Array] -or $value -is [System.Collections.ArrayList]) {
$snake_dict.$snake_key = Convert-ListToSnakeCase -list $value $snake_dict.$snake_key = Convert-ListToSnakeCase -list $value
} else { } else {
$snake_dict.$snake_key = $value $snake_dict.$snake_key = $value
} }
} }
return $snake_dict return ,$snake_dict
} }
# this line must stay at the bottom to ensure all defined module parts are exported # this line must stay at the bottom to ensure all defined module parts are exported

View file

@ -3,7 +3,7 @@
#Requires -Module Ansible.ModuleUtils.Legacy #Requires -Module Ansible.ModuleUtils.Legacy
#Requires -Module Ansible.ModuleUtils.CamelConversion #Requires -Module Ansible.ModuleUtils.CamelConversion
$ErrorActionPreference = 'Continue' $ErrorActionPreference = 'Stop'
Function Assert-Equals($actual, $expected) { Function Assert-Equals($actual, $expected) {
if ($actual -cne $expected) { if ($actual -cne $expected) {
@ -31,6 +31,8 @@ $input_dict = @{
ID = 'id' ID = 'id'
IEnumerable = 'i_enumerable' IEnumerable = 'i_enumerable'
} }
emptyList = @()
singleList = @("a")
} }
$output_dict = Convert-DictToSnakeCase -dict $input_dict $output_dict = Convert-DictToSnakeCase -dict $input_dict
@ -38,13 +40,14 @@ foreach ($entry in $output_dict.GetEnumerator()) {
$key = $entry.Name $key = $entry.Name
$value = $entry.Value $value = $entry.Value
$type = $value.GetType()
if ($value -is [Hashtable]) { if ($value -is [Hashtable]) {
Assert-Equals -actual $key -expected "inner_hash_table" Assert-Equals -actual $key -expected "inner_hash_table"
foreach ($inner_hash in $value.GetEnumerator()) { foreach ($inner_hash in $value.GetEnumerator()) {
Assert-Equals -actual $inner_hash.Name -expected $inner_hash.Value Assert-Equals -actual $inner_hash.Name -expected $inner_hash.Value
} }
} elseif ($value -is [Array]) { } elseif ($value -is [Array] -or $value -is [System.Collections.ArrayList]) {
# there is one array in our original dict, we know the structure if ($key -eq "list_dict") {
foreach ($inner_list in $value) { foreach ($inner_list in $value) {
if ($inner_list -is [Hashtable]) { if ($inner_list -is [Hashtable]) {
foreach ($inner_list_hash in $inner_list.GetEnumerator()) { foreach ($inner_list_hash in $inner_list.GetEnumerator()) {
@ -57,6 +60,13 @@ foreach ($entry in $output_dict.GetEnumerator()) {
Assert-Equals -actual $inner_list -expected 0 Assert-Equals -actual $inner_list -expected 0
} }
} }
} elseif ($key -eq "empty_list") {
Assert-Equals -actual $value.Count -expected 0
} elseif ($key -eq "single_list") {
Assert-Equals -actual $value.Count -expected 1
} else {
Fail-Json -obj $result -message "invalid key found for list $key"
}
} else { } else {
Assert-Equals -actual $key -expected $value Assert-Equals -actual $key -expected $value
} }