-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Irregular.tests.ps1
815 lines (708 loc) · 29.4 KB
/
Irregular.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
#requires -Module Pester, Irregular
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingCmdletAliases", "", Justification="Irregular Uses Smart Aliases")]
param()
describe Get-Regex {
it "Lets you keep a library of Regular Expressions" {
Get-RegEx
}
it "Lets you get a particular item by name" {
Get-RegEx -Name TrueOrFalse
}
it 'Can get RegExs from a -FilePath' {
Get-Regex -FilePath (Get-Command Get-Regex | Get-Module | Split-Path)
}
it 'Can get Regexes -FromModule ' {
Get-Regex -FromModule Irregular
}
it 'Can get a Regex -As a String' {
Get-Regex -Name Digits -As String | should -Be '\d+'
}
it 'Can get a RegEx -As a File' {
Get-Regex -Name Digits -As File |
Select-Object -ExpandProperty Name |
should -Be Digits.regex.txt
}
it 'Can get a RegEx -As a Pattern' {
Get-Regex -Name Digits -As Pattern | should -BeLike '*\d+*'
}
it 'Can get a RegEx -As a Hashtable' {
Get-RegEx -Name Digits -As Hashtable | should -BeLike '@{*'
}
it 'Can get a RegEx -As a Variable' {
Get-RegEx -Name Digits -As Variable | should -BeLike '$digits*=*\d+*'
}
it 'Can get a RegEx -As an Alias' {
Get-RegEx -Name Digits -As Alias | should -Be 'Set-Alias ?<Digits> Use-RegEx'
}
it 'Can get a RegEx -As a Lambda' {
$lambda = Get-RegEx -Name Digits -as Lambda
$lambda | should -BeLike '*RegexLibrary*'
$lambda | should -BeLike '*Digits*'
$lambda | should -BeLike '*UseRegex*=*'
$lambda | should -BeLike '*${?<Digits>}*=*$UseRegex'
}
}
describe Import-Regex {
it 'Imports Regular Expressions into the library' {
Import-RegEx
}
it 'Can import -FromModule' {
Import-RegEx -FromModule Irregular -PassThru
}
it 'Can Import from a -FilePath' {
Import-RegEx -FilePath (Get-Module Irregular | Split-path) -PassThru
}
it 'Can import a -Pattern directly' {
Import-RegEx -Pattern "(?<AnySymbol>\p{S})"
(?<AnySymbol>).GetType() | should -Be ([Regex])
}
}
describe Show-Regex {
it 'Is an interactive tool to preview simple Regex operations' {
$o = Show-RegEx -Pattern '?<Digits>' -Match abc123def456
$o.Output.Count | should -Be 2
}
it 'Can -Remove content' {
$o = Show-RegEx -Pattern '?<Digits>' -Match abc123def456 -Remove
$o.Output | should -Be abcdef
}
it 'Can -Replace content' {
$o = Show-RegEx -Pattern '?<Digits>' -Match abc123def456 -Remove
$o.Output | should -Be abcdef
}
it 'Will return the Regex is no other parameters than Pattern are passed' {
$o = Show-RegEx -Pattern '?<Digits>'
"$o" |should -BeLike *\d+*
}
it 'Will show that invalid patterns are invalid' {
$o = Show-RegEx -Pattern '('
$o.IsValid | should -Be $false
}
}
describe Use-Regex {
it 'Is normally used with an alias of a named expression (e.g. ?<Digits>)' {
(?<Digits>).GetType() | should -Be ([Regex])
}
it 'Lets you find all matches' {
?<Digits> -Match "123abc456" |
Measure-Object |
Select-Object -ExpandProperty Count|
should -Be 2
}
it 'Lets you find a single match' {
?<Digits> -Match '123abc456' -Count 1 |
Measure-Object |
Select-Object -ExpandProperty Count|
should -Be 1
}
it 'Lets you piped in multiple inputs' {
"123abc456", "def789" | ?<Digits> |
Measure-Object |
Select-Object -ExpandProperty Count |
should -Be 3
}
it 'Can search -RightToLeft' {
"123abc456" |
?<Digits> -RightToLeft -Count 1 |
Select-Object -ExpandProperty Value |
should -Be 456
}
it 'Can -Extract results' {
@('123abc456' |
?<Digits> -Extract |
Select-Object -ExpandProperty Digits) | should match '\d+'
}
it 'Will assume -Extract if called with a .' {
'123abc456' | . ?<Digits> | Select-Object -ExpandProperty Digits | should match \d+
}
it 'Can -Coerce (or -Cast) results to a type' {
'123abc456' |
?<Digits> -Coerce @{Digits=[int]} -Count 1 |
Select-Object -ExpandProperty Digits |
should -Be 123
}
it 'Can -Coerce results with a [ScriptBlock]' {
'123abc456' |
?<Digits> -Coerce @{
Digits={($_ -as [int]) * 2}
} -Count 1 |
Select-Object -ExpandProperty Digits |
should -Be 246
}
it 'Can filter results with -Where' {
@('123abc456' |
?<Digits> -Where {$_.Digits %2 } |
should -Be '123')
}
it 'Can -Remove matches' {
'123abc456' |?<Digits> -Remove | should -Be abc
}
it 'Can -Replace matches' {
'123abc456' |?<Digits> -Replace ' $1 ' | should -Be ' 123 abc 456 '
}
it 'Can -ReplaceIf a conidition is met' {
'123abc456' |
?<Digits> -ReplaceIf @{ { $_.Digits % 2 } = '$1 (is odd) '} |
should -Be '123 (is odd) abc456'
}
it 'Can use a -Replacer [ScriptBlock]' {
'123abc456' |
?<Digits> -ReplaceEvaluator { '_' } |
should -Be _abc_
}
it 'Can -Replace a -Count' {
'123abc456' | ?<Digits> -Remove -Count 1 | should -Be abc456
}
it 'Can -Transform matches into something else' {
@('123abc456' | ?<Digits> -Transform '-$1' ) -join ' ' | should -Be '-123 -456'
}
it 'Can transform a match -If a condition is met' {
'123abc456' |
?<Digits> -If @{{$_.Digits %2 } = '$1 is odd' } |
should -Be '123 is odd'
}
it 'Can run a script -If a condition is met' {
'123abc456' |
?<Digits> -If @{{$_.Digits %2 } = {"$([int]$_.Digits * 2) is even" }} |
should -Be '246 is even'
}
it 'Can return an arbitrary value -If a condition is met' {
$randomNumber = [Random]::new().Next()
'123' | ?<Digits> -If @{{$_} = $randomNumber } | should -Be $randomNumber
}
it 'Will -Coerce before -If' {
'123abc456' |?<Digits> -Coerce @{Digits=[int]} -If @{{$_.Digits %2 } = {"$($_.Digits * 2 ) is even" }} | should -Be '246 is even'
}
context '-Split' {
it 'Will -Split a string' {
"key:value" |?<Colon> -Split | Select-Object -First 1 | should -Be key
}
it 'Will -Split a string -RightToLeft' {
'key: value' | ?<Colon> -Split -Trim -RightToLeft | Select-Object -First 1 | should -Be value
}
it 'Will -Split -StartAt at point' {
'prefix: key: value' |
?<Colon> -Split -StartAt 'prefix:'.Length -Trim |
Select-Object -First 1 |
should -Be key
}
it 'Can -IncludeMatch with a -Split' {
$k,$s, $v = "key:value" |?<Colon> -Split -IncludeMatch
$s | should -Be ':'
$k,$s, $v = "key:value" |?<Colon> -Split -IncludeMatch -RightToLeft
$s | should -Be ':'
}
it 'Will -Split -Count number of times' {
'key: value: with a colon' |
?<Colon> -Split -Count 1 -Trim |
Select-Object -First 1 -Skip 1 |
should -Be 'value: with a colon'
}
it 'Will -Split -Count items from -RightToLeft' {
'lkey: value:value:rkey' |
?<Colon> -Split -Count 1 -RightToLeft -Trim |
Select-Object -First 1 |
should -Be 'rkey'
}
}
context '-Until' {
it 'Can get content -Until a point' {
"How much wood would a woodchuck chuck if a woodchuck could chuck wood?" |
?<Punctuation> -Until |
should -Be "How much wood would a woodchuck chuck if a woodchuck could chuck wood"
}
it 'Can -IncludeMatch with -Until' {
"How much wood would a woodchuck chuck if a woodchuck could chuck wood?" |
?<Punctuation> -Until -IncludeMatch |
should -Be "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
}
it 'Can -Measure the distance -Until a match' {
'key:value' |?<Colon> -Until -Measure | should -Be 3
'key:value' |?<Colon> -Until -Measure -RightToLeft | should -Be 7
}
}
it 'Can make any RegEx -CaseSensitive' {
Use-RegEx -Pattern 'param' -Match 'Param' -IsMatch -CaseSensitive | should -Be $false
}
it 'Can -Measure the number of matches' {
Use-RegEx -Pattern '(?>\r\n|\n)' -Measure -Match ([Environment]::NewLine * 6) |
should -Be 6
Use-RegEx -Pattern '(?>\r\n|\n)' -Measure -Match ([Environment]::NewLine * 8) -Count 4 |
should -Be 4
}
it 'Can seek from one match to the next' {
@($txt = "true or false or true or false"
$m = $txt | ?<TrueOrFalse> -Count 1
do {
$m
$m = $m | ?<TrueOrFalse> -Count 1 -Scan
} while ($m)) -join ' ' | # Looping over each match until non are found. ?<TrueOrFalse> is an alias to Use-RegEx
should -Be 'true false true false'
}
it 'Can seek from one match to the next (from -RightToLeft)' {
@($txt = "true or false or true or false"
$m = $txt | ?<TrueOrFalse> -Count 1 -RightToLeft
do {
$m
$m = $m | ?<TrueOrFalse> -Count 1 -RightToLeft -Scan
} while ($m)) -join ' ' | # Looping over each match until non are found. ?<TrueOrFalse> is an alias to Use-RegEx
should -Be 'false true false true'
}
context 'Special Piping Behavior' {
it 'Will match the contents if piped in a file' {
(Get-Command New-RegEx |
Select-Object -ExpandProperty ScriptBlock |
Select-Object -ExpandProperty File) -as [IO.FileInfo] |
?<PowerShell_HelpField> |
Select-Object -ExpandProperty InputObject |
Select-Object -ExpandProperty Name |
should -Be New-RegEx.ps1
}
it 'Will match the script contents if passed an external script' {
Get-Command ((Get-Command New-RegEx |
Select-Object -ExpandProperty ScriptBlock |
Select-Object -ExpandProperty File)) |
?<PowerShell_HelpField> |
Select-Object -ExpandProperty InputObject |
Select-Object -ExpandProperty Name |
should -Be New-RegEx.ps1
}
it 'Will match the definition if passed a function' {
Get-Command New-RegEx |
?<PowerShell_HelpField> |
Select-Object -ExpandProperty InputObject |
Select-Object -ExpandProperty Name |
should -Be New-RegEx
}
}
context Generators {
it 'Can use a .regex.ps1 to generate a Pattern' {
"{'hello world'}" | ?<BalancedCode> |
Select-Object -ExpandProperty Value |
should -Be "{'hello world'}"
}
it 'Can pass named -Parameter[s] to a generator' {
"{'hello world'}" | ?<BalancedCode> -ExpressionParameter @{Open='{'} |
Select-Object -ExpandProperty Value |
should -Be "{'hello world'}"
}
it 'Can pass -Arguments to a generator' {
"['hello world']" | ?<BalancedCode> -ExpressionArgumentList '[' |
Select-Object -ExpandProperty Value |
should -Be "['hello world']"
}
it 'Can use a dynamic generator' {
$rx = Use-RegEx -Generator {param($t) "$t"} -ExpressionParameter @{t='hi'}
"$rx"| should -BeLike *hi*
}
}
context 'Fault Tolerance' {
it 'Will complain if -If is passed keys that are not script blocks' {
{ '123' |?<Digits> -If @{a='b'}} | should throw
}
it 'Will complain if -ReplaceIf is passed keys that are not script blocks' {
{ '123' |?<Digits> -ReplaceIf @{a='b'}} | should throw
}
it 'Will complain if -Coerce is passed non-strings as keys' {
{ '123' |?<Digits> -Coerce @{{'Digits'} = [int]}} | should throw
}
it 'Will complain if -Coerce is passed non-types as values ' {
{ '123' |?<Digits> -Coerce @{'Digits' = "alksldj"} } | should throw
}
it 'Will complain when a Named expression is passed a pattern' {
{ ?<Digits> -Pattern 'blah' -ErrorAction Stop } | should throw
}
}
}
describe New-RegEx {
it "Helps you write -CharacterClasses" {
New-RegEx -CharacterClass LowerCaseLetter |
Select-Object -ExpandProperty Pattern |
should -Be '\p{Ll}'
}
it "Helps you write -CharacterClasses and -ExcludeCharacterClass" {
$r = New-RegEx -CharacterClass Punctuation -ExcludeCharacterClass PunctuationOpen, PunctuationClose
$r.Match("(a)").Success | Should -Be $false
$r.Match(".").Success | Should -Be $true
}
it "Lets you look for repeated content" {
New-RegEx -CharacterClass Digit -Repeat |
Select-Object -ExpandProperty Pattern |
should -Be '\d+'
}
it "Simplifies lookahead with -Before (aka -LookAhead)" {
New-RegEx -Expression 'q' -LookAhead u | # Matches a q that is followed by a u
Select-Object -ExpandProperty Pattern |
should -Be 'q(?=u)'
}
it 'Simplifies lookbehind with -After (aka -LookBehind)' {
New-RegEx -Expression u -LookBehind q | # Matches a u that is preceeded by a q
Select-Object -ExpandProperty Pattern |
should -Be '(?<=q)u'
}
it 'Simplifies negative lookahead with -NotBefore' {
New-RegEx -Expression q -NotBefore u | # Matches a q that isn't followed by a u
Select-Object -ExpandProperty Pattern |
should -Be 'q(?!u)'
}
it "Simplifies negative lookbehind with -NotAfter (aka -NegativeLookBehind)" {
New-RegEx -Expression '"' -NegativeLookBehind '\\' |
Select-Object -ExpandProperty Pattern |
should -Be '(?<!\\)"'
}
it "Can pipe to itself to compound expressions" {
New-RegEx -Pattern '"' |
New-RegEx -CharacterClass Any -Repeat -Lazy -Before (
New-RegEx -Pattern '"' -NotAfter '\\|`'
) |
New-RegEx -Pattern '"' |
Select-Object -ExpandProperty Pattern |
should -Be '".+?(?=(?<!\\|`)")"'
}
it 'Can combine more than on -CharacterClass' {
New-RegEx -CharacterClass Digit, Word |
Select-Object -ExpandProperty Pattern |
should -Be '[\d\w]'
}
it 'Can negate a -CharacterClass' {
New-RegEx -CharacterClass Digit, Word -Not |
Select-Object -ExpandProperty Pattern |
should -Be '[^\d\w]'
}
it 'Can handle -LiteralCharacters' {
?<> -Name UserName -LiteralCharacter .- -CharacterClass Word -Repeat |
?<> (?<> '\@' -NoCapture) |
?<> -Name Domain -LiteralCharacter .- -CharacterClass Word -Repeat
}
it 'Can negate a -LiteralCharacter' {
New-RegEx -LiteralCharacter '.' -Not | Should -Be '[^\.]'
}
it 'Can use a -StartAnchor or -EndAnchor' {
New-RegEx -CharacterClass Whitespace -Min 0 -StartAnchor LineStart -EndAnchor LineEnd |
Select-Object -ExpandProperty Pattern |
should -Be '^\s{0,}$'
}
it 'Can check for -Min and -Max occurances' {
New-RegEx -CharacterClass Whitespace -Min 0 -Max 4 |
Select-Object -ExpandProperty Pattern |
should -Be '\s{0,4}'
}
it 'Can leave a comment' {
New-RegEx -CharacterClass Whitespace -Comment "Whitespace" |
Select-Object -ExpandProperty Pattern |
should -BeLike "\s # Whitespace*"
}
it 'Can write a description' {
New-RegEx -CharacterClass Whitespace -Description "Whitespace" |
Select-Object -ExpandProperty Pattern |
should -Be "# Whitespace$([Environment]::NewLine)\s"
}
it 'Can name a capture' {
New-RegEx -Name Digits -CharacterClass Digit -Repeat |
Select-Object -ExpandProperty Pattern |
should -Be '(?<Digits>\d+)'
}
it 'Can write an expression that will always fail' {
New-RegEx -Not | select -ExpandProperty Pattern | should -Be '(?!)'
}
it 'Can write an anti expression' {
New-RegEx -Not foo | Select-Object -ExpandProperty pattern | should -Be '\A((?!(foo)).)*\Z'
}
it 'Can Be -Atomic' {
New-RegEx -Atomic -Pattern 'do', 'die' -Or | select-object -expand Pattern | should -BeLike '(?>*do*|*die*)'
}
it 'Can Be -Greedy or -Lazy (or both)' {
New-RegEx -Pattern '(.|\s)' -Greedy -Lazy | Select-Object -ExpandProperty Pattern | Should -Be '(.|\s)*?'
}
it "Doesn't have to capture (with -NoCapture)" {
New-RegEx -NoCapture '\d+' |
Select-Object -ExpandProperty Pattern | should -Be '(?:\d+)'
}
it 'Can -Be optional' {
New-RegEx -Pattern do, die -Or -Optional | select-object -expand Pattern | should -BeLike '(?:*do*|*die*)?'
}
it 'Can use Saved Expressions (with the format ?<Name>)' {
New-RegEx ?<Digits> | Select-Object -ExpandProperty Pattern | should -BeLike '*\d+*'
}
it 'Can write conditionals' {
New-RegEx '((?<Digit>\d)|(?<NotDigit>\D))' -If Digit -Then '\D' -Else '\d' |
Use-RegEx -IsMatch 'a1' |
should -Be $true
New-RegEx '(?<Digit>\d)' |
New-RegEx -If Digit -Then '[abcdef]'
}
it 'Can write backreferences' {
New-RegEx -Backreference previousCapture |
Select-Object -ExpandProperty Pattern |
Should -Be '\k<previousCapture>'
$(New-RegEx -Backreference 1).ToString() |
Should -Be '\1'
}
it 'Can refer to other saved captures in a pattern (by putting ?<CaptureName> without leading comments)' {
New-RegEx -Pattern '?<Digits>' |
Use-RegEx -IsMatch -Match 1 |
should -Be true
}
it 'Can rename a saved capture (by putting (?<NewCaptureName>?<OldCaptureName>)' {
New-RegEx -Pattern '(?<MyDigits>?<Digits>)' |
Use-RegEx -Extract -Match 1 |
Select-Object -ExpandProperty MyDigits |
should -Be 1
}
it 'Can match -Until a pattern' {
$writeRegexCmd = $ExecutionContext.SessionState.InvokeCommand.GetCommand('New-RegEx','Function')
New-RegEx -Pattern \<\# |
New-RegEx -Name Block -Until \#> |
New-RegEx -Pattern \#\> |
Use-RegEx -Extract -Match $writeRegexCmd.Definition |
Select-Object -ExpandProperty Block |
should -BeLike *New-RegEx*
}
context '-Between' {
it 'Makes it easy to match double quotes' {
New-RegEx -Between '"' -Name InQuotes |
Use-RegEx -Extract -Match 'this is not in quotes. "This Is \"". This is not in quotes' |
Select-Object -ExpandProperty InQuotes |
should -Be 'This Is \"'
}
it 'Makes it easy to match single quotes' {
New-RegEx -Between "'" -Name InQuotes -EscapeSequence "''" |
Use-RegEx -Extract -Match "this is not in quotes. 'This Is '''. This is not in quotes" |
Select-Object -ExpandProperty InQuotes |
should -Be "This Is ''"
}
it 'Makes it easy to match block comments' {
New-RegEx -Name BlockComment -Between '\<\#', '\#\>' -EscapeSequence '' |
Use-RegEx -Extract -Match @'
1 <#BlockComment#>
2
'@ |
Select-Object -ExpandProperty BlockComment |
should -Be BlockComment
}
}
it 'Can Match a series of Digits up to a -DigitMax' {
$r = New-RegEx -DigitMax 16 -StartAnchor '^' -NotBefore '\d'
$r.Match("16").Success | Should -Be $true
$r.Match("17").Success | Should -Be $false
}
it 'Can refer to a capture generator (parameters can -Be passed with () or {})' {
New-RegEx -Pattern '?<BalancedCode>{(}' |
Use-RegEx -IsMatch -Match '({}' |
should -Be $false
New-RegEx -Pattern '?<BalancedCode>({)' |
Use-RegEx -IsMatch -Match '({}' |
should -Be $true
}
}
describe Export-RegEx {
it 'Can Export a RegEx as a -Variable' {
Export-RegEx -Name Digits -As Variable | should -BeLike '$digits*=*\d+*'
}
it 'Can Export to a Path' {
if ($env:TEMP) {
Export-RegEx -Name Digits -Path $env:TEMP
Get-Content (Join-Path $env:TEMP 'Digits.regex.txt') -raw |
should -BeLike *\d+*
}
}
if (-not $env:Agent_ID -and $PSVersionTable.Platform -ne 'Unix') { # Skipping this test in AzureDev ops due to disk issues
it 'Can Export -As a Script to a Temporary Path' {
if ($env:TEMP) {
$exFile= (Join-Path $env:TEMP Digits.ps1)
Export-RegEx -Name Digits -Path $exFile -As Script
$exFileContent = Get-Content $exFile -Raw
$exFileContent|
should -BeLike '*\d+*'
$exFileContent | should -BeLike '*function UseRegex*'
$exFileContent | should -BeLike '*Set-Alias ?<Digits> UseRegex*'
$exFile | Remove-Item
}
}
it 'Can Export a Temporary Pattern' {
Import-RegEx -Pattern '(?<SomeMoreDigits>\d+)'
Export-RegEx -Name SomeMoreDigits
$createdFile = Get-Module Irregular |
Split-Path |
Join-Path -Path { $_ } -ChildPath RegEx |
Get-ChildItem -Filter SomeMoreDigits.regex.txt
$createdFile.Name | should -Be SomeMoreDigits.regex.txt
$createdFile | Remove-Item
}
}
if ($PSVersionTable.Platform -ne 'Unix') {
it 'Will complain when passed a filepath and multiple names (if -As is file)' {
{
Export-RegEx -Name Digits, OptionalWhitespace -Path "$env:TEMP\DigitsAndWhitespace.regex.txt" -ErrorAction Stop
} | should throw
}
if (-not $env:Agent_ID) {
it 'Can Export a RegEx as a -Script' {
$irregularPath = Get-Module Irregular | Split-Path
$ex = Export-RegEx -Name Digits -As Script
Get-Command Export-RegEx |
Select-Object -ExpandProperty Module|
Remove-Module
. ([ScriptBlock]::Create($ex))
'abc123' |
?<Digits> |
Select-Object -Property *
Import-Module $irregularPath
}
}
}
}
describe 'Expressions' {
$rootDir = $PSScriptRoot
foreach ($file in Get-ChildItem $rootDir -Recurse) {
if ($file.name -notlike '*.regex.input*') {
continue
}
$regexName, $restOfName = $file.name -split '\.regex\.input', 2
$specificInput =
if ($restOfName -match '\.(?<Number>\d+)\.') {
$extension = $restOfName -replace '\.(?<Number>\d+)\.', ''
$matches.Number
} else {
$extension = $restOfName
''
}
$outputFile =
$(foreach ($siblingFile in $file.directory.EnumerateFiles()) {
if ($siblingFile -notlike '*.regex.output*') {
continue
}
$siblingFile = [IO.FileInfo]$siblingFile
$siblingFileName, $restofSiblingFile = $siblingFile.name -split '\.regex\.output', 2
if ($siblingFileName -ne $regexName) { continue }
if ($specificInput -and $siblingFileName -notlike "*.$specificInput.*") { continue }
$siblingFile;break
})
$regexFullName =
if ($file.Directory.Name -ne 'Regex') {
'?<' + $file.Directory.Name + "_" + $regexName + '>'
} else {
"?<$regexName>"
}
$testScriptBlock = ([ScriptBlock]::Create(@"
`$inputFile = '$($file.FullName.Replace("'","''"))'
`$outputFile = '$(if ($outputFile) {$outputFile.FullName.Replace("'","''")})'
`$regexResult = Get-Item -LiteralPath `$inputFile | $regexFullName
`$regexResult | should -Not -Be `$null
"@+ {
if (-not $outputFile) { return }
$outputFileInfo = [IO.FileInfo]$outputFile
$outputFileContent = [IO.File]::ReadAllText($outputFile)
foreach ($item in $regexResult) {
$outputFileContent | should -BeLike "*$($item)*"
}
}))
it "$regexFullName" $testScriptBlock
}
context '?<EmailAddress>' {
it 'Will extract an email and domain' {
?<EmailAddress> -Extract |
% {
$_.Username | should -Be foo
$_.Domain | should -Be bar.com
}
}
it 'Will not match a psuedo-email' {
'psued@oemail' | ?<EmailAddress> |should -Be $null
}
}
context '?<Code_Namespace>' {
it 'Will match a namespace' {
$nsExtract = @'
namespace MyNamespace {
public class foo() {}
}
'@ | ?<Code_Namespace> -Extract
$nsExtract.Content | should -BeLike '{*foo()*}'
$nsExtract.Name | should -Be MyNamespace
}
}
context '?<REST_Variable>' {
it 'Will extract variables in a REST url string' {
$restMatches = ?<REST_Variable> -Match 'https://`$server/[organization]/{project}?api-version=`$apiVersion'
$restMatches[0].Value | should -Be '/`$server'
$restMatches[1].Value | should -Be '/[organization]'
$restMatches[2].Value | should -Be '/{project}'
$restMatches[3].Value | should -Be '?api-version=`$apiVersion'
}
}
}
describe 'Generators' {
context '?<MultilineComment>' {
it 'Will auto-detect comment types' {
Get-Module Irregular |
Split-Path |
Get-ChildItem -Recurse -Filter *.ps1 |
Select-Object -First 1 |
?<MultilineComment> -Count 1 |
should -BeLike '<#*#>'
}
it 'Will extract comments from a function' {
Get-Command New-RegEx |
?<MultilineComment> -Count 1 |
should -BeLike '<#*#>'
}
}
}
describe Set-Regex {
it 'Lets you store Regular Expressions' {
Get-RegEx -Name Digits |
Set-Regex -Confirm:$false
}
if (-not $env:Agent_ID -and $PSVersionTable.Platform -ne 'Unix') {
it 'Lets you declare them temporarily' {
Set-Regex -Name Period -Pattern '\.' -Temporary
Use-RegEx -Pattern '?<Period>' -Match '.' -IsMatch | should -Be true
}
it 'Will infer the name' {
Set-Regex -Pattern '(?<Period>\.)' -Description 'A period' -Temporary
}
it 'Will complain if the pattern was not named' {
{Set-Regex -Pattern blah -Temporary -errorAction Stop} | should throw
}
it 'Can append to a an inline description' {
Set-Regex -Pattern '# a math symbol
(?<MathSymbol>\p{Sm})' -Description 'Using the special character class math' -Temporary
New-RegEx '?<MathSymbol>' | should -BeLike '*\p{Sm}*'
}
it 'Can accept the output of New-RegEx' {
New-RegEx -LiteralCharacter := -Name ColonOrEquals |
Set-Regex
Get-Module Irregular |
Split-Path |
Join-Path -ChildPath 'Regex' |
Join-Path -ChildPath 'ColonOrEquals.regex.txt' |
Remove-Item
}
}
it 'Can set a regex in an arbitrary path' {
if ($env:TEMP) {
Set-RegEx -Pattern '(?<Period>\.)' -Path $env:TEMP -PassThru |
Select-Object -ExpandProperty Name |
should -Be Period.regex.txt
} elseif (Test-Path '/tmp') {
Set-RegEx -Pattern '(?<Period>\.)' -Path '/tmp' -PassThru |
Select-Object -ExpandProperty Name |
should -Be Period.regex.txt
} else {
'No temp directory found'
}
}
}
describe Remove-RegEx {
it 'Can remove a regex' {
Set-RegEx -Name Period -Pattern '\.' -Description "A Period" -Temporary -PassThru
$periodRegex = Get-RegEx -Name Period
Remove-RegEx -Name Period -Confirm:$false
if ($periodRegex.Path) {
Test-Path $periodRegex.Path | Should -Be $false
}
$ExecutionContext.SessionState.InvokeCommand.GetCommand('?<Period>', 'Alias') | Should -Be $null
}
}