File size: 5,416 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe 'Tests for OrderedHashtable' -Tag 'CI' {

    It 'Can create an empty OrderedHashtable' {
        $oh =[System.Management.Automation.OrderedHashtable]::new()
        $oh.Count | Should -Be 0
        $oh.GetType().Name | Should -BeExactly 'OrderedHashtable'
    }

    It 'Can create an empty OrderedHashtable with a capacity' {
        $oh = [System.Management.Automation.OrderedHashtable]::new(10)
        $oh.Count | Should -Be 0
        $oh.GetType().Name | Should -BeExactly 'OrderedHashtable'
    }

    It 'Can create an OrderedHashtable with an initial dictionary' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh.Count | Should -Be 2
        $oh['a'] | Should -Be $h['a']
        $oh['b'] | Should -Be $h['b']
    }

    It 'Can use the Add() method' {
        $oh = [System.Management.Automation.OrderedHashtable]::new()
        $oh.Add('a', 1)
        $oh.Add('b', 2)
        $oh.Count | Should -Be 2
        $oh['a'] | Should -Be 1
        $oh['b'] | Should -Be 2
    }

    It 'Can use the Clear() method' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh.Count | Should -Be 2
        $oh.Clear()
        $oh.Count | Should -Be 0
    }

    It 'Can use the Clone() method' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh2 = $oh.Clone()
        $oh2.Count | Should -Be 2
        $oh2['a'] | Should -Be $h['a']
        $oh2['b'] | Should -Be $h['b']
    }

    It 'Can use the Contains() method' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh.Contains('a') | Should -BeTrue
        $oh.Contains('b') | Should -BeTrue
        $oh.Contains('c') | Should -BeFalse
    }

    It 'Can use the ContainsKey() method' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh.ContainsKey('a') | Should -BeTrue
        $oh.ContainsKey('b') | Should -BeTrue
        $oh.ContainsKey('c') | Should -BeFalse
    }

    It 'Can use the ContainsValue() method' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh.ContainsValue(1) | Should -BeTrue
        $oh.ContainsValue(2) | Should -BeTrue
        $oh.ContainsValue(3) | Should -BeFalse
    }

    It 'Can use the CopyTo() method' {
        $oh = [System.Management.Automation.OrderedHashtable]::new()
        $oh.Add('a', 1)
        $oh.Add('b', 2)
        $oh.Add('c', 3)
        $a = (4, 5, 6, 7)
        $oh.CopyTo($a, 1)
        $a[0] | Should -Be 4
        # OrderedDictionary.CopyTo() doesn't guarantee to preserve order
        # so we can't easily test the values
        $a[1].GetType().Name | Should -BeExactly 'DictionaryEntry'
        $a[2].GetType().Name | Should -BeExactly 'DictionaryEntry'
        $a[3].GetType().Name | Should -BeExactly 'DictionaryEntry'
    }

    It 'Can use the Equals() method' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh2 = $oh.Clone()
        $oh3 = $oh
        $oh.Equals($oh2) | Should -BeFalse
        $oh.Equals($oh3) | Should -BeTrue
    }

    It 'Can use the GetEnumerator() method' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh.GetEnumerator().GetType().Name | Should -BeExactly 'OrderedDictionaryEnumerator'
    }

    It 'Can use the GetHashCode() method' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh.GetHashCode() | Should -BeGreaterThan 0
        $oh2 = $oh.Clone()
        $oh.GetHashCode() | Should -Not -Be $oh2.GetHashCode()
        $oh3 = $oh
        $oh.GetHashCode() | Should -Be $oh3.GetHashCode()
    }

    It 'Can use Remove() method' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh.Remove('a')
        $oh.Count | Should -Be 1
        $oh.Contains('a') | Should -BeFalse
    }

    It 'Can use Item property' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh['a'] | Should -Be 1
        $oh['b'] | Should -Be 2
    }

    It 'Can use IsFixedSize property' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh.IsFixedSize | Should -BeFalse
    }

    It 'Can use IsReadOnly property' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh.IsReadOnly | Should -BeFalse
    }

    It 'Can use IsSynchronized property' {
        $h = @{ a = 1; b = 2 }
        $oh = [System.Management.Automation.OrderedHashtable]::new($h)
        $oh.IsSynchronized | Should -BeFalse
    }

    It 'Can use Keys property' {
        $oh = [System.Management.Automation.OrderedHashtable]::new()
        $oh['a'] = 1
        $oh['b'] = 2
        $oh.Keys | Should -Be ('a', 'b')
    }

    It 'Can use Values property' {
        $oh = [System.Management.Automation.OrderedHashtable]::new()
        $oh['a'] = 1
        $oh['b'] = 2
        $oh.Values | Should -Be (1, 2)
    }
}