Skip to content

Commit 804dc2f

Browse files
committed
testing: better approach to deserializing test items
1 parent 93d5dd8 commit 804dc2f

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

src/vs/workbench/api/common/extHostTestItem.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
*--------------------------------------------------------------------------------------------*/
55
import * as editorRange from 'vs/editor/common/core/range';
66
import { createPrivateApiFor, getPrivateApiFor, IExtHostTestItemApi } from 'vs/workbench/api/common/extHostTestingPrivateApi';
7-
import { TestIdPathParts } from 'vs/workbench/contrib/testing/common/testId';
7+
import { TestId, TestIdPathParts } from 'vs/workbench/contrib/testing/common/testId';
88
import { createTestItemChildren, ExtHostTestItemEvent, ITestChildrenLike, ITestItemApi, ITestItemChildren, TestItemCollection, TestItemEventOp } from 'vs/workbench/contrib/testing/common/testItemCollection';
9-
import { ITestItem } from 'vs/workbench/contrib/testing/common/testTypes';
9+
import { denamespaceTestTag, ITestItem, ITestItemContext } from 'vs/workbench/contrib/testing/common/testTypes';
1010
import type * as vscode from 'vscode';
1111
import * as Convert from 'vs/workbench/api/common/extHostTypeConverters';
12+
import { URI } from 'vs/base/common/uri';
1213

1314
const testItemPropAccessor = <K extends keyof vscode.TestItem>(
1415
api: IExtHostTestItemApi,
@@ -83,6 +84,27 @@ const makePropDescriptors = (api: IExtHostTestItemApi, label: string): { [K in k
8384
})),
8485
});
8586

87+
const toItemFromPlain = (item: ITestItem.Serialized): TestItemImpl => {
88+
const testId = TestId.fromString(item.extId);
89+
const testItem = new TestItemImpl(testId.controllerId, testId.localId, item.label, URI.revive(item.uri) || undefined);
90+
testItem.range = Convert.Range.to(item.range || undefined);
91+
testItem.description = item.description || undefined;
92+
testItem.sortText = item.sortText || undefined;
93+
testItem.tags = item.tags.map(t => Convert.TestTag.to({ id: denamespaceTestTag(t).tagId }));
94+
return testItem;
95+
};
96+
97+
export const toItemFromContext = (context: ITestItemContext): TestItemImpl => {
98+
let node: TestItemImpl | undefined;
99+
for (const test of context.tests) {
100+
const next = toItemFromPlain(test.item);
101+
getPrivateApiFor(next).parent = node;
102+
node = next;
103+
}
104+
105+
return node!;
106+
};
107+
86108
export class TestItemImpl implements vscode.TestItem {
87109
public readonly id!: string;
88110
public readonly uri!: vscode.Uri | undefined;

src/vs/workbench/api/common/extHostTesting.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { generateUuid } from 'vs/base/common/uuid';
1717
import { ExtHostTestingShape, ILocationDto, MainContext, MainThreadTestingShape } from 'vs/workbench/api/common/extHost.protocol';
1818
import { ExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
1919
import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService';
20-
import { ExtHostTestItemCollection, TestItemImpl, TestItemRootImpl } from 'vs/workbench/api/common/extHostTestItem';
20+
import { ExtHostTestItemCollection, TestItemImpl, TestItemRootImpl, toItemFromContext } from 'vs/workbench/api/common/extHostTestItem';
2121
import * as Convert from 'vs/workbench/api/common/extHostTypeConverters';
2222
import { TestRunProfileKind, TestRunRequest } from 'vs/workbench/api/common/extHostTypes';
2323
import { TestId, TestIdPathParts, TestPosition } from 'vs/workbench/contrib/testing/common/testId';
@@ -48,7 +48,7 @@ export class ExtHostTesting implements ExtHostTestingShape {
4848

4949
commands.registerArgumentProcessor({
5050
processArgument: arg =>
51-
arg?.$mid === MarshalledId.TestItemContext ? Convert.TestItem.toItemFromContext(arg) : arg,
51+
arg?.$mid === MarshalledId.TestItemContext ? toItemFromContext(arg) : arg,
5252
});
5353
}
5454

src/vs/workbench/api/common/extHostTypeConverters.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import * as notebooks from 'vs/workbench/contrib/notebook/common/notebookCommon'
3232
import { ICellRange } from 'vs/workbench/contrib/notebook/common/notebookRange';
3333
import * as search from 'vs/workbench/contrib/search/common/search';
3434
import { TestId } from 'vs/workbench/contrib/testing/common/testId';
35-
import { CoverageDetails, denamespaceTestTag, DetailType, ICoveredCount, IFileCoverage, ISerializedTestResults, ITestErrorMessage, ITestItem, ITestItemContext, ITestTag, namespaceTestTag, TestMessageType, TestResultItem } from 'vs/workbench/contrib/testing/common/testTypes';
35+
import { CoverageDetails, denamespaceTestTag, DetailType, ICoveredCount, IFileCoverage, ISerializedTestResults, ITestErrorMessage, ITestItem, ITestTag, namespaceTestTag, TestMessageType, TestResultItem } from 'vs/workbench/contrib/testing/common/testTypes';
3636
import { EditorGroupColumn } from 'vs/workbench/services/editor/common/editorGroupColumn';
3737
import { ACTIVE_GROUP, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';
3838
import type * as vscode from 'vscode';
@@ -1786,25 +1786,6 @@ export namespace TestItem {
17861786
sortText: item.sortText || undefined,
17871787
};
17881788
}
1789-
1790-
export function toItemFromContext(context: ITestItemContext): vscode.TestItem {
1791-
let node: vscode.TestItem | undefined;
1792-
for (const test of context.tests) {
1793-
const next = toPlain(test.item);
1794-
(node as any).children = {
1795-
add: () => { },
1796-
delete: () => { },
1797-
forEach(fn) { fn(next, this); },
1798-
get: id => id === test.item.extId ? test.item : undefined,
1799-
replace: () => { },
1800-
size: 1,
1801-
} as vscode.TestItemCollection;
1802-
(next as any).parent = node;
1803-
node = next;
1804-
}
1805-
1806-
return node!;
1807-
}
18081789
}
18091790

18101791
export namespace TestTag {

0 commit comments

Comments
 (0)