Skip to content

Commit 51b9e4e

Browse files
committed
(GH-1336) Add integration tests for the Folding Provider
Previously there were no tests to verify the folding provider. Due to the provider depending on 3rd party libraries (vscode-textmate and PowerShell grammar file) these tests will provide a degree of detection if breaking changes occur.
1 parent b8e3d8c commit 51b9e4e

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

test/features/folding.test.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as assert from "assert";
2+
import * as path from "path";
3+
import * as vscode from "vscode";
4+
import { DocumentSelector } from "vscode-languageclient";
5+
import * as folding from "../../src/features/Folding";
6+
import { MockLogger } from "../test_utils";
7+
8+
const fixturePath = path.join(__dirname, "..", "..", "..", "test", "fixtures");
9+
10+
function assertFoldingRegions(result, expected): void {
11+
assert.equal(result.length, expected.length);
12+
13+
for (let i = 0; i < expected.length; i++) {
14+
const failMessage = `expected ${JSON.stringify(expected[i])}, actual ${JSON.stringify(result[i])}`;
15+
assert.equal(result[i].start, expected[i].start, failMessage);
16+
assert.equal(result[i].end, expected[i].end, failMessage);
17+
assert.equal(result[i].kind, expected[i].kind, failMessage);
18+
}
19+
}
20+
21+
suite("Features", () => {
22+
23+
suite("Folding Provider", () => {
24+
const logger: MockLogger = new MockLogger();
25+
const mockSelector: DocumentSelector = [
26+
{ language: "powershell", scheme: "file" },
27+
];
28+
const psGrammar = (new folding.FoldingFeature(logger, mockSelector)).grammar(logger);
29+
const provider = (new folding.FoldingProvider(psGrammar));
30+
31+
test("Can detect the PowerShell Grammar", () => {
32+
assert.notEqual(psGrammar, null);
33+
});
34+
35+
test("Can detect all of the foldable regions in a document", async () => {
36+
// Integration test against the test fixture 'folding.ps1' that contains
37+
// all of the different types of folding available
38+
const uri = vscode.Uri.file(path.join(fixturePath, "folding.ps1"));
39+
const document = await vscode.workspace.openTextDocument(uri);
40+
const result = await provider.provideFoldingRanges(document, null, null);
41+
42+
const expected = [
43+
{ start: 1, end: 6, kind: 1 },
44+
{ start: 7, end: 46, kind: 3 },
45+
{ start: 8, end: 13, kind: 1 },
46+
{ start: 14, end: 17, kind: 3 },
47+
{ start: 21, end: 23, kind: 1 },
48+
{ start: 25, end: 35, kind: 3 },
49+
{ start: 27, end: 31, kind: 3 },
50+
{ start: 37, end: 39, kind: 3 },
51+
{ start: 42, end: 45, kind: 3 },
52+
];
53+
54+
assertFoldingRegions(result, expected);
55+
});
56+
});
57+
});

test/fixtures/folding.ps1

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function short-func {};
2+
<#
3+
.SYNOPSIS
4+
Displays a list of WMI Classes based upon a search criteria
5+
.EXAMPLE
6+
Get-WmiClasses -class disk -ns rootcimv2"
7+
#>
8+
function New-VSCodeCannotFold {
9+
<#
10+
.SYNOPSIS
11+
Displays a list of WMI Classes based upon a search criteria
12+
.EXAMPLE
13+
Get-WmiClasses -class disk -ns rootcimv2"
14+
#>
15+
$I = @'
16+
cannot fold
17+
18+
'@
19+
20+
# this won't be folded
21+
22+
# This should be foldable
23+
# This should be foldable
24+
# This should be foldable
25+
26+
#region This fools the indentation folding.
27+
Write-Host "Hello"
28+
# region
29+
Write-Host "Hello"
30+
# comment1
31+
Write-Host "Hello"
32+
#endregion
33+
Write-Host "Hello"
34+
# comment2
35+
Write-Host "Hello"
36+
# endregion
37+
38+
$c = {
39+
Write-Host "Hello"
40+
}
41+
42+
# Array fools indentation folding
43+
$d = @(
44+
'element1',
45+
'elemet2'
46+
)
47+
}

test/test_utils.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Logger, LogLevel } from "../src/logging";
2+
3+
export class MockLogger extends Logger {
4+
// Note - This is not a true mock as the constructor is inherited and causes errors due to trying load
5+
// the "PowerShell Extension Logs" multiple times. Ideally logging should be via an interface and then
6+
// we can mock correctly.
7+
8+
public dispose() { return undefined; }
9+
10+
public getLogFilePath(baseName: string): string { return "mock"; }
11+
12+
public writeAtLevel(logLevel: LogLevel, message: string, ...additionalMessages: string[]) { return undefined; }
13+
14+
public write(message: string, ...additionalMessages: string[]) { return undefined; }
15+
16+
public writeDiagnostic(message: string, ...additionalMessages: string[]) { return undefined; }
17+
18+
public writeVerbose(message: string, ...additionalMessages: string[]) { return undefined; }
19+
20+
public writeWarning(message: string, ...additionalMessages: string[]) { return undefined; }
21+
22+
public writeAndShowWarning(message: string, ...additionalMessages: string[]) { return undefined; }
23+
24+
public writeError(message: string, ...additionalMessages: string[]) { return undefined; }
25+
26+
public writeAndShowError(message: string, ...additionalMessages: string[]) { return undefined; }
27+
28+
public startNewLog(minimumLogLevel: string = "Normal") { return undefined; }
29+
}

0 commit comments

Comments
 (0)