__tests__/PSStringScanner.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
$p = Resolve-Path "$PSScriptRoot\..\PSStringScanner.psd1"

Import-Module $p -Force

Describe "Test match results" {

    It "Should have groups" {
        $scanner = New-PSStringScanner '4.7'

        $scanner.Check('(\d)\.(\d)') | Should Be $true
        $scanner.regexMatch.Groups | Should Not Be Null
        $scanner.regexMatch.Groups.Count | Should Be 3
        $scanner.regexMatch.Groups[1].Value | Should Be 4
        $scanner.regexMatch.Groups[2].Value | Should Be 7

        $matches = $scanner.Matches()
        $matches.Count | Should Be 3
        $matches[1].Value | Should Be 4
        $matches[2].Value | Should Be 7
    }
}
Describe "Test words, whitespace and eos" {
    Context "String being parsed ['This is an example string']" {
        It "Should work" {
            $scanner = New-PSStringScanner 'This is an example string'

            $scanner.EoS()        | Should Be $false
            $scanner.Scan("\w+")  | Should BeExactly 'This'
            $scanner.Scan("\s+")  | Should Be ' '
            $scanner.Scan("\w+")  | Should BeExactly 'is'
            $scanner.EoS()        | Should Be $false
            $scanner.Scan("\s+")  | Should Be ' '
            $scanner.Scan("\w+")  | Should BeExactly 'an'
            $scanner.Scan("\s+")  | Should Be ' '
            $scanner.Scan("\w+")  | Should BeExactly  'example'
            $scanner.Scan("\s+")  | Should Be ' '
            $scanner.Scan("\w+")  | Should BeExactly 'string'
            $scanner.EoS()        | Should Be $true
        }
    }
}

Describe "Test words, whitespace and eos step-by-step" {
    Context "String being parsed ['This is an example string']" {
        $scanner = New-PSStringScanner 'This is an example string'

        It "Verifying not EoS [$($scanner.EoS())]" {
            $scanner.EoS()        | Should Be $false
        }

        It "Next word ['This']" {
            $scanner.Scan("\w+")  | Should BeExactly 'This'
        }

        It "Followed by a space [' ']" {
            $scanner.Scan("\s+")  | Should BeExactly ' '
        }

        It "Next word ['is']" {
            $scanner.Scan("\w+")  | Should BeExactly 'is'
        }

        It "Verifying not EoS [$($scanner.EoS())]" {
            $scanner.EoS()        | Should Be $false
        }

        It "Followed by a space [' ']" {
            $scanner.Scan("\s+")  | Should BeExactly ' '
        }

        It "Next word ['an']" {
            $scanner.Scan("\w+")  | Should BeExactly 'an'
        }

        It "Followed by a space [' ']" {
            $scanner.Scan("\s+")  | Should BeExactly ' '
        }

        It "Next word ['example']" {
            $scanner.Scan("\w+")  | Should BeExactly 'example'
        }

        It "Followed by a space [' ']" {
            $scanner.Scan("\s+")  | Should BeExactly ' '
        }

        It "Next word ['string']" {
            $scanner.Scan("\w+")  | Should BeExactly 'string'
        }

        It "Verifying EoS is [$($scanner.EoS())]" {
            $scanner.EoS()        | Should Be $true
        }
    }
}

Describe "Testing String Scanner with string ['This is an example string']" {
    BeforeAll {
        $script:scanner = New-PSStringScanner 'This is an example string'
    }

    It "First word should be 'This'" {
        $scanner.Scan("\w+") | Should BeExactly "This"
    }

    It "Followed by 'is'" {
        $scanner.Scan("\w+") | Should BeExactly "is"
    }

    It "Followed by 'an'" {
        $scanner.Scan("\w+") | Should BeExactly "an"
    }

    It "Followed by 'example'" {
        $scanner.Scan("\w+") | Should BeExactly "example"
    }

    It "Followed by 'string'" {
        $scanner.Scan("\w+") | Should BeExactly "string"
    }

    It "End Of String should be $($scanner.EOS())" {
        $scanner.EOS() | Should be $true
    }
}

Describe "Test skip method" {
    BeforeAll {
        $script:scanner = New-PSStringScanner 'stra strb strc'
    }

    It "Should return correct length if found" {
        $scanner.Skip("\w+") | Should Be 4
        $scanner.Skip("\s+") | Should Be 1
        $scanner.Skip("\w+") | Should Be 4
        $scanner.Skip("\s+") | Should Be 1
        $scanner.Skip("\w+") | Should Be 4
        $scanner.Skip("\s+") | Should Be $null
        $scanner.Skip("\w+") | Should Be $null
    }
}
Describe "Test check & skip methods" {
    Context "List items being parsed ['Eggs, cheese, onion, potato, peas']" {
        BeforeEach {
            $script:scanner = New-PSStringScanner 'Eggs, cheese, onion, potato, peas'
        }

        It "Should find 'Eggs' first" {
            $actual = $scanner.scan("\w+")
            $actual | Should Be 'Eggs'
        }

        It "Should find 'cheese' next" {
            $null = $scanner.scan("\w+")
            $scanner.Check(',') | Should Be $true
            $scanner.Skip(',\s+')

            $actual = $scanner.scan("\w+")
            $actual | Should BeExactly "cheese"
        }

        It "Should find 'onion' next" {
            $null = $scanner.scan("\w+")
            $null = $scanner.scan("\w+")
            $scanner.Check(',') | Should Be $true
            $scanner.Skip(',\s+')

            $actual = $scanner.scan("\w+")
            $actual | Should BeExactly "onion"
        }

        It "Should find 'potato' next" {
            $null = $scanner.scan("\w+")
            $null = $scanner.scan("\w+")
            $null = $scanner.scan("\w+")
            $scanner.Check(',') | Should Be $true
            $scanner.Skip(',\s+')

            $actual = $scanner.scan("\w+")
            $actual | Should BeExactly "potato"
        }

        It "Should find 'peas' next" {
            $null = $scanner.scan("\w+")
            $null = $scanner.scan("\w+")
            $null = $scanner.scan("\w+")
            $null = $scanner.scan("\w+")
            $scanner.Check(',') | Should Be $true
            $scanner.Skip(',\s+')

            $actual = $scanner.scan("\w+")
            $actual | Should BeExactly "peas"
        }
    }
}

Describe "Test loops on List ['Eggs, cheese, onion, potato, peas']" {

    Context "Generate items With Check() & Skip() methods" {

        $script:scanner = New-PSStringScanner 'Eggs, cheese, onion, potato, peas'

        $actualItems = @()
        while ($true) {
            $actualItems += $scanner.scan("\w+")
            if ($scanner.Check(',')) {
                $scanner.Skip(',\s+')
            }
            else {
                break
            }
        }

        It "Item count should be 5" {
            $actualItems.Count | Should Be 5
        }

        It "Array items ['Eggs, cheese, onion, potato, peas']" {
            $actualItems | Should Be 'Eggs', 'cheese', 'onion', 'potato', 'peas'
        }
    }

    Context "Generate Items with Do {} Until ()" {

        $script:scanner = New-PSStringScanner 'Eggs, cheese, onion, potato, peas'

        $actualItems = do {$scanner.scan("\w+")} until ($scanner.EoS())

        It "Item count should be 5" {
            $actualItems.Count | Should Be 5
        }

        It "Array items ['Eggs, cheese, onion, potato, peas']" {
            $actualItems | Should Be 'Eggs', 'cheese', 'onion', 'potato', 'peas'
        }
    }
}

Describe "Test SkipUntil" {
    BeforeEach {
        $script:scanner = New-PSStringScanner 'Foo Bar Baz'
    }

    It "Check for 'Foo' in string 'Foo Bar Baz' pos [3]" {
        $scanner.SkipUntil("Foo") | Should Be 3
        $scanner.pos              | Should Be 3
    }

    It "Check for 'Bar' in string 'Foo Bar Baz' pos [7]" {
        $scanner.SkipUntil("Bar") | Should Be 7
        $scanner.pos              | Should Be 7
    }

    It "Check for 'Qux' in string 'Foo Bar Baz' pos[`$null] and EoS [`$false]" {
        $scanner.SkipUntil("Qux") | Should Be $null
        $scanner.EoS()            | Should Be $false
    }

    It "Check for '12' in string 'Fri Dec 12 1975 14:39' pos [10]" {
        $script:scanner = New-PSStringScanner 'Fri Dec 12 1975 14:39'
        $scanner.SkipUntil('12') | Should Be 10
    }
}

Describe "Test Terminate" {

    BeforeAll {
        $script:scanner = New-PSStringScanner 'Foo Bar Baz'
    }

    It "Should not be at the EoS" {
        $scanner.EoS() | Should Be $false
    }

    It "Should be at the EoS" {
        $scanner.Terminate()
        $scanner.EoS() | Should Be $true
    }

    It "Should not find Bar" {
        $scanner.Scan("Bar") | Should BeNullOrEmpty
    }
}

Describe "Test CheckUntil" {
    BeforeEach {
        $script:scanner = New-PSStringScanner 'Foo Bar Baz'
    }

    It "Check for 'Foo' in 'Foo Bar Baz'" {
        $scanner.CheckUntil("Foo") | Should Be 'Foo'
        $scanner.pos              | Should Be 0
    }

    It "Check for 'Foo Bar' in 'Foo Bar Baz'" {
        $scanner.CheckUntil("Foo Bar") | Should Be 'Foo Bar'
        $scanner.pos              | Should Be 0
    }

    It "Check for 'Bar' in 'Foo Bar Baz'" {
        $scanner.CheckUntil("Bar") | Should Be "Bar"
        $scanner.pos              | Should Be 0
    }

    It "Check for 'Qux' in 'Foo Bar Baz'" {
        $scanner.CheckUntil("Qux") | Should Be $null
        $scanner.EoS()            | Should Be $false
    }

    It "Check for '12' in 'Fri Dec 12 1975 14:39'" {
        $script:scanner = New-PSStringScanner 'Fri Dec 12 1975 14:39'
        $scanner.CheckUntil("12") | Should Be 12
        $scanner.pos              | Should Be 0
        $scanner.EoS()            | Should Be $false
    }

}

Describe "Test ScanUntil" {
    BeforeEach {
        $script:scanner = New-PSStringScanner 'DougFinke'
    }

    It "Test 'DougFinke'" {
        $scanner.ScanUntil("g") | Should Be "Doug"
        $scanner.pos | Should Be 4
        $scanner.ScanUntil("ke") | Should Be "Finke"
        $scanner.pos | Should Be 9
        $scanner.EoS() | Should Be $true

        # Unicode challenges
        #$scanner.ScanUntil("ä") | Should Be "abcä"
        #$scanner.pos | Should Be 4
        #$scanner.ScanUntil("ö") | Should Be "defö"
        #$scanner.pos | Should Be 8
    }
}

Describe "Test Parse Config" {
    It "parse kvp" {
        $scanner = New-PSStringScanner @"
name = "Alice's website"
description = "Alice's personal blog"
url = "http://alice.example.com/"
public = "true"
version = "24"
"@

        $NAME = '[a-z]+'
        $WHITESPACE = '\s+'
        $QUOTE = '"'

        $kvp = [Ordered]@{}

        do {
            $key = $scanner.Scan($NAME)
            $scanner.Skip($WHITESPACE)
            $scanner.Scan('=')
            $scanner.Skip($WHITESPACE)
            $scanner.Scan($QUOTE)
            $value = $scanner.ScanUntil("(?=$QUOTE)")
            $kvp.$key = $value
            $scanner.Scan($QUOTE)
        } until ($scanner.EoS())

        $kvp.Keys.Count | Should Be 5
        $kvp.Contains("name") | Should Be $true
        $kvp.name | Should BeExactly "Alice's website"
        $kvp.Contains("description") | Should Be $true
        $kvp.description | Should BeExactly "Alice's personal blog"
        $kvp.Contains("url") | Should Be $true
        $kvp.url | Should BeExactly "http://alice.example.com/"
        $kvp.Contains("public") | Should Be $true
        $kvp.public | Should BeExactly "true"
        $kvp.Contains("version") | Should Be $true
        $kvp.version | Should Be 24
    }
}

Describe "Scan 'The quick brown fox jumped over the lazy dog.'" {
    BeforeAll {
        $script:scanner = New-PSStringScanner "The quick brown fox jumped over the lazy dog."
    }

    It "Position should be 0" {
        $scanner.pos         | Should Be 0
    }

    It "Should be 'The'" {
        $scanner.Scan("The") | Should Be "The"
    }

    It "Position should be 3" {
        $scanner.pos         | Should Be 3
    }

    It "Should be empty string" {
        $scanner.Scan("The") | Should BeNullOrEmpty
    }

    It "Position should be 3" {
        $scanner.pos         | Should Be 3
    }
}

Describe "Test Clone() method" {

    BeforeAll {
        $script:scanner = New-PSStringScanner "The quick brown fox jumped over the lazy dog."
    }

    It "Should Clone" {
        $d = $scanner.Clone()

        $d.s   | Should be $scanner.s
        $d.pos | Should Be $scanner.pos
    }

    It "Only the Clone should change" {

        $d = $scanner.Clone()

        $d.s         | Should Be $scanner.s
        $d.pos       | Should Be $scanner.pos

        $actual = $d.Scan("jumped")

        $actual      | Should BeExactly "jumped"
        $d.pos       | Should Be 26
        $scanner.pos | Should Be 0
    }
}

Describe "Test Clone() method" {

    It "Should find countries" {

        $str = @"
Here are the top ten countries by population, as of 2013 when the
world population was 7 billion.
 
China: 1,361,540,000
India: 1,237,510,000
United States: 317,234,000
Indonesia: 237,641,326
Brazil: 201,032,714
Pakistan: 185,028,000
Nigeria: 173,615,000
Bangladesh: 152,518,015
Russia: 143,600,000
Japan: 127,290,000
"@


        $countries = "[a-zA-Z ]+:"

        $actual = $str.Scan($countries)

        $actual.Count | Should Be 10
    }

    It "Should return null or empty" {

        $str = @"
Here are the top ten countries by population, as of 2013 when the
world population was 7 billion.
 
China: 1,361,540,000
India: 1,237,510,000
United States: 317,234,000
Indonesia: 237,641,326
Brazil: 201,032,714
Pakistan: 185,028,000
Nigeria: 173,615,000
Bangladesh: 152,518,015
Russia: 143,600,000
Japan: 127,290,000
"@


        $actual = $str.Scan()
        $actual | Should BeNullOrEmpty
    }
}

Describe "Test Clone() method" {

    It "Should Reset the scan pointer to the beginning" {
        $str = "The quick brown fox jumped over the lazy dog."
        $scanner = New-PSStringScanner $str

        $actual = $scanner.Scan("dog\.")

        $actual      | Should BeExactly "dog."
        $scanner.pos | Should Be $str.Length

        $scanner.Reset()
        $scanner.pos | Should Be 0

        $actual = $scanner.Scan("quick")
        $actual      | Should BeExactly "quick"
        $scanner.pos | Should Be 9
    }
}

Describe "Test Peek" {
    It "Should Peek" {
        $scanner = New-PSStringScanner "test string"

        $scanner.Peek(7)  | Should BeExactly "test st"
        $scanner.Peek(7)  | Should BeExactly "test st"
        $scanner.Scan("test")
        $scanner.Peek(5)  | Should BeExactly " stri"
        $scanner.Peek(10) | Should BeExactly " string"
        $scanner.Scan("string")
        $scanner.Peek(10) | Should BeNullOrEmpty
    }
}

Describe "Test GetCh" {

    It "Should GetCh" {
        $scanner = New-PSStringScanner 'abcde'

        $scanner.GetCh() | Should BeExactly 'a'
        $scanner.GetCh() | Should BeExactly 'b'
        $scanner.GetCh() | Should BeExactly 'c'
        $scanner.GetCh() | Should BeExactly 'd'
        $scanner.GetCh() | Should BeExactly 'e'
        $scanner.GetCh() | Should BeNullOrEmpty
    }

    It "Should GetCh with Scan" {
        $scanner = New-PSStringScanner 'test string'

        $scanner.GetCh() | Should BeExactly 't'
        $scanner.GetCh() | Should BeExactly 'e'
        $scanner.Scan("str")
        $scanner.GetCh() | Should BeExactly 'i'
        $scanner.GetCh() | Should BeExactly 'n'
        $scanner.GetCh() | Should BeExactly 'g'
        $scanner.GetCh() | Should BeNullOrEmpty
    }
}