diff --git a/src/vs/editor/contrib/smartSelect/test/common/tokenSelectionSupport.test.ts b/src/vs/editor/contrib/smartSelect/test/common/tokenSelectionSupport.test.ts index 5027d74874066..b685bc6eb831b 100644 --- a/src/vs/editor/contrib/smartSelect/test/common/tokenSelectionSupport.test.ts +++ b/src/vs/editor/contrib/smartSelect/test/common/tokenSelectionSupport.test.ts @@ -5,11 +5,12 @@ 'use strict'; import * as assert from 'assert'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import URI from 'vs/base/common/uri'; import {Range} from 'vs/editor/common/core/range'; import {IMode, IndentAction} from 'vs/editor/common/modes'; import {TokenSelectionSupport} from 'vs/editor/contrib/smartSelect/common/tokenSelectionSupport'; -import {createMockModelService} from 'vs/editor/test/common/servicesTestUtils'; +import {createMockModelService} from 'vs/test/utils/servicesTestUtils'; import {MockTokenizingMode} from 'vs/editor/test/common/mocks/mockMode'; import {LanguageConfigurationRegistry} from 'vs/editor/common/modes/languageConfigurationRegistry'; @@ -59,10 +60,15 @@ class MockJSMode extends MockTokenizingMode { suite('TokenSelectionSupport', () => { - let modelService = createMockModelService(); - let tokenSelectionSupport = new TokenSelectionSupport(modelService); + let modelService; + let tokenSelectionSupport; let _mode: IMode = new MockJSMode(); + setup(() => { + modelService= createMockModelService(new TestInstantiationService()); + tokenSelectionSupport = new TokenSelectionSupport(modelService); + }); + function assertGetRangesToPosition(text:string[], lineNumber:number, column:number, ranges:Range[]): void { let uri = URI.file('test.js'); modelService.createModel(text.join('\n'), _mode, uri); diff --git a/src/vs/editor/test/common/modes/modesRegistry.test.ts b/src/vs/editor/test/common/modes/modesRegistry.test.ts index 6c036b7f52b29..e18da23120443 100644 --- a/src/vs/editor/test/common/modes/modesRegistry.test.ts +++ b/src/vs/editor/test/common/modes/modesRegistry.test.ts @@ -5,12 +5,22 @@ 'use strict'; import 'vs/languages/html/common/html.contribution'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import * as assert from 'assert'; -import {createMockModeService} from 'vs/editor/test/common/servicesTestUtils'; +import { ModeServiceImpl } from 'vs/editor/common/services/modeServiceImpl'; +import {IExtensionService} from 'vs/platform/extensions/common/extensions'; suite('Editor Modes - Modes Registry', () => { + + let instantiationService: TestInstantiationService; + + setup(() => { + instantiationService= new TestInstantiationService(); + instantiationService.stub(IExtensionService); + }); + test('Bug 12104: [f12] createModel not successfully handling mime type list?', () => { - let modeService = createMockModeService(); + let modeService = instantiationService.createInstance(ModeServiceImpl); assert.equal(modeService.getModeId('text/html,text/plain'), 'html'); }); }); diff --git a/src/vs/editor/test/common/servicesTestUtils.ts b/src/vs/editor/test/common/servicesTestUtils.ts deleted file mode 100644 index abb17d1801441..0000000000000 --- a/src/vs/editor/test/common/servicesTestUtils.ts +++ /dev/null @@ -1,83 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -'use strict'; - -import URI from 'vs/base/common/uri'; -import {TPromise} from 'vs/base/common/winjs.base'; -import {ConfigurationService, IContent, IStat} from 'vs/platform/configuration/common/configurationService'; -import {EventService} from 'vs/platform/event/common/eventService'; -import {IExtensionService} from 'vs/platform/extensions/common/extensions'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; -import {BaseWorkspaceContextService} from 'vs/platform/workspace/common/baseWorkspaceContextService'; -import {IModeService} from 'vs/editor/common/services/modeService'; -import {ModeServiceImpl} from 'vs/editor/common/services/modeServiceImpl'; -import {IModelService} from 'vs/editor/common/services/modelService'; -import {ModelServiceImpl} from 'vs/editor/common/services/modelServiceImpl'; -import {MockExtensionService} from 'vs/editor/test/common/mocks/mockExtensionService'; - -class MockModeService extends ModeServiceImpl {} - -class MockModelService extends ModelServiceImpl { } - -export function createMockModeService(): IModeService { - let services = new ServiceCollection(); - let inst = new InstantiationService(services); - - var extensionService = new MockExtensionService(); - services.set(IExtensionService, extensionService); - - var modeService = new MockModeService(inst, extensionService); - services.set(IModeService, modeService); - - return modeService; -} - -export function createMockModelService(): IModelService { - let contextService = new BaseWorkspaceContextService({ - resource: URI.from({ scheme: 'inmemory', authority: 'model', path: '/' }), - id: null, - name: null, - uid: null, - mtime: null - }, {}); - - let eventService = new EventService(); - - let configurationService = new MockConfigurationService(contextService, eventService); - - return new MockModelService(null, configurationService, null); -} - -export class MockConfigurationService extends ConfigurationService { - - protected resolveContents(resources: URI[]): TPromise { - return TPromise.as(resources.map((resource) => { - return { - resource: resource, - value: '' - }; - })); - } - - protected resolveContent(resource: URI): TPromise { - return TPromise.as({ - resource: resource, - value: '' - }); - } - - protected resolveStat(resource: URI): TPromise { - return TPromise.as({ - resource: resource, - isDirectory: false - }); - } - - setUserConfiguration(key: any, value: any) : Thenable { - return TPromise.as(null); - } - -} diff --git a/src/vs/platform/actions/test/common/actions.test.ts b/src/vs/platform/actions/test/common/actions.test.ts index ac46922480a8e..3940812cfb38c 100644 --- a/src/vs/platform/actions/test/common/actions.test.ts +++ b/src/vs/platform/actions/test/common/actions.test.ts @@ -6,11 +6,10 @@ import assert = require('assert'); import WinJS = require('vs/base/common/winjs.base'); +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import {DeferredAction} from 'vs/platform/actions/common/actions'; import Actions = require('vs/base/common/actions'); import EventEmitter = require('vs/base/common/eventEmitter'); -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; import {AsyncDescriptor} from 'vs/platform/instantiation/common/descriptors'; import {IEventService} from 'vs/platform/event/common/event'; @@ -36,10 +35,11 @@ class TestEventService extends EventEmitter.EventEmitter { } suite('Platform actions', () => { + test('DeferredAction', (done) => { - let services = new ServiceCollection([IEventService, {}]); - let instantiationService = new InstantiationService(services); + let instantiationService: TestInstantiationService= new TestInstantiationService(); + instantiationService.stub(IEventService); let action = new DeferredAction( instantiationService, diff --git a/src/vs/platform/telemetry/test/node/commonProperties.test.ts b/src/vs/platform/telemetry/test/node/commonProperties.test.ts index 9e02b7d0d19d1..c014a8d9b81ad 100644 --- a/src/vs/platform/telemetry/test/node/commonProperties.test.ts +++ b/src/vs/platform/telemetry/test/node/commonProperties.test.ts @@ -7,7 +7,7 @@ import * as assert from 'assert'; import {TPromise} from 'vs/base/common/winjs.base'; import {resolveWorkbenchCommonProperties} from 'vs/platform/telemetry/node/workbenchCommonProperties'; -import {TestStorageService, TestContextService} from 'vs/workbench/test/common/servicesTestUtils'; +import {TestStorageService, TestContextService} from 'vs/test/utils/servicesTestUtils'; suite('Telemetry - common properties', function () { diff --git a/src/vs/platform/telemetry/test/node/sinon.d.ts b/src/vs/platform/telemetry/test/node/sinon.d.ts deleted file mode 100644 index ebe5282512d25..0000000000000 --- a/src/vs/platform/telemetry/test/node/sinon.d.ts +++ /dev/null @@ -1,375 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ -// Type definitions for Sinon 1.5 -// Project: http://sinonjs.org/ -// Definitions by: William Sears -// DefinitelyTyped: https://github.com/borisyankov/DefinitelyTyped -// Minor change to export interfaces - -declare module Sinon { - export interface SinonSpyCallApi { - // Properties - thisValue: any; - args: any[]; - exception: any; - returnValue: any; - - // Methods - calledOn(obj: any): boolean; - calledWith(...args: any[]): boolean; - calledWithExactly(...args: any[]): boolean; - calledWithMatch(...args: SinonMatcher[]): boolean; - notCalledWith(...args: any[]): boolean; - notCalledWithMatch(...args: SinonMatcher[]): boolean; - returned(value: any): boolean; - threw(): boolean; - threw(type: string): boolean; - threw(obj: any): boolean; - callArg(pos: number): void; - callArgOn(pos: number, obj: any, ...args: any[]): void; - callArgWith(pos: number, ...args: any[]): void; - callArgOnWith(pos: number, obj: any, ...args: any[]): void; - yield(...args: any[]): void; - yieldOn(obj: any, ...args: any[]): void; - yieldTo(property: string, ...args: any[]): void; - yieldToOn(property: string, obj: any, ...args: any[]): void; - } - - export interface SinonSpyCall extends SinonSpyCallApi { - calledBefore(call: SinonSpyCall): boolean; - calledAfter(call: SinonSpyCall): boolean; - calledWithNew(call: SinonSpyCall): boolean; - } - - export interface SinonSpy extends SinonSpyCallApi { - // Properties - callCount: number; - called: boolean; - notCalled: boolean; - calledOnce: boolean; - calledTwice: boolean; - calledThrice: boolean; - firstCall: SinonSpyCall; - secondCall: SinonSpyCall; - thirdCall: SinonSpyCall; - lastCall: SinonSpyCall; - thisValues: any[]; - args: any[][]; - exceptions: any[]; - returnValues: any[]; - - // Methods - (...args: any[]): any; - calledBefore(anotherSpy: SinonSpy): boolean; - calledAfter(anotherSpy: SinonSpy): boolean; - calledWithNew(spy: SinonSpy): boolean; - withArgs(...args: any[]): void; - alwaysCalledOn(obj: any); - alwaysCalledWith(...args: any[]); - alwaysCalledWithExactly(...args: any[]); - alwaysCalledWithMatch(...args: SinonMatcher[]); - neverCalledWith(...args: any[]); - neverCalledWithMatch(...args: SinonMatcher[]); - alwaysThrew(): boolean; - alwaysThrew(type: string); - alwaysThrew(obj: any); - alwaysReturned(): boolean; - invokeCallback(...args: any[]): void; - getCall(n: number): SinonSpyCall; - reset(): void; - printf(format: string, ...args: any[]); - restore(): void; - } - - export interface SinonSpyStatic { - (): SinonSpy; - (func: any): SinonSpy; - (obj: any, method: string): SinonSpy; - } - - export var spy: SinonSpyStatic; - - export interface SinonStub extends SinonSpy { - resetBehavior(): void; - returns(obj: any): SinonStub; - returnsArg(index: number): SinonStub; - throws(type?: string): SinonStub; - throws(obj: any): SinonStub; - callsArg(index: number): SinonStub; - callsArgOn(index: number, context: any): SinonStub; - callsArgWith(index: number, ...args: any[]): SinonStub; - callsArgOnWith(index: number, context: any, ...args: any[]): SinonStub; - callsArgAsync(index: number): SinonStub; - callsArgOnAsync(index: number, context: any): SinonStub; - callsArgWithAsync(index: number, ...args: any[]): SinonStub; - callsArgOnWithAsync(index: number, context: any, ...args: any[]): SinonStub; - yields(...args: any[]): SinonStub; - yieldsOn(context: any, ...args: any[]): SinonStub; - yieldsTo(property: string, ...args: any[]): SinonStub; - yieldsToOn(property: string, context: any, ...args: any[]): SinonStub; - yieldsAsync(...args: any[]): SinonStub; - yieldsOnAsync(context: any, ...args: any[]): SinonStub; - yieldsToAsync(property: string, ...args: any[]): SinonStub; - yieldsToOnAsync(property: string, context: any, ...args: any[]): SinonStub; - } - - export interface SinonStubStatic { - (): SinonStub; - (obj: any): SinonStub; - (obj: any, method: string): SinonStub; - (obj: any, method: string, func: any): SinonStub; - } - - export var stub: SinonStubStatic; - - export interface SinonExpectation { - atLeast(n: number): SinonExpectation; - atMost(n: number): SinonExpectation; - never(): SinonExpectation; - once(): SinonExpectation; - twice(): SinonExpectation; - thrice(): SinonExpectation; - exactly(n: number): SinonExpectation; - withArgs(...args: any[]): SinonExpectation; - withExactArgs(...args: any[]): SinonExpectation; - on(obj: any): SinonExpectation; - verify(): SinonExpectation; - restore(): void; - } - - export interface SinonExpectationStatic { - create(methodName?: string): SinonExpectation; - } - - export interface SinonMock { - expects(method: string): SinonExpectationStatic; - restore(): void; - verify(): void; - } - - export interface SinonMockStatic { - (): SinonExpectation; - (obj: any): SinonMock; - } - - export var expectation: SinonExpectationStatic; - export var mock: SinonMockStatic; - - export interface SinonFakeTimers { - now: number; - create(now: number): SinonFakeTimers; - setTimeout(callback: (...args: any[]) => void , timeout: number, ...args: any[]): number; - clearTimeout(id: number): void; - setInterval(callback: (...args: any[]) => void , timeout: number, ...args: any[]): number; - clearInterval(id: number): void; - tick(ms: number): number; - reset(): void; - Date(): Date; - Date(year: number): Date; - Date(year: number, month: number): Date; - Date(year: number, month: number, day: number): Date; - Date(year: number, month: number, day: number, hour: number): Date; - Date(year: number, month: number, day: number, hour: number, minute: number): Date; - Date(year: number, month: number, day: number, hour: number, minute: number, second: number): Date; - Date(year: number, month: number, day: number, hour: number, minute: number, second: number, ms: number): Date; - restore(): void; - } - - export interface SinonFakeTimersStatic { - (): SinonFakeTimers; - (...timers: string[]): SinonFakeTimers; - (now: number, ...timers: string[]): SinonFakeTimers; - } - - export var useFakeTimers: SinonFakeTimersStatic; - export var clock: SinonFakeTimers; - - export interface SinonFakeXMLHttpRequest { - // Properties - onCreate: (xhr: SinonFakeXMLHttpRequest) => void; - url: string; - method: string; - requestHeaders: any; - requestBody: string; - status: number; - statusText: string; - async: boolean; - username: string; - password: string; - responseXML: Document; - getResponseHeader(header: string): string; - getAllResponseHeaders(): any; - - // Methods - restore(): void; - useFilters: boolean; - addFilter(filter: (method, url, async, username, password) => boolean): void; - setResponseHeaders(headers: any): void; - setResponseBody(body: string): void; - respond(status: number, headers: any, body: string): void; - autoRespond(ms: number): void; - } - - export interface SinonFakeXMLHttpRequestStatic { - (): SinonFakeXMLHttpRequest; - } - - export var useFakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic; - export var FakeXMLHttpRequest: SinonFakeXMLHttpRequest; - - export interface SinonFakeServer { - // Properties - autoRespond: boolean; - autoRespondAfter: number; - fakeHTTPMethods: boolean; - getHTTPMethod: (request: SinonFakeXMLHttpRequest) => string; - - // Methods - respondWith(body: string): void; - respondWith(response: any[]): void; - respondWith(fn: (SinonFakeXMLHttpRequest) => void ): void; - respondWith(url: string, body: string): void; - respondWith(url: string, response: any[]): void; - respondWith(url: string, fn: (SinonFakeXMLHttpRequest) => void ): void; - respondWith(method: string, url: string, body: string): void; - respondWith(method: string, url: string, response: any[]): void; - respondWith(method: string, url: string, fn: (SinonFakeXMLHttpRequest) => void ): void; - respondWith(url: RegExp, body: string): void; - respondWith(url: RegExp, response: any[]): void; - respondWith(url: RegExp, fn: (SinonFakeXMLHttpRequest) => void ): void; - respondWith(method: string, url: RegExp, body: string): void; - respondWith(method: string, url: RegExp, response: any[]): void; - respondWith(method: string, url: RegExp, fn: (SinonFakeXMLHttpRequest) => void ): void; - respond(): void; - restore(): void; - } - - export interface SinonFakeServerStatic { - create(): SinonFakeServer; - } - - export var fakeServer: SinonFakeServerStatic; - export var fakeServerWithClock: SinonFakeServerStatic; - - export interface SinonExposeOptions { - prefix?: string; - includeFail?: boolean; - } - - export interface SinonAssert { - // Properties - failException: string; - fail: (message?: string) => void; // Overridable - pass: (assertion: any) => void; // Overridable - - // Methods - notCalled(spy: SinonSpy): void; - called(spy: SinonSpy): void; - calledOnce(spy: SinonSpy): void; - calledTwice(spy: SinonSpy): void; - calledThrice(spy: SinonSpy): void; - callCount(spy: SinonSpy, count: number): void; - callOrder(...spies: SinonSpy[]): void; - calledOn(spy: SinonSpy, obj: any): void; - alwaysCalledOn(spy: SinonSpy, obj: any): void; - calledWith(spy: SinonSpy, ...args: any[]): void; - alwaysCalledWith(spy: SinonSpy, ...args: any[]): void; - neverCalledWith(spy: SinonSpy, ...args: any[]): void; - calledWithExactly(spy: SinonSpy, ...args: any[]): void; - alwaysCalledWithExactly(spy: SinonSpy, ...args: any[]): void; - calledWithMatch(spy: SinonSpy, ...args: SinonMatcher[]): void; - alwaysCalledWithMatch(spy: SinonSpy, ...args: SinonMatcher[]): void; - neverCalledWithMatch(spy: SinonSpy, ...args: SinonMatcher[]): void; - threw(spy: SinonSpy): void; - threw(spy: SinonSpy, exception: string): void; - threw(spy: SinonSpy, exception: any): void; - alwaysThrew(spy: SinonSpy): void; - alwaysThrew(spy: SinonSpy, exception: string): void; - alwaysThrew(spy: SinonSpy, exception: any): void; - expose(obj: any, options?: SinonExposeOptions): void; - } - - export var assert: SinonAssert; - - export interface SinonMatcher { - and(expr: SinonMatch): SinonMatcher; - or(expr: SinonMatch): SinonMatcher; - } - - export interface SinonMatch { - (value: number): SinonMatcher; - (value: string): SinonMatcher; - (expr: RegExp): SinonMatcher; - (obj: any): SinonMatcher; - (callback: (value: any) => boolean): SinonMatcher; - any: SinonMatcher; - defined: SinonMatcher; - truthy: SinonMatcher; - falsy: SinonMatcher; - boolean: SinonMatcher; - number: SinonMatcher; - string: SinonMatcher; - object: SinonMatcher; - func: SinonMatcher; - array: SinonMatcher; - regexp: SinonMatcher; - date: SinonMatcher; - same(obj: any): SinonMatcher; - typeOf(type: string): SinonMatcher; - instanceOf(type: any): SinonMatcher; - has(property: string, expect?: any): SinonMatcher; - hasOwn(property: string, expect?: any): SinonMatcher; - } - - export var match: SinonMatch; - - export interface SinonSandboxConfig { - injectInto?: any; - properties?: string[]; - useFakeTimers?: any; - useFakeServer?: any; - } - - export interface SinonSandbox { - clock: SinonFakeTimers; - requests: SinonFakeXMLHttpRequest; - server: SinonFakeServer; - spy(): SinonSpy; - stub(): SinonStub; - mock(): SinonMock; - useFakeTimers: SinonFakeTimers; - useFakeXMLHttpRequest: SinonFakeXMLHttpRequest; - restore(): void; - } - - export interface SinonSandboxStatic { - create(): SinonSandbox; - create(config: SinonSandboxConfig): SinonSandbox; - } - - export var sandbox: SinonSandboxStatic; - - export interface SinonTestConfig { - injectIntoThis?: boolean; - injectInto?: any; - properties?: string[]; - useFakeTimers?: boolean; - useFakeServer?: boolean; - } - - export interface SinonTestWrapper extends SinonSandbox { - (...args: any[]): any; - } - - export function format (obj: any): string; - export function log (message: string): void; - export var config: SinonTestConfig; - export function test(fn: (...args: any[]) => any): SinonTestWrapper; - export function testCase(tests: any): any; -} - -declare module 'sinon' { - export = Sinon; -} \ No newline at end of file diff --git a/src/vs/test/lib/sinon.d.ts b/src/vs/test/lib/sinon.d.ts new file mode 100644 index 0000000000000..0ca6c3840a063 --- /dev/null +++ b/src/vs/test/lib/sinon.d.ts @@ -0,0 +1,448 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +// Type definitions for Sinon 1.16.0 +// Project: http://sinonjs.org/ +// Definitions by: William Sears +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module '~sinon/lib/sinon' { + module Sinon { + export interface SinonSpyCallApi { + // Properties + thisValue: any; + args: any[]; + exception: any; + returnValue: any; + + // Methods + calledOn(obj: any): boolean; + calledWith(...args: any[]): boolean; + calledWithExactly(...args: any[]): boolean; + calledWithMatch(...args: any[]): boolean; + notCalledWith(...args: any[]): boolean; + notCalledWithMatch(...args: any[]): boolean; + returned(value: any): boolean; + threw(): boolean; + threw(type: string): boolean; + threw(obj: any): boolean; + callArg(pos: number): void; + callArgOn(pos: number, obj: any, ...args: any[]): void; + callArgWith(pos: number, ...args: any[]): void; + callArgOnWith(pos: number, obj: any, ...args: any[]): void; + yield(...args: any[]): void; + yieldOn(obj: any, ...args: any[]): void; + yieldTo(property: string, ...args: any[]): void; + yieldToOn(property: string, obj: any, ...args: any[]): void; + } + + export interface SinonSpyCall extends SinonSpyCallApi { + calledBefore(call: SinonSpyCall): boolean; + calledAfter(call: SinonSpyCall): boolean; + calledWithNew(call: SinonSpyCall): boolean; + } + + export interface SinonSpy extends SinonSpyCallApi { + // Properties + callCount: number; + called: boolean; + notCalled: boolean; + calledOnce: boolean; + calledTwice: boolean; + calledThrice: boolean; + firstCall: SinonSpyCall; + secondCall: SinonSpyCall; + thirdCall: SinonSpyCall; + lastCall: SinonSpyCall; + thisValues: any[]; + args: any[][]; + exceptions: any[]; + returnValues: any[]; + + // Methods + (...args: any[]): any; + calledBefore(anotherSpy: SinonSpy): boolean; + calledAfter(anotherSpy: SinonSpy): boolean; + calledWithNew(spy: SinonSpy): boolean; + withArgs(...args: any[]): SinonSpy; + alwaysCalledOn(obj: any): boolean; + alwaysCalledWith(...args: any[]): boolean; + alwaysCalledWithExactly(...args: any[]): boolean; + alwaysCalledWithMatch(...args: any[]): boolean; + neverCalledWith(...args: any[]): boolean; + neverCalledWithMatch(...args: any[]): boolean; + alwaysThrew(): boolean; + alwaysThrew(type: string): boolean; + alwaysThrew(obj: any): boolean; + alwaysReturned(): boolean; + invokeCallback(...args: any[]): void; + getCall(n: number): SinonSpyCall; + getCalls(): SinonSpyCall[]; + reset(): void; + printf(format: string, ...args: any[]): string; + restore(): void; + } + + export interface SinonSpyStatic { + (): SinonSpy; + (func: any): SinonSpy; + (obj: any, method: string): SinonSpy; + } + + export interface SinonStatic { + spy: SinonSpyStatic; + } + + export interface SinonStub extends SinonSpy { + resetBehavior(): void; + returns(obj: any): SinonStub; + returnsArg(index: number): SinonStub; + returnsThis(): SinonStub; + throws(type?: string): SinonStub; + throws(obj: any): SinonStub; + callsArg(index: number): SinonStub; + callsArgOn(index: number, context: any): SinonStub; + callsArgWith(index: number, ...args: any[]): SinonStub; + callsArgOnWith(index: number, context: any, ...args: any[]): SinonStub; + callsArgAsync(index: number): SinonStub; + callsArgOnAsync(index: number, context: any): SinonStub; + callsArgWithAsync(index: number, ...args: any[]): SinonStub; + callsArgOnWithAsync(index: number, context: any, ...args: any[]): SinonStub; + onCall(n: number): SinonStub; + onFirstCall(): SinonStub; + onSecondCall(): SinonStub; + onThirdCall(): SinonStub; + yields(...args: any[]): SinonStub; + yieldsOn(context: any, ...args: any[]): SinonStub; + yieldsTo(property: string, ...args: any[]): SinonStub; + yieldsToOn(property: string, context: any, ...args: any[]): SinonStub; + yieldsAsync(...args: any[]): SinonStub; + yieldsOnAsync(context: any, ...args: any[]): SinonStub; + yieldsToAsync(property: string, ...args: any[]): SinonStub; + yieldsToOnAsync(property: string, context: any, ...args: any[]): SinonStub; + withArgs(...args: any[]): SinonStub; + } + + export interface SinonStubStatic { + (): SinonStub; + (obj: any): SinonStub; + (obj: any, method: string): SinonStub; + (obj: any, method: string, func: any): SinonStub; + } + + export interface SinonStatic { + stub: SinonStubStatic; + } + + export interface SinonExpectation extends SinonStub { + atLeast(n: number): SinonExpectation; + atMost(n: number): SinonExpectation; + never(): SinonExpectation; + once(): SinonExpectation; + twice(): SinonExpectation; + thrice(): SinonExpectation; + exactly(n: number): SinonExpectation; + withArgs(...args: any[]): SinonExpectation; + withExactArgs(...args: any[]): SinonExpectation; + on(obj: any): SinonExpectation; + verify(): SinonExpectation; + restore(): void; + } + + export interface SinonExpectationStatic { + create(methodName?: string): SinonExpectation; + } + + export interface SinonMock { + expects(method: string): SinonExpectation; + restore(): void; + verify(): void; + } + + export interface SinonMockStatic { + (): SinonExpectation; + (obj: any): SinonMock; + } + + export interface SinonStatic { + expectation: SinonExpectationStatic; + mock: SinonMockStatic; + } + + export interface SinonFakeTimers { + now: number; + create(now: number): SinonFakeTimers; + setTimeout(callback: (...args: any[]) => void, timeout: number, ...args: any[]): number; + clearTimeout(id: number): void; + setInterval(callback: (...args: any[]) => void, timeout: number, ...args: any[]): number; + clearInterval(id: number): void; + tick(ms: number): number; + reset(): void; + Date(): Date; + Date(year: number): Date; + Date(year: number, month: number): Date; + Date(year: number, month: number, day: number): Date; + Date(year: number, month: number, day: number, hour: number): Date; + Date(year: number, month: number, day: number, hour: number, minute: number): Date; + Date(year: number, month: number, day: number, hour: number, minute: number, second: number): Date; + Date(year: number, month: number, day: number, hour: number, minute: number, second: number, ms: number): Date; + restore(): void; + + /** + * Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp + * without affecting timers, intervals or immediates. + * @param now The new 'now' in unix milliseconds + */ + setSystemTime(now: number): void; + /** + * Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp + * without affecting timers, intervals or immediates. + * @param now The new 'now' as a JavaScript Date + */ + setSystemTime(date: Date): void; + } + + export interface SinonFakeTimersStatic { + (): SinonFakeTimers; + (...timers: string[]): SinonFakeTimers; + (now: number, ...timers: string[]): SinonFakeTimers; + } + + export interface SinonStatic { + useFakeTimers: SinonFakeTimersStatic; + clock: SinonFakeTimers; + } + + export interface SinonFakeUploadProgress { + eventListeners: { + progress: any[]; + load: any[]; + abort: any[]; + error: any[]; + }; + + addEventListener(event: string, listener: (e: Event) => any): void; + removeEventListener(event: string, listener: (e: Event) => any): void; + dispatchEvent(event: Event): void; + } + + export interface SinonFakeXMLHttpRequest { + // Properties + onCreate: (xhr: SinonFakeXMLHttpRequest) => void; + url: string; + method: string; + requestHeaders: any; + requestBody: string; + status: number; + statusText: string; + async: boolean; + username: string; + password: string; + withCredentials: boolean; + upload: SinonFakeUploadProgress; + responseXML: Document; + getResponseHeader(header: string): string; + getAllResponseHeaders(): any; + + // Methods + restore(): void; + useFilters: boolean; + addFilter(filter: (method: string, url: string, async: boolean, username: string, password: string) => boolean): void; + setResponseHeaders(headers: any): void; + setResponseBody(body: string): void; + respond(status: number, headers: any, body: string): void; + autoRespond(ms: number): void; + } + + export interface SinonFakeXMLHttpRequestStatic { + (): SinonFakeXMLHttpRequest; + } + + export interface SinonStatic { + useFakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic; + FakeXMLHttpRequest: SinonFakeXMLHttpRequest; + } + + export interface SinonFakeServer { + // Properties + autoRespond: boolean; + autoRespondAfter: number; + fakeHTTPMethods: boolean; + getHTTPMethod: (request: SinonFakeXMLHttpRequest) => string; + requests: SinonFakeXMLHttpRequest[]; + respondImmediately: boolean; + + // Methods + respondWith(body: string): void; + respondWith(response: any[]): void; + respondWith(fn: (xhr: SinonFakeXMLHttpRequest) => void): void; + respondWith(url: string, body: string): void; + respondWith(url: string, response: any[]): void; + respondWith(url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void; + respondWith(method: string, url: string, body: string): void; + respondWith(method: string, url: string, response: any[]): void; + respondWith(method: string, url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void; + respondWith(url: RegExp, body: string): void; + respondWith(url: RegExp, response: any[]): void; + respondWith(url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void; + respondWith(method: string, url: RegExp, body: string): void; + respondWith(method: string, url: RegExp, response: any[]): void; + respondWith(method: string, url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void; + respond(): void; + restore(): void; + } + + export interface SinonFakeServerStatic { + create(): SinonFakeServer; + } + + export interface SinonStatic { + fakeServer: SinonFakeServerStatic; + fakeServerWithClock: SinonFakeServerStatic; + } + + export interface SinonExposeOptions { + prefix?: string; + includeFail?: boolean; + } + + export interface SinonAssert { + // Properties + failException: string; + fail: (message?: string) => void; // Overridable + pass: (assertion: any) => void; // Overridable + + // Methods + notCalled(spy: SinonSpy): void; + called(spy: SinonSpy): void; + calledOnce(spy: SinonSpy): void; + calledTwice(spy: SinonSpy): void; + calledThrice(spy: SinonSpy): void; + callCount(spy: SinonSpy, count: number): void; + callOrder(...spies: SinonSpy[]): void; + calledOn(spy: SinonSpy, obj: any): void; + alwaysCalledOn(spy: SinonSpy, obj: any): void; + calledWith(spy: SinonSpy, ...args: any[]): void; + alwaysCalledWith(spy: SinonSpy, ...args: any[]): void; + neverCalledWith(spy: SinonSpy, ...args: any[]): void; + calledWithExactly(spy: SinonSpy, ...args: any[]): void; + alwaysCalledWithExactly(spy: SinonSpy, ...args: any[]): void; + calledWithMatch(spy: SinonSpy, ...args: any[]): void; + alwaysCalledWithMatch(spy: SinonSpy, ...args: any[]): void; + neverCalledWithMatch(spy: SinonSpy, ...args: any[]): void; + threw(spy: SinonSpy): void; + threw(spy: SinonSpy, exception: string): void; + threw(spy: SinonSpy, exception: any): void; + alwaysThrew(spy: SinonSpy): void; + alwaysThrew(spy: SinonSpy, exception: string): void; + alwaysThrew(spy: SinonSpy, exception: any): void; + expose(obj: any, options?: SinonExposeOptions): void; + } + + export interface SinonStatic { + assert: SinonAssert; + } + + export interface SinonMatcher { + and(expr: SinonMatcher): SinonMatcher; + or(expr: SinonMatcher): SinonMatcher; + } + + export interface SinonMatch { + (value: number): SinonMatcher; + (value: string): SinonMatcher; + (expr: RegExp): SinonMatcher; + (obj: any): SinonMatcher; + (callback: (value: any) => boolean): SinonMatcher; + any: SinonMatcher; + defined: SinonMatcher; + truthy: SinonMatcher; + falsy: SinonMatcher; + bool: SinonMatcher; + number: SinonMatcher; + string: SinonMatcher; + object: SinonMatcher; + func: SinonMatcher; + array: SinonMatcher; + regexp: SinonMatcher; + date: SinonMatcher; + same(obj: any): SinonMatcher; + typeOf(type: string): SinonMatcher; + instanceOf(type: any): SinonMatcher; + has(property: string, expect?: any): SinonMatcher; + hasOwn(property: string, expect?: any): SinonMatcher; + } + + export interface SinonStatic { + match: SinonMatch; + } + + export interface SinonSandboxConfig { + injectInto?: any; + properties?: string[]; + useFakeTimers?: any; + useFakeServer?: any; + } + + export interface SinonSandbox { + clock: SinonFakeTimers; + requests: SinonFakeXMLHttpRequest; + server: SinonFakeServer; + spy: SinonSpyStatic; + stub: SinonStubStatic; + mock: SinonMockStatic; + useFakeTimers: SinonFakeTimersStatic; + useFakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic; + useFakeServer(): SinonFakeServer; + restore(): void; + } + + export interface SinonSandboxStatic { + create(): SinonSandbox; + create(config: SinonSandboxConfig): SinonSandbox; + } + + export interface SinonStatic { + sandbox: SinonSandboxStatic; + } + + export interface SinonTestConfig { + injectIntoThis?: boolean; + injectInto?: any; + properties?: string[]; + useFakeTimers?: boolean; + useFakeServer?: boolean; + } + + export interface SinonTestWrapper extends SinonSandbox { + (...args: any[]): any; + } + + export interface SinonStatic { + config: SinonTestConfig; + test(fn: (...args: any[]) => any): SinonTestWrapper; + testCase(tests: any): any; + } + + // Utility overridables + export interface SinonStatic { + createStubInstance(constructor: any): SinonStub; + format(obj: any): string; + log(message: string): void; + restore(object: any): void; + } + } + + var Sinon: Sinon.SinonStatic; + + export = Sinon; +} +declare module 'sinon/lib/sinon' { + import main = require('~sinon/lib/sinon'); + export = main; +} +declare module 'sinon' { + import main = require('~sinon/lib/sinon'); + export = main; +} \ No newline at end of file diff --git a/src/vs/test/utils/instantiationTestUtils.ts b/src/vs/test/utils/instantiationTestUtils.ts new file mode 100644 index 0000000000000..c6b7d6dbfe08c --- /dev/null +++ b/src/vs/test/utils/instantiationTestUtils.ts @@ -0,0 +1,139 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. +*--------------------------------------------------------------------------------------------*/ + +import * as sinon from 'sinon'; +import { TPromise } from 'vs/base/common/winjs.base'; +import { SimpleMap } from 'vs/base/common/map'; +import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService'; +import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection'; +import { ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation'; + +// Known services +import {IEventService} from 'vs/platform/event/common/event'; +import {EventService} from 'vs/platform/event/common/eventService'; +import { ITelemetryService, NullTelemetryService } from 'vs/platform/telemetry/common/telemetry'; +import { ISearchService } from 'vs/platform/search/common/search'; +import { SearchService } from 'vs/workbench/services/search/node/searchService'; +import { IHistoryService } from 'vs/workbench/services/history/common/history'; +import { HistoryService } from 'vs/workbench/services/history/browser/history'; +import { IModeService } from 'vs/editor/common/services/modeService'; +import { ModeServiceImpl } from 'vs/editor/common/services/modeServiceImpl'; +import { IExtensionService } from 'vs/platform/extensions/common/extensions'; +import { SimpleExtensionService } from 'vs/editor/browser/standalone/simpleServices'; +import { MarkerService } from 'vs/platform/markers/common/markerService'; +import { IMarkerService } from 'vs/platform/markers/common/markers'; + +interface IServiceMock { + id: ServiceIdentifier; + service: any; +} + +export class TestInstantiationService extends InstantiationService { + + private _servciesMap: SimpleMap, any>; + + constructor(private _serviceCollection: ServiceCollection = new ServiceCollection()) { + super(_serviceCollection); + + this._servciesMap= new SimpleMap, any>(); + this._servciesMap.set(ITelemetryService, NullTelemetryService); + this._servciesMap.set(IEventService, EventService); + this._servciesMap.set(ISearchService, SearchService); + this._servciesMap.set(IHistoryService, HistoryService); + this._servciesMap.set(IModeService, ModeServiceImpl); + this._servciesMap.set(IExtensionService, SimpleExtensionService); + this._servciesMap.set(IMarkerService, MarkerService); + } + + public mock(service:ServiceIdentifier): T | sinon.SinonMock { + return this._create(service, {mock: true}); + } + + public stub(service?:ServiceIdentifier, ctor?: any): T + public stub(service?:ServiceIdentifier, obj?: any): T + public stub(service?:ServiceIdentifier, ctor?: any, fnProperty?: string, value?: any): sinon.SinonStub + public stub(service?:ServiceIdentifier, obj?: any, fnProperty?: string, value?: any): sinon.SinonStub + public stub(service?:ServiceIdentifier, fnProperty?: string, value?: any): sinon.SinonStub + public stub(serviceIdentifier?: ServiceIdentifier, arg2?: any, arg3?: string, arg4?: any): sinon.SinonStub { + let service= typeof arg2 !== 'string' ? arg2 : void 0; + let serviceMock: IServiceMock= {id: serviceIdentifier, service: service}; + let fnProperty= typeof arg2 === 'string' ? arg2 : arg3; + let value= typeof arg2 === 'string' ? arg3 : arg4; + + let stubObject= this._create(serviceMock, {stub: true}); + if (fnProperty) { + if (stubObject[fnProperty].hasOwnProperty('restore')) { + stubObject[fnProperty].restore(); + } + if (typeof value === 'function') { + stubObject[fnProperty] = value; + } else { + let stub = value ? sinon.stub().returns(value) : sinon.stub(); + stubObject[fnProperty] = stub; + return stub; + } + } + return stubObject; + } + + public stubPromise(service?:ServiceIdentifier, fnProperty?: string, value?: any): T | sinon.SinonStub + public stubPromise(service?:ServiceIdentifier, ctor?: any, fnProperty?: string, value?: any): sinon.SinonStub + public stubPromise(service?:ServiceIdentifier, obj?: any, fnProperty?: string, value?: any): sinon.SinonStub + public stubPromise(arg1?:any, arg2?: any, arg3?: any, arg4?: any): sinon.SinonStub { + arg3= typeof arg2 === 'string' ? TPromise.as(arg3) : arg3; + arg4= typeof arg2 !== 'string' && typeof arg3 === 'string' ? TPromise.as(arg4) : arg4; + return this.stub(arg1, arg2, arg3, arg4); + } + + public spy(service:ServiceIdentifier, fnProperty: string): sinon.SinonSpy { + let spy= sinon.spy(); + this.stub(service, fnProperty, spy); + return spy; + } + + private _create(serviceMock: IServiceMock, options: SinonOptions): any + private _create(ctor: any, options: SinonOptions): any + private _create(arg1: any, options: SinonOptions): any { + if (this.isServiceMock(arg1)) { + let service= this._getOrCreateService(arg1, options); + this._serviceCollection.set(arg1.id, service); + return service; + } + return options.mock ? sinon.mock(arg1) : this._createStub(arg1); + } + + private _getOrCreateService(serviceMock: IServiceMock, opts: SinonOptions): any { + let service:any = this._serviceCollection.get(serviceMock.id); + if (service) { + if (opts.mock && service['sinonOptions'] && !!service['sinonOptions'].mock) { + return service; + } + if (opts.stub && service['sinonOptions'] && !!service['sinonOptions'].stub) { + return service; + } + } + return this._createService(serviceMock, opts); + } + + private _createService(serviceMock: IServiceMock, opts: SinonOptions): any { + serviceMock.service= serviceMock.service ? serviceMock.service : this._servciesMap.get(serviceMock.id); + let service= opts.mock ? sinon.mock(serviceMock.service) : this._createStub(serviceMock.service); + service['sinonOptions']= opts; + return service; + } + + private _createStub(arg: any): any { + return typeof arg === 'object' ? arg : sinon.createStubInstance(arg); + } + + private isServiceMock(arg1: any): boolean { + return typeof arg1 === 'object' && arg1.hasOwnProperty('id'); + } +} + +interface SinonOptions { + mock?: boolean; + stub?: boolean; +} \ No newline at end of file diff --git a/src/vs/test/utils/promiseTestUtils.ts b/src/vs/test/utils/promiseTestUtils.ts new file mode 100644 index 0000000000000..4096e44e0fc40 --- /dev/null +++ b/src/vs/test/utils/promiseTestUtils.ts @@ -0,0 +1,42 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. +*--------------------------------------------------------------------------------------------*/ + +import { TPromise, PPromise, TValueCallback, TProgressCallback } from 'vs/base/common/winjs.base'; +import errors = require('vs/base/common/errors'); + +export class DeferredTPromise extends TPromise { + + constructor(init:(complete: TValueCallback, error:(err:any)=>void)=>void) { + super((c, e) => setTimeout(() => init(c, e))); + } + +} + +export class DeferredPPromise extends PPromise { + + private completeCallback:TValueCallback; + private errorCallback:(err:any)=>void; + private progressCallback:TProgressCallback

; + + constructor(init:(complete: TValueCallback, error:(err:any)=>void, progress: TProgressCallback

)=>void = (c, e, p) => {}, oncancel?: any) { + super((c, e, p) => {this.completeCallback= c; this.errorCallback= e; this.progressCallback= p;}, oncancel ? oncancel : () => this.oncancel); + } + + private oncancel(): void { + this.errorCallback(errors.canceled()); + } + + public complete(c: C) { + this.completeCallback(c); + } + + public progress(p: P) { + this.progressCallback(p); + } + + public error(e: any) { + this.errorCallback(e); + } +} \ No newline at end of file diff --git a/src/vs/workbench/test/common/servicesTestUtils.ts b/src/vs/test/utils/servicesTestUtils.ts similarity index 94% rename from src/vs/workbench/test/common/servicesTestUtils.ts rename to src/vs/test/utils/servicesTestUtils.ts index 202c8e25fdb10..d26b70bd5dbf8 100644 --- a/src/vs/workbench/test/common/servicesTestUtils.ts +++ b/src/vs/test/utils/servicesTestUtils.ts @@ -6,6 +6,7 @@ 'use strict'; import {Promise, TPromise} from 'vs/base/common/winjs.base'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import EventEmitter = require('vs/base/common/eventEmitter'); import Paths = require('vs/base/common/paths'); import URI from 'vs/base/common/uri'; @@ -17,6 +18,7 @@ import Types = require('vs/base/common/types'); import Severity from 'vs/base/common/severity'; import http = require('vs/base/common/http'); import {IConfigurationService} from 'vs/platform/configuration/common/configuration'; +import {IContent, IStat} from 'vs/platform/configuration/common/configurationService'; import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage'; import WorkbenchEditorService = require('vs/workbench/services/editor/common/editorService'); import QuickOpenService = require('vs/workbench/services/quickopen/common/quickOpenService'); @@ -29,7 +31,6 @@ import {IMessageService, IConfirmation} from 'vs/platform/message/common/message import {BaseRequestService} from 'vs/platform/request/common/baseRequestService'; import {IWorkspace, IConfiguration} from 'vs/platform/workspace/common/workspace'; import {ILifecycleService, ShutdownEvent} from 'vs/platform/lifecycle/common/lifecycle'; -import {IHistoryService} from 'vs/workbench/services/history/common/history'; import {EditorStacksModel} from 'vs/workbench/common/editor/editorStacksModel'; import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; @@ -38,6 +39,7 @@ import {TextFileService} from 'vs/workbench/parts/files/common/textFileServices' import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation'; import {IFileService, IResolveContentOptions} from 'vs/platform/files/common/files'; import {IModelService} from 'vs/editor/common/services/modelService'; +import {ModelServiceImpl} from 'vs/editor/common/services/modelServiceImpl'; import {IRawTextContent} from 'vs/workbench/parts/files/common/files'; import {RawText} from 'vs/editor/common/model/textModel'; @@ -53,34 +55,6 @@ export const TestConfiguration: IConfiguration = { env: Object.create(null) }; -export class TestHistoryService implements IHistoryService { - public _serviceBrand: any; - - public forward(): void { - - } - - public back(): void { - - } - - public clear(): void { - - } - - public remove(input: IEditorInput): void { - - } - - public popLastClosedEditor(): IEditorInput { - return null; - } - - public getHistory(): IEditorInput[] { - return []; - } -} - export class TestContextService implements WorkspaceContextService.IWorkspaceContextService { public _serviceBrand: any; @@ -620,6 +594,29 @@ export class TestConfigurationService extends EventEmitter.EventEmitter implemen private configuration = Object.create(null); + protected resolveContents(resources: URI[]): TPromise { + return TPromise.as(resources.map((resource) => { + return { + resource: resource, + value: '' + }; + })); + } + + protected resolveContent(resource: URI): TPromise { + return TPromise.as({ + resource: resource, + value: '' + }); + } + + protected resolveStat(resource: URI): TPromise { + return TPromise.as({ + resource: resource, + isDirectory: false + }); + } + public loadConfiguration(section?: string): TPromise { return TPromise.as(this.getConfiguration()); } @@ -632,15 +629,14 @@ export class TestConfigurationService extends EventEmitter.EventEmitter implemen return false; } - public onDidUpdateConfiguration() { - return { dispose() { } }; - } - public setUserConfiguration(key: any, value: any): Thenable { this.configuration[key] = value; - return TPromise.as(null); } + + public onDidUpdateConfiguration() { + return { dispose() { } }; + } } export class TestLifecycleService implements ILifecycleService { @@ -664,4 +660,9 @@ export class TestLifecycleService implements ILifecycleService { public get onShutdown(): Event { return this._onShutdown.event; } +} + +export function createMockModelService(instantiationService: TestInstantiationService): IModelService { + instantiationService.stub(IConfigurationService, new TestConfigurationService()); + return instantiationService.createInstance(ModelServiceImpl); } \ No newline at end of file diff --git a/src/vs/workbench/parts/files/test/browser/fileEditorInput.test.ts b/src/vs/workbench/parts/files/test/browser/fileEditorInput.test.ts index 3800fc3ac0c02..a44f170f58dec 100644 --- a/src/vs/workbench/parts/files/test/browser/fileEditorInput.test.ts +++ b/src/vs/workbench/parts/files/test/browser/fileEditorInput.test.ts @@ -6,6 +6,7 @@ import 'vs/workbench/parts/files/browser/files.contribution'; // load our contribution into the test import * as assert from 'assert'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import URI from 'vs/base/common/uri'; import {join} from 'vs/base/common/paths'; import {FileEditorInput} from 'vs/workbench/parts/files/common/editors/fileEditorInput'; @@ -19,14 +20,11 @@ import {IConfigurationService} from 'vs/platform/configuration/common/configurat import {ILifecycleService, NullLifecycleService} from 'vs/platform/lifecycle/common/lifecycle'; import {IFileService} from 'vs/platform/files/common/files'; import {IQuickOpenService} from 'vs/workbench/services/quickopen/common/quickOpenService'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService'; import {IPartService} from 'vs/workbench/services/part/common/partService'; import {ITextFileService} from 'vs/workbench/parts/files/common/files'; import {FileTracker} from 'vs/workbench/parts/files/browser/fileTracker'; -import {TestTextFileService, TestEditorGroupService, TestHistoryService, TestFileService, TestEditorService, TestPartService, TestConfigurationService, TestEventService, TestContextService, TestQuickOpenService, TestStorageService} from 'vs/workbench/test/common/servicesTestUtils'; -import {createMockModelService, createMockModeService} from 'vs/editor/test/common/servicesTestUtils'; +import {createMockModelService, TestTextFileService, TestEditorGroupService, TestFileService, TestEditorService, TestPartService, TestConfigurationService, TestEventService, TestContextService, TestQuickOpenService, TestStorageService} from 'vs/test/utils/servicesTestUtils'; import {IHistoryService} from 'vs/workbench/services/history/common/history'; import {IEditorGroupService} from 'vs/workbench/services/group/common/groupService'; @@ -36,28 +34,32 @@ function toResource(path) { suite('Files - FileEditorInput', () => { + let instantiationService: TestInstantiationService; + + setup(() => { + instantiationService= new TestInstantiationService(); + instantiationService.stub(IHistoryService, 'getHistory', []); + }); + test('FileEditorInput', function (done) { let editorService = new TestEditorService(function () { }); let eventService = new TestEventService(); let telemetryService = NullTelemetryService; let contextService = new TestContextService(); - let services = new ServiceCollection(); - let instantiationService = new InstantiationService(services); - - services.set(IEventService, eventService); - services.set(IWorkspaceContextService, contextService); - services.set(IFileService, TestFileService); - services.set(IStorageService, new TestStorageService()); - services.set(IWorkbenchEditorService, editorService); - services.set(IPartService, new TestPartService()); - services.set(IModeService, createMockModeService()); - services.set(IModelService, createMockModelService()); - services.set(ITelemetryService, telemetryService); - services.set(IEditorGroupService, new TestEditorGroupService()); - services.set(ILifecycleService, NullLifecycleService); - services.set(IConfigurationService, new TestConfigurationService()); - services.set(ITextFileService, instantiationService.createInstance( TestTextFileService)); + instantiationService.stub(IEventService, eventService); + instantiationService.stub(IWorkspaceContextService, contextService); + instantiationService.stub(IFileService, TestFileService); + instantiationService.stub(IStorageService, new TestStorageService()); + instantiationService.stub(IWorkbenchEditorService, editorService); + instantiationService.stub(IPartService, new TestPartService()); + instantiationService.stub(IModeService); + instantiationService.stub(IModelService, createMockModelService(instantiationService)); + instantiationService.stub(ITelemetryService, telemetryService); + instantiationService.stub(IEditorGroupService, new TestEditorGroupService()); + instantiationService.stub(ILifecycleService, NullLifecycleService); + instantiationService.stub(IConfigurationService, new TestConfigurationService()); + instantiationService.stub(ITextFileService, instantiationService.createInstance( TestTextFileService)); let input = instantiationService.createInstance(FileEditorInput, toResource('/foo/bar/file.js'), 'text/javascript', void 0); let otherInput = instantiationService.createInstance(FileEditorInput, toResource('foo/bar/otherfile.js'), 'text/javascript', void 0); @@ -76,7 +78,7 @@ suite('Files - FileEditorInput', () => { input = instantiationService.createInstance(FileEditorInput, toResource('/foo/bar.html'), 'text/html', void 0); - let inputToResolve = instantiationService.createInstance(FileEditorInput, toResource('/foo/bar/file.js'), 'text/javascript', void 0); + let inputToResolve:any = instantiationService.createInstance(FileEditorInput, toResource('/foo/bar/file.js'), 'text/javascript', void 0); let sameOtherInput = instantiationService.createInstance(FileEditorInput, toResource('/foo/bar/file.js'), 'text/javascript', void 0); return editorService.resolveEditorModel(inputToResolve, true).then(function (resolved) { @@ -122,12 +124,9 @@ suite('Files - FileEditorInput', () => { let eventService = new TestEventService(); let contextService = new TestContextService(); - let services = new ServiceCollection(); - let instantiationService = new InstantiationService(services); - - services.set(IEventService, eventService); - services.set(IWorkspaceContextService, contextService); - services.set(ITextFileService, instantiationService.createInstance( TestTextFileService)); + instantiationService.stub(IEventService, eventService); + instantiationService.stub(IWorkspaceContextService, contextService); + instantiationService.stub(ITextFileService, instantiationService.createInstance( TestTextFileService)); let fileEditorInput = instantiationService.createInstance(FileEditorInput, toResource('/foo/bar/updatefile.js'), 'text/javascript', void 0); let contentEditorInput2 = instantiationService.createInstance(FileEditorInput, toResource('/foo/bar/updatefile.js'), 'text/javascript', void 0); @@ -144,24 +143,20 @@ suite('Files - FileEditorInput', () => { let eventService = new TestEventService(); - let services = new ServiceCollection(); - let instantiationService = new InstantiationService(services); - - services.set(IEventService, eventService); - services.set(IWorkspaceContextService, contextService); - services.set(IFileService, TestFileService); - services.set(IStorageService, new TestStorageService()); - services.set(IWorkbenchEditorService, editorService); - services.set(IHistoryService, new TestHistoryService()); - services.set(IQuickOpenService, new TestQuickOpenService()); - services.set(IPartService, new TestPartService()); - services.set(IModeService, createMockModeService()); - services.set(IEditorGroupService, new TestEditorGroupService()); - services.set(IModelService, createMockModelService()); - services.set(ITelemetryService, telemetryService); - services.set(ILifecycleService, NullLifecycleService); - services.set(IConfigurationService, new TestConfigurationService()); - services.set(ITextFileService, instantiationService.createInstance( TestTextFileService)); + instantiationService.stub(IEventService, eventService); + instantiationService.stub(IWorkspaceContextService, contextService); + instantiationService.stub(IFileService, TestFileService); + instantiationService.stub(IStorageService, new TestStorageService()); + instantiationService.stub(IWorkbenchEditorService, editorService); + instantiationService.stub(IQuickOpenService, new TestQuickOpenService()); + instantiationService.stub(IPartService, new TestPartService()); + instantiationService.stub(IModeService); + instantiationService.stub(IEditorGroupService, new TestEditorGroupService()); + instantiationService.stub(IModelService, createMockModelService(instantiationService)); + instantiationService.stub(ITelemetryService, telemetryService); + instantiationService.stub(ILifecycleService, NullLifecycleService); + instantiationService.stub(IConfigurationService, new TestConfigurationService()); + instantiationService.stub(ITextFileService, instantiationService.createInstance( TestTextFileService)); let tracker = instantiationService.createInstance(FileTracker); @@ -190,24 +185,20 @@ suite('Files - FileEditorInput', () => { let eventService = new TestEventService(); - let services = new ServiceCollection(); - let instantiationService = new InstantiationService(services); - - services.set(IEventService, eventService); - services.set(IWorkspaceContextService, contextService); - services.set(IFileService, TestFileService); - services.set(IStorageService, new TestStorageService()); - services.set(IWorkbenchEditorService, editorService); - services.set(IPartService, new TestPartService()); - services.set(IModeService, createMockModeService()); - services.set(IEditorGroupService, new TestEditorGroupService()); - services.set(IQuickOpenService, new TestQuickOpenService()); - services.set(IModelService, createMockModelService()); - services.set(ITelemetryService, telemetryService); - services.set(ILifecycleService, NullLifecycleService); - services.set(IConfigurationService, new TestConfigurationService()); - services.set(IHistoryService, new TestHistoryService()); - services.set(ITextFileService, instantiationService.createInstance( TestTextFileService)); + instantiationService.stub(IEventService, eventService); + instantiationService.stub(IWorkspaceContextService, contextService); + instantiationService.stub(IFileService, TestFileService); + instantiationService.stub(IStorageService, new TestStorageService()); + instantiationService.stub(IWorkbenchEditorService, editorService); + instantiationService.stub(IPartService, new TestPartService()); + instantiationService.stub(IModeService); + instantiationService.stub(IEditorGroupService, new TestEditorGroupService()); + instantiationService.stub(IQuickOpenService, new TestQuickOpenService()); + instantiationService.stub(IModelService, createMockModelService(instantiationService)); + instantiationService.stub(ITelemetryService, telemetryService); + instantiationService.stub(ILifecycleService, NullLifecycleService); + instantiationService.stub(IConfigurationService, new TestConfigurationService()); + instantiationService.stub(ITextFileService, instantiationService.createInstance( TestTextFileService)); let tracker = instantiationService.createInstance(FileTracker); diff --git a/src/vs/workbench/parts/files/test/browser/fileEditorModel.test.ts b/src/vs/workbench/parts/files/test/browser/fileEditorModel.test.ts index 8dab922bd9120..6d7b44579d68f 100644 --- a/src/vs/workbench/parts/files/test/browser/fileEditorModel.test.ts +++ b/src/vs/workbench/parts/files/test/browser/fileEditorModel.test.ts @@ -6,13 +6,13 @@ 'use strict'; import * as assert from 'assert'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import {TPromise} from 'vs/base/common/winjs.base'; import URI from 'vs/base/common/uri'; import paths = require('vs/base/common/paths'); import {FileEditorInput} from 'vs/workbench/parts/files/common/editors/fileEditorInput'; import {TextFileEditorModel, CACHE} from 'vs/workbench/parts/files/common/editors/textFileEditorModel'; -import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation'; -import {ITelemetryService, NullTelemetryService} from 'vs/platform/telemetry/common/telemetry'; +import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry'; import {IEventService} from 'vs/platform/event/common/event'; import {IMessageService} from 'vs/platform/message/common/message'; import {IModelService} from 'vs/editor/common/services/modelService'; @@ -22,58 +22,50 @@ import {IStorageService} from 'vs/platform/storage/common/storage'; import {IConfigurationService} from 'vs/platform/configuration/common/configuration'; import {ILifecycleService, NullLifecycleService} from 'vs/platform/lifecycle/common/lifecycle'; import {IFileService} from 'vs/platform/files/common/files'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; import {IUntitledEditorService} from 'vs/workbench/services/untitled/common/untitledEditorService'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService'; import PartService = require('vs/workbench/services/part/common/partService'); import {ITextFileService, EventType} from 'vs/workbench/parts/files/common/files'; -import {TestTextFileService, TestFileService, TestPartService, TestEditorService, TestConfigurationService, TestUntitledEditorService, TestStorageService, TestContextService, TestMessageService, TestEventService} from 'vs/workbench/test/common/servicesTestUtils'; -import {createMockModelService, createMockModeService} from 'vs/editor/test/common/servicesTestUtils'; +import {createMockModelService, TestTextFileService, TestFileService, TestPartService, TestEditorService, TestConfigurationService, TestUntitledEditorService, TestStorageService, TestContextService, TestMessageService, TestEventService} from 'vs/test/utils/servicesTestUtils'; function toResource(path) { return URI.file(paths.join('C:\\', path)); } -let baseInstantiationService: IInstantiationService; -let messageService: TestMessageService; -let eventService: TestEventService; +let eventService: IEventService; let textFileService: TestTextFileService; suite('Files - TextFileEditorModel', () => { + let instantiationService: TestInstantiationService; + setup(() => { + instantiationService= new TestInstantiationService(); eventService = new TestEventService(); - messageService = new TestMessageService(); - - let services = new ServiceCollection(); - - services.set(IEventService, eventService); - services.set(IMessageService, messageService); - services.set(IFileService, TestFileService); - services.set(IWorkspaceContextService, new TestContextService()); - services.set(ITelemetryService, NullTelemetryService); - services.set(IStorageService, new TestStorageService()); - services.set(IUntitledEditorService, new TestUntitledEditorService()); - services.set(IWorkbenchEditorService, new TestEditorService()); - services.set(PartService.IPartService, new TestPartService()); - services.set(IModeService, createMockModeService()); - services.set(IModelService, createMockModelService()); - services.set(ILifecycleService, NullLifecycleService); - services.set(IConfigurationService, new TestConfigurationService()); - - baseInstantiationService = new InstantiationService(services); - textFileService = baseInstantiationService.createInstance(TestTextFileService); - services.set(ITextFileService, textFileService); + eventService= instantiationService.stub(IEventService, new TestEventService()); + instantiationService.stub(IMessageService, new TestMessageService()); + instantiationService.stub(IFileService, TestFileService); + instantiationService.stub(IWorkspaceContextService, new TestContextService()); + instantiationService.stub(ITelemetryService); + instantiationService.stub(IStorageService, new TestStorageService()); + instantiationService.stub(IUntitledEditorService, new TestUntitledEditorService()); + instantiationService.stub(IWorkbenchEditorService, new TestEditorService()); + instantiationService.stub(PartService.IPartService, new TestPartService()); + instantiationService.stub(IModeService); + instantiationService.stub(IModelService, createMockModelService(instantiationService)); + instantiationService.stub(ILifecycleService, NullLifecycleService); + instantiationService.stub(IConfigurationService, new TestConfigurationService()); + + textFileService = instantiationService.createInstance(TestTextFileService); + instantiationService.stub(ITextFileService, textFileService); }); teardown(() => { - eventService.dispose(); CACHE.clear(); }); test('Load does not trigger save', function (done) { - let m1 = baseInstantiationService.createInstance(TextFileEditorModel, toResource('/path/index.txt'), 'utf8'); + let m1 = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index.txt'), 'utf8'); eventService.addListener2('files:internalFileChanged', () => { assert.ok(false); @@ -97,7 +89,7 @@ suite('Files - TextFileEditorModel', () => { }); test('Load returns dirty model as long as model is dirty', function (done) { - let m1 = baseInstantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8'); + let m1 = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8'); m1.load().then(() => { m1.textEditorModel.setValue('foo'); @@ -120,7 +112,7 @@ suite('Files - TextFileEditorModel', () => { eventCounter++; }); - let m1 = baseInstantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8'); + let m1 = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8'); m1.load().then(() => { m1.textEditorModel.setValue('foo'); @@ -140,7 +132,7 @@ suite('Files - TextFileEditorModel', () => { }); test('Conflict Resolution Mode', function (done) { - let m1 = baseInstantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8'); + let m1 = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8'); m1.load().then(() => { m1.setConflictResolutionMode(); @@ -166,7 +158,7 @@ suite('Files - TextFileEditorModel', () => { test('Auto Save triggered when model changes', function (done) { let eventCounter = 0; - let m1 = baseInstantiationService.createInstance(TextFileEditorModel, toResource('/path/index.txt'), 'utf8'); + let m1 = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index.txt'), 'utf8'); (m1).autoSaveAfterMillies = 10; (m1).autoSaveAfterMilliesEnabled = true; @@ -195,7 +187,7 @@ suite('Files - TextFileEditorModel', () => { test('Dirty tracking', function (done) { let resource = toResource('/path/index_async.txt'); - let i1 = baseInstantiationService.createInstance(FileEditorInput, resource, 'text/plain', 'utf8'); + let i1 = instantiationService.createInstance(FileEditorInput, resource, 'text/plain', 'utf8'); i1.resolve().then((m1: TextFileEditorModel) => { let dirty = m1.getLastDirtyTime(); @@ -216,8 +208,8 @@ suite('Files - TextFileEditorModel', () => { }); test('save() and isDirty() - proper with check for mtimes', function (done) { - let c1 = baseInstantiationService.createInstance(FileEditorInput, toResource('/path/index_async2.txt'), 'text/plain', 'utf8'); - let c2 = baseInstantiationService.createInstance(FileEditorInput, toResource('/path/index_async.txt'), 'text/plain', 'utf8'); + let c1 = instantiationService.createInstance(FileEditorInput, toResource('/path/index_async2.txt'), 'text/plain', 'utf8'); + let c2 = instantiationService.createInstance(FileEditorInput, toResource('/path/index_async.txt'), 'text/plain', 'utf8'); c1.resolve().then((m1: TextFileEditorModel) => { c2.resolve().then((m2: TextFileEditorModel) => { @@ -254,7 +246,7 @@ suite('Files - TextFileEditorModel', () => { test('Save Participant', function (done) { let eventCounter = 0; - let m1 = baseInstantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8'); + let m1 = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8'); eventService.addListener2(EventType.FILE_SAVED, (e) => { assert.equal(m1.getValue(), 'bar'); diff --git a/src/vs/workbench/parts/files/test/browser/textFileEditor.test.ts b/src/vs/workbench/parts/files/test/browser/textFileEditor.test.ts index bc01bf4376af8..b83a182b0d1f8 100644 --- a/src/vs/workbench/parts/files/test/browser/textFileEditor.test.ts +++ b/src/vs/workbench/parts/files/test/browser/textFileEditor.test.ts @@ -8,16 +8,15 @@ import {strictEqual, equal} from 'assert'; import {join} from 'vs/base/common/paths'; import URI from 'vs/base/common/uri'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import {FileEditorDescriptor} from 'vs/workbench/parts/files/browser/files'; import {Registry} from 'vs/platform/platform'; import {SyncDescriptor} from 'vs/platform/instantiation/common/descriptors'; import {FileEditorInput} from 'vs/workbench/parts/files/common/editors/fileEditorInput'; import {Extensions} from 'vs/workbench/common/editor'; -import {TestTextFileService, TestEventService, TestContextService} from 'vs/workbench/test/common/servicesTestUtils'; +import {TestTextFileService, TestEventService, TestContextService} from 'vs/test/utils/servicesTestUtils'; import {IEventService} from 'vs/platform/event/common/event'; import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; import {ITextFileService} from 'vs/workbench/parts/files/common/files'; const ExtensionId = Extensions.Editors; @@ -46,12 +45,11 @@ suite('Files - TextFileEditor', () => { let eventService = new TestEventService(); let contextService = new TestContextService(); - let services = new ServiceCollection(); - let instantiationService = new InstantiationService(services); + let instantiationService = new TestInstantiationService(); - services.set(IEventService, eventService); - services.set(IWorkspaceContextService, contextService); - services.set(ITextFileService, instantiationService.createInstance( TestTextFileService)); + instantiationService.stub(IEventService, eventService); + instantiationService.stub(IWorkspaceContextService, contextService); + instantiationService.stub(ITextFileService, instantiationService.createInstance( TestTextFileService)); strictEqual(Registry.as(ExtensionId).getEditor(instantiationService.createInstance(FileEditorInput, URI.file(join('C:\\', '/foo/bar/foobar.html')), 'test-text/html', void 0)), d1); strictEqual(Registry.as(ExtensionId).getEditor(instantiationService.createInstance(FileEditorInput, URI.file(join('C:\\', '/foo/bar/foobar.js')), 'test-text/javascript', void 0)), d1); diff --git a/src/vs/workbench/parts/output/test/outputWorker.test.ts b/src/vs/workbench/parts/output/test/outputWorker.test.ts index 592155c554160..364c04daae83a 100644 --- a/src/vs/workbench/parts/output/test/outputWorker.test.ts +++ b/src/vs/workbench/parts/output/test/outputWorker.test.ts @@ -9,7 +9,7 @@ import * as assert from 'assert'; import URI from 'vs/base/common/uri'; import {isMacintosh, isLinux} from 'vs/base/common/platform'; import {OutputWorker} from 'vs/workbench/parts/output/common/outputWorker'; -import {TestContextService} from 'vs/workbench/test/common/servicesTestUtils'; +import {TestContextService} from 'vs/test/utils/servicesTestUtils'; function toOSPath(p: string): string { if (isMacintosh || isLinux) { diff --git a/src/vs/workbench/parts/search/common/searchModel.ts b/src/vs/workbench/parts/search/common/searchModel.ts index 484e0c8b3d5b9..f20b7c56a6346 100644 --- a/src/vs/workbench/parts/search/common/searchModel.ts +++ b/src/vs/workbench/parts/search/common/searchModel.ts @@ -116,7 +116,7 @@ export class FileMatch extends Disposable { this.rawMatch.lineMatches.forEach((rawLineMatch) => { rawLineMatch.offsetAndLengths.forEach(offsetAndLength => { let match = new Match(this, rawLineMatch.preview, rawLineMatch.lineNumber, offsetAndLength[0], offsetAndLength[1]); - this._matches.set(match.id(), match); + this.add(match); }); }); } @@ -169,7 +169,7 @@ export class FileMatch extends Disposable { matches.forEach(range => { let match= new Match(this, this._model.getLineContent(range.startLineNumber), range.startLineNumber - 1, range.startColumn - 1, range.endColumn - range.startColumn); if (!this._removedMatches.contains(match.id())) { - this._matches.set(match.id(), match); + this.add(match); } }); @@ -234,6 +234,13 @@ export class FileMatch extends Disposable { this._onDispose.fire(); super.dispose(); } + + public add(match: Match, trigger?: boolean) { + this._matches.set(match.id(), match); + if (trigger) { + this._onChange.fire(true); + } + } } export interface IChangeEvent { @@ -457,12 +464,13 @@ export class SearchModel extends Disposable { } private onSearchCompleted(completed: ISearchComplete): ISearchComplete { + this.progressTimer.stop(); this.timerEvent.stop(); - this.telemetryService.publicLog('searchResultsShown', { count: this._searchResult.count(), fileCount: this._searchResult.fileCount() }); this.doneTimer.stop(); if (completed) { this._searchResult.add(completed.results, false); } + this.telemetryService.publicLog('searchResultsShown', { count: this._searchResult.count(), fileCount: this._searchResult.fileCount() }); return completed; } @@ -472,6 +480,7 @@ export class SearchModel extends Disposable { } else { this.progressTimer.stop(); this.doneTimer.stop(); + this.timerEvent.stop(); } } diff --git a/src/vs/workbench/parts/search/test/browser/searchViewlet.test.ts b/src/vs/workbench/parts/search/test/browser/searchViewlet.test.ts index cfe291d29e9b8..f51d78d6ef5fc 100644 --- a/src/vs/workbench/parts/search/test/browser/searchViewlet.test.ts +++ b/src/vs/workbench/parts/search/test/browser/searchViewlet.test.ts @@ -7,9 +7,7 @@ import * as assert from 'assert'; import uri from 'vs/base/common/uri'; import {Match, FileMatch, SearchResult} from 'vs/workbench/parts/search/common/searchModel'; -import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import {SearchSorter, SearchDataSource} from 'vs/workbench/parts/search/browser/searchResultsView'; import {IFileMatch, ILineMatch} from 'vs/platform/search/common/search'; @@ -22,10 +20,10 @@ function aFileMatch(path: string, searchResult?: SearchResult, ...lineMatches: I } suite('Search - Viewlet', () => { - let instantiation: IInstantiationService; + let instantiation: TestInstantiationService; setup(() => { - instantiation = new InstantiationService(new ServiceCollection()); + instantiation = new TestInstantiationService(); }); test('Data Source', function () { diff --git a/src/vs/workbench/parts/search/test/common/searchModel.test.ts b/src/vs/workbench/parts/search/test/common/searchModel.test.ts index d575589cabe44..ab788624f2cf0 100644 --- a/src/vs/workbench/parts/search/test/common/searchModel.test.ts +++ b/src/vs/workbench/parts/search/test/common/searchModel.test.ts @@ -5,134 +5,300 @@ 'use strict'; import * as assert from 'assert'; -import {Match, FileMatch, SearchResult} from 'vs/workbench/parts/search/common/searchModel'; +import * as sinon from 'sinon'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; +import { DeferredPPromise } from 'vs/test/utils/promiseTestUtils'; +import { PPromise } from 'vs/base/common/winjs.base'; +import { nullEvent } from 'vs/base/common/timer'; +import { SearchModel } from 'vs/workbench/parts/search/common/searchModel'; import URI from 'vs/base/common/uri'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; -import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation'; import {IFileMatch, ILineMatch} from 'vs/platform/search/common/search'; +import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry'; +import { ISearchService, ISearchComplete, ISearchProgressItem } from 'vs/platform/search/common/search'; +import { Range } from 'vs/editor/common/core/range'; -function aFileMatch(path: string, searchResult?: SearchResult, ...lineMatches: ILineMatch[]): FileMatch { - let rawMatch: IFileMatch= { - resource: URI.file('C:\\' + path), - lineMatches: lineMatches - }; - return new FileMatch(null, searchResult, rawMatch, null, null); -} +suite('SearchModel', () => { -suite('Search - Model', () => { - let instantiation: IInstantiationService; + let instantiationService: TestInstantiationService; + let restoreStubs; setup(() => { - instantiation = new InstantiationService(new ServiceCollection()); - }); - - test('Line Match', function () { - let fileMatch = aFileMatch('folder\\file.txt'); - let lineMatch = new Match(fileMatch, 'foo bar', 1, 0, 3); - assert.equal(lineMatch.text(), 'foo bar'); - assert.equal(lineMatch.range().startLineNumber, 2); - assert.equal(lineMatch.range().endLineNumber, 2); - assert.equal(lineMatch.range().startColumn, 1); - assert.equal(lineMatch.range().endColumn, 4); - }); - - test('Line Match - Remove', function () { - let fileMatch = aFileMatch('folder\\file.txt', null, ...[{ - preview: 'foo bar', - lineNumber: 1, - offsetAndLengths: [[0, 3]] - }]); - let lineMatch = fileMatch.matches()[0]; - fileMatch.remove(lineMatch); - assert.equal(fileMatch.matches().length, 0); - }); - - test('File Match', function () { - let fileMatch = aFileMatch('folder\\file.txt'); - assert.equal(fileMatch.matches(), 0); - assert.equal(fileMatch.resource().toString(), 'file:///c%3A/folder/file.txt'); - assert.equal(fileMatch.name(), 'file.txt'); - - fileMatch = aFileMatch('file.txt'); - assert.equal(fileMatch.matches(), 0); - assert.equal(fileMatch.resource().toString(), 'file:///c%3A/file.txt'); - assert.equal(fileMatch.name(), 'file.txt'); - }); - - test('Search Result', function () { - - let searchResult = instantiation.createInstance(SearchResult, null); - assert.equal(searchResult.isEmpty(), true); - - let raw: IFileMatch[] = []; - for (let i = 0; i < 10; i++) { - raw.push({ - resource: URI.parse('file://c:/' + i), - lineMatches: [{ - preview: String(i), - lineNumber: 1, - offsetAndLengths: [[0, 1]] - }] - }); - } - searchResult.add(raw); - - assert.equal(searchResult.isEmpty(), false); - assert.equal(searchResult.matches().length, 10); - }); - - test('Alle Drei Zusammen', function () { - - let searchResult = instantiation.createInstance(SearchResult, null); - let fileMatch = aFileMatch('far\\boo', searchResult); - let lineMatch = new Match(fileMatch, 'foo bar', 1, 0, 3); - - assert(lineMatch.parent() === fileMatch); - assert(fileMatch.parent() === searchResult); - }); - - //// ----- utils - //function lineHasDecorations(model: editor.IModel, lineNumber: number, decorations: { start: number; end: number; }[]): void { - // let lineDecorations:typeof decorations = []; - // let decs = model.getLineDecorations(lineNumber); - // for (let i = 0, len = decs.length; i < len; i++) { - // lineDecorations.push({ - // start: decs[i].range.startColumn, - // end: decs[i].range.endColumn - // }); - // } - // assert.deepEqual(lineDecorations, decorations); - //} - // - //function lineHasNoDecoration(model: editor.IModel, lineNumber: number): void { - // lineHasDecorations(model, lineNumber, []); - //} - // - //function lineHasDecoration(model: editor.IModel, lineNumber: number, start: number, end: number): void { - // lineHasDecorations(model, lineNumber, [{ - // start: start, - // end: end - // }]); - //} - //// ----- end utils - // - //test('Model Highlights', function () { - // - // let fileMatch = instantiation.createInstance(FileMatch, null, toUri('folder\\file.txt')); - // fileMatch.add(new Match(fileMatch, 'line2', 1, 0, 2)); - // fileMatch.connect(); - // lineHasDecoration(oneModel, 2, 1, 3); - //}); - // - //test('Dispose', function () { - // - // let fileMatch = instantiation.createInstance(FileMatch, null, toUri('folder\\file.txt')); - // fileMatch.add(new Match(fileMatch, 'line2', 1, 0, 2)); - // fileMatch.connect(); - // lineHasDecoration(oneModel, 2, 1, 3); - // - // fileMatch.dispose(); - // lineHasNoDecoration(oneModel, 2); - //}); + restoreStubs= []; + instantiationService= new TestInstantiationService(); + instantiationService.stub(ITelemetryService); + }); + + teardown(() => { + restoreStubs.forEach(element => { + element.restore(); + }); + }); + + test('Search Model: Search adds to results', function () { + let results= [aRawMatch('file://c:/1', aLineMatch('preview 1', 1, [[1, 3], [4, 7]])), aRawMatch('file://c:/2', aLineMatch('preview 2'))]; + instantiationService.stub(ISearchService, 'search', PPromise.as({results: results})); + + let testObject= instantiationService.createInstance(SearchModel); + testObject.search({contentPattern: {pattern: 'somestring'}, type: 1}); + + let actual= testObject.searchResult.matches(); + + assert.equal(2, actual.length); + assert.equal('file://c:/1', actual[0].resource().toString()); + + let actuaMatches= actual[0].matches(); + assert.equal(2, actuaMatches.length); + assert.equal('preview 1', actuaMatches[0].text()); + assert.ok(new Range(2, 2, 2, 5).equalsRange(actuaMatches[0].range())); + assert.equal('preview 1', actuaMatches[1].text()); + assert.ok(new Range(2, 5, 2, 12).equalsRange(actuaMatches[1].range())); + + actuaMatches= actual[1].matches(); + assert.equal(1, actuaMatches.length); + assert.equal('preview 2', actuaMatches[0].text()); + assert.ok(new Range(2, 1, 2, 2).equalsRange(actuaMatches[0].range())); + }); + + test('Search Model: Search adds to results during progress', function (done) { + let results= [aRawMatch('file://c:/1', aLineMatch('preview 1', 1, [[1, 3], [4, 7]])), aRawMatch('file://c:/2', aLineMatch('preview 2'))]; + let promise= new DeferredPPromise(); + instantiationService.stub(ISearchService, 'search', promise); + + let testObject= instantiationService.createInstance(SearchModel); + let result= testObject.search({contentPattern: {pattern: 'somestring'}, type: 1}); + + promise.progress(results[0]); + promise.progress(results[1]); + promise.complete({results: []}); + + result.done(() => { + let actual= testObject.searchResult.matches(); + + assert.equal(2, actual.length); + assert.equal('file://c:/1', actual[0].resource().toString()); + + let actuaMatches= actual[0].matches(); + assert.equal(2, actuaMatches.length); + assert.equal('preview 1', actuaMatches[0].text()); + assert.ok(new Range(2, 2, 2, 5).equalsRange(actuaMatches[0].range())); + assert.equal('preview 1', actuaMatches[1].text()); + assert.ok(new Range(2, 5, 2, 12).equalsRange(actuaMatches[1].range())); + + actuaMatches= actual[1].matches(); + assert.equal(1, actuaMatches.length); + assert.equal('preview 2', actuaMatches[0].text()); + assert.ok(new Range(2, 1, 2, 2).equalsRange(actuaMatches[0].range())); + + done(); + }); + }); + + test('Search Model: Search reports telemetry on search completed', function () { + let target= instantiationService.spy(ITelemetryService, 'publicLog'); + let results= [aRawMatch('file://c:/1', aLineMatch('preview 1', 1, [[1, 3], [4, 7]])), aRawMatch('file://c:/2', aLineMatch('preview 2'))]; + instantiationService.stub(ISearchService, 'search', PPromise.as({results: results})); + + let testObject= instantiationService.createInstance(SearchModel); + testObject.search({contentPattern: {pattern: 'somestring'}, type: 1}); + + assert.ok(target.calledOnce); + assert.deepEqual(['searchResultsShown', {count: 3, fileCount: 2}], target.args[0]); + }); + + test('Search Model: Search reports timed telemetry on search when progress is not called', function () { + let target2= sinon.spy(); + stub(nullEvent, 'stop', target2); + let target1= sinon.stub().returns(nullEvent); + instantiationService.stub(ITelemetryService, 'timedPublicLog', target1); + + instantiationService.stub(ISearchService, 'search', PPromise.as({results: []})); + + let testObject= instantiationService.createInstance(SearchModel); + testObject.search({contentPattern: {pattern: 'somestring'}, type: 1}); + + assert.ok(target1.calledTwice); + assert.ok(target1.calledWith('searchResultsFirstRender')); + assert.ok(target1.calledWith('searchResultsFinished')); + assert.ok(target2.calledThrice); + }); + + test('Search Model: Search reports timed telemetry on search when progress is called', function (done) { + let target2= sinon.spy(); + stub(nullEvent, 'stop', target2); + let target1= sinon.stub().returns(nullEvent); + instantiationService.stub(ITelemetryService, 'timedPublicLog', target1); + + let promise= new DeferredPPromise(); + instantiationService.stub(ISearchService, 'search', promise); + + let testObject= instantiationService.createInstance(SearchModel); + let result= testObject.search({contentPattern: {pattern: 'somestring'}, type: 1}); + + promise.progress(aRawMatch('file://c:/1', aLineMatch('some preview'))); + promise.complete({results: []}); + + result.done(() => { + assert.ok(target1.calledTwice); + assert.ok(target1.calledWith('searchResultsFirstRender')); + assert.ok(target1.calledWith('searchResultsFinished')); + assert.equal(4, target2.callCount); + + done(); + }); + }); + + test('Search Model: Search reports timed telemetry on search when error is called', function (done) { + let target2= sinon.spy(); + stub(nullEvent, 'stop', target2); + let target1= sinon.stub().returns(nullEvent); + instantiationService.stub(ITelemetryService, 'timedPublicLog', target1); + + let promise= new DeferredPPromise(); + instantiationService.stub(ISearchService, 'search', promise); + + let testObject= instantiationService.createInstance(SearchModel); + let result= testObject.search({contentPattern: {pattern: 'somestring'}, type: 1}); + + promise.error('error'); + + result.done(() => {}, () => { + assert.ok(target1.calledTwice); + assert.ok(target1.calledWith('searchResultsFirstRender')); + assert.ok(target1.calledWith('searchResultsFinished')); + assert.ok(target2.calledThrice); + + done(); + }); + }); + + test('Search Model: Search reports timed telemetry on search when error is cancelled error', function (done) { + let target2= sinon.spy(); + stub(nullEvent, 'stop', target2); + let target1= sinon.stub().returns(nullEvent); + instantiationService.stub(ITelemetryService, 'timedPublicLog', target1); + + let promise= new DeferredPPromise(); + instantiationService.stub(ISearchService, 'search', promise); + + let testObject= instantiationService.createInstance(SearchModel); + let result= testObject.search({contentPattern: {pattern: 'somestring'}, type: 1}); + + promise.cancel(); + + result.done(() => {}, () => { + assert.ok(target1.calledTwice); + assert.ok(target1.calledWith('searchResultsFirstRender')); + assert.ok(target1.calledWith('searchResultsFinished')); + assert.ok(target2.calledThrice); + done(); + }); + }); + + test('Search Model: Search results are cleared during search', function () { + let results= [aRawMatch('file://c:/1', aLineMatch('preview 1', 1, [[1, 3], [4, 7]])), aRawMatch('file://c:/2', aLineMatch('preview 2'))]; + instantiationService.stub(ISearchService, 'search', PPromise.as({results: results})); + let testObject:SearchModel= instantiationService.createInstance(SearchModel); + testObject.search({contentPattern: {pattern: 'somestring'}, type: 1}); + assert.ok(!testObject.searchResult.isEmpty()); + + instantiationService.stub(ISearchService, 'search', new DeferredPPromise()); + + testObject.search({contentPattern: {pattern: 'somestring'}, type: 1}); + assert.ok(testObject.searchResult.isEmpty()); + }); + + test('Search Model: Previous search is cancelled when new search is called', function () { + let target= sinon.spy(); + instantiationService.stub(ISearchService, 'search', new DeferredPPromise((c, e, p) => {}, target)); + let testObject:SearchModel= instantiationService.createInstance(SearchModel); + + testObject.search({contentPattern: {pattern: 'somestring'}, type: 1}); + instantiationService.stub(ISearchService, 'search', new DeferredPPromise()); + testObject.search({contentPattern: {pattern: 'somestring'}, type: 1}); + + assert.ok(target.calledOnce); + }); + + test('Search Model: isReplaceActive return false if no replace text is set', function () { + let testObject:SearchModel= instantiationService.createInstance(SearchModel); + + assert.ok(!testObject.isReplaceActive()); + }); + + test('Search Model: isReplaceActive return false if replace text is set to null', function () { + let testObject:SearchModel= instantiationService.createInstance(SearchModel); + testObject.replaceText= null; + + assert.ok(!testObject.isReplaceActive()); + }); + + test('Search Model: isReplaceActive return false if replace text is set to undefined', function () { + let testObject:SearchModel= instantiationService.createInstance(SearchModel); + testObject.replaceText= void 0; + + assert.ok(!testObject.isReplaceActive()); + }); + + test('Search Model: isReplaceActive return true if replace text is set to empty string', function () { + let testObject:SearchModel= instantiationService.createInstance(SearchModel); + testObject.replaceText= ''; + + assert.ok(testObject.isReplaceActive()); + }); + + test('Search Model: isReplaceActive return true if replace text is set to non empty string', function () { + let testObject:SearchModel= instantiationService.createInstance(SearchModel); + testObject.replaceText= 'some value'; + + assert.ok(testObject.isReplaceActive()); + }); + + test('Search Model: hasReplaceText return false if no replace text is set', function () { + let testObject:SearchModel= instantiationService.createInstance(SearchModel); + + assert.ok(!testObject.hasReplaceText()); + }); + + test('Search Model: hasReplaceText return false if replace text is set to null', function () { + let testObject:SearchModel= instantiationService.createInstance(SearchModel); + testObject.replaceText= null; + + assert.ok(!testObject.hasReplaceText()); + }); + + test('Search Model: hasReplaceText return false if replace text is set to undefined', function () { + let testObject:SearchModel= instantiationService.createInstance(SearchModel); + testObject.replaceText= void 0; + + assert.ok(!testObject.hasReplaceText()); + }); + + test('Search Model: hasReplaceText return false if replace text is set to empty string', function () { + let testObject:SearchModel= instantiationService.createInstance(SearchModel); + testObject.replaceText= ''; + + assert.ok(!testObject.hasReplaceText()); + }); + + test('Search Model: hasReplaceText return true if replace text is set to non empty string', function () { + let testObject:SearchModel= instantiationService.createInstance(SearchModel); + testObject.replaceText= 'some value'; + + assert.ok(testObject.hasReplaceText()); + }); + + function aRawMatch(resource: string, ...lineMatches: ILineMatch[]): IFileMatch { + return { resource: URI.parse(resource), lineMatches }; + } + + function aLineMatch(preview: string, lineNumber: number = 1, offsetAndLengths: number[][] = [[0, 1]]): ILineMatch { + return { preview, lineNumber, offsetAndLengths }; + } + + function stub(arg1, arg2, arg3) : sinon.SinonStub { + const stub= sinon.stub(arg1, arg2, arg3); + restoreStubs.push(stub); + return stub; + } + }); \ No newline at end of file diff --git a/src/vs/workbench/parts/search/test/common/searchResult.test.ts b/src/vs/workbench/parts/search/test/common/searchResult.test.ts new file mode 100644 index 0000000000000..2d1abbaaa04fa --- /dev/null +++ b/src/vs/workbench/parts/search/test/common/searchResult.test.ts @@ -0,0 +1,212 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +'use strict'; + +import * as assert from 'assert'; +import * as sinon from 'sinon'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; +import { Match, FileMatch, SearchResult, SearchModel } from 'vs/workbench/parts/search/common/searchModel'; +import URI from 'vs/base/common/uri'; +import {IFileMatch, ILineMatch} from 'vs/platform/search/common/search'; +import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry'; +import { Range } from 'vs/editor/common/core/range'; + +suite('SearchResult', () => { + + let instantiationService: TestInstantiationService; + + setup(() => { + instantiationService= new TestInstantiationService(); + instantiationService.stub(ITelemetryService); + }); + + test('Line Match', function () { + let fileMatch = aFileMatch('folder\\file.txt'); + let lineMatch = new Match(fileMatch, 'foo bar', 1, 0, 3); + assert.equal(lineMatch.text(), 'foo bar'); + assert.equal(lineMatch.range().startLineNumber, 2); + assert.equal(lineMatch.range().endLineNumber, 2); + assert.equal(lineMatch.range().startColumn, 1); + assert.equal(lineMatch.range().endColumn, 4); + }); + + test('Line Match - Remove', function () { + let fileMatch = aFileMatch('folder\\file.txt', null, ...[{ + preview: 'foo bar', + lineNumber: 1, + offsetAndLengths: [[0, 3]] + }]); + let lineMatch = fileMatch.matches()[0]; + fileMatch.remove(lineMatch); + assert.equal(fileMatch.matches().length, 0); + }); + + test('File Match', function () { + let fileMatch = aFileMatch('folder\\file.txt'); + assert.equal(fileMatch.matches(), 0); + assert.equal(fileMatch.resource().toString(), 'file:///c%3A/folder/file.txt'); + assert.equal(fileMatch.name(), 'file.txt'); + + fileMatch = aFileMatch('file.txt'); + assert.equal(fileMatch.matches(), 0); + assert.equal(fileMatch.resource().toString(), 'file:///c%3A/file.txt'); + assert.equal(fileMatch.name(), 'file.txt'); + }); + + test('Alle Drei Zusammen', function () { + let searchResult = instantiationService.createInstance(SearchResult, null); + let fileMatch = aFileMatch('far\\boo', searchResult); + let lineMatch = new Match(fileMatch, 'foo bar', 1, 0, 3); + + assert(lineMatch.parent() === fileMatch); + assert(fileMatch.parent() === searchResult); + }); + + test('Search Result: Adding a raw match will add a file match with line matches', function () { + let testObject = aSearchResult(); + let target= [aRawMatch('file://c:/', aLineMatch('preview 1', 1, [[1, 3], [4, 7]]), aLineMatch('preview 2'))]; + + testObject.add(target); + + assert.equal(3, testObject.count()); + + let actual= testObject.matches(); + assert.equal(1, actual.length); + assert.equal('file://c:/', actual[0].resource().toString()); + + let actuaMatches= actual[0].matches(); + assert.equal(3, actuaMatches.length); + + assert.equal('preview 1', actuaMatches[0].text()); + assert.ok(new Range(2, 2, 2, 5).equalsRange(actuaMatches[0].range())); + + assert.equal('preview 1', actuaMatches[1].text()); + assert.ok(new Range(2, 5, 2, 12).equalsRange(actuaMatches[1].range())); + + assert.equal('preview 2', actuaMatches[2].text()); + assert.ok(new Range(2, 1, 2, 2).equalsRange(actuaMatches[2].range())); + }); + + test('Search Result: Adding multiple raw matches', function () { + let testObject = aSearchResult(); + let target= [aRawMatch('file://c:/1', aLineMatch('preview 1', 1, [[1, 3], [4, 7]])), aRawMatch('file://c:/2', aLineMatch('preview 2'))]; + + testObject.add(target); + + assert.equal(3, testObject.count()); + + let actual= testObject.matches(); + assert.equal(2, actual.length); + assert.equal('file://c:/1', actual[0].resource().toString()); + + let actuaMatches= actual[0].matches(); + assert.equal(2, actuaMatches.length); + assert.equal('preview 1', actuaMatches[0].text()); + assert.ok(new Range(2, 2, 2, 5).equalsRange(actuaMatches[0].range())); + assert.equal('preview 1', actuaMatches[1].text()); + assert.ok(new Range(2, 5, 2, 12).equalsRange(actuaMatches[1].range())); + + actuaMatches= actual[1].matches(); + assert.equal(1, actuaMatches.length); + assert.equal('preview 2', actuaMatches[0].text()); + assert.ok(new Range(2, 1, 2, 2).equalsRange(actuaMatches[0].range())); + }); + + test('Search Result: Dispose disposes matches', function () { + let target1= sinon.spy(); + let target2= sinon.spy(); + + let testObject = aSearchResult(); + testObject.add([aRawMatch('file://c:/1', aLineMatch('preview 1')), aRawMatch('file://c:/2', aLineMatch('preview 2'))]); + + testObject.matches()[0].onDispose(target1); + testObject.matches()[1].onDispose(target2); + + testObject.dispose(); + + assert.ok(testObject.isEmpty()); + assert.ok(target1.calledOnce); + assert.ok(target2.calledOnce); + }); + + test('Search Result: Removing all line matches and adding back will add file back to result', function () { + let testObject = aSearchResult(); + testObject.add([aRawMatch('file://c:/1', aLineMatch('preview 1'))]); + let target= testObject.matches()[0]; + let matchToRemove= target.matches()[0]; + target.remove(matchToRemove); + + assert.ok(testObject.isEmpty()); + target.add(matchToRemove, true); + + assert.equal(1, testObject.fileCount()); + assert.equal(target, testObject.matches()[0]); + }); + + //// ----- utils + //function lineHasDecorations(model: editor.IModel, lineNumber: number, decorations: { start: number; end: number; }[]): void { + // let lineDecorations:typeof decorations = []; + // let decs = model.getLineDecorations(lineNumber); + // for (let i = 0, len = decs.length; i < len; i++) { + // lineDecorations.push({ + // start: decs[i].range.startColumn, + // end: decs[i].range.endColumn + // }); + // } + // assert.deepEqual(lineDecorations, decorations); + //} + // + //function lineHasNoDecoration(model: editor.IModel, lineNumber: number): void { + // lineHasDecorations(model, lineNumber, []); + //} + // + //function lineHasDecoration(model: editor.IModel, lineNumber: number, start: number, end: number): void { + // lineHasDecorations(model, lineNumber, [{ + // start: start, + // end: end + // }]); + //} + //// ----- end utils + // + //test('Model Highlights', function () { + // + // let fileMatch = instantiation.createInstance(FileMatch, null, toUri('folder\\file.txt')); + // fileMatch.add(new Match(fileMatch, 'line2', 1, 0, 2)); + // fileMatch.connect(); + // lineHasDecoration(oneModel, 2, 1, 3); + //}); + // + //test('Dispose', function () { + // + // let fileMatch = instantiation.createInstance(FileMatch, null, toUri('folder\\file.txt')); + // fileMatch.add(new Match(fileMatch, 'line2', 1, 0, 2)); + // fileMatch.connect(); + // lineHasDecoration(oneModel, 2, 1, 3); + // + // fileMatch.dispose(); + // lineHasNoDecoration(oneModel, 2); + //}); + + function aFileMatch(path: string, searchResult?: SearchResult, ...lineMatches: ILineMatch[]): FileMatch { + let rawMatch: IFileMatch = { + resource: URI.file('C:\\' + path), + lineMatches: lineMatches + }; + return new FileMatch(null, searchResult, rawMatch, null, null); + } + + function aSearchResult(): SearchResult { + let searchModel = instantiationService.createInstance(SearchModel); + return searchModel.searchResult; + } + + function aRawMatch(resource: string, ...lineMatches: ILineMatch[]): IFileMatch { + return { resource: URI.parse(resource), lineMatches }; + } + + function aLineMatch(preview: string, lineNumber: number = 1, offsetAndLengths: number[][] = [[0, 1]]): ILineMatch { + return { preview, lineNumber, offsetAndLengths }; + } +}); \ No newline at end of file diff --git a/src/vs/workbench/parts/welcome/test/common/gettingStarted.test.ts b/src/vs/workbench/parts/welcome/test/common/gettingStarted.test.ts index 894c0c7aecd88..3e57cf0d7862e 100644 --- a/src/vs/workbench/parts/welcome/test/common/gettingStarted.test.ts +++ b/src/vs/workbench/parts/welcome/test/common/gettingStarted.test.ts @@ -6,14 +6,11 @@ 'use strict'; import * as assert from 'assert'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import {AbstractGettingStarted} from 'vs/workbench/parts/welcome/common/abstractGettingStarted'; -import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation'; import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace'; import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry'; import {IStorageService} from 'vs/platform/storage/common/storage'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; -import {TPromise} from 'vs/base/common/winjs.base'; class TestGettingStarted extends AbstractGettingStarted { public lastUrl: string; @@ -24,15 +21,15 @@ class TestGettingStarted extends AbstractGettingStarted { } suite('Workbench - GettingStarted', () => { - let instantiation: IInstantiationService = null; + let instantiation: TestInstantiationService = null; let welcomePageEnvConfig: string = null; let hideWelcomeSettingsValue: string = null; let machineId: string = null; let appName: string = null; suiteSetup(() => { - let services = new ServiceCollection(); - services.set(IWorkspaceContextService, { + instantiation = new TestInstantiationService(); + instantiation.stub(IWorkspaceContextService, { getConfiguration: () => { return { env: { @@ -42,14 +39,10 @@ suite('Workbench - GettingStarted', () => { }; } }); - services.set(ITelemetryService, { - getTelemetryInfo: () => TPromise.as({ machineId: machineId }) - }); - services.set(IStorageService, { + instantiation.stub(IStorageService, { get: () => hideWelcomeSettingsValue, store: (value) => hideWelcomeSettingsValue = value }); - instantiation = new InstantiationService(services); }); suiteTeardown(() => { @@ -71,6 +64,7 @@ suite('Workbench - GettingStarted', () => { welcomePageEnvConfig = 'base url'; appName = 'some app'; machineId = '123'; + instantiation.stubPromise(ITelemetryService, 'getTelemetryInfo', { machineId: machineId }); let gettingStarted = instantiation.createInstance(TestGettingStarted); assert(gettingStarted.lastUrl === `${welcomePageEnvConfig}&&from=${appName}&&id=${machineId}`, 'a page is opened when welcomePage is configured && first run'); assert(hideWelcomeSettingsValue !== null, 'a flag is set to hide welcome page'); diff --git a/src/vs/workbench/test/browser/part.test.ts b/src/vs/workbench/test/browser/part.test.ts index 7190431e04790..e8a83e1964d36 100644 --- a/src/vs/workbench/test/browser/part.test.ts +++ b/src/vs/workbench/test/browser/part.test.ts @@ -9,7 +9,7 @@ import * as assert from 'assert'; import {Build, Builder} from 'vs/base/browser/builder'; import {Part} from 'vs/workbench/browser/part'; import * as Types from 'vs/base/common/types'; -import * as TestUtils from 'vs/workbench/test/common/servicesTestUtils'; +import * as TestUtils from 'vs/test/utils/servicesTestUtils'; import {BaseWorkspaceContextService} from 'vs/platform/workspace/common/baseWorkspaceContextService'; import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace'; import {IStorageService} from 'vs/platform/storage/common/storage'; diff --git a/src/vs/workbench/test/browser/parts/editor/baseEditor.test.ts b/src/vs/workbench/test/browser/parts/editor/baseEditor.test.ts index d3251f6aa4bc2..d97d2ca2622de 100644 --- a/src/vs/workbench/test/browser/parts/editor/baseEditor.test.ts +++ b/src/vs/workbench/test/browser/parts/editor/baseEditor.test.ts @@ -8,8 +8,8 @@ import * as assert from 'assert'; import {BaseEditor, EditorInputAction, EditorInputActionContributor, EditorDescriptor} from 'vs/workbench/browser/parts/editor/baseEditor'; import {EditorInput, EditorOptions, Extensions, IEditorRegistry, IEditorInputFactory} from 'vs/workbench/common/editor'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; import * as Platform from 'vs/platform/platform'; import {SyncDescriptor} from 'vs/platform/instantiation/common/descriptors'; import {StringEditorInput} from 'vs/workbench/common/editor/stringEditorInput'; @@ -190,7 +190,7 @@ suite('Workbench BaseEditor', () => { EditorRegistry.registerEditor(d2, new SyncDescriptor(StringEditorInput)); EditorRegistry.registerEditor(d1, new SyncDescriptor(MyStringInput)); - let inst = new InstantiationService(); + let inst = new TestInstantiationService(); inst.createInstance(EditorRegistry.getEditor(inst.createInstance(MyStringInput, 'fake', '', '', mime.MIME_TEXT, false)), 'id').then(function (editor) { assert.strictEqual(editor.getId(), 'myEditor'); @@ -211,7 +211,7 @@ suite('Workbench BaseEditor', () => { EditorRegistry.registerEditor(d1, new SyncDescriptor(StringEditorInput)); - let inst = new InstantiationService(); + let inst = new TestInstantiationService(); inst.createInstance(EditorRegistry.getEditor(inst.createInstance(MyStringInput, 'fake', '', '', mime.MIME_TEXT, false)), 'id').then(function (editor) { assert.strictEqual('myOtherEditor', editor.getId()); @@ -221,7 +221,7 @@ suite('Workbench BaseEditor', () => { }); test('Editor Input Action - triggers isEnabled properly', function () { - let inst = new InstantiationService(); + let inst = new TestInstantiationService(); let action = new MyAction('id', 'label'); action.input = inst.createInstance(StringEditorInput, 'input', '', '', mime.MIME_TEXT, false); @@ -229,7 +229,7 @@ suite('Workbench BaseEditor', () => { }); test('Editor Input Action Contributor', function () { - let inst = new InstantiationService(); + let inst = new TestInstantiationService(); let contributor = new MyEditorInputActionContributor(); @@ -265,7 +265,7 @@ suite('Workbench BaseEditor', () => { }); test('Editor Input Factory', function () { - EditorRegistry.setInstantiationService(new InstantiationService()); + EditorRegistry.setInstantiationService(new TestInstantiationService()); EditorRegistry.registerEditorInputFactory('myInputId', MyInputFactory); let factory = EditorRegistry.getEditorInputFactory('myInputId'); diff --git a/src/vs/workbench/test/browser/parts/quickOpen/quickopen.test.ts b/src/vs/workbench/test/browser/parts/quickOpen/quickopen.test.ts index b735ff398c032..733980a421a84 100644 --- a/src/vs/workbench/test/browser/parts/quickOpen/quickopen.test.ts +++ b/src/vs/workbench/test/browser/parts/quickOpen/quickopen.test.ts @@ -7,7 +7,7 @@ import * as assert from 'assert'; import 'vs/workbench/browser/parts/editor/editor.contribution'; // make sure to load all contributed editor things into tests -import {TestQuickOpenService} from 'vs/workbench/test/common/servicesTestUtils'; +import {TestQuickOpenService} from 'vs/test/utils/servicesTestUtils'; import {Registry} from 'vs/platform/platform'; import {QuickOpenHandlerDescriptor, IQuickOpenRegistry, Extensions as QuickOpenExtensions, QuickOpenAction} from 'vs/workbench/browser/quickopen'; import {Extensions} from 'vs/workbench/common/editor'; diff --git a/src/vs/workbench/test/browser/services.test.ts b/src/vs/workbench/test/browser/services.test.ts index 080825826fd75..ed527a31e5f39 100644 --- a/src/vs/workbench/test/browser/services.test.ts +++ b/src/vs/workbench/test/browser/services.test.ts @@ -6,6 +6,7 @@ 'use strict'; import * as assert from 'assert'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import {Promise, TPromise} from 'vs/base/common/winjs.base'; import paths = require('vs/base/common/paths'); import URI from 'vs/base/common/uri'; @@ -17,8 +18,6 @@ import {IStorageService} from 'vs/platform/storage/common/storage'; import {IConfigurationService} from 'vs/platform/configuration/common/configuration'; import {ILifecycleService, NullLifecycleService} from 'vs/platform/lifecycle/common/lifecycle'; import {IFileService} from 'vs/platform/files/common/files'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService'; import PartService = require('vs/workbench/services/part/common/partService'); import {BaseEditor} from 'vs/workbench/browser/parts/editor/baseEditor'; @@ -28,7 +27,7 @@ import {StringEditorModel} from 'vs/workbench/common/editor/stringEditorModel'; import {FileEditorInput} from 'vs/workbench/parts/files/common/editors/fileEditorInput'; import {TextFileEditorModel} from 'vs/workbench/parts/files/common/editors/textFileEditorModel'; import {ITextFileService} from 'vs/workbench/parts/files/common/files'; -import {TestTextFileService, TestEventService, TestPartService, TestStorageService, TestConfigurationService, TestRequestService, TestContextService, TestWorkspace, TestEditorService, MockRequestService} from 'vs/workbench/test/common/servicesTestUtils'; +import {createMockModelService, TestTextFileService, TestEventService, TestPartService, TestStorageService, TestConfigurationService, TestRequestService, TestContextService, TestWorkspace, TestEditorService, MockRequestService} from 'vs/test/utils/servicesTestUtils'; import {Viewlet} from 'vs/workbench/browser/viewlet'; import {EventType} from 'vs/workbench/common/events'; import {ITelemetryService, NullTelemetryService} from 'vs/platform/telemetry/common/telemetry'; @@ -39,7 +38,6 @@ import {IViewletService} from 'vs/workbench/services/viewlet/common/viewletServi import {IViewlet} from 'vs/workbench/common/viewlet'; import {Position, Direction, IEditor} from 'vs/platform/editor/common/editor'; import {IEventService} from 'vs/platform/event/common/event'; -import {createMockModeService, createMockModelService} from 'vs/editor/test/common/servicesTestUtils'; let activeViewlet: Viewlet = {}; let activeEditor: BaseEditor = { @@ -269,32 +267,29 @@ suite('Workbench UI Services', () => { return null; }); - let telemetryService = NullTelemetryService; - let services = new ServiceCollection(); - let inst = new InstantiationService(services); - services.set(IEventService, eventService); - services.set(IWorkspaceContextService, contextService); - services.set(IRequestService, requestService); - services.set(ITelemetryService, telemetryService); - services.set(IConfigurationService, new TestConfigurationService()); - services.set(IUntitledEditorService, inst.createInstance(UntitledEditorService)); - services.set(IStorageService, new TestStorageService()); - services.set(IWorkbenchEditorService, editorService); - services.set(PartService.IPartService, new TestPartService()); - services.set(IModeService, createMockModeService()); - services.set(IModelService, createMockModelService()); - services.set(ILifecycleService, NullLifecycleService); - services.set(IFileService, TestFileService); - - services.set(ITextFileService, inst.createInstance(TestTextFileService)); - services['instantiationService'] = inst; - - let activeInput: EditorInput = inst.createInstance(FileEditorInput, toResource('/something.js'), 'text/javascript', void 0); + let instantiationService= new TestInstantiationService(); + instantiationService.stub(IEventService, eventService); + instantiationService.stub(IWorkspaceContextService, contextService); + instantiationService.stub(IRequestService, requestService); + instantiationService.stub(ITelemetryService); + instantiationService.stub(IConfigurationService, new TestConfigurationService()); + instantiationService.stub(IUntitledEditorService, instantiationService.createInstance(UntitledEditorService)); + instantiationService.stub(IStorageService, new TestStorageService()); + instantiationService.stub(IWorkbenchEditorService, editorService); + instantiationService.stub(PartService.IPartService, new TestPartService()); + instantiationService.stub(IModeService); + instantiationService.stub(IModelService, createMockModelService(instantiationService)); + instantiationService.stub(ILifecycleService, NullLifecycleService); + instantiationService.stub(IFileService, TestFileService); + + instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService)); + + let activeInput: EditorInput = instantiationService.createInstance(FileEditorInput, toResource('/something.js'), 'text/javascript', void 0); let testEditorPart = new TestEditorPart(); testEditorPart.setActiveEditorInput(activeInput); - let service: WorkbenchEditorService = inst.createInstance(WorkbenchEditorService, testEditorPart); + let service: WorkbenchEditorService = instantiationService.createInstance(WorkbenchEditorService, testEditorPart); assert.strictEqual(service.getActiveEditor(), activeEditor); assert.strictEqual(service.getActiveEditorInput(), activeInput); @@ -331,7 +326,7 @@ suite('Workbench UI Services', () => { }); // Resolve Editor Model (Typed EditorInput) - let input = inst.createInstance(StringEditorInput, 'name', 'description', 'hello world', 'text/plain', false); + let input = instantiationService.createInstance(StringEditorInput, 'name', 'description', 'hello world', 'text/plain', false); service.resolveEditorModel(input, true).then((model: StringEditorModel) => { assert(model instanceof StringEditorModel); @@ -357,25 +352,24 @@ suite('Workbench UI Services', () => { let requestService = new TestRequestService(); let telemetryService = NullTelemetryService; - let services = new ServiceCollection(); - let inst = new InstantiationService(services); - services.set(IEventService, eventService); - services.set(IWorkspaceContextService, contextService); - services.set(IRequestService, requestService); - services.set(ITelemetryService, telemetryService); - services.set(IStorageService, new TestStorageService()); - services.set(IUntitledEditorService, inst.createInstance(UntitledEditorService)); - services.set(IWorkbenchEditorService, editorService); - services.set(PartService.IPartService, new TestPartService()); - services.set(ILifecycleService, NullLifecycleService); - services.set(IConfigurationService, new TestConfigurationService()); - services.set(ITextFileService, inst.createInstance(TestTextFileService)); - let activeInput: EditorInput = inst.createInstance(FileEditorInput, toResource('/something.js'), 'text/javascript', void 0); + let instantiationService = new TestInstantiationService(); + instantiationService.stub(IEventService, eventService); + instantiationService.stub(IRequestService, requestService); + instantiationService.stub(ITelemetryService, telemetryService); + instantiationService.stub(IStorageService, new TestStorageService()); + instantiationService.stub(IUntitledEditorService, instantiationService.createInstance(UntitledEditorService)); + instantiationService.stub(IWorkbenchEditorService, editorService); + instantiationService.stub(PartService.IPartService, new TestPartService()); + instantiationService.stub(ILifecycleService, NullLifecycleService); + instantiationService.stub(IWorkspaceContextService, contextService); + instantiationService.stub(IConfigurationService, new TestConfigurationService()); + instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService)); + let activeInput: EditorInput = instantiationService.createInstance(FileEditorInput, toResource('/something.js'), 'text/javascript', void 0); let testEditorPart = new TestEditorPart(); testEditorPart.setActiveEditorInput(activeInput); - inst.createInstance(WorkbenchEditorService, testEditorPart); + instantiationService.createInstance(WorkbenchEditorService, testEditorPart); class MyEditor extends BaseEditor { constructor(id: string) { @@ -394,10 +388,10 @@ suite('Workbench UI Services', () => { } } - let ed = inst.createInstance(MyEditor, 'my.editor'); + let ed = instantiationService.createInstance(MyEditor, 'my.editor'); - let inp = inst.createInstance(StringEditorInput, 'name', 'description', 'hello world', 'text/plain', false); - let delegate: any = inst.createInstance(DelegatingWorkbenchEditorService, (input: EditorInput, options?: EditorOptions, arg3?: any) => { + let inp = instantiationService.createInstance(StringEditorInput, 'name', 'description', 'hello world', 'text/plain', false); + let delegate: any = instantiationService.createInstance(DelegatingWorkbenchEditorService, (input: EditorInput, options?: EditorOptions, arg3?: any) => { assert.strictEqual(input, inp); return TPromise.as(ed); diff --git a/src/vs/workbench/test/common/editor/editorModel.test.ts b/src/vs/workbench/test/common/editor/editorModel.test.ts index 1c49d3f07d041..bd69729cd874a 100644 --- a/src/vs/workbench/test/common/editor/editorModel.test.ts +++ b/src/vs/workbench/test/common/editor/editorModel.test.ts @@ -6,6 +6,7 @@ 'use strict'; import * as assert from 'assert'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import {EditorModel} from 'vs/workbench/common/editor'; import {BaseTextEditorModel} from 'vs/workbench/common/editor/textEditorModel'; import {TextDiffEditorModel} from 'vs/workbench/common/editor/textDiffEditorModel'; @@ -13,15 +14,21 @@ import {DiffEditorInput} from 'vs/workbench/common/editor/diffEditorInput'; import {StringEditorInput} from 'vs/workbench/common/editor/stringEditorInput'; import {IModelService} from 'vs/editor/common/services/modelService'; import {IModeService} from 'vs/editor/common/services/modeService'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; -import {createMockModelService, createMockModeService} from 'vs/editor/test/common/servicesTestUtils'; +import { createMockModelService } from 'vs/test/utils/servicesTestUtils'; class MyEditorModel extends EditorModel { } class MyTextEditorModel extends BaseTextEditorModel { } suite('Workbench - EditorModel', () => { + let instantiationService: TestInstantiationService; + let modeService: IModeService; + + setup(() => { + instantiationService= new TestInstantiationService(); + modeService= instantiationService.stub(IModeService); + }); + test('EditorModel', function (done) { let m = new MyEditorModel(); m.load().then(function (model) { @@ -31,8 +38,7 @@ suite('Workbench - EditorModel', () => { }); test('BaseTextEditorModel', function (done) { - let modelService = createMockModelService(); - let modeService = createMockModeService(); + let modelService = createMockModelService(instantiationService); let m = new MyTextEditorModel(modelService, modeService); m.load().then(function (model: any) { @@ -47,12 +53,9 @@ suite('Workbench - EditorModel', () => { }); test('TextDiffEditorModel', function (done) { - let services = new ServiceCollection(); - services.set(IModeService, createMockModeService()); - services.set(IModelService, createMockModelService()); - let inst = new InstantiationService(services); - let input = inst.createInstance(StringEditorInput, 'name', 'description', 'value', 'text/plain', false); - let otherInput = inst.createInstance(StringEditorInput, 'name2', 'description', 'value2', 'text/plain', false); + instantiationService.stub(IModelService, createMockModelService(instantiationService)); + let input = instantiationService.createInstance(StringEditorInput, 'name', 'description', 'value', 'text/plain', false); + let otherInput = instantiationService.createInstance(StringEditorInput, 'name2', 'description', 'value2', 'text/plain', false); let diffInput = new DiffEditorInput('name', 'description', input, otherInput); diffInput.resolve(true).then(function (model: any) { diff --git a/src/vs/workbench/test/common/editor/editorStacksModel.test.ts b/src/vs/workbench/test/common/editor/editorStacksModel.test.ts index 13fd92ec52bd2..9a2410d238d8b 100644 --- a/src/vs/workbench/test/common/editor/editorStacksModel.test.ts +++ b/src/vs/workbench/test/common/editor/editorStacksModel.test.ts @@ -9,10 +9,9 @@ import * as assert from 'assert'; import {EditorStacksModel, EditorGroup} from 'vs/workbench/common/editor/editorStacksModel'; import {EditorInput, IFileEditorInput, IEditorIdentifier, IEditorGroup, IStacksModelChangeEvent, IEditorRegistry, Extensions as EditorExtensions, IEditorInputFactory} from 'vs/workbench/common/editor'; import URI from 'vs/base/common/uri'; -import {TestStorageService, TestConfigurationService, TestLifecycleService, TestContextService, TestWorkspace, TestConfiguration} from 'vs/workbench/test/common/servicesTestUtils'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; +import {TestStorageService, TestConfigurationService, TestLifecycleService, TestContextService, TestWorkspace, TestConfiguration} from 'vs/test/utils/servicesTestUtils'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import {IConfigurationService} from 'vs/platform/configuration/common/configuration'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; import {IStorageService} from 'vs/platform/storage/common/storage'; import {ILifecycleService} from 'vs/platform/lifecycle/common/lifecycle'; import {IWorkspaceContextService} from 'vs/workbench/services/workspace/common/contextService'; @@ -23,16 +22,15 @@ import {DiffEditorInput} from 'vs/workbench/common/editor/diffEditorInput'; import 'vs/workbench/browser/parts/editor/baseEditor'; function create(): EditorStacksModel { - let services = new ServiceCollection(); - services.set(IStorageService, new TestStorageService()); - services.set(ILifecycleService, new TestLifecycleService()); - services.set(IWorkspaceContextService, new TestContextService()); + let inst = new TestInstantiationService(); + inst.stub(IStorageService, new TestStorageService()); + inst.stub(ILifecycleService, new TestLifecycleService()); + inst.stub(IWorkspaceContextService, new TestContextService()); const config = new TestConfigurationService(); config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' } }); - services.set(IConfigurationService, config); + inst.stub(IConfigurationService, config); - let inst = new InstantiationService(services); return inst.createInstance(EditorStacksModel); } @@ -637,16 +635,15 @@ suite('Editor Stacks Model', () => { }); test('Stack - Multiple Editors - Pinned and Active (DEFAULT_OPEN_EDITOR_DIRECTION = Direction.LEFT)', function () { - let services = new ServiceCollection(); - services.set(IStorageService, new TestStorageService()); - services.set(ILifecycleService, new TestLifecycleService()); - services.set(IWorkspaceContextService, new TestContextService()); + let inst = new TestInstantiationService(); + inst.stub(IStorageService, new TestStorageService()); + inst.stub(ILifecycleService, new TestLifecycleService()); + inst.stub(IWorkspaceContextService, new TestContextService()); const config = new TestConfigurationService(); - services.set(IConfigurationService, config); + inst.stub(IConfigurationService, config); config.setUserConfiguration('workbench', { editor: { openPositioning: 'left' } }); - let inst = new InstantiationService(services); const model = inst.createInstance(EditorStacksModel); @@ -1170,17 +1167,16 @@ suite('Editor Stacks Model', () => { }); test('Stack - Single Group, Single Editor - persist', function () { - let services = new ServiceCollection(); + let inst = new TestInstantiationService(); - services.set(IStorageService, new TestStorageService()); - services.set(IWorkspaceContextService, new TestContextService()); + inst.stub(IStorageService, new TestStorageService()); + inst.stub(IWorkspaceContextService, new TestContextService()); const lifecycle = new TestLifecycleService(); - services.set(ILifecycleService, lifecycle); + inst.stub(ILifecycleService, lifecycle); const config = new TestConfigurationService(); config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' } }); - services.set(IConfigurationService, config); + inst.stub(IConfigurationService, config); - let inst = new InstantiationService(services); (Registry.as(EditorExtensions.Editors)).setInstantiationService(inst); @@ -1214,17 +1210,16 @@ suite('Editor Stacks Model', () => { }); test('Stack - Multiple Groups, Multiple editors - persist', function () { - let services = new ServiceCollection(); + let inst = new TestInstantiationService(); - services.set(IStorageService, new TestStorageService()); - services.set(IWorkspaceContextService, new TestContextService()); + inst.stub(IStorageService, new TestStorageService()); + inst.stub(IWorkspaceContextService, new TestContextService()); const lifecycle = new TestLifecycleService(); - services.set(ILifecycleService, lifecycle); + inst.stub(ILifecycleService, lifecycle); const config = new TestConfigurationService(); config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' } }); - services.set(IConfigurationService, config); + inst.stub(IConfigurationService, config); - let inst = new InstantiationService(services); (Registry.as(EditorExtensions.Editors)).setInstantiationService(inst); @@ -1296,17 +1291,16 @@ suite('Editor Stacks Model', () => { }); test('Stack - Single group, multiple editors - persist (some not persistable)', function () { - let services = new ServiceCollection(); + let inst = new TestInstantiationService(); - services.set(IStorageService, new TestStorageService()); - services.set(IWorkspaceContextService, new TestContextService()); + inst.stub(IStorageService, new TestStorageService()); + inst.stub(IWorkspaceContextService, new TestContextService()); const lifecycle = new TestLifecycleService(); - services.set(ILifecycleService, lifecycle); + inst.stub(ILifecycleService, lifecycle); const config = new TestConfigurationService(); config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' } }); - services.set(IConfigurationService, config); + inst.stub(IConfigurationService, config); - let inst = new InstantiationService(services); (Registry.as(EditorExtensions.Editors)).setInstantiationService(inst); @@ -1346,17 +1340,16 @@ suite('Editor Stacks Model', () => { }); test('Stack - Multiple groups, multiple editors - persist (some not persistable, causes empty group)', function () { - let services = new ServiceCollection(); + let inst = new TestInstantiationService(); - services.set(IStorageService, new TestStorageService()); - services.set(IWorkspaceContextService, new TestContextService()); + inst.stub(IStorageService, new TestStorageService()); + inst.stub(IWorkspaceContextService, new TestContextService()); const lifecycle = new TestLifecycleService(); - services.set(ILifecycleService, lifecycle); + inst.stub(ILifecycleService, lifecycle); const config = new TestConfigurationService(); config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' } }); - services.set(IConfigurationService, config); + inst.stub(IConfigurationService, config); - let inst = new InstantiationService(services); (Registry.as(EditorExtensions.Editors)).setInstantiationService(inst); @@ -1388,17 +1381,16 @@ suite('Editor Stacks Model', () => { }); test('Stack - Multiple groups, multiple editors - persist (ignore persisted when editors to open on startup)', function () { - let services = new ServiceCollection(); + let inst = new TestInstantiationService(); - services.set(IStorageService, new TestStorageService()); - services.set(IWorkspaceContextService, new TestContextService(TestWorkspace, TestConfiguration, { filesToCreate: [true] })); + inst.stub(IStorageService, new TestStorageService()); + inst.stub(IWorkspaceContextService, new TestContextService(TestWorkspace, TestConfiguration, { filesToCreate: [true] })); const lifecycle = new TestLifecycleService(); - services.set(ILifecycleService, lifecycle); + inst.stub(ILifecycleService, lifecycle); const config = new TestConfigurationService(); config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' } }); - services.set(IConfigurationService, config); + inst.stub(IConfigurationService, config); - let inst = new InstantiationService(services); (Registry.as(EditorExtensions.Editors)).setInstantiationService(inst); diff --git a/src/vs/workbench/test/common/editor/stringEditorInput.test.ts b/src/vs/workbench/test/common/editor/stringEditorInput.test.ts index c2c7136ed240d..723b5d4fd1a57 100644 --- a/src/vs/workbench/test/common/editor/stringEditorInput.test.ts +++ b/src/vs/workbench/test/common/editor/stringEditorInput.test.ts @@ -6,33 +6,38 @@ 'use strict'; import * as assert from 'assert'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import URI from 'vs/base/common/uri'; import {StringEditorInput} from 'vs/workbench/common/editor/stringEditorInput'; import {ResourceEditorInput} from 'vs/workbench/common/editor/resourceEditorInput'; import {ResourceEditorModel} from 'vs/workbench/common/editor/resourceEditorModel'; -import {TestEditorService} from 'vs/workbench/test/common/servicesTestUtils'; +import {createMockModelService, TestEditorService} from 'vs/test/utils/servicesTestUtils'; import {IModelService} from 'vs/editor/common/services/modelService'; import {IModeService} from 'vs/editor/common/services/modeService'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; - -import {createMockModelService, createMockModeService} from 'vs/editor/test/common/servicesTestUtils'; +import WorkbenchEditorService = require('vs/workbench/services/editor/common/editorService'); suite('Workbench - StringEditorInput', () => { + let instantiationService: TestInstantiationService; + let editorService: WorkbenchEditorService.IWorkbenchEditorService; + let modelService: IModelService; + let modeService: IModeService; + + setup(() => { + instantiationService= new TestInstantiationService(); + editorService= instantiationService.stub(WorkbenchEditorService.IWorkbenchEditorService, new TestEditorService(function () { })); + modeService= instantiationService.stub(IModeService); + modelService= instantiationService.stub(IModelService, createMockModelService(instantiationService)); + }); + test('StringEditorInput', function (done) { - let editorService = new TestEditorService(function () { }); - let services = new ServiceCollection(); - services.set(IModeService, createMockModeService()); - services.set(IModelService, createMockModelService()); - let inst = new InstantiationService(services); - - let input = inst.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', false); - let otherInput = inst.createInstance(StringEditorInput, 'name', 'description', 'othervalue', 'mime', false); - let otherInputSame = inst.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', false); - - let inputSingleton = inst.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', true); - let otherInputSingleton = inst.createInstance(StringEditorInput, 'name', 'description', 'othervalue', 'mime', true); + + let input = instantiationService.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', false); + let otherInput = instantiationService.createInstance(StringEditorInput, 'name', 'description', 'othervalue', 'mime', false); + let otherInputSame = instantiationService.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', false); + + let inputSingleton = instantiationService.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', true); + let otherInputSingleton = instantiationService.createInstance(StringEditorInput, 'name', 'description', 'othervalue', 'mime', true); assert(inputSingleton.matches(otherInputSingleton)); (otherInputSingleton).singleton = false; assert(!inputSingleton.matches(otherInputSingleton)); @@ -43,15 +48,15 @@ suite('Workbench - StringEditorInput', () => { assert(!input.matches(null)); assert(input.getName()); - input = inst.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', false); + input = instantiationService.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', false); - input = inst.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', false); + input = instantiationService.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', false); editorService.resolveEditorModel(input, true).then(function (resolved) { let resolvedModelA = resolved; return editorService.resolveEditorModel(input, true).then(function (resolved) { assert(resolvedModelA === resolved); // assert: Resolved Model cached per instance - let otherInput = inst.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', false); + let otherInput = instantiationService.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', false); return editorService.resolveEditorModel(otherInput, true).then(function (resolved) { assert(resolvedModelA !== resolved); // NOT assert: Different instance, different model @@ -73,11 +78,7 @@ suite('Workbench - StringEditorInput', () => { }); test('StringEditorInput - setValue, clearValue, append', function () { - let services = new ServiceCollection(); - services.set(IModeService, createMockModeService()); - services.set(IModelService, createMockModelService()); - let inst = new InstantiationService(services); - let input = inst.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', false); + let input = instantiationService.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', false); assert.strictEqual(input.getValue(), 'value'); input.setValue('foo'); @@ -91,7 +92,7 @@ suite('Workbench - StringEditorInput', () => { }); test('Input.matches() - StringEditorInput', function () { - let inst = new InstantiationService(); + let inst = new TestInstantiationService(); let stringEditorInput = inst.createInstance(StringEditorInput, 'name', 'description', 'value', 'mime', false); let promiseEditorInput = inst.createInstance(ResourceEditorInput, 'name', 'description', URI.from({ scheme: 'inMemory', authority: null, path: 'thePath' })); @@ -110,16 +111,9 @@ suite('Workbench - StringEditorInput', () => { }); test('ResourceEditorInput', function (done) { - let modelService = createMockModelService(); - let modeService = createMockModeService(); - let services = new ServiceCollection(); - services.set(IModeService, modeService); - services.set(IModelService, modelService); - let inst = new InstantiationService(services); - let resource = URI.from({ scheme: 'inMemory', authority: null, path: 'thePath' }); modelService.createModel('function test() {}', modeService.getOrCreateMode('text'), resource); - let input: ResourceEditorInput = inst.createInstance(ResourceEditorInput, 'The Name', 'The Description', resource); + let input: ResourceEditorInput = instantiationService.createInstance(ResourceEditorInput, 'The Name', 'The Description', resource); input.resolve().then((model: ResourceEditorModel) => { assert.ok(model); diff --git a/src/vs/workbench/test/common/editor/stringEditorModel.test.ts b/src/vs/workbench/test/common/editor/stringEditorModel.test.ts index fd7d5a864ece2..e2909991dcfef 100644 --- a/src/vs/workbench/test/common/editor/stringEditorModel.test.ts +++ b/src/vs/workbench/test/common/editor/stringEditorModel.test.ts @@ -6,22 +6,24 @@ 'use strict'; import * as assert from 'assert'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import {StringEditorModel} from 'vs/workbench/common/editor/stringEditorModel'; import {IModelService} from 'vs/editor/common/services/modelService'; import {IModeService} from 'vs/editor/common/services/modeService'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; - -import {createMockModelService, createMockModeService} from 'vs/editor/test/common/servicesTestUtils'; +import {createMockModelService} from 'vs/test/utils/servicesTestUtils'; suite('Workbench - StringEditorModel', () => { + let instantiationService: TestInstantiationService; + + setup(() => { + instantiationService= new TestInstantiationService(); + instantiationService.stub(IModeService); + }); + test('StringEditorModel', function (done) { - let services = new ServiceCollection(); - services.set(IModeService, createMockModeService()); - services.set(IModelService, createMockModelService()); - let inst = new InstantiationService(services); - let m = inst.createInstance(StringEditorModel, 'value', 'mime', null); + instantiationService.stub(IModelService, createMockModelService(instantiationService)); + let m = instantiationService.createInstance(StringEditorModel, 'value', 'mime', null); m.load().then(function (model) { assert(model === m); @@ -42,11 +44,8 @@ suite('Workbench - StringEditorModel', () => { }); test('StringEditorModel - setValue, clearValue, append, trim', function (done) { - let services = new ServiceCollection(); - services.set(IModeService, createMockModeService()); - services.set(IModelService, createMockModelService()); - let inst = new InstantiationService(services); - let m = inst.createInstance(StringEditorModel, 'value', 'mime', null); + instantiationService.stub(IModelService, createMockModelService(instantiationService)); + let m = instantiationService.createInstance(StringEditorModel, 'value', 'mime', null); m.load().then(function (model) { assert(model === m); diff --git a/src/vs/workbench/test/common/memento.test.ts b/src/vs/workbench/test/common/memento.test.ts index fe3475ec0bf90..fb0c32ce9916d 100644 --- a/src/vs/workbench/test/common/memento.test.ts +++ b/src/vs/workbench/test/common/memento.test.ts @@ -8,7 +8,7 @@ import * as assert from 'assert'; import {BaseWorkspaceContextService} from 'vs/platform/workspace/common/baseWorkspaceContextService'; import {StorageScope} from 'vs/platform/storage/common/storage'; -import * as TestUtils from 'vs/workbench/test/common/servicesTestUtils'; +import * as TestUtils from 'vs/test/utils/servicesTestUtils'; import {Memento, Scope} from 'vs/workbench/common/memento'; import {Storage, InMemoryLocalStorage} from 'vs/workbench/common/storage'; diff --git a/src/vs/workbench/test/common/storage.test.ts b/src/vs/workbench/test/common/storage.test.ts index 0e95c4b61dd48..185393de3a8b5 100644 --- a/src/vs/workbench/test/common/storage.test.ts +++ b/src/vs/workbench/test/common/storage.test.ts @@ -8,7 +8,7 @@ import * as assert from 'assert'; import {clone} from 'vs/base/common/objects'; import {StorageScope} from 'vs/platform/storage/common/storage'; -import {TestContextService, TestWorkspace} from 'vs/workbench/test/common/servicesTestUtils'; +import {TestContextService, TestWorkspace} from 'vs/test/utils/servicesTestUtils'; import {Storage, InMemoryLocalStorage} from 'vs/workbench/common/storage'; suite('Workbench Storage', () => { diff --git a/src/vs/workbench/test/node/api/extHostApiCommands.test.ts b/src/vs/workbench/test/node/api/extHostApiCommands.test.ts index 1605ad2727772..516a506633c9d 100644 --- a/src/vs/workbench/test/node/api/extHostApiCommands.test.ts +++ b/src/vs/workbench/test/node/api/extHostApiCommands.test.ts @@ -7,14 +7,13 @@ import * as assert from 'assert'; import {setUnexpectedErrorHandler, errorHandler} from 'vs/base/common/errors'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import URI from 'vs/base/common/uri'; import {TPromise} from 'vs/base/common/winjs.base'; import * as types from 'vs/workbench/api/node/extHostTypes'; import * as EditorCommon from 'vs/editor/common/editorCommon'; import {Model as EditorModel} from 'vs/editor/common/model/model'; import {TestThreadService} from 'vs/workbench/test/node/api/testThreadService'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; import {MarkerService} from 'vs/platform/markers/common/markerService'; import {IMarkerService} from 'vs/platform/markers/common/markers'; import {IThreadService} from 'vs/workbench/services/thread/common/threadService'; @@ -55,20 +54,19 @@ suite('ExtHostLanguageFeatureCommands', function() { originalErrorHandler = errorHandler.getUnexpectedErrorHandler(); setUnexpectedErrorHandler(() => { }); - let services = new ServiceCollection(); - let instantiationService = new InstantiationService(services); + let instantiationService = new TestInstantiationService(); threadService = new TestThreadService(); - services.set(ICommandService, { + instantiationService.stub(ICommandService, { _serviceBrand: undefined, executeCommand(id, args): any { let {handler} = CommandsRegistry.getCommands()[id]; return TPromise.as(instantiationService.invokeFunction(handler, args)); } }); - services.set(IMarkerService, new MarkerService()); - services.set(IThreadService, threadService); - services.set(IModelService, { + instantiationService.stub(IMarkerService, new MarkerService()); + instantiationService.stub(IThreadService, threadService); + instantiationService.stub(IModelService, { _serviceBrand: IModelService, getModel(): any { return model; }, createModel(): any { throw new Error(); }, diff --git a/src/vs/workbench/test/node/api/extHostLanguageFeatures.test.ts b/src/vs/workbench/test/node/api/extHostLanguageFeatures.test.ts index c929e2469761a..72fa3878810ad 100644 --- a/src/vs/workbench/test/node/api/extHostLanguageFeatures.test.ts +++ b/src/vs/workbench/test/node/api/extHostLanguageFeatures.test.ts @@ -6,6 +6,7 @@ 'use strict'; import * as assert from 'assert'; +import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils'; import {setUnexpectedErrorHandler, errorHandler} from 'vs/base/common/errors'; import URI from 'vs/base/common/uri'; import * as types from 'vs/workbench/api/node/extHostTypes'; @@ -14,9 +15,6 @@ import {Model as EditorModel} from 'vs/editor/common/model/model'; import {Position as EditorPosition} from 'vs/editor/common/core/position'; import {Range as EditorRange} from 'vs/editor/common/core/range'; import {TestThreadService} from './testThreadService'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; -import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService'; -import {MarkerService} from 'vs/platform/markers/common/markerService'; import {IMarkerService} from 'vs/platform/markers/common/markers'; import {IThreadService} from 'vs/workbench/services/thread/common/threadService'; import {ExtHostLanguageFeatures} from 'vs/workbench/api/node/extHostLanguageFeatures'; @@ -62,11 +60,10 @@ suite('ExtHostLanguageFeatures', function() { suiteSetup(() => { - let services = new ServiceCollection(); - let instantiationService = new InstantiationService(services); threadService = new TestThreadService(); - services.set(IMarkerService, new MarkerService()); - services.set(IThreadService, threadService); + let instantiationService= new TestInstantiationService(); + instantiationService.stub(IThreadService, threadService); + instantiationService.stub(IMarkerService); originalErrorHandler = errorHandler.getUnexpectedErrorHandler(); setUnexpectedErrorHandler(() => { }); @@ -102,7 +99,7 @@ suite('ExtHostLanguageFeatures', function() { extHost = new ExtHostLanguageFeatures(threadService, extHostDocuments, commands, diagnostics); threadService.set(ExtHostContext.ExtHostLanguageFeatures, extHost); - mainThread = threadService.setTestInstance(MainContext.MainThreadLanguageFeatures, instantiationService.createInstance(MainThreadLanguageFeatures)); + mainThread = threadService.setTestInstance(MainContext.MainThreadLanguageFeatures, instantiationService.createInstance(MainThreadLanguageFeatures)); }); suiteTeardown(() => {