One way to control the mock instance so it can emit our events

This commit is contained in:
Rob 2015-10-27 09:30:27 -07:00
Родитель 6a3137631a
Коммит 3d24711cce
1 изменённых файлов: 51 добавлений и 11 удалений

Просмотреть файл

@ -6,11 +6,15 @@ import * as mockery from 'mockery';
import * as assert from 'assert';
import * as testUtils from '../testUtils';
import {EventEmitter} from 'events';
/** Utilities without mocks - use for type only */
import {WebKitDebugAdapter as _WebKitDebugAdapter} from '../../webkit/webKitDebugAdapter';
const MODULE_UNDER_TEST = '../../webkit/webKitDebugAdapter';
suite('WebKitDebugAdapter', () => {
let mockEventEmitter: EventEmitter;
setup(() => {
testUtils.setupUnhandledRejectionListener();
mockery.enable({ useCleanCache: true, warnOnReplace: false });
@ -26,7 +30,7 @@ suite('WebKitDebugAdapter', () => {
'./v8Protocol',
'events']);
registerMockWebKitConnection();
mockEventEmitter = registerMockWebKitConnection();
mockery.registerMock('child_process', { });
mockery.registerMock('url', { });
mockery.registerMock('path', { });
@ -53,7 +57,7 @@ suite('WebKitDebugAdapter', () => {
}
});
return wkda.attach({ address: 'localhost', 'port': 9222 }).then(() => {
return attach(wkda).then(() => {
if (!initializedFired) {
assert.fail('Attach completed without firing the initialized event');
}
@ -70,35 +74,71 @@ suite('WebKitDebugAdapter', () => {
assert.fail('Not expecting any event in this scenario');
});
return wkda.attach({ address: 'localhost', 'port': 9222 }).then(
return attach(wkda).then(
() => assert.fail('Expecting promise to be rejected'),
e => done());
});
});
suite('setBreakpoints()', () => {
test('works', () => {
const wkda = instantiateWKDA();
return attach(wkda).then(() => {
mockEventEmitter.emit('Debugger.scriptParsed', { id: "id", url: "a.js" });
wkda.setBreakpoints({ source: { path: "a.js" }, lines: [1] });
});
});
});
suite('launch()', () => { });
suite('setExceptionBreakpoints()', () => { });
suite('stepping', () => { });
suite('stackTrace()', () => { });
suite('scopes()', () => { });
suite('variables()', () => { });
suite('source()', () => { });
suite('threads()', () => { });
suite('evaluate()', () => { });
suite('Debugger.resume', () => { });
suite('Debugger.pause', () => { });
suite('target close/error/detach', () => { });
});
class DefaultMockWebKitConnection {
public on(eventName: string, handler: (msg: any) => void): void {
}
function attach(wkda: _WebKitDebugAdapter): Promise<void> {
return wkda.attach({ address: 'localhost', 'port': 9222 });
}
class DefaultMockWebKitConnection {
public attach(port: number): Promise<void> {
return Promise.resolve<void>();
}
}
function registerMockWebKitConnection(partialImpl?: any): void {
const mock = () => { };
/**
* Registers a mock WebKitConnection based off the above default impl, patched when whatever is in partialImpl
*/
function registerMockWebKitConnection(partialImpl?: any): EventEmitter {
const mockType = () => { };
Object.getOwnPropertyNames(DefaultMockWebKitConnection).forEach(name => {
mock[name] = DefaultMockWebKitConnection[name];
mockType[name] = DefaultMockWebKitConnection[name];
});
if (partialImpl) {
Object.getOwnPropertyNames(partialImpl).forEach(name => {
mock[name] = partialImpl[name];
mockType[name] = partialImpl[name];
});
}
mockery.registerMock('./webKitConnection', { WebKitConnection: mock });
// Instantiate the mock so we can inject an event emitter to simulate events from the WebKitConnection
const mockInstance = new mockType();
const ee = new EventEmitter();
mockInstance['on'] = ee.on.bind(ee);
// Register a fake constructor so that our instance will be called when the adapter does 'new WebKitConnection'
mockery.registerMock('./webKitConnection', { WebKitConnection: () => mockInstance });
return ee;
}
function instantiateWKDA(): _WebKitDebugAdapter {