forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestTreeViewItem.ts
More file actions
164 lines (153 loc) · 5.73 KB
/
testTreeViewItem.ts
File metadata and controls
164 lines (153 loc) · 5.73 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
// tslint:disable:max-classes-per-file
import { ThemeIcon, TreeItem, TreeItemCollapsibleState, Uri } from 'vscode';
import { Commands } from '../../common/constants';
import { getIcon } from '../../common/utils/icons';
import { noop } from '../../common/utils/misc';
import { Icons } from '../common/constants';
import { getTestDataItemType, isSubtestsParent } from '../common/testUtils';
import { TestResult, TestStatus, TestSuite } from '../common/types';
import { TestDataItem, TestDataItemType } from '../types';
function getDefaultCollapsibleState(data: TestDataItem): TreeItemCollapsibleState {
return getTestDataItemType(data) === TestDataItemType.function
? TreeItemCollapsibleState.None
: TreeItemCollapsibleState.Collapsed;
}
/**
* Class that represents a visual node on the
* Test Explorer tree view. Is essentially a wrapper for the underlying
* TestDataItem.
*/
export class TestTreeItem extends TreeItem {
public readonly testType: TestDataItemType;
constructor(
public readonly resource: Uri,
public readonly data: Readonly<TestDataItem>,
collapsibleStatue: TreeItemCollapsibleState = getDefaultCollapsibleState(data)
) {
super(data.name, collapsibleStatue);
this.testType = getTestDataItemType(this.data);
this.setCommand();
}
// @ts-ignore https://devblogs.microsoft.com/typescript/announcing-typescript-4-0-rc/#properties-overridding-accessors-and-vice-versa-is-an-error
public get contextValue(): string {
return this.testType;
}
// @ts-ignore https://devblogs.microsoft.com/typescript/announcing-typescript-4-0-rc/#properties-overridding-accessors-and-vice-versa-is-an-error
public get iconPath(): string | Uri | { light: string | Uri; dark: string | Uri } | ThemeIcon {
if (this.testType === TestDataItemType.workspaceFolder) {
return ThemeIcon.Folder;
}
if (!this.data) {
return '';
}
const status = this.data.status;
switch (status) {
case TestStatus.Error:
case TestStatus.Fail: {
return getIcon(Icons.failed);
}
case TestStatus.Pass: {
return getIcon(Icons.passed);
}
case TestStatus.Discovering:
case TestStatus.Running: {
return getIcon(Icons.discovering);
}
case TestStatus.Idle:
case TestStatus.Unknown: {
return getIcon(Icons.unknown);
}
default: {
return getIcon(Icons.unknown);
}
}
}
// @ts-ignore https://devblogs.microsoft.com/typescript/announcing-typescript-4-0-rc/#properties-overridding-accessors-and-vice-versa-is-an-error
public get tooltip(): string {
if (!this.data || this.testType === TestDataItemType.workspaceFolder) {
return '';
}
const result = this.data as TestResult;
if (
!result.status ||
result.status === TestStatus.Idle ||
result.status === TestStatus.Unknown ||
result.status === TestStatus.Skipped
) {
return '';
}
if (this.testType !== TestDataItemType.function) {
if (result.functionsPassed === undefined) {
return '';
}
if (result.functionsDidNotRun) {
return `${result.functionsFailed} failed, ${result.functionsDidNotRun} not run and ${result.functionsPassed} passed`;
}
return `${result.functionsFailed} failed, ${result.functionsPassed} passed`;
}
switch (this.data.status) {
case TestStatus.Error:
case TestStatus.Fail: {
return `Failed in ${+result.time.toFixed(3)} seconds`;
}
case TestStatus.Pass: {
return `Passed in ${+result.time.toFixed(3)} seconds`;
}
case TestStatus.Discovering:
case TestStatus.Running: {
return 'Loading...';
}
default: {
return '';
}
}
}
/**
* Tooltip for our tree nodes is the test status
*/
public get testStatus(): string {
return this.data.status ? this.data.status : TestStatus.Unknown;
}
private setCommand() {
switch (this.testType) {
case TestDataItemType.file: {
this.command = {
command: Commands.navigateToTestFile,
title: 'Open',
arguments: [this.resource, this.data]
};
break;
}
case TestDataItemType.function: {
this.command = {
command: Commands.navigateToTestFunction,
title: 'Open',
arguments: [this.resource, this.data, false]
};
break;
}
case TestDataItemType.suite: {
if (isSubtestsParent(this.data as TestSuite)) {
this.command = {
command: Commands.navigateToTestFunction,
title: 'Open',
arguments: [this.resource, this.data, false]
};
break;
}
this.command = {
command: Commands.navigateToTestSuite,
title: 'Open',
arguments: [this.resource, this.data, false]
};
break;
}
default: {
noop();
}
}
}
}