summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/module_utils_Ansible.ModuleUtils.CamelConversion/library/camel_conversion_test.ps1
blob: bcb9558e3330ba6233ed6c9c4811dcdd679e0ed9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!powershell

#Requires -Module Ansible.ModuleUtils.Legacy
#Requires -Module Ansible.ModuleUtils.CamelConversion

$ErrorActionPreference = 'Stop'

Function Assert-Equal($actual, $expected) {
    if ($actual -cne $expected) {
        Fail-Json @{} "actual != expected`nActual: $actual`nExpected: $expected"
    }
}

$input_dict = @{
    alllower = 'alllower'
    ALLUPPER = 'allupper'
    camelCase = 'camel_case'
    mixedCase_withCamel = 'mixed_case_with_camel'
    TwoWords = 'two_words'
    AllUpperAtEND = 'all_upper_at_end'
    AllUpperButPLURALs = 'all_upper_but_plurals'
    TargetGroupARNs = 'target_group_arns'
    HTTPEndpoints = 'http_endpoints'
    PLURALs = 'plurals'
    listDict = @(
        @{ entry1 = 'entry1'; entryTwo = 'entry_two' },
        'stringTwo',
        0
    )
    INNERHashTable = @{
        ID = 'id'
        IEnumerable = 'i_enumerable'
    }
    emptyList = @()
    singleList = @("a")
}

$output_dict = Convert-DictToSnakeCase -dict $input_dict
foreach ($entry in $output_dict.GetEnumerator()) {
    $key = $entry.Name
    $value = $entry.Value

    if ($value -is [Hashtable]) {
        Assert-Equal -actual $key -expected "inner_hash_table"
        foreach ($inner_hash in $value.GetEnumerator()) {
            Assert-Equal -actual $inner_hash.Name -expected $inner_hash.Value
        }
    }
    elseif ($value -is [Array] -or $value -is [System.Collections.ArrayList]) {
        if ($key -eq "list_dict") {
            foreach ($inner_list in $value) {
                if ($inner_list -is [Hashtable]) {
                    foreach ($inner_list_hash in $inner_list.GetEnumerator()) {
                        Assert-Equal -actual $inner_list_hash.Name -expected $inner_list_hash.Value
                    }
                }
                elseif ($inner_list -is [String]) {
                    # this is not a string key so we need to keep it the same
                    Assert-Equal -actual $inner_list -expected "stringTwo"
                }
                else {
                    Assert-Equal -actual $inner_list -expected 0
                }
            }
        }
        elseif ($key -eq "empty_list") {
            Assert-Equal -actual $value.Count -expected 0
        }
        elseif ($key -eq "single_list") {
            Assert-Equal -actual $value.Count -expected 1
        }
        else {
            Fail-Json -obj $result -message "invalid key found for list $key"
        }
    }
    else {
        Assert-Equal -actual $key -expected $value
    }
}

Exit-Json @{ data = 'success' }