Skip to content

Commit 284dc74

Browse files
committed
Merge pull request #67 from vors/token_ut
New unit test framework
2 parents ce4b90e + 0e5d2f1 commit 284dc74

File tree

10 files changed

+483
-68
lines changed

10 files changed

+483
-68
lines changed

Support/PowershellSyntax.tmLanguage

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656
</dict>
5757
</array>
5858
</dict>
59+
<dict>
60+
<key>match</key>
61+
<string>[2-6]&gt;&amp;1|&gt;&gt;|&gt;|&lt;&lt;|&lt;|&gt;|&gt;\||[1-6]&gt;|[1-6]&gt;&gt;</string>
62+
<key>name</key>
63+
<string>keyword.operator.redirection.powershell</string>
64+
</dict>
5965
<dict>
6066
<key>include</key>
6167
<string>#commands</string>
@@ -226,55 +232,55 @@
226232
</dict>
227233
<dict>
228234
<key>match</key>
229-
<string>-([ci]?[lg][te]|eq|ne)</string>
235+
<string>(?&lt;!\w)-([ci]?[lg][te]|eq|ne)</string>
230236
<key>name</key>
231237
<string>keyword.operator.logical.powershell</string>
232238
</dict>
233239
<dict>
234240
<key>match</key>
235-
<string>(?i:[a-z][a-z0-9]+-?[a-z][a-z0-9]+)(?i:\.(?:exe|cmd|bat|ps1))</string>
241+
<string>(?i:([\S&amp;&amp;[^&lt;&gt;"/\|?*]])+)(?i:\.(?i:exe|cmd|bat|ps1))</string>
236242
<key>name</key>
237243
<string>support.function.powershell</string>
238244
</dict>
239245
<dict>
240246
<key>match</key>
241-
<string>(?&lt;!\w)((?i:begin|break|catch|class|continue|data|define|do|dynamicparam|else|elseif|end|exit|filter|finally|for|foreach(?!=-object)|from|if|in|inlinescript|parallel|param|process|return|switch|throw|trap|try|until|using|var|where(?!=-object)|while|workflow)|%|\?)(?!\w)</string>
247+
<string>(?&lt;!\w)((?i:begin|break|catch|class|continue|data|define|do|dynamicparam|else|elseif|end|exit|filter|finally|for|foreach(?!-object)|from|if|in|inlinescript|parallel|param|process|return|switch|throw|trap|try|until|using|var|where(?!=-object)|while|workflow)|%|\?)(?!\w)</string>
242248
<key>name</key>
243249
<string>keyword.control.powershell</string>
244250
</dict>
245251
<dict>
246252
<key>match</key>
247-
<string>-(?i:is(?:not)?|as)\b</string>
253+
<string>(?&lt;!\w)-(?i:is(?:not)?|as)\b</string>
248254
<key>name</key>
249255
<string>keyword.operator.comparison.powershell</string>
250256
</dict>
251257
<dict>
252258
<key>match</key>
253-
<string>-(?i:[ic]?(?:eq|ne|[gl][te]|(?:not)?(?:like|match|contains|in)|replace))(?!\p{L})</string>
259+
<string>(?&lt;!\w)-(?i:[ic]?(?:eq|ne|[gl][te]|(?:not)?(?:like|match|contains|in)|replace))(?!\p{L})</string>
254260
<key>name</key>
255261
<string>keyword.operator.comparison.powershell</string>
256262
</dict>
257263
<dict>
258264
<key>match</key>
259-
<string>-(?i:join|split)(?!\p{L})|!</string>
265+
<string>(?&lt;!\w)-(?i:join|split)(?!\p{L})|!</string>
260266
<key>name</key>
261267
<string>keyword.operator.unary.powershell</string>
262268
</dict>
263269
<dict>
264270
<key>match</key>
265-
<string>-(?i:and|or|not|xor)(?!\p{L})|!</string>
271+
<string>(?&lt;!\w)-(?i:and|or|not|xor)(?!\p{L})|!</string>
266272
<key>name</key>
267273
<string>keyword.operator.logical.powershell</string>
268274
</dict>
269275
<dict>
270276
<key>match</key>
271-
<string>-(?i:band|bor|bnot|bxor)(?!\p{L})</string>
277+
<string>(?&lt;!\w)-(?i:band|bor|bnot|bxor)(?!\p{L})</string>
272278
<key>name</key>
273279
<string>keyword.operator.bitwise.powershell</string>
274280
</dict>
275281
<dict>
276282
<key>match</key>
277-
<string>-(?i:f)(?!\p{L})</string>
283+
<string>(?&lt;!\w)-(?i:f)(?!\p{L})</string>
278284
<key>name</key>
279285
<string>keyword.operator.string-format.powershell</string>
280286
</dict>
@@ -284,12 +290,6 @@
284290
<key>name</key>
285291
<string>keyword.operator.assignment.powershell</string>
286292
</dict>
287-
<dict>
288-
<key>match</key>
289-
<string>[2-6]&gt;&amp;1|&gt;&gt;|&gt;|&lt;&lt;|&lt;|&gt;|&gt;\||[1-6]&gt;|[1-6]&gt;&gt;</string>
290-
<key>name</key>
291-
<string>keyword.operator.redirection.powershell</string>
292-
</dict>
293293
<dict>
294294
<key>match</key>
295295
<string>\|{2}|&amp;{2}|;</string>
@@ -387,12 +387,25 @@
387387
</dict>
388388
<key>commands</key>
389389
<dict>
390-
<key>comment</key>
391-
<string>Verb-Noun pattern:</string>
392-
<key>match</key>
393-
<string>(?:(\p{L}|\d|_|-|\\|\:)*\\)?\b(?i:Add|Approve|Assert|Backup|Block|Checkpoint|Clear|Close|Compare|Complete|Compress|Confirm|Connect|Convert|ConvertFrom|ConvertTo|Copy|Debug|Deny|Disable|Disconnect|Dismount|Edit|Enable|Enter|Exit|Expand|Export|Find|Format|Get|Grant|Group|Hide|Import|Initialize|Install|Invoke|Join|Limit|Lock|Measure|Merge|Mount|Move|New|Open|Optimize|Out|Ping|Pop|Protect|Publish|Push|Read|Receive|Redo|Register|Remove|Rename|Repair|Request|Reset|Resize|Resolve|Restart|Restore|Resume|Revoke|Save|Search|Select|Send|Set|Show|Skip|Split|Start|Step|Stop|Submit|Suspend|Switch|Sync|Test|Trace|Unblock|Undo|Uninstall|Unlock|Unprotect|Unpublish|Unregister|Update|Use|Wait|Watch|Write)\-.+?(?:\.(?:exe|cmd|bat|ps1))?\b</string>
394-
<key>name</key>
395-
<string>support.function.powershell</string>
390+
<key>patterns</key>
391+
<array>
392+
<dict>
393+
<key>comment</key>
394+
<string>Verb-Noun pattern:</string>
395+
<key>match</key>
396+
<string>(?:(\p{L}|\d|_|-|\\|\:)*\\)?\b(?i:Add|Approve|Assert|Backup|Block|Checkpoint|Clear|Close|Compare|Complete|Compress|Confirm|Connect|Convert|ConvertFrom|ConvertTo|Copy|Debug|Deny|Disable|Disconnect|Dismount|Edit|Enable|Enter|Exit|Expand|Export|Find|Format|Get|Grant|Group|Hide|Import|Initialize|Install|Invoke|Join|Limit|Lock|Measure|Merge|Mount|Move|New|Open|Optimize|Out|Ping|Pop|Protect|Publish|Push|Read|Receive|Redo|Register|Remove|Rename|Repair|Request|Reset|Resize|Resolve|Restart|Restore|Resume|Revoke|Save|Search|Select|Send|Set|Show|Skip|Split|Start|Step|Stop|Submit|Suspend|Switch|Sync|Test|Trace|Unblock|Undo|Uninstall|Unlock|Unprotect|Unpublish|Unregister|Update|Use|Wait|Watch|Write)\-.+?(?:\.(?i:exe|cmd|bat|ps1))?\b</string>
397+
<key>name</key>
398+
<string>support.function.powershell</string>
399+
</dict>
400+
<dict>
401+
<key>comment</key>
402+
<string>Builtin cmdlets with reserved verbs</string>
403+
<key>match</key>
404+
<string>(?&lt;!\w)(?i:foreach-object)(?!\w)</string>
405+
<key>name</key>
406+
<string>support.function.powershell</string>
407+
</dict>
408+
</array>
396409
</dict>
397410
<key>commentEmbeddedDocs</key>
398411
<dict>
@@ -488,7 +501,7 @@
488501
<key>function</key>
489502
<dict>
490503
<key>begin</key>
491-
<string>(function|configuration)\s+((?:\p{L}|\d|_|-)+)</string>
504+
<string>(?i:function|configuration)\s+((?:\p{L}|\d|_|-)+)</string>
492505
<key>beginCaptures</key>
493506
<dict>
494507
<key>0</key>

appveyor.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
install:
2-
# TODO: Use Desired state configuration or a block script.
3-
- ps: write-output "installing sublime text 3"
2+
- ps: write-host -ForegroundColor Yellow "installing sublime text 3"
43
- ps: start-filedownload "http://c758482.r82.cf2.rackcdn.com/Sublime%20Text%20Build%203059%20x64.zip"
5-
- ps: write-output "installing Sublime%20Text%20Build%203059%20x64.zip"
4+
- ps: write-host -ForegroundColor Yellow "installing Sublime%20Text%20Build%203059%20x64.zip"
65
- ps: 7z.exe x "Sublime%20Text%20Build%203059%20x64.zip" -o"C:\st" > $null
76
- ps: mkdir "C:\st\Data\Packages" -force > $null
87
- ps: mkdir "C:\st\Data\Packages\PowerShell" -force > $null
98
- ps: cp -Recurse * "C:\st\Data\Packages\PowerShell" -Force
10-
- ps: git clone -q --branch=master https://github.com/my-personal-forks/UnitTesting.git "C:\st\Data\Packages\UnitTesting"
9+
- ps: git clone -q --branch=master https://github.com/randy3k/UnitTesting.git "C:\st\Data\Packages\UnitTesting"
10+
- ps: write-host -ForegroundColor Yellow "installing pester"
11+
- ps: cinst pester
1112

1213
build: false
1314

1415
test_script:
15-
- ps: C:\st\Data\Packages\UnitTesting\sbin\run.ps1 "PowerShell"
16+
- ps: write-host -ForegroundColor Yellow "Run python tests"
17+
- ps: C:\st\Data\Packages\UnitTesting\sbin\run.ps1 "PowerShell"
18+
- ps: write-host -ForegroundColor Yellow "Run pester tests"
19+
- ps: $res = Invoke-Pester -OutputFormat NUnitXml -OutputFile TestsResults.xml -PassThru
20+
- ps: (New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path .\TestsResults.xml))
21+
- ps: if ($res.FailedCount -gt 0) { throw "$($res.FailedCount) tests failed."}

tests/__init__.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

tests/pester/Syntax.Tests.ps1

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
$sublimeRoot = "C:\st"
2+
3+
Import-Module "$PSScriptRoot\SyntaxHelper.psm1"
4+
5+
Describe "Syntax highlighting" {
6+
7+
Context "test-file.ps1" {
8+
9+
$tokensFolder = Join-Path $sublimeRoot "Data\Packages\User\UnitTesting\tokens"
10+
$scopesFile = Join-Path $tokensFolder "test-file.ps1.tokens"
11+
12+
$testFile = Resolve-Path "$PSScriptRoot\..\samples\test-file.ps1"
13+
14+
if (-not (Test-Path $scopesFile)) {
15+
Write-Warning 'This Pester UT suite consumes scopes generated by python unit test Test_TokenGenerator. You need to run python tests first.'
16+
return
17+
}
18+
19+
# splitted in two lines, because of a bug in Sort-Object
20+
$stScopes = cat -Raw $scopesFile | ConvertFrom-Json; $stScopes = $stScopes | sort -Property @('startOffset', 'endOffset')
21+
# tokens are already sorted
22+
$psScopes = Get-TokensFromFile $testFile | Convert-TokenToScope
23+
24+
It "split tokens across the scopes 0 times" {
25+
$stIndex = 0
26+
$errorCounter = 0
27+
28+
$psScopes | %{
29+
while ($stScopes[$stIndex].endOffset -le $_.startOffset) {
30+
$stIndex++
31+
if ($stIndex -ge $stScopes.Length) {
32+
break
33+
}
34+
}
35+
36+
$stScope = $stScopes[$stIndex]
37+
$psScope = $_
38+
39+
#Write-Host "PowerShell scope $psScope"
40+
#Write-Host "SublimeText scope $stScope"
41+
42+
if (-not (Test-ScopeInclosure $psScope $stScope)) {
43+
#Write-Host "PowerShell scope $psScope not found in SublimeText scopes"
44+
if (-not (Test-ScopeDisclosure $psScope $stScope)) {
45+
$ignore = $false
46+
47+
# These are minor things
48+
if (@('$', '${', '-', '<#', '($') -contains $stScope.Text) {
49+
$ignore = $true
50+
}
51+
52+
# TODO: These are bugs
53+
if (@('Number', 'Redirection') -contains $psScope.Kind) {
54+
$ignore = $true
55+
}
56+
57+
if (-not $ignore) {
58+
Write-Warning "PowerShell scope $psScope overlap with SublimeText scope $stScope"
59+
$errorCounter++
60+
}
61+
}
62+
}
63+
}
64+
# TODO: These are bugs, make it 0
65+
$errorCounter | Should be @(0..2)
66+
}
67+
68+
It "produces same tokens for lower case" {
69+
$stOtherScopes = Get-SublimeScopesFromFile (Join-Path $tokensFolder "test-file.ps1.lower.tokens" )
70+
Write-Host -ForegroundColor Cyan "Scopes count = $($stScopes.Length) "
71+
Write-Host -ForegroundColor Cyan "Lower scopes count = $($stOtherScopes.Length) "
72+
73+
$n = @($stScopes.Length, $stOtherScopes.Length) | Get-Max
74+
0..($n-1) | % {
75+
if (-not (Test-ScopesEqual $stScopes[$_] $stOtherScopes[$_])) {
76+
#Write-Warning "Scopes are different $($stScopes[$_]) $($stOtherScopes[$_])"
77+
$stScopes[$_] | Should be $stOtherScopes[$_]
78+
}
79+
}
80+
}
81+
82+
It "produces same tokens for upper case" {
83+
$stOtherScopes = Get-SublimeScopesFromFile (Join-Path $tokensFolder "test-file.ps1.upper.tokens" )
84+
Write-Host -ForegroundColor Cyan "Scopes count = $($stScopes.Length) "
85+
Write-Host -ForegroundColor Cyan "Upper scopes count = $($stOtherScopes.Length) "
86+
87+
$n = @($stScopes.Length, $stOtherScopes.Length) | Get-Max
88+
0..($n-1) | % {
89+
if (-not (Test-ScopesEqual $stScopes[$_] $stOtherScopes[$_])) {
90+
#Write-Warning "Scopes are different $($stScopes[$_]) $($stOtherScopes[$_])"
91+
$stScopes[$_] | Should be $stOtherScopes[$_]
92+
}
93+
}
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)