|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// tslint:disable:no-any |
| 7 | + |
| 8 | +import { assert, expect } from 'chai'; |
| 9 | +import { mock } from 'ts-mockito'; |
| 10 | +import * as typemoq from 'typemoq'; |
| 11 | +import { DocumentSymbolProvider, EventEmitter, Uri } from 'vscode'; |
| 12 | +import { IWorkspaceService } from '../../../client/common/application/types'; |
| 13 | +import { IFileSystem } from '../../../client/common/platform/types'; |
| 14 | +import { IServiceContainer } from '../../../client/ioc/types'; |
| 15 | +import { LanguageServerSymbolProvider } from '../../../client/providers/symbolProvider'; |
| 16 | +import { TestFileCodeLensProvider } from '../../../client/testing/codeLenses/testFiles'; |
| 17 | +import { ITestCollectionStorageService } from '../../../client/testing/common/types'; |
| 18 | + |
| 19 | +// tslint:disable-next-line: max-func-body-length |
| 20 | +suite('Code lenses - Test files', () => { |
| 21 | + let testCollectionStorage: typemoq.IMock<ITestCollectionStorageService>; |
| 22 | + let workspaceService: typemoq.IMock<IWorkspaceService>; |
| 23 | + let fileSystem: typemoq.IMock<IFileSystem>; |
| 24 | + let serviceContainer: typemoq.IMock<IServiceContainer>; |
| 25 | + let symbolProvider: DocumentSymbolProvider; |
| 26 | + let onDidChange: EventEmitter<void>; |
| 27 | + let codeLensProvider: TestFileCodeLensProvider; |
| 28 | + setup(() => { |
| 29 | + workspaceService = typemoq.Mock.ofType<IWorkspaceService>(); |
| 30 | + fileSystem = typemoq.Mock.ofType<IFileSystem>(); |
| 31 | + testCollectionStorage = typemoq.Mock.ofType<ITestCollectionStorageService>(); |
| 32 | + serviceContainer = typemoq.Mock.ofType<IServiceContainer>(); |
| 33 | + symbolProvider = mock(LanguageServerSymbolProvider); |
| 34 | + onDidChange = new EventEmitter<void>(); |
| 35 | + serviceContainer |
| 36 | + .setup(c => c.get(typemoq.It.isValue(IWorkspaceService))) |
| 37 | + .returns(() => workspaceService.object); |
| 38 | + serviceContainer |
| 39 | + .setup(c => c.get(typemoq.It.isValue(IFileSystem))) |
| 40 | + .returns(() => fileSystem.object); |
| 41 | + codeLensProvider = new TestFileCodeLensProvider(onDidChange, symbolProvider, testCollectionStorage.object, serviceContainer.object); |
| 42 | + }); |
| 43 | + |
| 44 | + teardown(() => { |
| 45 | + onDidChange.dispose(); |
| 46 | + }); |
| 47 | + |
| 48 | + test('Function getTestFileWhichNeedsCodeLens() returns `undefined` if there are no workspace corresponding to document', async () => { |
| 49 | + const document = { |
| 50 | + uri: Uri.file('path/to/document') |
| 51 | + }; |
| 52 | + workspaceService |
| 53 | + .setup(w => w.getWorkspaceFolder(document.uri)) |
| 54 | + .returns(() => undefined) |
| 55 | + .verifiable(typemoq.Times.once()); |
| 56 | + testCollectionStorage |
| 57 | + .setup(w => w.getTests(typemoq.It.isAny())) |
| 58 | + .returns(() => undefined) |
| 59 | + .verifiable(typemoq.Times.never()); |
| 60 | + const files = codeLensProvider.getTestFileWhichNeedsCodeLens(document as any); |
| 61 | + expect(files).to.equal(undefined, 'No files should be returned'); |
| 62 | + workspaceService.verifyAll(); |
| 63 | + testCollectionStorage.verifyAll(); |
| 64 | + }); |
| 65 | + |
| 66 | + test('Function getTestFileWhichNeedsCodeLens() returns `undefined` if test storage is empty', async () => { |
| 67 | + const document = { |
| 68 | + uri: Uri.file('path/to/document') |
| 69 | + }; |
| 70 | + const workspaceUri = Uri.file('path/to/workspace'); |
| 71 | + const workspace = { uri: workspaceUri }; |
| 72 | + workspaceService |
| 73 | + .setup(w => w.getWorkspaceFolder(document.uri)) |
| 74 | + .returns(() => workspace as any) |
| 75 | + .verifiable(typemoq.Times.once()); |
| 76 | + testCollectionStorage |
| 77 | + .setup(w => w.getTests(workspaceUri)) |
| 78 | + .returns(() => undefined) |
| 79 | + .verifiable(typemoq.Times.once()); |
| 80 | + const files = codeLensProvider.getTestFileWhichNeedsCodeLens(document as any); |
| 81 | + expect(files).to.equal(undefined, 'No files should be returned'); |
| 82 | + workspaceService.verifyAll(); |
| 83 | + testCollectionStorage.verifyAll(); |
| 84 | + }); |
| 85 | + |
| 86 | + test('Function getTestFileWhichNeedsCodeLens() returns `undefined` if tests returned from storage does not contain document', async () => { |
| 87 | + const document = { |
| 88 | + uri: Uri.file('path/to/document5') |
| 89 | + }; |
| 90 | + const workspaceUri = Uri.file('path/to/workspace'); |
| 91 | + const workspace = { uri: workspaceUri }; |
| 92 | + const tests = { |
| 93 | + testFiles: [ |
| 94 | + { |
| 95 | + fullPath: 'path/to/document1' |
| 96 | + }, |
| 97 | + { |
| 98 | + fullPath: 'path/to/document2' |
| 99 | + } |
| 100 | + ] |
| 101 | + }; |
| 102 | + workspaceService |
| 103 | + .setup(w => w.getWorkspaceFolder(document.uri)) |
| 104 | + .returns(() => workspace as any) |
| 105 | + .verifiable(typemoq.Times.once()); |
| 106 | + testCollectionStorage |
| 107 | + .setup(w => w.getTests(workspaceUri)) |
| 108 | + .returns(() => tests as any) |
| 109 | + .verifiable(typemoq.Times.once()); |
| 110 | + fileSystem |
| 111 | + .setup(f => f.arePathsSame('path/to/document1', 'path/to/document5')) |
| 112 | + .returns(() => false); |
| 113 | + fileSystem |
| 114 | + .setup(f => f.arePathsSame('path/to/document2', 'path/to/document5')) |
| 115 | + .returns(() => false); |
| 116 | + const files = codeLensProvider.getTestFileWhichNeedsCodeLens(document as any); |
| 117 | + expect(files).to.equal(undefined, 'No files should be returned'); |
| 118 | + workspaceService.verifyAll(); |
| 119 | + testCollectionStorage.verifyAll(); |
| 120 | + }); |
| 121 | + |
| 122 | + test('Function getTestFileWhichNeedsCodeLens() returns test file if tests returned from storage contains document', async () => { |
| 123 | + const document = { |
| 124 | + uri: Uri.file('path/to/document2') |
| 125 | + }; |
| 126 | + const workspaceUri = Uri.file('path/to/workspace'); |
| 127 | + const workspace = { uri: workspaceUri }; |
| 128 | + const testFile2 = { |
| 129 | + fullPath: 'path/to/document2' |
| 130 | + }; |
| 131 | + const tests = { |
| 132 | + testFiles: [ |
| 133 | + { |
| 134 | + fullPath: 'path/to/document1' |
| 135 | + }, |
| 136 | + testFile2 |
| 137 | + ] |
| 138 | + }; |
| 139 | + workspaceService |
| 140 | + .setup(w => w.getWorkspaceFolder(document.uri)) |
| 141 | + .returns(() => workspace as any) |
| 142 | + .verifiable(typemoq.Times.once()); |
| 143 | + testCollectionStorage |
| 144 | + .setup(w => w.getTests(workspaceUri)) |
| 145 | + .returns(() => tests as any) |
| 146 | + .verifiable(typemoq.Times.once()); |
| 147 | + fileSystem |
| 148 | + .setup(f => f.arePathsSame('path/to/document1', 'path/to/document2')) |
| 149 | + .returns(() => false); |
| 150 | + fileSystem |
| 151 | + .setup(f => f.arePathsSame('path/to/document2', 'path/to/document2')) |
| 152 | + .returns(() => true); |
| 153 | + const files = codeLensProvider.getTestFileWhichNeedsCodeLens(document as any); |
| 154 | + assert.deepEqual(files, testFile2 as any); |
| 155 | + workspaceService.verifyAll(); |
| 156 | + testCollectionStorage.verifyAll(); |
| 157 | + }); |
| 158 | +}); |
0 commit comments