Tests/HashTableTesting.ps1

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
82
83
84
[CmdletBinding(SupportsShouldProcess = $True)]
Param(    
)
#region localfunctions
function ShowHash($hash) {
    foreach($key in $hash.Keys) {
        Msg("$key -> " + $hash[$key])
    }
}
function localAppendValue($this,$key,$value) {

            if ($this.ContainsKey($key)) {
                $currentData = $this[$key]                
                $this.Remove($key)
                $this.Add($key,@($currentData,$value))
                
                #OLD code
                #switch ($currentData.GetType()) {
                # "String" {
                # $this.Add($key,$currentData + $data)
                # break
                # }
                # "Int*" {
                # $this.Add($key,$currentData + $data)
                # break
                # }
                # default {
                # $newData = @($currentData,$value)
                # $this.Add($key,$newData)
                # break
                # }
                #}
            } else {
                $this.Add($key,$data)
            }
}
#end region

#Load the module
get-module PSJumpStart | Remove-Module;
Import-Module PSJumpStart -Force

Msg "Start Execution"    

Msg "Create a std HashTable"
$hashis = @{
    "Apple"="Green"
    "Lemon"="Yellow"
    "Banana"="Yellow"
    "MyNum" = 3
}

Msg "---------- Init ---------"
ShowHash $hashis

localAppendValue $hashis "Banana" "Green"
$hashis.AppendValue("MyNum",8)
$hashis.Replace("Apple","Red")

Msg "------ Changed --------"
ShowHash $hashis


Msg "------ Add hash to Hash --------"
$hashis.Add("SubHash",$hashis)
ShowHash $hashis


Msg "------ Append hash to key 'SubHash' --------"
$hashis.AppendValue("SubHash",$hashis)
ShowHash $hashis

Msg "------ Object added --------"
$hashis.Add("web",([net.WebRequest]::Create("https://www.powershellgallery.com")))
ShowHash $hashis


Msg "------ Object appended to key 'web' --------"
$hashis.AppendValue("web",(New-Object Net.WebClient))
ShowHash $hashis


Msg "End Execution"