SQL Operations Studio Public Preview 1 (0.23) release source code

This commit is contained in:
Karl Burtram
2017-11-09 14:30:27 -08:00
parent b88ecb8d93
commit 3cdac41339
8829 changed files with 759707 additions and 286 deletions

View File

@@ -0,0 +1,397 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import assert = require('assert');
import { createDecorator, optional, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
let IService1 = createDecorator<IService1>('service1');
interface IService1 {
_serviceBrand: any;
c: number;
}
class Service1 implements IService1 {
_serviceBrand: any;
c = 1;
}
let IService2 = createDecorator<IService2>('service2');
interface IService2 {
_serviceBrand: any;
d: boolean;
}
class Service2 implements IService2 {
_serviceBrand: any;
d = true;
}
let IService3 = createDecorator<IService3>('service3');
interface IService3 {
_serviceBrand: any;
s: string;
}
class Service3 implements IService3 {
_serviceBrand: any;
s = 'farboo';
}
let IDependentService = createDecorator<IDependentService>('dependentService');
interface IDependentService {
_serviceBrand: any;
name: string;
}
class DependentService implements IDependentService {
_serviceBrand: any;
constructor( @IService1 service: IService1) {
assert.equal(service.c, 1);
}
name = 'farboo';
}
class Service1Consumer {
constructor( @IService1 service1: IService1) {
assert.ok(service1);
assert.equal(service1.c, 1);
}
}
class Target2Dep {
constructor( @IService1 service1: IService1, @IService2 service2) {
assert.ok(service1 instanceof Service1);
assert.ok(service2 instanceof Service2);
}
}
class TargetWithStaticParam {
constructor(v: boolean, @IService1 service1: IService1) {
assert.ok(v);
assert.ok(service1);
assert.equal(service1.c, 1);
}
}
class TargetNotOptional {
constructor( @IService1 service1: IService1, @IService2 service2: IService2) {
}
}
class TargetOptional {
constructor( @IService1 service1: IService1, @optional(IService2) service2: IService2) {
assert.ok(service1);
assert.equal(service1.c, 1);
assert.ok(service2 === void 0);
}
}
class DependentServiceTarget {
constructor( @IDependentService d) {
assert.ok(d);
assert.equal(d.name, 'farboo');
}
}
class DependentServiceTarget2 {
constructor( @IDependentService d: IDependentService, @IService1 s: IService1) {
assert.ok(d);
assert.equal(d.name, 'farboo');
assert.ok(s);
assert.equal(s.c, 1);
}
}
class ServiceLoop1 implements IService1 {
_serviceBrand: any;
c = 1;
constructor( @IService2 s: IService2) {
}
}
class ServiceLoop2 implements IService2 {
_serviceBrand: any;
d = true;
constructor( @IService1 s: IService1) {
}
}
suite('Instantiation Service', () => {
test('service collection, cannot overwrite', function () {
let collection = new ServiceCollection();
let result = collection.set(IService1, null);
assert.equal(result, undefined);
result = collection.set(IService1, new Service1());
assert.equal(result, null);
});
test('service collection, add/has', function () {
let collection = new ServiceCollection();
collection.set(IService1, null);
assert.ok(collection.has(IService1));
collection.set(IService2, null);
assert.ok(collection.has(IService1));
assert.ok(collection.has(IService2));
});
test('@Param - simple clase', function () {
let collection = new ServiceCollection();
let service = new InstantiationService(collection);
collection.set(IService1, new Service1());
collection.set(IService2, new Service2());
collection.set(IService3, new Service3());
service.createInstance(Service1Consumer);
});
test('@Param - fixed args', function () {
let collection = new ServiceCollection();
let service = new InstantiationService(collection);
collection.set(IService1, new Service1());
collection.set(IService2, new Service2());
collection.set(IService3, new Service3());
service.createInstance(TargetWithStaticParam, true);
});
test('service collection is live', function () {
let collection = new ServiceCollection();
collection.set(IService1, new Service1());
let service = new InstantiationService(collection);
service.createInstance(Service1Consumer);
// no IService2
assert.throws(() => service.createInstance(Target2Dep));
service.invokeFunction(function (a) {
assert.ok(a.get(IService1));
assert.ok(!a.get(IService2, optional));
});
collection.set(IService2, new Service2());
service.createInstance(Target2Dep);
service.invokeFunction(function (a) {
assert.ok(a.get(IService1));
assert.ok(a.get(IService2));
});
});
test('@Param - optional', function () {
let collection = new ServiceCollection([IService1, new Service1()]);
let service = new InstantiationService(collection, true);
service.createInstance(TargetOptional);
assert.throws(() => service.createInstance(TargetNotOptional));
service = new InstantiationService(collection, false);
service.createInstance(TargetOptional);
service.createInstance(TargetNotOptional);
});
// we made this a warning
// test('@Param - too many args', function () {
// let service = instantiationService.create(Object.create(null));
// service.addSingleton(IService1, new Service1());
// service.addSingleton(IService2, new Service2());
// service.addSingleton(IService3, new Service3());
// assert.throws(() => service.createInstance(ParameterTarget2, true, 2));
// });
// test('@Param - too few args', function () {
// let service = instantiationService.create(Object.create(null));
// service.addSingleton(IService1, new Service1());
// service.addSingleton(IService2, new Service2());
// service.addSingleton(IService3, new Service3());
// assert.throws(() => service.createInstance(ParameterTarget2));
// });
test('SyncDesc - no dependencies', function () {
let collection = new ServiceCollection();
let service = new InstantiationService(collection);
collection.set(IService1, new SyncDescriptor<IService1>(Service1));
service.invokeFunction(accessor => {
let service1 = accessor.get(IService1);
assert.ok(service1);
assert.equal(service1.c, 1);
let service2 = accessor.get(IService1);
assert.ok(service1 === service2);
});
});
test('SyncDesc - service with service dependency', function () {
let collection = new ServiceCollection();
let service = new InstantiationService(collection);
collection.set(IService1, new SyncDescriptor<IService1>(Service1));
collection.set(IDependentService, new SyncDescriptor<IDependentService>(DependentService));
service.invokeFunction(accessor => {
let d = accessor.get(IDependentService);
assert.ok(d);
assert.equal(d.name, 'farboo');
});
});
test('SyncDesc - target depends on service future', function () {
let collection = new ServiceCollection();
let service = new InstantiationService(collection);
collection.set(IService1, new SyncDescriptor<IService1>(Service1));
collection.set(IDependentService, new SyncDescriptor<IDependentService>(DependentService));
let d = service.createInstance(DependentServiceTarget);
assert.ok(d instanceof DependentServiceTarget);
let d2 = service.createInstance(DependentServiceTarget2);
assert.ok(d2 instanceof DependentServiceTarget2);
});
test('SyncDesc - explode on loop', function () {
let collection = new ServiceCollection();
let service = new InstantiationService(collection);
collection.set(IService1, new SyncDescriptor<IService1>(ServiceLoop1));
collection.set(IService2, new SyncDescriptor<IService2>(ServiceLoop2));
assert.throws(() => {
service.invokeFunction(accessor => {
accessor.get(IService1);
});
});
assert.throws(() => {
service.invokeFunction(accessor => {
accessor.get(IService2);
});
});
try {
service.invokeFunction(accessor => {
accessor.get(IService1);
});
} catch (err) {
assert.ok(err.name);
assert.ok(err.message);
}
});
test('Invoke - get services', function () {
let collection = new ServiceCollection();
let service = new InstantiationService(collection);
collection.set(IService1, new Service1());
collection.set(IService2, new Service2());
function test(accessor: ServicesAccessor) {
assert.ok(accessor.get(IService1) instanceof Service1);
assert.equal(accessor.get(IService1).c, 1);
return true;
}
assert.equal(service.invokeFunction(test), true);
});
test('Invoke - get service, optional', function () {
let collection = new ServiceCollection([IService1, new Service1()]);
let service = new InstantiationService(collection);
function test(accessor: ServicesAccessor) {
assert.ok(accessor.get(IService1) instanceof Service1);
assert.throws(() => accessor.get(IService2));
assert.equal(accessor.get(IService2, optional), undefined);
return true;
}
assert.equal(service.invokeFunction(test), true);
});
test('Invoke - keeping accessor NOT allowed', function () {
let collection = new ServiceCollection();
let service = new InstantiationService(collection);
collection.set(IService1, new Service1());
collection.set(IService2, new Service2());
let cached: ServicesAccessor;
function test(accessor: ServicesAccessor) {
assert.ok(accessor.get(IService1) instanceof Service1);
assert.equal(accessor.get(IService1).c, 1);
cached = accessor;
return true;
}
assert.equal(service.invokeFunction(test), true);
assert.throws(() => cached.get(IService2));
});
test('Invoke - throw error', function () {
let collection = new ServiceCollection();
let service = new InstantiationService(collection);
collection.set(IService1, new Service1());
collection.set(IService2, new Service2());
function test(accessor: ServicesAccessor) {
throw new Error();
}
assert.throws(() => service.invokeFunction(test));
});
test('Create child', function () {
let serviceInstanceCount = 0;
const CtorCounter = class implements Service1 {
_serviceBrand: any;
c = 1;
constructor() {
serviceInstanceCount += 1;
}
};
// creating the service instance BEFORE the child service
let service = new InstantiationService(new ServiceCollection([IService1, new SyncDescriptor(CtorCounter)]));
service.createInstance(Service1Consumer);
// second instance must be earlier ONE
let child = service.createChild(new ServiceCollection([IService2, new Service2()]));
child.createInstance(Service1Consumer);
assert.equal(serviceInstanceCount, 1);
// creating the service instance AFTER the child service
serviceInstanceCount = 0;
service = new InstantiationService(new ServiceCollection([IService1, new SyncDescriptor(CtorCounter)]));
child = service.createChild(new ServiceCollection([IService2, new Service2()]));
// second instance must be earlier ONE
service.createInstance(Service1Consumer);
child.createInstance(Service1Consumer);
assert.equal(serviceInstanceCount, 1);
});
});

View File

@@ -0,0 +1,138 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as sinon from 'sinon';
import { TPromise } from 'vs/base/common/winjs.base';
import * as types from 'vs/base/common/types';
import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation';
interface IServiceMock<T> {
id: ServiceIdentifier<T>;
service: any;
}
export class TestInstantiationService extends InstantiationService {
private _servciesMap: Map<ServiceIdentifier<any>, any>;
constructor(private _serviceCollection: ServiceCollection = new ServiceCollection()) {
super(_serviceCollection);
this._servciesMap = new Map<ServiceIdentifier<any>, any>();
}
public get<T>(service: ServiceIdentifier<T>): T {
return <T>this._serviceCollection.get(service);
}
public set<T>(service: ServiceIdentifier<T>, instance: T): T {
return <T>this._serviceCollection.set(service, instance);
}
public mock<T>(service: ServiceIdentifier<T>): T | sinon.SinonMock {
return <T>this._create(service, { mock: true });
}
public stub<T>(service?: ServiceIdentifier<T>, ctor?: any): T
public stub<T>(service?: ServiceIdentifier<T>, obj?: any): T
public stub<T>(service?: ServiceIdentifier<T>, ctor?: any, property?: string, value?: any): sinon.SinonStub
public stub<T>(service?: ServiceIdentifier<T>, obj?: any, property?: string, value?: any): sinon.SinonStub
public stub<T>(service?: ServiceIdentifier<T>, property?: string, value?: any): sinon.SinonStub
public stub<T>(serviceIdentifier?: ServiceIdentifier<T>, arg2?: any, arg3?: string, arg4?: any): sinon.SinonStub {
let service = typeof arg2 !== 'string' ? arg2 : void 0;
let serviceMock: IServiceMock<any> = { id: serviceIdentifier, service: service };
let property = typeof arg2 === 'string' ? arg2 : arg3;
let value = typeof arg2 === 'string' ? arg3 : arg4;
let stubObject = <any>this._create(serviceMock, { stub: true });
if (property) {
if (stubObject[property]) {
if (stubObject[property].hasOwnProperty('restore')) {
stubObject[property].restore();
}
if (typeof value === 'function') {
stubObject[property] = value;
} else {
let stub = value ? sinon.stub().returns(value) : sinon.stub();
stubObject[property] = stub;
return stub;
}
} else {
stubObject[property] = value;
}
}
return stubObject;
}
public stubPromise<T>(service?: ServiceIdentifier<T>, fnProperty?: string, value?: any): T | sinon.SinonStub
public stubPromise<T>(service?: ServiceIdentifier<T>, ctor?: any, fnProperty?: string, value?: any): sinon.SinonStub
public stubPromise<T>(service?: ServiceIdentifier<T>, obj?: any, fnProperty?: string, value?: any): sinon.SinonStub
public stubPromise<T>(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<T>(service: ServiceIdentifier<T>, fnProperty: string): sinon.SinonSpy {
let spy = sinon.spy();
this.stub(service, fnProperty, spy);
return spy;
}
private _create<T>(serviceMock: IServiceMock<T>, options: SinonOptions): any
private _create<T>(ctor: any, options: SinonOptions): any
private _create<T>(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<T>(serviceMock: IServiceMock<T>, 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<any>, 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');
}
}
export function stubFunction<T>(ctor: any, fnProperty: string, value: any): T | sinon.SinonStub {
let stub = sinon.createStubInstance(ctor);
stub[fnProperty].restore();
sinon.stub(stub, fnProperty, types.isFunction(value) ? value : () => { return value; });
return stub;
}
interface SinonOptions {
mock?: boolean;
stub?: boolean;
}