Skip to content

Commit

Permalink
Each test instantiates its own mode
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Apr 7, 2016
1 parent 84503d5 commit 3af5cc7
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 66 deletions.
18 changes: 12 additions & 6 deletions src/vs/editor/contrib/smartSelect/common/tokenTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class TokenScanner {

let tokenType = this._currentLineTokens.getTokenType(this._currentTokenIndex);
let tokenEndIndex = this._currentLineTokens.getTokenEndIndex(this._currentTokenIndex, this._currentLineText.length);
let tmpTokenEndIndex = tokenEndIndex;

let nextBracket: Range = null;
if (this._currentModeBrackets && !ignoreBracketsInToken(tokenType)) {
Expand All @@ -172,7 +173,7 @@ class TokenScanner {

if (nextBracket && this._currentTokenStart < nextBracket.startColumn - 1) {
// found a bracket, but it is not at the beginning of the token
tokenEndIndex = nextBracket.startColumn - 1;
tmpTokenEndIndex = nextBracket.startColumn - 1;
nextBracket = null;
}

Expand All @@ -192,13 +193,18 @@ class TokenScanner {
startLineNumber: this._currentLineNumber,
startColumn: 1 + this._currentTokenStart,
endLineNumber: this._currentLineNumber,
endColumn: 1 + tokenEndIndex
endColumn: 1 + tmpTokenEndIndex
}
};
// console.log('TOKEN: <<' + this._currentLineText.substring(this._currentTokenStart, tokenEndIndex) + '>>');

this._currentTokenIndex += 1;
this._currentTokenStart = (this._currentTokenIndex < this._currentLineTokens.getTokenCount() ? this._currentLineTokens.getTokenStartIndex(this._currentTokenIndex) : 0);
// console.log('TOKEN: <<' + this._currentLineText.substring(this._currentTokenStart, tmpTokenEndIndex) + '>>');

if (tmpTokenEndIndex < tokenEndIndex) {
// there is a bracket somewhere in this token...
this._currentTokenStart = tmpTokenEndIndex;
} else {
this._currentTokenIndex += 1;
this._currentTokenStart = (this._currentTokenIndex < this._currentLineTokens.getTokenCount() ? this._currentLineTokens.getTokenStartIndex(this._currentTokenIndex) : 0);
}
return token;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,61 @@
*--------------------------------------------------------------------------------------------*/
'use strict';

import 'vs/languages/typescript/common/typescript.contribution';
import * as assert from 'assert';
import URI from 'vs/base/common/uri';
import {Range} from 'vs/editor/common/core/range';
import {IMode} from 'vs/editor/common/modes';
import {IMode, IRichEditSupport, IndentAction} from 'vs/editor/common/modes';
import {TokenSelectionSupport} from 'vs/editor/contrib/smartSelect/common/tokenSelectionSupport';
import {load} from 'vs/editor/test/common/modesUtil';
import {createMockModelService} from 'vs/editor/test/common/servicesTestUtils';
import {MockTokenizingMode} from 'vs/editor/test/common/mocks/mockMode';
import {RichEditSupport} from 'vs/editor/common/modes/supports/richEditSupport';

class MockJSMode extends MockTokenizingMode {

public richEditSupport: IRichEditSupport;

constructor() {
super('js', 'mock-js');

this.richEditSupport = new RichEditSupport(this.getId(), null, {
brackets: [
['(', ')'],
['{', '}'],
['[', ']']
],

onEnterRules: [
{
// e.g. /** | */
beforeText: /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
afterText: /^\s*\*\/$/,
action: { indentAction: IndentAction.IndentOutdent, appendText: ' * ' }
},
{
// e.g. /** ...|
beforeText: /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
action: { indentAction: IndentAction.None, appendText: ' * ' }
},
{
// e.g. * ...|
beforeText: /^(\t|(\ \ ))*\ \*(\ ([^\*]|\*(?!\/))*)?$/,
action: { indentAction: IndentAction.None, appendText: '* ' }
},
{
// e.g. */|
beforeText: /^(\t|(\ \ ))*\ \*\/\s*$/,
action: { indentAction: IndentAction.None, removeText: 1 }
}
]
});
}
}

suite('TokenSelectionSupport', () => {

let modelService = createMockModelService();
let tokenSelectionSupport = new TokenSelectionSupport(modelService);
let _mode: IMode;

suiteSetup((done) => {
load('javascript').then(mode => {
_mode = mode;
done();
});
});
let _mode: IMode = new MockJSMode();

function assertGetRangesToPosition(text:string[], lineNumber:number, column:number, ranges:Range[]): void {
let uri = URI.file('test.js');
Expand Down Expand Up @@ -62,7 +96,7 @@ suite('TokenSelectionSupport', () => {
new Range(3, 11, 3, 26),
new Range(3, 17, 3, 26),
new Range(3, 18, 3, 25),
new Range(3, 19, 3, 20)
// new Range(3, 19, 3, 20)
]);
});
});
12 changes: 0 additions & 12 deletions src/vs/editor/test/common/modesUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ export function assertWords(actual:string[], expected:string[], message?:string)
assert.deepEqual(actual, expected, message);
}

export function load(modeId: string, preloadModes: string[] = [] ): TPromise<modes.IMode> {
var toLoad:string[] = [].concat(preloadModes).concat([modeId]);

var modeService = createMockModeService();

var promises = toLoad.map(modeId => modeService.getOrCreateMode(modeId));

return TPromise.join(promises).then(modes => {
return modes[modes.length -1];
});
}

export function assertTokenization(tokenizationSupport: modes.ITokenizationSupport, tests: ITestItem[]): void {
var state = tokenizationSupport.getInitialState();
for (var i = 0, len = tests.length; i < len; i++) {
Expand Down
28 changes: 19 additions & 9 deletions src/vs/languages/css/test/common/css.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*--------------------------------------------------------------------------------------------*/
'use strict';

import 'vs/languages/css/common/css.contribution';
import cssMode = require('vs/languages/css/common/css');
import EditorCommon = require('vs/editor/common/editorCommon');
import Modes = require('vs/editor/common/modes');
import modesUtil = require('vs/editor/test/common/modesUtil');
import {cssTokenTypes} from 'vs/languages/css/common/css';
import {cssTokenTypes, CSSMode} from 'vs/languages/css/common/css';
import {NULL_THREAD_SERVICE} from 'vs/platform/test/common/nullThreadService';
import {createInstantiationService} from 'vs/platform/instantiation/common/instantiationService';

suite('CSS Colorizing', () => {

Expand All @@ -18,14 +19,23 @@ suite('CSS Colorizing', () => {
var tokenizationSupport: Modes.ITokenizationSupport;
var assertOnEnter: modesUtil.IOnEnterAsserter;

suiteSetup((done) => {
modesUtil.load('css').then(mode => {
tokenizationSupport = mode.tokenizationSupport;
assertOnEnter = modesUtil.createOnEnterAsserter(mode.getId(), mode.richEditSupport);
wordDefinition = mode.richEditSupport.wordDefinition;
done();
(function() {
let threadService = NULL_THREAD_SERVICE;
let inst = createInstantiationService({
threadService: threadService,
});
});
threadService.setInstantiationService(inst);

let mode = new CSSMode(
{ id: 'css' },
inst,
threadService
);

tokenizationSupport = mode.tokenizationSupport;
assertOnEnter = modesUtil.createOnEnterAsserter(mode.getId(), mode.richEditSupport);
wordDefinition = mode.richEditSupport.wordDefinition;
})();

test('Skip whitespace', () => {
modesUtil.assertTokenization(tokenizationSupport, [{
Expand Down
26 changes: 20 additions & 6 deletions src/vs/languages/html/test/common/html-worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/
'use strict';

import 'vs/languages/html/common/html.contribution';
import assert = require('assert');
import mm = require('vs/editor/common/model/mirrorModel');
import htmlWorker = require('vs/languages/html/common/htmlWorker');
Expand All @@ -16,17 +15,32 @@ import WinJS = require('vs/base/common/winjs.base');
import modesUtil = require('vs/editor/test/common/modesUtil');
import servicesUtil2 = require('vs/editor/test/common/servicesTestUtils');
import {NULL_THREAD_SERVICE} from 'vs/platform/test/common/nullThreadService';
import {HTMLMode} from 'vs/languages/html/common/html';
import {createInstantiationService} from 'vs/platform/instantiation/common/instantiationService';
import {MockModeService} from 'vs/editor/test/common/mocks/mockModeService';

suite('HTML - worker', () => {

var mode: Modes.IMode;

suiteSetup((done) => {
modesUtil.load('html').then(_mode => {
mode = _mode;
done();
(function() {

let threadService = NULL_THREAD_SERVICE;
let modeService = new MockModeService();
let inst = createInstantiationService({
threadService: threadService,
modeService: modeService
});
});
threadService.setInstantiationService(inst);

mode = new HTMLMode<htmlWorker.HTMLWorker>(
{ id: 'html' },
inst,
modeService,
threadService
);

})();

var mockHtmlWorkerEnv = function (url: URI, content: string): { worker: htmlWorker.HTMLWorker; model: mm.MirrorModel; } {
var resourceService = new ResourceService.ResourceService();
Expand Down
2 changes: 1 addition & 1 deletion src/vs/languages/html/test/common/html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import htmlWorker = require('vs/languages/html/common/htmlWorker');
import {MockTokenizingMode} from 'vs/editor/test/common/mocks/mockMode';
import {RichEditSupport} from 'vs/editor/common/modes/supports/richEditSupport';

export class MockJSMode extends MockTokenizingMode {
class MockJSMode extends MockTokenizingMode {

public richEditSupport: Modes.IRichEditSupport;

Expand Down
26 changes: 19 additions & 7 deletions src/vs/languages/json/test/common/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,38 @@
*--------------------------------------------------------------------------------------------*/
'use strict';

import 'vs/languages/json/common/json.contribution';
import jsonMode = require('vs/languages/json/common/json');
import EditorCommon = require('vs/editor/common/editorCommon');
import Modes = require('vs/editor/common/modes');
import modesUtil = require('vs/editor/test/common/modesUtil');
import tokenization = require('vs/languages/json/common/features/tokenization');
import jsonTokenTypes = require('vs/languages/json/common/features/jsonTokenTypes');
import {NULL_THREAD_SERVICE} from 'vs/platform/test/common/nullThreadService';
import {createInstantiationService} from 'vs/platform/instantiation/common/instantiationService';

suite('JSON - tokenization', () => {

var tokenizationSupport: Modes.ITokenizationSupport;
var assertOnEnter: modesUtil.IOnEnterAsserter;

setup((done) => {
modesUtil.load('json').then(mode => {
tokenizationSupport = mode.tokenizationSupport;
assertOnEnter = modesUtil.createOnEnterAsserter(mode.getId(), mode.richEditSupport);
done();
(function() {

let threadService = NULL_THREAD_SERVICE;
let inst = createInstantiationService({
threadService: threadService
});
});
threadService.setInstantiationService(inst);

let mode = new jsonMode.JSONMode(
{ id: 'json' },
inst,
threadService
)

tokenizationSupport = mode.tokenizationSupport;
assertOnEnter = modesUtil.createOnEnterAsserter(mode.getId(), mode.richEditSupport);

})();

test('', () => {
modesUtil.executeTests(tokenizationSupport,[
Expand Down
29 changes: 23 additions & 6 deletions src/vs/languages/razor/test/common/razor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,38 @@
*--------------------------------------------------------------------------------------------*/
'use strict';

import 'vs/languages/razor/common/razor.contribution';
import modesUtil = require('vs/editor/test/common/modesUtil');
import Modes = require('vs/editor/common/modes');
import razorTokenTypes = require('vs/languages/razor/common/razorTokenTypes');
import {htmlTokenTypes} from 'vs/languages/html/common/html';
import {RAZORMode} from 'vs/languages/razor/common/razor';
import {NULL_THREAD_SERVICE} from 'vs/platform/test/common/nullThreadService';
import {MockModeService} from 'vs/editor/test/common/mocks/mockModeService';
import {createInstantiationService} from 'vs/platform/instantiation/common/instantiationService';

suite('Syntax Highlighting - Razor', () => {

var tokenizationSupport: Modes.ITokenizationSupport;
setup((done) => {
modesUtil.load('razor').then(mode => {
tokenizationSupport = mode.tokenizationSupport;
done();

(function() {

let threadService = NULL_THREAD_SERVICE;
let modeService = new MockModeService();
let inst = createInstantiationService({
threadService: threadService,
modeService: modeService
});
});
threadService.setInstantiationService(inst);

let mode = new RAZORMode(
{ id: 'razor' },
inst,
modeService,
threadService
);

tokenizationSupport = mode.tokenizationSupport;
})();

test('', () => {
modesUtil.executeTests(tokenizationSupport,[
Expand Down
33 changes: 26 additions & 7 deletions src/vs/languages/sass/test/common/sass.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,43 @@
*--------------------------------------------------------------------------------------------*/
'use strict';

import 'vs/languages/sass/common/sass.contribution';
import SASS = require('vs/languages/sass/common/sass');
import modesUtil = require('vs/editor/test/common/modesUtil');
import Modes = require('vs/editor/common/modes');
import * as sassTokenTypes from 'vs/languages/sass/common/sassTokenTypes';
import {NULL_THREAD_SERVICE} from 'vs/platform/test/common/nullThreadService';
import {MockModeService} from 'vs/editor/test/common/mocks/mockModeService';
import {createInstantiationService} from 'vs/platform/instantiation/common/instantiationService';

suite('Sass Colorizer', () => {

var tokenizationSupport: Modes.ITokenizationSupport;
var assertOnEnter: modesUtil.IOnEnterAsserter;

setup((done) => {
modesUtil.load('sass').then(mode => {
tokenizationSupport = mode.tokenizationSupport;
assertOnEnter = modesUtil.createOnEnterAsserter(mode.getId(), mode.richEditSupport);
done();
(function() {

let threadService = NULL_THREAD_SERVICE;
let modeService = new MockModeService();
let inst = createInstantiationService({
threadService: threadService,
modeService: modeService
});
});
threadService.setInstantiationService(inst);


let mode = new SASS.SASSMode(
{ id: 'sass' },
inst,
threadService,
modeService,
null,
null
);

tokenizationSupport = mode.tokenizationSupport;
assertOnEnter = modesUtil.createOnEnterAsserter(mode.getId(), mode.richEditSupport);

})();

test('', () => {
modesUtil.executeTests(tokenizationSupport, [
Expand Down

0 comments on commit 3af5cc7

Please sign in to comment.