Skip to content

Commit b6ca72d

Browse files
committed
Impliment doesn't split tokens across the scopes test
1 parent 9ed6e96 commit b6ca72d

File tree

3 files changed

+95
-11
lines changed

3 files changed

+95
-11
lines changed

tests/pester/Syntax.Tests.ps1

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
$sublimeRoot = "C:\st"
2-
$scopesFile = Join-Path $sublimeRoot "Data\Packages\User\UnitTesting\tokens\PowerShell_tokens"
3-
$testFile = Resolve-Path "$PSScriptRoot\..\samples\test-file.ps1"
42

53
Import-Module "$PSScriptRoot\SyntaxHelper.psm1"
64

@@ -16,11 +14,31 @@ You need to run python tests first to make it work.
1614

1715
Context "test-file.ps1" {
1816

19-
$scopes = cat -Raw $scopesFile | ConvertFrom-Json
20-
$tokens = Get-TokensFromFile $testFile
17+
$scopesFile = Join-Path $sublimeRoot "Data\Packages\User\UnitTesting\tokens\PowerShell_tokens"
18+
$testFile = Resolve-Path "$PSScriptRoot\..\samples\test-file.ps1"
2119

22-
It "doesn't split tokens to scopes" {
23-
20+
# splitted in two lines, because of a bug in Sort-Object
21+
$stScopes = cat -Raw $scopesFile | ConvertFrom-Json; $stScopes = $stScopes | sort -Property @('startOffset', 'endOffset')
22+
# tokens are already sorted
23+
$psScopes = Get-TokensFromFile $testFile | Convert-TokenToScope
24+
25+
It "doesn't split tokens across the scopes" {
26+
$stIndex = 0
27+
$psScopes | %{
28+
while ($stScopes[$stIndex].endOffset -le $_.startOffset) {
29+
$stIndex++
30+
}
31+
#Write-Host "PowerShell scope $_ "
32+
#Write-Host "SublimeText scope $($stScopes[$stIndex])"
33+
34+
if (-not (Test-ScopeInclosure $_ $stScopes[$stIndex])) {
35+
#Write-Warning "PowerShell scope not found in SublimeText scopes $_ "
36+
if (-not (Test-ScopeDisclosure $_ $stScopes[$stIndex])) {
37+
Write-Error "PowerShell scope $_ overlap with SublimeText scope $($stScopes[$stIndex]) "
38+
$false | Should be $true
39+
}
40+
}
41+
}
2442
}
2543
}
2644
}

tests/pester/SyntaxHelper.psm1

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,72 @@ function Get-TokensFromInput
2828

2929
return $tokens
3030
}
31+
32+
function Convert-TokenToScope
33+
{
34+
[CmdletBinding()]
35+
param(
36+
[Parameter(ValueFromPipeline)]
37+
[System.Management.Automation.Language.Token] $token
38+
)
39+
40+
process
41+
{
42+
if (@('NewLine', 'EndOfInput') -contains $token.Kind) {
43+
# ignore these tokens
44+
return
45+
}
46+
47+
$h = @{
48+
text = $token.Text
49+
# Adjust offsets for CRLF
50+
startOffset = $token.Extent.StartOffset - $token.Extent.StartLineNumber + 1
51+
endOffset = $token.Extent.EndOffset - $token.Extent.EndLineNumber + 1
52+
kind = $token.Kind
53+
}
54+
55+
New-Object -TypeName PSObject -Property $h
56+
}
57+
}
58+
59+
function Test-ScopeInclosure
60+
{
61+
[CmdletBinding()]
62+
param(
63+
$inScope,
64+
$outScope
65+
)
66+
67+
if ($inScope.startOffset -lt $outScope.startOffset)
68+
{
69+
return $false
70+
}
71+
72+
if ($inScope.endOffset -gt $outScope.endOffset)
73+
{
74+
return $false
75+
}
76+
77+
return $true
78+
}
79+
80+
function Test-ScopeDisclosure
81+
{
82+
[CmdletBinding()]
83+
param(
84+
$leftScope,
85+
$rightScope
86+
)
87+
88+
if ($leftScope.startOffset -le $rightScope.endOffset)
89+
{
90+
return $true
91+
}
92+
93+
if ($leftScope.endOffset -ge $rightScope.endOffset)
94+
{
95+
return $false
96+
}
97+
98+
return $false
99+
}

tests/py/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ def setUp(self):
3939

4040
class PowerShellSyntaxTokenTest(PowerShellSyntaxTest):
4141

42-
def getTokenHeader(self):
43-
return ['scope_name', 'str', 'start', 'end']
44-
4542
def getTokens(self):
4643
selectors = [
4744
"comment.block",
@@ -79,7 +76,7 @@ def getTokens(self):
7976
"meta.scriptblock",
8077
"punctuation.end.definition.comment.block",
8178
"punctuation.start.definition.comment.block",
82-
"source",
79+
#"source", # this scope represents the whole file
8380
"storage",
8481
"storage.modifier.scope",
8582
"string.quoted.double",
@@ -100,5 +97,5 @@ def getTokens(self):
10097
for selector in selectors:
10198
regions = self.view.find_by_selector(selector)
10299
for region in regions:
103-
tokens += [{ 'scope_name': selector, 'str': self.view.substr(region), 'start': region.a, 'end': region.b }]
100+
tokens += [{ 'kind': selector, 'text': self.view.substr(region), 'startOffset': region.a, 'endOffset': region.b }]
104101
return tokens

0 commit comments

Comments
 (0)