|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { |
| 7 | + Event, EventEmitter, |
| 8 | + ProviderResult, TreeDataProvider |
| 9 | +} from 'vscode'; |
| 10 | +import { |
| 11 | + PythonTestTreeItem, |
| 12 | + PythonTestTreeItemType |
| 13 | +} from './testTreeViewItem'; |
| 14 | + |
| 15 | +export class PythonTestTreeViewProvider implements TreeDataProvider<PythonTestTreeItem> { |
| 16 | + /** |
| 17 | + * This will trigger the view to update the changed element/root and its children recursively (if shown). |
| 18 | + * To signal that root has changed, do not pass any argument or pass `undefined` or `null`. |
| 19 | + */ |
| 20 | + public readonly onDidChangeTreeData: Event<PythonTestTreeItem | undefined>; |
| 21 | + |
| 22 | + private _onDidChangeTreeData: EventEmitter<PythonTestTreeItem | undefined> = new EventEmitter<PythonTestTreeItem | undefined>(); |
| 23 | + private root: PythonTestTreeItem[]; |
| 24 | + |
| 25 | + constructor() { |
| 26 | + this.onDidChangeTreeData = this._onDidChangeTreeData.event; |
| 27 | + // set up some dummy data to just show that the test explorer loads. |
| 28 | + this.root = this.getTestTree(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Get [TreeItem](#TreeItem) representation of the `element` |
| 33 | + * |
| 34 | + * @param element The element for which [TreeItem](#TreeItem) representation is asked for. |
| 35 | + * @return [TreeItem](#TreeItem) representation of the element |
| 36 | + */ |
| 37 | + public async getTreeItem(element: PythonTestTreeItem): Promise<PythonTestTreeItem> { |
| 38 | + return element; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Get the children of `element` or root if no element is passed. |
| 43 | + * |
| 44 | + * @param element The element from which the provider gets children. Can be `undefined`. |
| 45 | + * @return Children of `element` or root if no element is passed. |
| 46 | + */ |
| 47 | + public getChildren(element?: PythonTestTreeItem): ProviderResult<PythonTestTreeItem[]> { |
| 48 | + if (element === undefined) { |
| 49 | + return this.root; |
| 50 | + } |
| 51 | + return element.children; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Optional method to return the parent of `element`. |
| 56 | + * Return `null` or `undefined` if `element` is a child of root. |
| 57 | + * |
| 58 | + * **NOTE:** This method should be implemented in order to access [reveal](#TreeView.reveal) API. |
| 59 | + * |
| 60 | + * @param element The element for which the parent has to be returned. |
| 61 | + * @return Parent of `element`. |
| 62 | + */ |
| 63 | + public getParent?(element: PythonTestTreeItem): ProviderResult<PythonTestTreeItem> { |
| 64 | + return element.parent; |
| 65 | + } |
| 66 | + |
| 67 | + private getTestTree(): PythonTestTreeItem[] { |
| 68 | + // create a sample tree just to get the feature up and running |
| 69 | + const roots: PythonTestTreeItem[] = []; |
| 70 | + const root1: PythonTestTreeItem = new PythonTestTreeItem(PythonTestTreeItemType.Root, undefined, [], '/test', '/test'); |
| 71 | + roots.push(root1); |
| 72 | + |
| 73 | + const root1_pkg1: PythonTestTreeItem = new PythonTestTreeItem(PythonTestTreeItemType.Package, root1, [], '/test/module1', 'module1'); |
| 74 | + root1.children.push(root1_pkg1); |
| 75 | + |
| 76 | + const root1_pkg1_file1: PythonTestTreeItem = new PythonTestTreeItem(PythonTestTreeItemType.File, root1_pkg1, [], '/test/module1/test_file1.py', 'test_file1.py'); |
| 77 | + root1_pkg1.children.push(root1_pkg1_file1); |
| 78 | + |
| 79 | + const root1_pkg1_file1_fn1: PythonTestTreeItem = new PythonTestTreeItem(PythonTestTreeItemType.Function, root1_pkg1_file1, undefined, '/test/module1/test_file1.py::test_function_1', 'test_function_1'); |
| 80 | + root1_pkg1_file1.children.push(root1_pkg1_file1_fn1); |
| 81 | + |
| 82 | + const root1_pkg1_file1_fn2: PythonTestTreeItem = new PythonTestTreeItem(PythonTestTreeItemType.Function, root1_pkg1_file1, undefined, '/test/module1/test_file1.py::test_function_2', 'test_function_2'); |
| 83 | + root1_pkg1_file1.children.push(root1_pkg1_file1_fn2); |
| 84 | + |
| 85 | + const root1_pkg1_file1_suite1: PythonTestTreeItem = new PythonTestTreeItem(PythonTestTreeItemType.Suite, root1_pkg1_file1, [], '/test/module1/test_file1.py::TestSuite1', 'TestSuite1'); |
| 86 | + root1_pkg1_file1.children.push(root1_pkg1_file1_suite1); |
| 87 | + |
| 88 | + const root1_pkg1_file1_suite1_fn1: PythonTestTreeItem = new PythonTestTreeItem(PythonTestTreeItemType.Function, root1_pkg1_file1_suite1, undefined, '/test/module1/test_file1.py::TestSuite1::test_suite1_fn1', 'test_suite1_fn1'); |
| 89 | + root1_pkg1_file1_suite1.children.push(root1_pkg1_file1_suite1_fn1); |
| 90 | + |
| 91 | + const root1_pkg1_file1_suite1_fn2: PythonTestTreeItem = new PythonTestTreeItem(PythonTestTreeItemType.Function, root1_pkg1_file1_suite1, undefined, '/test/module1/test_file1.py::TestSuite1::test_suite1_fn2', 'test_suite1_fn2'); |
| 92 | + root1_pkg1_file1_suite1.children.push(root1_pkg1_file1_suite1_fn2); |
| 93 | + |
| 94 | + const root1_pkg1_file1_suite2: PythonTestTreeItem = new PythonTestTreeItem(PythonTestTreeItemType.Suite, root1_pkg1_file1, [], '/test/module1/test_file1.py::TestSuite2', 'TestSuite2'); |
| 95 | + root1_pkg1_file1.children.push(root1_pkg1_file1_suite2); |
| 96 | + |
| 97 | + const root1_pkg1_file1_suite2_fn1: PythonTestTreeItem = new PythonTestTreeItem(PythonTestTreeItemType.Function, root1_pkg1_file1_suite2, undefined, '/test/module1/test_file1.py::TestSuite2::test_suite2_fn1', 'test_suite2_fn1'); |
| 98 | + root1_pkg1_file1_suite2.children.push(root1_pkg1_file1_suite2_fn1); |
| 99 | + |
| 100 | + const root1_pkg1_file1_suite2_fn2: PythonTestTreeItem = new PythonTestTreeItem(PythonTestTreeItemType.Function, root1_pkg1_file1_suite2, undefined, '/test/module1/test_file1.py::TestSuite2::test_suite2_fn2', 'test_suite2_fn2'); |
| 101 | + root1_pkg1_file1_suite2.children.push(root1_pkg1_file1_suite2_fn2); |
| 102 | + |
| 103 | + return roots; |
| 104 | + } |
| 105 | +} |
0 commit comments