-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathDocumentSymbolTestDiscovery.ts
81 lines (73 loc) · 2.94 KB
/
DocumentSymbolTestDiscovery.ts
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import * as vscode from "vscode";
import { TestClass } from "./TestDiscovery";
import { parseTestsFromSwiftTestListOutput } from "./SPMTestDiscovery";
export function parseTestsFromDocumentSymbols(
target: string,
symbols: vscode.DocumentSymbol[],
uri: vscode.Uri
): TestClass[] {
// Converts a document into the output of `swift test list`.
// This _only_ looks for XCTests.
const locationLookup = new Map<string, vscode.Location | undefined>();
const swiftTestListOutput = symbols
.filter(
symbol =>
symbol.kind === vscode.SymbolKind.Class ||
symbol.kind === vscode.SymbolKind.Namespace
)
.flatMap(symbol => {
const functions = symbol.children
.filter(func => func.kind === vscode.SymbolKind.Method)
.filter(func => func.name.match(/^test.*\(\)/))
.map(func => {
const openBrackets = func.name.indexOf("(");
let funcName = func.name;
if (openBrackets) {
funcName = func.name.slice(0, openBrackets);
}
return {
name: funcName,
location: new vscode.Location(uri, func.range),
};
});
const location =
symbol.kind === vscode.SymbolKind.Class
? new vscode.Location(uri, symbol.range)
: undefined;
locationLookup.set(`${target}.${symbol.name}`, location);
return functions.map(func => {
const testName = `${target}.${symbol.name}/${func.name}`;
locationLookup.set(testName, func.location);
return testName;
});
})
.join("\n");
const tests = parseTestsFromSwiftTestListOutput(swiftTestListOutput);
// The locations for each test case/suite were captured when processing the
// symbols. Annotate the processed TestClasses with their locations.
const annotatedTests = annotateTestsWithLocations(tests, locationLookup);
return annotatedTests;
}
function annotateTestsWithLocations(
tests: TestClass[],
locations: Map<string, vscode.Location | undefined>
): TestClass[] {
return tests.map(test => ({
...test,
location: locations.get(test.id),
children: annotateTestsWithLocations(test.children, locations),
}));
}