debugModel.test.ts 12.8 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import assert = require('assert');
import uri from 'vs/base/common/uri';
I
isidor 已提交
8
import severity from 'vs/base/common/severity';
E
Erich Gamma 已提交
9
import debugmodel = require('vs/workbench/parts/debug/common/debugModel');
10
import * as sinon from 'sinon';
I
isidor 已提交
11
import {MockDebugService} from 'vs/workbench/parts/debug/test/common/mockDebugService';
E
Erich Gamma 已提交
12 13 14 15 16

suite('Debug - Model', () => {
	var model: debugmodel.Model;

	setup(() => {
I
isidor 已提交
17
		model = new debugmodel.Model([], true, [], [], []);
E
Erich Gamma 已提交
18 19 20 21 22 23 24 25 26 27
	});

	teardown(() => {
		model = null;
	});

	// Breakpoints

	test('breakpoints simple', () => {
		var modelUri = uri.file('/myfolder/myfile.js');
28
		model.addBreakpoints([{ uri: modelUri, lineNumber: 5, enabled: true }, { uri: modelUri, lineNumber: 10, enabled: false }]);
E
Erich Gamma 已提交
29 30 31
		assert.equal(model.areBreakpointsActivated(), true);
		assert.equal(model.getBreakpoints().length, 2);

32
		model.removeBreakpoints(model.getBreakpoints());
E
Erich Gamma 已提交
33 34 35 36 37
		assert.equal(model.getBreakpoints().length, 0);
	});

	test('breakpoints toggling', () => {
		var modelUri = uri.file('/myfolder/myfile.js');
38 39
		model.addBreakpoints([{ uri: modelUri, lineNumber: 5, enabled: true }, { uri: modelUri, lineNumber: 10, enabled: false }]);
		model.addBreakpoints([{ uri: modelUri, lineNumber: 12, enabled: true, condition: 'fake condition'}]);
E
Erich Gamma 已提交
40
		assert.equal(model.getBreakpoints().length, 3);
41
		model.removeBreakpoints([model.getBreakpoints().pop()]);
E
Erich Gamma 已提交
42 43
		assert.equal(model.getBreakpoints().length, 2);

44
		model.setBreakpointsActivated(false);
E
Erich Gamma 已提交
45
		assert.equal(model.areBreakpointsActivated(), false);
46
		model.setBreakpointsActivated(true);
E
Erich Gamma 已提交
47 48 49 50 51 52
		assert.equal(model.areBreakpointsActivated(), true);
	});

	test('breakpoints two files', () => {
		var modelUri1 = uri.file('/myfolder/my file first.js');
		var modelUri2 = uri.file('/secondfolder/second/second file.js')
53 54
		model.addBreakpoints([{ uri: modelUri1, lineNumber: 5, enabled: true }, { uri: modelUri1, lineNumber: 10, enabled: false }]);
		model.addBreakpoints([{ uri: modelUri2, lineNumber: 1, enabled: true }, { uri: modelUri2, lineNumber: 2, enabled: true }, { uri: modelUri2, lineNumber: 3, enabled: false }]);
E
Erich Gamma 已提交
55 56 57 58

		assert.equal(model.getBreakpoints().length, 5);
		var bp = model.getBreakpoints()[0];
		var originalLineLumber = bp.lineNumber;
59 60 61
		const update:any = {};
		update[bp.getId()] = { line: 100, verified: false };
		model.updateBreakpoints(update);
E
Erich Gamma 已提交
62 63 64 65 66 67 68
		assert.equal(bp.lineNumber, 100);
		assert.equal(bp.desiredLineNumber, originalLineLumber);

		model.enableOrDisableAllBreakpoints(false);
		model.getBreakpoints().forEach(bp => {
			assert.equal(bp.enabled, false);
		});
69
		model.setEnablement(bp, true);
E
Erich Gamma 已提交
70 71
		assert.equal(bp.enabled, true);

72
		model.removeBreakpoints(model.getBreakpoints().filter(bp => bp.source.uri.toString() === modelUri1.toString()));
E
Erich Gamma 已提交
73 74 75 76 77 78 79
		assert.equal(model.getBreakpoints().length, 3);
	});

	// Threads

	test('threads simple', () => {
		var threadId = 1;
80
		var threadName = 'firstThread';
E
Erich Gamma 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94
		model.rawUpdate({
			threadId: threadId,
			thread: {
				id: threadId,
				name: threadName
			}
		});

		var threads = model.getThreads();
		assert.equal(threads[threadId].name, threadName);

		model.clearThreads(true);
		assert.equal(model.getThreads[threadId], null);
	});
I
isidor 已提交
95

96 97 98 99 100
	test('threads multiple wtih allThreadsStopped', () => {
		const mockDebugService = new MockDebugService();
		const sessionStub = sinon.spy(mockDebugService.getActiveSession(), 'stackTrace');

		const threadId1 = 1;
101
		const threadName1 = 'firstThread';
102
		const threadId2 = 2;
103 104
		const threadName2 = 'secondThread';
		const stoppedReason = 'breakpoint';
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

		// Add the threads
		model.rawUpdate({
			threadId: threadId1,
			thread: {
				id: threadId1,
				name: threadName1
			}
		});

		model.rawUpdate({
			threadId: threadId2,
			thread: {
				id: threadId2,
				name: threadName2
			}
		});

		// Stopped event with all threads stopped
		model.rawUpdate({
			threadId: threadId1,
			stoppedDetails: {
				reason: stoppedReason,
				threadId: 1
			},
			allThreadsStopped: true
		});

		const thread1 = model.getThreads()[threadId1];
		const thread2 = model.getThreads()[threadId2];

		// at the beginning, callstacks are obtainable but not available
		assert.equal(thread1.name, threadName1);
		assert.equal(thread1.stopped, true);
		assert.equal(thread1.getCachedCallStack(), undefined);
		assert.equal(thread1.stoppedDetails.reason, stoppedReason);
		assert.equal(thread2.name, threadName2);
		assert.equal(thread2.stopped, true);
		assert.equal(thread2.getCachedCallStack(), undefined);
		assert.equal(thread2.stoppedDetails.reason, stoppedReason);

		// after calling getCallStack, the callstack becomes available
		// and results in a request for the callstack in the debug adapter
		thread1.getCallStack(mockDebugService).then(() => {
			assert.notEqual(thread1.getCachedCallStack(), undefined);
			assert.equal(thread2.getCachedCallStack(), undefined);
			assert.equal(sessionStub.callCount, 1);
		});

		thread2.getCallStack(mockDebugService).then(() => {
			assert.notEqual(thread1.getCachedCallStack(), undefined);
			assert.notEqual(thread2.getCachedCallStack(), undefined);
			assert.equal(sessionStub.callCount, 2);
		});

		// calling multiple times getCallStack doesn't result in multiple calls
		// to the debug adapter
		thread1.getCallStack(mockDebugService).then(() => {
			return thread2.getCallStack(mockDebugService);
		}).then(() => {
			assert.equal(sessionStub.callCount, 2);
		});

		// clearing the callstack results in the callstack not being available
		thread1.clearCallStack();
		assert.equal(thread1.stopped, true);
		assert.equal(thread1.getCachedCallStack(), undefined);

		thread2.clearCallStack();
		assert.equal(thread2.stopped, true);
		assert.equal(thread2.getCachedCallStack(), undefined);

		model.clearThreads(true);
		assert.equal(model.getThreads[threadId1], null);
		assert.equal(model.getThreads[threadId2], null);
	});

	test('threads mutltiple without allThreadsStopped', () => {
		const mockDebugService = new MockDebugService();
		const sessionStub = sinon.spy(mockDebugService.getActiveSession(), 'stackTrace');

		const stoppedThreadId = 1;
187
		const stoppedThreadName = 'stoppedThread';
188
		const runningThreadId = 2;
189 190
		const runningThreadName = 'runningThread';
		const stoppedReason = 'breakpoint';
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

		// Add the threads
		model.rawUpdate({
			threadId: stoppedThreadId,
			thread: {
				id: stoppedThreadId,
				name: stoppedThreadName
			}
		});

		model.rawUpdate({
			threadId: runningThreadId,
			thread: {
				id: runningThreadId,
				name: runningThreadName
			}
		});

		// Stopped event with only one thread stopped
		model.rawUpdate({
			threadId: stoppedThreadId,
			stoppedDetails: {
				reason: stoppedReason,
				threadId: 1
			},
			allThreadsStopped: false
		});

		const stoppedThread = model.getThreads()[stoppedThreadId];
		const runningThread = model.getThreads()[runningThreadId];

		// the callstack for the stopped thread is obtainable but not available
		// the callstack for the running thread is not obtainable nor available
		assert.equal(stoppedThread.name, stoppedThreadName);
		assert.equal(stoppedThread.stopped, true);
		assert.equal(stoppedThread.getCachedCallStack(), undefined);
		assert.equal(stoppedThread.stoppedDetails.reason, stoppedReason);
		assert.equal(runningThread.name, runningThreadName);
		assert.equal(runningThread.stopped, false);
		assert.equal(runningThread.getCachedCallStack(), undefined);
		assert.equal(runningThread.stoppedDetails, undefined);

		// after calling getCallStack, the callstack becomes available
		// and results in a request for the callstack in the debug adapter
		stoppedThread.getCallStack(mockDebugService).then(() => {
			assert.notEqual(stoppedThread.getCachedCallStack(), undefined);
			assert.equal(runningThread.getCachedCallStack(), undefined);
			assert.equal(sessionStub.callCount, 1);
		});

		// calling getCallStack on the running thread returns empty array
		// and does not return in a request for the callstack in the debug
		// adapter
		runningThread.getCallStack(mockDebugService).then(callStack => {
			assert.deepEqual(callStack, []);
			assert.equal(sessionStub.callCount, 1);
		});

		// calling multiple times getCallStack doesn't result in multiple calls
		// to the debug adapter
		stoppedThread.getCallStack(mockDebugService).then(() => {
			assert.equal(sessionStub.callCount, 1);
		});

		// clearing the callstack results in the callstack not being available
		stoppedThread.clearCallStack();
		assert.equal(stoppedThread.stopped, true);
		assert.equal(stoppedThread.getCachedCallStack(), undefined);

		model.clearThreads(true);
		assert.equal(model.getThreads[stoppedThreadId], null);
		assert.equal(model.getThreads[runningThreadId], null);
	});

I
isidor 已提交
265 266 267 268 269 270 271 272 273 274 275 276
	// Expressions

	function assertWatchExpressions(watchExpressions: debugmodel.Expression[], expectedName: string) {
		assert.equal(watchExpressions.length, 2);
		watchExpressions.forEach(we => {
			assert.equal(we.available, false);
			assert.equal(we.reference, 0);
			assert.equal(we.name, expectedName);
		});
	}

	test('watch expressions', () => {
I
isidor 已提交
277
		assert.equal(model.getWatchExpressions().length, 0);
I
isidor 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290
		const stackFrame = new debugmodel.StackFrame(1, 1, null, 'app.js', 1, 1);
		model.addWatchExpression(null, stackFrame, 'console').done();
		model.addWatchExpression(null, stackFrame, 'console').done();
		const watchExpressions = model.getWatchExpressions();
		assertWatchExpressions(watchExpressions, 'console');

		model.renameWatchExpression(null, stackFrame, watchExpressions[0].getId(), 'new_name').done();
		model.renameWatchExpression(null, stackFrame, watchExpressions[1].getId(), 'new_name').done();
		assertWatchExpressions(model.getWatchExpressions(), 'new_name');

		model.clearWatchExpressionValues();
		assertWatchExpressions(model.getWatchExpressions(), 'new_name');

291
		model.removeWatchExpressions();
I
isidor 已提交
292 293 294 295
		assert.equal(model.getWatchExpressions().length, 0);
	});

	test('repl expressions', () => {
I
isidor 已提交
296
		assert.equal(model.getReplElements().length, 0);
I
isidor 已提交
297 298 299 300 301 302 303 304 305 306 307 308
		const stackFrame = new debugmodel.StackFrame(1, 1, null, 'app.js', 1, 1);
		model.addReplExpression(null, stackFrame, 'myVariable').done();
		model.addReplExpression(null, stackFrame, 'myVariable').done();
		model.addReplExpression(null, stackFrame, 'myVariable').done();

		assert.equal(model.getReplElements().length, 3);
		model.getReplElements().forEach(re => {
			assert.equal((<debugmodel.Expression> re).available, false);
			assert.equal((<debugmodel.Expression> re).name, 'myVariable');
			assert.equal((<debugmodel.Expression> re).reference, 0);
		});

309
		model.removeReplExpressions();
I
isidor 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
		assert.equal(model.getReplElements().length, 0);
	});

	// Repl output

	test('repl output', () => {
		model.logToRepl('first line', severity.Error);
		model.logToRepl('second line', severity.Warning);
		model.logToRepl('second line', severity.Warning);
		model.logToRepl('second line', severity.Error);

		let elements = <debugmodel.ValueOutputElement[]> model.getReplElements();
		assert.equal(elements.length, 3);
		assert.equal(elements[0].value, 'first line');
		assert.equal(elements[0].counter, 1);
		assert.equal(elements[0].severity, severity.Error);
		assert.equal(elements[1].value, 'second line');
		assert.equal(elements[1].counter, 2);
		assert.equal(elements[1].severity, severity.Warning);

		model.appendReplOutput('1', severity.Error);
		model.appendReplOutput('2', severity.Error);
		model.appendReplOutput('3', severity.Error);
		elements = <debugmodel.ValueOutputElement[]> model.getReplElements();
		assert.equal(elements.length, 4);
		assert.equal(elements[3].value, '123');
		assert.equal(elements[3].severity, severity.Error);

		const keyValueObject = { 'key1' : 2, 'key2': 'value' };
		model.logToRepl(keyValueObject);
		const element = <debugmodel.KeyValueOutputElement> model.getReplElements()[4];
		assert.equal(element.value, 'Object');
		assert.deepEqual(element.valueObj, keyValueObject);

344
		model.removeReplExpressions();
I
isidor 已提交
345 346 347 348 349 350
		assert.equal(model.getReplElements().length, 0);
	});

	// Utils

	test('full expression name', () => {
I
isidor 已提交
351 352 353 354
		const type = 'node';
		assert.equal(debugmodel.getFullExpressionName(new debugmodel.Expression(null, false), type), null);
		assert.equal(debugmodel.getFullExpressionName(new debugmodel.Expression('son', false), type), 'son');

355 356
		const scope = new debugmodel.Scope(1, 'myscope', 1, false, 1, 0);
		const son = new debugmodel.Variable(new debugmodel.Variable(new debugmodel.Variable(scope, 0, 'grandfather', '75', 1, 0), 0, 'father', '45', 1, 0), 0, 'son', '20', 1, 0);
I
isidor 已提交
357 358
		assert.equal(debugmodel.getFullExpressionName(son, type), 'grandfather.father.son');

359
		const grandson = new debugmodel.Variable(son, 0, '/weird_name', '1', 0, 0);
I
isidor 已提交
360
		assert.equal(debugmodel.getFullExpressionName(grandson, type), 'grandfather.father.son[\'/weird_name\']');
I
isidor 已提交
361
	});
E
Erich Gamma 已提交
362
});