forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd-Member.Tests.ps1
More file actions
299 lines (260 loc) · 9.94 KB
/
Copy pathAdd-Member.Tests.ps1
File metadata and controls
299 lines (260 loc) · 9.94 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
295
296
297
298
299
Describe "Add-Member DRT Unit Tests" -Tags "CI" {
It "Mandatory parameters should not be null nor empty" {
# when Name is null
try
{
Add-Member -Name $null
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
}
# when Name is empty
try
{
Add-Member -Name ""
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
}
# when MemberType is null
try
{
Add-Member -MemberType $null
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
}
# when MemberType is empty
try
{
Add-Member -MemberType ""
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.AddMemberCommand"
}
# when InputObject is null
try
{
Add-Member -InputObject $null
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}
# It only support on AliasProperty, ScriptProperty, CodeProperty and CodeMethod
It "Should Not Have Value2" {
$memberTypesWhereV1CannotBeNull = "CodeMethod", "MemberSet", "PropertySet", "ScriptMethod", "NoteProperty"
foreach ($memberType in $memberTypesWhereV1CannotBeNull)
{
try
{
Add-Member -InputObject a -memberType $memberType -Name Name -Value something -SecondValue somethingElse
Throw "Execution OK"
}
catch{
$_.FullyQualifiedErrorId | Should Be "Value2ShouldNotBeSpecified,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}
}
It "Cannot Add PS Property Or PS Method" {
$membersYouCannotAdd = "Method", "Property", "ParameterizedProperty"
foreach ($member in $membersYouCannotAdd)
{
try
{
Add-Member -InputObject a -memberType $member -Name Name
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "CannotAddMemberType,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}
try
{
Add-Member -InputObject a -memberType AnythingElse -Name Name
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}
It "Value1 And Value2 Should Not Both Null" {
$memberTypes = "CodeProperty", "ScriptProperty"
foreach ($memberType in $memberTypes)
{
try
{
Add-Member -memberType $memberType -Name PropertyName -Value $null -SecondValue $null -InputObject a
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "Value1AndValue2AreNotBothNull,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}
}
It "Fail to add unexisting type" {
try
{
Add-Member -InputObject a -MemberType AliasProperty -Name Name -Value something -SecondValue unexistingType
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "InvalidCastFromStringToType,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}
It "Successful alias, no type" {
$results = Add-Member -InputObject a -MemberType AliasProperty -Name Cnt -Value Length -passthru
$results.Cnt | Should BeOfType Int32
$results.Cnt | Should Be 1
}
It "Successful alias, with type" {
$results = add-member -InputObject a -MemberType AliasProperty -Name Cnt -Value Length -SecondValue String -passthru
$results.Cnt | Should BeOfType String
$results.Cnt | Should Be '1'
}
It "CodeProperty Reference Wrong Type" {
try
{
add-member -InputObject a -MemberType CodeProperty -Name Name -Value something
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}
It "Empty Member Set Null Value1" {
$results = add-member -InputObject a -MemberType MemberSet -Name Name -Value $null -passthru
$results.Length | Should Be 1
$results.Name.a | Should BeNullOrEmpty
}
It "Member Set With 1 Member" {
$members = new-object System.Collections.ObjectModel.Collection[System.Management.Automation.PSMemberInfo]
$n=new-object Management.Automation.PSNoteProperty a,1
$members.Add($n)
$r=add-member -InputObject a -MemberType MemberSet -Name Name -Value $members -passthru
$r.Name.a | Should Be '1'
}
It "MemberSet With Wrong Type For Value1" {
try
{
add-member -InputObject a -MemberType MemberSet -Name Name -Value ImNotACollection
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}
It "ScriptMethod Reference Wrong Type" {
try
{
add-member -InputObject a -MemberType ScriptMethod -Name Name -Value something
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}
It "Add ScriptMethod Success" {
$results = add-member -InputObject 'abc' -MemberType ScriptMethod -Name Name -Value {$this.length} -passthru
$results | Should Be abc
$results.Name() | Should Be 3
}
It "ScriptProperty Reference Wrong Type" {
try
{
add-member -InputObject a -MemberType ScriptProperty -Name Name -Value something
Throw "Execution OK"
}
catch
{
$_.FullyQualifiedErrorId | Should Be "ConvertToFinalInvalidCastException,Microsoft.PowerShell.Commands.AddMemberCommand"
}
}
It "Add ScriptProperty Success" {
set-alias ScriptPropertyTestAlias dir
$al=(get-alias ScriptPropertyTestAlias)
$al.Description="MyDescription"
$al | add-member -MemberType ScriptProperty -Name NewDescription -Value {$this.Description} -SecondValue {$this.Description=$args[0]}
$al.NewDescription | Should Be 'MyDescription'
$al.NewDescription = "some description"
$al.NewDescription | Should Be 'some description'
}
It "Add TypeName MemberSet Success" {
$a = 'string' | add-member -MemberType NoteProperty -Name TestNote -Value Any -TypeName MyType -passthru
$a.PSTypeNames[0] | Should Be MyType
}
It "Add TypeName Existing Name Success" {
$a = 'string' | add-member -TypeName System.Object -passthru
$a.PSTypeNames[0] | Should Be System.Object
}
It "Add Single Note To Array" {
$a=1,2,3
$a = Add-Member -InputObject $a -MemberType NoteProperty -Name Name -Value Value -PassThru
$a.Name | Should Be Value
}
It "Add Multiple Note Members" {
$obj=new-object psobject
$hash=@{Name='Name';TestInt=1;TestNull=$null}
add-member -InputObject $obj $hash
$obj.Name | Should Be 'Name'
$obj.TestInt | Should Be 1
$obj.TestNull | Should BeNullOrEmpty
}
It "Add Multiple Note With TypeName" {
$obj=new-object psobject
$hash=@{Name='Name';TestInt=1;TestNull=$null}
$obj = add-member -InputObject $obj $hash -TypeName MyType -Passthru
$obj.PSTypeNames[0] | Should Be MyType
}
It "Add Multiple Members With Force" {
$obj=new-object psobject
$hash=@{TestNote='hello'}
$obj | Add-Member -MemberType NoteProperty -Name TestNote -Value 1
$obj | add-member $hash -force
$obj.TestNote | Should Be 'hello'
}
It "Simplified Add-Member should support using 'Property' as the NoteProperty member name" {
$results = add-member -InputObject a property Any -passthru
$results.property | Should Be 'Any'
$results = add-member -InputObject a Method Any -passthru
$results.Method | Should Be 'Any'
$results = add-member -InputObject a 23 Any -passthru
$results.23 | Should Be 'Any'
$results = add-member -InputObject a 8 np Any -passthru
$results.np | Should Be 'Any'
$results = add-member -InputObject a 16 sp {1+1} -passthru
$results.sp | Should Be 2
}
}
Describe "Add-Member" -Tags "CI" {
It "should be able to see a newly added member of an object" {
$o = New-Object psobject
Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal"
$o.proppy | Should Not BeNullOrEmpty
$o.proppy | Should Be "superVal"
}
It "Should be able to add a member to an object that already has a member in it" {
$o = New-Object psobject
Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal"
Add-Member -InputObject $o -MemberType NoteProperty -Name AnotherMember -Value "AnotherValue"
$o.AnotherMember | Should Not BeNullOrEmpty
$o.AnotherMember | Should Be "AnotherValue"
}
}