forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-Member.Tests.ps1
More file actions
294 lines (268 loc) · 8.95 KB
/
Copy pathGet-Member.Tests.ps1
File metadata and controls
294 lines (268 loc) · 8.95 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
Describe "Get-Member" -Tags "CI" {
It "Should be able to be called on string objects, ints, arrays, etc" {
$a = 1 #test numbers
$b = 1.3
$c = $false #test bools
$d = @(1,3) # test arrays
$e = "anoeduntodeu" #test strings
$f = 'asntoheusth' #test strings
Get-Member -InputObject $a | Should Not BeNullOrEmpty
Get-Member -InputObject $b | Should Not BeNullOrEmpty
Get-Member -InputObject $c | Should Not BeNullOrEmpty
Get-Member -InputObject $d | Should Not BeNullOrEmpty
Get-Member -InputObject $e | Should Not BeNullOrEmpty
Get-Member -InputObject $f | Should Not BeNullOrEmpty
}
It "Should be able to extract a field from string objects, ints, arrays, etc" {
$a = 1 #test numbers
$b = 1.3
$c = $false #test bools
$d = @(1,3) # test arrays
$e = "anoeduntodeu" #test strings
$f = 'asntoheusth' #test strings
$a | Should BeOfType 'Int32'
$b | Should BeOfType 'Double'
$c | Should BeOfType 'Boolean'
,$d | Should BeOfType 'Object[]'
$e | Should BeOfType 'String'
$f | Should BeOfType 'String'
}
It "Should be able to be called on a newly created PSObject" {
$o = New-Object psobject
# this creates a dependency on the Add-Member cmdlet.
Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal"
Get-Member -InputObject $o | Should Not BeNullOrEmpty
}
}
Describe "Get-Member DRT Unit Tests" -Tags "CI" {
Context "Verify Get-Member with Class" {
if(-not ([System.Management.Automation.PSTypeName]'Employee').Type)
{
Add-Type -TypeDefinition @"
public class Employee
{
private string firstName;
private string lastName;
private int yearsInMS;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}
public int YearsInMS
{
get
{
return yearsInMS;
}
set
{
yearsInMS = value;
}
}
public Employee(string firstName, string lastName, int yearsInMS)
{
this.firstName = firstName;
this.lastName = lastName;
this.yearsInMS = yearsInMS;
}
}
"@
}
$fileToDeleteName = Join-Path $TestDrive -ChildPath "getMemberTest.ps1xml"
$XMLFile= @"
<Types>
<Type>
<Name>Employee</Name>
<Members>
<PropertySet>
<Name>PropertySetName</Name>
<ReferencedProperties>
<Name>FirstName</Name>
<Name>LastName</Name>
</ReferencedProperties>
</PropertySet>
<PropertySet>
<Name>FullSet</Name>
<ReferencedProperties>
<Name>FirstName</Name>
<Name>LastName</Name>
<Name>YearsInMS</Name>
</ReferencedProperties>
</PropertySet>
</Members>
</Type>
<Type>
<Name>EmployeePartTime</Name>
<Members>
<PropertySet>
<Name>PropertySetName</Name>
<ReferencedProperties>
<Name>FirstName</Name>
<Name>HoursPerWeek</Name>
</ReferencedProperties>
</PropertySet>
<PropertySet>
<Name>FullSet</Name>
<ReferencedProperties>
<Name>FirstName</Name>
<Name>LastName</Name>
<Name>HoursPerWeek</Name>
</ReferencedProperties>
</PropertySet>
</Members>
</Type>
</Types>
"@
$XMLFile > $fileToDeleteName
Update-TypeData -AppendPath $fileToDeleteName
It "Fail to get member without any input" {
try
{
Get-Member -MemberType All -ErrorAction Stop
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be 'NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand'
}
}
It 'Get the expected Properties of "Employee" object' {
$emps = [Employee]::New("john", "smith", 5), [Employee]::New("joesph", "smith", 15), [Employee]::New("john", "smyth", 2)
$results = $emps | Get-Member -MemberType Property
$results.Length | Should Be 3
$results[0].Name | Should Be "FirstName"
$results[1].Name | Should Be "LastName"
$results[2].Name | Should Be "YearsInMS"
}
It 'Get the Public Methods of "Employee" object' {
$emps = [Employee]::New("john", "smith", 5), [Employee]::New("joesph", "smith", 15), [Employee]::New("john", "smyth", 2)
$methodList = "GetHashCode", "Equals", "ToString", "GetType"
$results = $emps | Get-Member -MemberType Method
$results.Length | Should Be $methodList.Length
$methodFound = @()
for ($i = 0;$i -lt $methodList.Length;$i++)
{
for ($j = 0;$j -lt $results.Length;$j++)
{
if ($results[$j].Name.Equals($methodList[$i]))
{
$methodFound += $true
}
}
}
for ($i = 0;$i -lt $methodList.Length;$i++)
{
$methodFound[$i] | Should Be $true
}
}
It 'Get property sets defined in private members' {
$emps = [Employee]::New("john", "smith", 5), [Employee]::New("joesph", "smith", 15), [Employee]::New("john", "smyth", 2)
$results = $emps | Get-Member -MemberType PropertySet
$results.Length | Should Be 2
$results[0].Name | Should Be "FullSet"
$results[1].Name | Should Be "PropertySetName"
}
}
Context "Verify Get-Member with Static Parameter" {
It 'Get the static properties and methods of the object' {
$obj = New-Object -TypeName System.Int32
$results = $obj | Get-Member -Static
$members = "MaxValue", "MinValue", "Parse", "TryParse"
foreach ($member in $members)
{
foreach ($result in $results)
{
if($result.Name.Equals($member))
{
return $true
}
}
return $false
}
}
It "Get the static properties and methods of int instance" {
$results = 1 | Get-Member -Static
$members = "MaxValue", "MinValue", "Parse", "TryParse"
foreach ($member in $members)
{
foreach ($result in $results)
{
if($result.Name.Equals($member))
{
return $true
}
}
return $false
}
}
It "Get the static properties and methods of int instance Wrapped" {
$results = [pscustomobject]1 | Get-Member -Static
$members = "MaxValue", "MinValue", "Parse", "TryParse"
foreach ($member in $members)
{
foreach ($result in $results)
{
if($result.Name.Equals($member))
{
return $true
}
}
return $false
}
}
}
Context "Verify Get-Member with other parameters" {
It 'works with View Parameter' {
$results = [xml]'<a>some text</a>' | Get-Member -view adapted
$results.Length | Should Be 38
}
It 'Get hidden members'{
$results = 'abc' | Get-Member -force
$hiddenMembers = "psbase", "psextended", "psadapted", "pstypenames", "psobject"
foreach ($member in $hiddenMembers)
{
foreach ($result in $results)
{
if($result.Name.Equals($member))
{
return $true
}
}
return $false
}
}
It 'Get Set Property Accessors On PsBase'{
$results = ('abc').psbase | Get-Member -force get_*
$expectedMembers = "get_Chars", "get_Length"
foreach ($member in $expectedMembers)
{
foreach ($result in $results)
{
if($result.Name.Equals($member))
{
return $true
}
}
return $false
}
}
}
}