-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Splatter.tests.ps1
344 lines (294 loc) · 11.9 KB
/
Splatter.tests.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#requires -Module Pester, Splatter
describe Splatter {
context 'Get-Splat makes splatting more gettable' {
it 'Will Find Matching Parameters for a Command' {
$splatOut = @{id = $pid;Foo='bar'} | Get-Splat -Command Get-Process
$splatOut.Keys.Count | should -Be 1
}
it 'Will remove invalid input' {
@{id = $pid;Timeout=65kb} |
Get-Splat -Command Wait-Process |
Select-Object -ExpandProperty Count |
should -Be 1
@{id="blah"} | Get-Splat -Command Get-Process
}
it 'Will make strings into ScriptBlocks, if it has to' {
@{ScriptBlock = "'Hello World'"} | Get-Splat Invoke-Command
}
it 'Will let you pipe splats' {
@{id=$pid } | Use-Splat -Command Get-Process
}
it 'Will accept objects, and turn them into hashtables' {
New-Object PSObject -Property @{
Id = $PID
} |
Use-Splat -Command Get-Process
}
it 'Will handle parameter aliases' {
@{Pid = $PID} | Get-Splat -Command Get-Process
}
it 'Will handle piped input' {
Get-Process -Id $pid |
Get-Splat -Command Get-Process |
Use-Splat Get-Process |
Select-Object -ExpandProperty ID |
Should -Be $pid
}
it 'Can see if you can pass down $psBoundParameters' {
function foo([int]$id,[string]$Message) {
$foundSplat = $PSBoundParameters | Get-Splat get-process
$foundSplat | Use-Splat
}
(foo -id $pid).id|
should -Be $pid
}
}
it 'Will find commands that fit a splat' {
@{pid=$Pid } | Find-Splat -Command *-Process -Global
}
context 'Splatter Aliases are Sweet Syntax Sugar' {
it '?@ gets a splat' {
@{id=$pid } | Get-Splat -Command Get-Process
}
it '??@ finds a splat' {
@{splat=@{}} | Find-Splat -Local
}
it '*@ merges splats' {
@{a='b'}|
Merge-Splat -Add @{c='d'} |
Select-Object -ExpandProperty Keys | should -Be a,c
}
it '.@ (Use-Splat)' {
@{id=$pid} | Use-Splat gps
}
}
context 'Simplified splatting to script blocks' {
it 'Is easy to splat a script block' {
$splat =@{Message='hi'}
$splat | Use-Splat {
param([Parameter(Mandatory=$true)][string]$Message) $Message
} | should -Be hi
}
it 'Can find matching scripts for a piece of data' {
$Fruit, $vegetable = {
param(
[Parameter(Mandatory=$true)]
[string]$Name,
[Parameter(Mandatory=$true)]
[ValidateSet('Fruit')]
[string]$Type
)
"$Name is a $type"
}, {
param(
[Parameter(Mandatory=$true)]
[string]$Name,
[Parameter(Mandatory=$true)]
[ValidateSet('Vegetable')]
[string]$Type
)
"$Name is a $type"
}
$matchedSplat = @{name='apricot';type='fruit'} |
Get-Splat $Fruit,$vegetable
$matchedSplat |
Select-Object -ExpandProperty Command |
should -Be $Fruit
$matchedSplat | Use-Splat | should -Be 'apricot is a fruit'
}
it 'Can pass additional arguments' {
$2Splat = @{} | Get-Splat {$args}
$123 = $2Splat | Use-Splat -ArgumentList 1,2,3
$Another123 = @{} | Use-Splat {$args} 1 2 3
$123 | should -Be 1,2,3
$Another123 | should -Be 1,2,3
}
}
context 'Squishing Splats together with Merge-Splat' {
it 'Is easy to combine N hashtables and objects into one with Merge-Splat' {
@{a='b'}|
Merge-Splat -Add @{c='d'} |
Select-Object -ExpandProperty Keys |
should -Be a,c
}
it 'Is easy to remove keys from a Splat' {
@{a='b';c='d';e='f'} |
Merge-Splat -Remove c |
Select-Object -ExpandProperty Keys
}
it 'Can -Map new keys, if a key was found' {
@{a='b'} | Merge-Splat -Map @{a='b',@{c='d'},{@{e='f'}}} |
Select-Object -ExpandProperty Keys |
Sort-Object |
should -Be a,b,c,e
}
it 'Can -Map back objects,if a key was found' {
@{id=$Pid} | Merge-Splat -Map @{id={
Get-Process -id $args
}}
}
it 'Can -Exclude keys' {
@{a='b';"c$(Get-Random)"='d'} |
Merge-Splat -Exclude c* |
Select-Object -ExpandProperty Keys |
should -Be a
}
it 'Can -Include Keys' {
@{a='b';"c$(Get-Random)"='d'} |
Merge-Splat -Include c* |
Select-Object -ExpandProperty Keys |
should -Belike c*
}
it 'Will squish collisions' {
$merged = @{a='b'},[Ordered]@{a='a';b='b';c='c'} | Merge-Splat
$merged.keys | should -Be a,b,c
$merged.a | should -Be b,a
}
it 'If passed -Keep, it will Keep the first one' {
$merged = @{a='b'},[Ordered]@{a='a';b='b';c='c'} | Merge-Splat -Keep
$merged.keys | should -Be a,b,c
$merged.a | should -Be b
}
it 'If passed -Replace, it will Replace collisions with new items' {
$merged = @{a='b'},[Ordered]@{a='a';b='b';c='c'} | Merge-Splat -Replace
$merged.keys | should -Be a,b,c
$merged.a | should -Be a
}
}
context 'Find Splat helps you find commands to splat' {
it 'Will find them within a -Module' {
@{splat=@{}} | Find-Splat -Module Splatter
}
it 'Will find them within a -Module (with a -Command filter)' {
@{splat=@{}} | Find-Splat -Module Splatter -Command *-Splat
}
it 'Will find them commands within the -Local module' {
@{splat=@{}} | Find-Splat -Local
}
it 'Will find them commands within the -Local module (with a -Command filter)' {
@{splat=@{}} | Find-Splat -Local -Command *-Splat
}
<#it 'Will find commands globally (this may take a while)' {
$foundAnything = @{ThisIsAParameterNameThatShouldNotExist='blah'} | ??@ -Global
if ($foundAnything -ne $null) { throw "Should not have found a match" }
}#>
it 'Will find commands by wildcard' {
@{id=$pid} | Find-Splat Get-*process*
}
it 'Will find splats by specific name' {
@{id=$pid} | Find-Splat Get-Process
}
it 'Will pipe found splats into Use-Splat (use carefully)' {
@{Id=$pid} | Find-Splat -Global -Command Get*-*Process | Use-Splat
}
it 'Will find nothing when passed a module that does not exist' {
$o=@{Id=$pid} | Find-Splat -Module blalkjdasdlkjdajlks
if ($o -ne $null) { throw "Somehow it found something" }
}
it 'Can find using psBoundParameters' {
function foo([int]$id) {
$foundSplat = $PSBoundParameters | Find-Splat -Command get-process
$foundSplat | Use-Splat
}
(foo -id $pid).id|
should -Be $pid
}
}
context 'Embedding Splatter is Easy' {
it 'is as simple as Initialize-Splatter' {
$embeddedSplatter = Initialize-Splatter
. ([ScriptBlock]::Create($embeddedSplatter))
if (${.@} -isnot ([ScriptBlock])) {
throw 'Splatter failed to embed'
}
$splatterModule = Get-Module Splatter
${.@} | should -Not -Be $splatterModule.ExportedVariables['.@']
@{id=$pid} | & ${.@} gps
}
it 'is pretty small' {
$embeddedSplatter = Initialize-Splatter
$embeddedSplatter.Length | should -Belessthan 30kb
}
it 'can -Be minified and compressed' {
$embeddedSplatter = Initialize-Splatter -Minify -Compress
$embeddedSplatter.Length | should -Belessthan 10kb
}
it 'Can -Be embedded as a functionl' {
$embeddedSplatter = Initialize-Splatter -Verb Get
. ([ScriptBlock]::Create($embeddedSplatter))
}
it 'can pick out a command or two' {
$embeddedSplatter = Initialize-Splatter -Verb Get,Use
${??@} = $null
. ([ScriptBlock]::Create($embeddedSplatter))
if (${??@} -ne $null) {
throw '${??@} should -Be undefined'
}
$embeddedSplatter.Length | should -Belessthan 20kb
}
}
context 'Out-Splat' {
it 'Can write you a splatting script' {
$splatScript = Out-Splat -CommandName Get-Command -DefaultParameter @{Module='Splatter';CommandType='Alias'}
$splatScript| should -Belike '*Get-Command*@*'
}
it 'Can write you a splating function' {
$splatFunction =
Out-Splat -FunctionName Get-SplatterAlias -CommandName Get-Command -DefaultParameter @{
Module='Splatter';CommandType='Alias'
} -ExcludeParameter * -Synopsis 'Gets Splatter Aliases' -Description 'Gets aliases from the module Splatter'
$splatFunction | should -Belike '*function Get-SplatterAlias*{*Get-Command*@*'
}
it 'Can write -Examples' {
$splatFunction =
Out-Splat -FunctionName Get-SplatterAlias -CommandName Get-Command -DefaultParameter @{
Module='Splatter';CommandType='Alias'
} -ExcludeParameter * -Synopsis 'Gets Splatter Aliases' -Description 'Gets aliases from the module Splatter' -Example 'Get-SplatterAlias'
$splatFunction | should -Belike '*.Example*Get-SplatterAlias*'
}
}
context 'Splatter can -Be smart about pipelines' {
it 'Can determine which parameters can pipe' {
$r =
@{Foo='Bar';Baz='Bing'} |
Get-Splat {
param(
[Parameter(ValueFromPipelineByPropertyName)]
$Foo,
$baz
)
}
$r.PipelineParameter.Keys | should -Be foo
$r.NonPipelineParameter.Keys | should -Be baz
}
it 'Can -Stream splats' {
@{Foo='Bar';Baz='Bing'},
@{Foo='Foo';Baz='Bing2'} |
Use-Splat {
param(
[Parameter(ValueFromPipelineByPropertyName)]
[PSObject]
$Foo,
$baz
)
Begin { $baz }
process { $foo }
} -Stream | should -Be bing,bar,foo
}
}
context 'Splatter tries to -Be fault-tolerant' {
it 'Will complain if Use-Splat is not provided with a -Command' {
$problem = $null
@{aSplat='IMadeMySelf'} | Use-Splat -ErrorAction SilentlyContinue -ErrorVariable Problem
if (-not $Problem) { throw "There should hae -Been a problem" }
}
it 'Will output properties containing invalid parameters' {
$o = @{Date='akllaksjasklj'} | Get-Splat Get-Date -Force
$o.Invalid.keys | should -Be Date
}
it 'Will mark parameters that could not -Be turned into a ScriptBlock as invalid' {
$o = @{Command='{"hi"'} | Get-Splat Invoke-Command -Force
$o.Invalid.keys | should -Be Command
}
}
}