提交 8bc4d854 编写于 作者: A Alex Dima

Minor cleanup in tests

上级 458a1c46
......@@ -8,10 +8,11 @@ import * as assert from 'assert';
import {ShiftCommand} from 'vs/editor/common/commands/shiftCommand';
import {Selection} from 'vs/editor/common/core/selection';
import {IIdentifiedSingleEditOperation} from 'vs/editor/common/editorCommon';
import {IMode, IRichEditSupport, IndentAction} from 'vs/editor/common/modes';
import {IRichEditSupport, IndentAction} from 'vs/editor/common/modes';
import {RichEditSupport} from 'vs/editor/common/modes/supports/richEditSupport';
import {createSingleEditOp, getEditOperation, testCommand} from 'vs/editor/test/common/commands/commandTestUtils';
import {withEditorModel} from 'vs/editor/test/common/editorTestUtils';
import {MockMode} from 'vs/editor/test/common/mocks/mockMode';
function testShiftCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
testCommand(lines, null, selection, (sel) => new ShiftCommand(sel, {
......@@ -29,11 +30,12 @@ function testUnshiftCommand(lines: string[], selection: Selection, expectedLines
}), expectedLines, expectedSelection);
}
class DocBlockCommentMode implements IMode {
class DocBlockCommentMode extends MockMode {
public richEditSupport: IRichEditSupport;
constructor() {
super();
this.richEditSupport = new RichEditSupport(this.getId(), null, {
brackets: [
['(', ')'],
......@@ -66,15 +68,6 @@ class DocBlockCommentMode implements IMode {
]
});
}
public getId(): string {
return 'docBlockCommentMode';
}
public toSimplifiedMode(): IMode {
return this;
}
}
function testShiftCommandInDocBlockCommentMode(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
......
......@@ -7,9 +7,10 @@
import * as assert from 'assert';
import {TrimTrailingWhitespaceCommand, trimTrailingWhitespace} from 'vs/editor/common/commands/trimTrailingWhitespaceCommand';
import {Selection} from 'vs/editor/common/core/selection';
import {Position} from 'vs/editor/common/core/position';
import {IIdentifiedSingleEditOperation, IPosition} from 'vs/editor/common/editorCommon';
import {createInsertDeleteSingleEditOp, createSingleEditOp, getEditOperation} from 'vs/editor/test/common/commands/commandTestUtils';
import {pos, withEditorModel} from 'vs/editor/test/common/editorTestUtils';
import {withEditorModel} from 'vs/editor/test/common/editorTestUtils';
function assertTrimTrailingWhitespaceCommand(text:string[], expected:IIdentifiedSingleEditOperation[]): void {
return withEditorModel(text, (model) => {
......@@ -49,9 +50,9 @@ suite('Editor Commands - Trim Trailing Whitespace Command', () => {
]);
assertTrimTrailingWhitespace(['text '], [pos(1,1), pos(1,2), pos(1,3)], [createInsertDeleteSingleEditOp(null, 1, 5, 1, 8)]);
assertTrimTrailingWhitespace(['text '], [pos(1,1), pos(1,5)], [createInsertDeleteSingleEditOp(null, 1, 5, 1, 8)]);
assertTrimTrailingWhitespace(['text '], [pos(1,1), pos(1,5), pos(1,6)], [createInsertDeleteSingleEditOp(null, 1, 6, 1, 8)]);
assertTrimTrailingWhitespace(['text '], [new Position(1,1), new Position(1,2), new Position(1,3)], [createInsertDeleteSingleEditOp(null, 1, 5, 1, 8)]);
assertTrimTrailingWhitespace(['text '], [new Position(1,1), new Position(1,5)], [createInsertDeleteSingleEditOp(null, 1, 5, 1, 8)]);
assertTrimTrailingWhitespace(['text '], [new Position(1,1), new Position(1,5), new Position(1,6)], [createInsertDeleteSingleEditOp(null, 1, 6, 1, 8)]);
assertTrimTrailingWhitespace([
'some text\t',
'some more text',
......@@ -70,7 +71,7 @@ suite('Editor Commands - Trim Trailing Whitespace Command', () => {
'\t ',
'even more text ',
'and some mixed\t \t'
], [pos(1,11), pos(3,2), pos(5,1), pos(4,1), pos(5,10)], [
], [new Position(1,11), new Position(3,2), new Position(5,1), new Position(4,1), new Position(5,10)], [
createInsertDeleteSingleEditOp(null, 3, 2, 3, 4),
createInsertDeleteSingleEditOp(null, 4, 15, 4, 17),
createInsertDeleteSingleEditOp(null, 5, 15, 5, 20)
......
/*---------------------------------------------------------------------------------------------
* 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 {TextModel} from 'vs/editor/common/model/textModel';
import {DefaultEndOfLine} from 'vs/editor/common/editorCommon';
suite('Editor Config - CommonEditorConfig', () => {
test('Configuration.normalizeIndentation', () => {
var model = new TextModel([], {
length: 0,
lines: [],
BOM: '',
EOL: '\n',
options: {
tabSize: 4,
insertSpaces: false,
defaultEOL: DefaultEndOfLine.LF
}
});
assert.equal(model.normalizeIndentation('\t'), '\t');
assert.equal(model.normalizeIndentation(' '), '\t');
assert.equal(model.normalizeIndentation(' '), ' ');
assert.equal(model.normalizeIndentation(' '), ' ');
assert.equal(model.normalizeIndentation(' '), ' ');
assert.equal(model.normalizeIndentation(''), '');
assert.equal(model.normalizeIndentation(' \t '), '\t\t');
assert.equal(model.normalizeIndentation(' \t '), '\t ');
assert.equal(model.normalizeIndentation(' \t '), '\t ');
assert.equal(model.normalizeIndentation(' \t'), '\t ');
assert.equal(model.normalizeIndentation('\ta'), '\ta');
assert.equal(model.normalizeIndentation(' a'), '\ta');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation('a'), 'a');
assert.equal(model.normalizeIndentation(' \t a'), '\t\ta');
assert.equal(model.normalizeIndentation(' \t a'), '\t a');
assert.equal(model.normalizeIndentation(' \t a'), '\t a');
assert.equal(model.normalizeIndentation(' \ta'), '\t a');
model.dispose();
model = new TextModel([], {
length: 0,
lines: [],
BOM: '',
EOL: '\n',
options: {
tabSize: 4,
insertSpaces: true,
defaultEOL: DefaultEndOfLine.LF
}
});
assert.equal(model.normalizeIndentation('\ta'), ' a');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation('a'), 'a');
assert.equal(model.normalizeIndentation(' \t a'), ' a');
assert.equal(model.normalizeIndentation(' \t a'), ' a');
assert.equal(model.normalizeIndentation(' \t a'), ' a');
assert.equal(model.normalizeIndentation(' \ta'), ' a');
model.dispose();
});
});
......@@ -4,16 +4,8 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import {IPosition} from 'vs/editor/common/editorCommon';
import {Model} from 'vs/editor/common/model/model';
export function pos(lineNumber:number, column:number): IPosition {
return {
lineNumber: lineNumber,
column: column
};
}
export function withEditorModel(text:string[], callback:(model:Model) => void): void {
var model = new Model(text.join('\n'), Model.DEFAULT_CREATION_OPTIONS, null);
callback(model);
......
/*---------------------------------------------------------------------------------------------
* 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 modes from 'vs/editor/common/modes';
export class MockMode implements modes.IMode {
private _id:string;
constructor(id:string = 'mockMode') {
this._id = id;
}
public getId():string {
return this._id;
}
public toSimplifiedMode(): modes.IMode {
return this;
}
}
......@@ -454,4 +454,69 @@ suite('Editor Model - TextModel', () => {
assert.throws(() => m.modifyPosition(new Position(2, 2), -100));
assert.throws(() => m.modifyPosition(new Position(2, 9), -18));
});
test('normalizeIndentation 1', () => {
let model = new TextModel([], {
length: 0,
lines: [],
BOM: '',
EOL: '\n',
options: {
tabSize: 4,
insertSpaces: false,
defaultEOL: DefaultEndOfLine.LF
}
});
assert.equal(model.normalizeIndentation('\t'), '\t');
assert.equal(model.normalizeIndentation(' '), '\t');
assert.equal(model.normalizeIndentation(' '), ' ');
assert.equal(model.normalizeIndentation(' '), ' ');
assert.equal(model.normalizeIndentation(' '), ' ');
assert.equal(model.normalizeIndentation(''), '');
assert.equal(model.normalizeIndentation(' \t '), '\t\t');
assert.equal(model.normalizeIndentation(' \t '), '\t ');
assert.equal(model.normalizeIndentation(' \t '), '\t ');
assert.equal(model.normalizeIndentation(' \t'), '\t ');
assert.equal(model.normalizeIndentation('\ta'), '\ta');
assert.equal(model.normalizeIndentation(' a'), '\ta');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation('a'), 'a');
assert.equal(model.normalizeIndentation(' \t a'), '\t\ta');
assert.equal(model.normalizeIndentation(' \t a'), '\t a');
assert.equal(model.normalizeIndentation(' \t a'), '\t a');
assert.equal(model.normalizeIndentation(' \ta'), '\t a');
model.dispose();
});
test('normalizeIndentation 2', () => {
let model = new TextModel([], {
length: 0,
lines: [],
BOM: '',
EOL: '\n',
options: {
tabSize: 4,
insertSpaces: true,
defaultEOL: DefaultEndOfLine.LF
}
});
assert.equal(model.normalizeIndentation('\ta'), ' a');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation(' a'), ' a');
assert.equal(model.normalizeIndentation('a'), 'a');
assert.equal(model.normalizeIndentation(' \t a'), ' a');
assert.equal(model.normalizeIndentation(' \t a'), ' a');
assert.equal(model.normalizeIndentation(' \t a'), ' a');
assert.equal(model.normalizeIndentation(' \ta'), ' a');
model.dispose();
});
});
......@@ -9,6 +9,7 @@ import {IMode, IStream, ITokenizationResult, ITokenizationSupport} from 'vs/edit
import {AbstractState} from 'vs/editor/common/modes/abstractState';
import {TokenizationSupport} from 'vs/editor/common/modes/supports/tokenizationSupport';
import {tokenizeToHtmlContent} from 'vs/editor/common/modes/textToHtmlTokenizer';
import {MockMode} from 'vs/editor/test/common/mocks/mockMode';
suite('Editor Modes - textToHtmlTokenizer', () => {
test('TextToHtmlTokenizer', () => {
......@@ -71,21 +72,14 @@ class State extends AbstractState {
}
}
class Mode implements IMode {
class Mode extends MockMode {
public tokenizationSupport: ITokenizationSupport;
constructor() {
super();
this.tokenizationSupport = new TokenizationSupport(this, {
getInitialState: () => new State(this)
}, false, false);
}
public getId(): string {
return 'testMode';
}
public toSimplifiedMode(): IMode {
return this;
}
}
......@@ -11,7 +11,8 @@ import * as modes from 'vs/editor/common/modes';
import {AbstractState} from 'vs/editor/common/modes/abstractState';
import {handleEvent} from 'vs/editor/common/modes/supports';
import {IEnteringNestedModeData, ILeavingNestedModeData, TokenizationSupport} from 'vs/editor/common/modes/supports/tokenizationSupport';
import {createLineContext} from 'vs/editor/test/common/modesTestUtils';
import {createMockLineContext} from 'vs/editor/test/common/modesTestUtils';
import {MockMode} from 'vs/editor/test/common/mocks/mockMode';
export class State extends AbstractState {
......@@ -28,23 +29,16 @@ export class State extends AbstractState {
}
}
export class Mode implements modes.IMode {
export class Mode extends MockMode {
public tokenizationSupport: modes.ITokenizationSupport;
constructor() {
super();
this.tokenizationSupport = new TokenizationSupport(this, {
getInitialState: () => new State(this)
}, false, false);
}
public getId(): string {
return 'testMode';
}
public toSimplifiedMode(): modes.IMode {
return this;
}
}
......@@ -98,27 +92,18 @@ export class StateMemorizingLastWord extends AbstractState {
}
}
export class SwitchingMode implements modes.IMode {
export class SwitchingMode extends MockMode {
private _id:string;
private _switchingModeDescriptor:IModeSwitchingDescriptor;
public tokenizationSupport: modes.ITokenizationSupport;
constructor(id:string, descriptor:IModeSwitchingDescriptor) {
this._id = id;
super(id);
this._switchingModeDescriptor = descriptor;
this.tokenizationSupport = new TokenizationSupport(this, this, true, false);
}
public getId():string {
return this._id;
}
public toSimplifiedMode(): modes.IMode {
return this;
}
public addSupportChangedListener(callback: (e: IModeSupportChangedEvent) => void): IDisposable {
return EmptyDisposable;
}
......@@ -354,7 +339,7 @@ suite('Editor Modes - Tokenization', () => {
{ startIndex: 5, id: 'B' }
]);
handleEvent(createLineContext('abc (def', lineTokens), 0, (mode:modes.IMode, context:modes.ILineContext, offset:number) => {
handleEvent(createMockLineContext('abc (def', lineTokens), 0, (mode:modes.IMode, context:modes.ILineContext, offset:number) => {
assert.deepEqual(mode.getId(), 'A');
assert.equal(context.getTokenCount(), 3);
assert.equal(context.getTokenStartIndex(0), 0);
......@@ -367,7 +352,7 @@ suite('Editor Modes - Tokenization', () => {
assert.equal(context.getLineContent(), 'abc (');
});
handleEvent(createLineContext('abc (def', lineTokens), 6, (mode:modes.IMode, context:modes.ILineContext, offset:number) => {
handleEvent(createMockLineContext('abc (def', lineTokens), 6, (mode:modes.IMode, context:modes.ILineContext, offset:number) => {
assert.deepEqual(mode.getId(), 'B');
assert.equal(context.getTokenCount(), 1);
assert.equal(context.getTokenStartIndex(0), 0);
......
......@@ -7,31 +7,22 @@
import {Arrays} from 'vs/editor/common/core/arrays';
import * as modes from 'vs/editor/common/modes';
import {RichEditSupport} from 'vs/editor/common/modes/supports/richEditSupport';
import {MockMode} from 'vs/editor/test/common/mocks/mockMode';
class SimpleTokenTypeClassificationMode implements modes.IMode {
private _id:string;
class ModeWithRichEditSupport extends MockMode {
public richEditSupport: modes.IRichEditSupport;
constructor(id:string, wordRegExp:RegExp) {
this._id = id;
this.richEditSupport = new RichEditSupport(this._id, null, {
super(id);
this.richEditSupport = new RichEditSupport(id, null, {
wordPattern: wordRegExp
});
}
public getId(): string {
return this._id;
}
public toSimplifiedMode(): modes.IMode {
return this;
}
}
export function createMockMode(id:string, wordRegExp:RegExp = null):modes.IMode {
return new SimpleTokenTypeClassificationMode(id, wordRegExp);
return new ModeWithRichEditSupport(id, wordRegExp);
}
export interface TokenText {
......@@ -53,7 +44,7 @@ export function createLineContextFromTokenText(tokens: TokenText[]): modes.ILine
return new TestLineContext(line, processedTokens, null);
}
export function createLineContext(line:string, tokens:modes.ILineTokens): modes.ILineContext {
export function createMockLineContext(line:string, tokens:modes.ILineTokens): modes.ILineContext {
return new TestLineContext(line, tokens.tokens, tokens.modeTransitions);
}
......
......@@ -12,6 +12,7 @@ import {compile} from 'vs/editor/common/modes/monarch/monarchCompile';
import {createTokenizationSupport} from 'vs/editor/common/modes/monarch/monarchLexer';
import {ILanguage} from 'vs/editor/common/modes/monarch/monarchTypes';
import {createMockModeService} from 'vs/editor/test/common/servicesTestUtils';
import {MockMode} from 'vs/editor/test/common/mocks/mockMode';
export interface IRelaxedToken {
startIndex:number;
......@@ -59,29 +60,12 @@ export interface IOnEnterAsserter {
indentsOutdents(oneLineAboveText:string, beforeText:string, afterText:string): void;
}
class SimpleMode implements modes.IMode {
private _id:string;
constructor(id:string) {
this._id = id;
}
public getId(): string {
return this._id;
}
public toSimplifiedMode(): modes.IMode {
return this;
}
}
export function createOnEnterAsserter(modeId:string, richEditSupport: modes.IRichEditSupport): IOnEnterAsserter {
var assertOne = (oneLineAboveText:string, beforeText:string, afterText:string, expected: modes.IndentAction) => {
var model = new Model(
[ oneLineAboveText, beforeText + afterText ].join('\n'),
Model.DEFAULT_CREATION_OPTIONS,
new SimpleMode(modeId)
new MockMode(modeId)
);
var actual = richEditSupport.onEnter.onEnter(model, { lineNumber: 2, column: beforeText.length + 1 });
if (expected === modes.IndentAction.None) {
......@@ -120,7 +104,7 @@ export function executeMonarchTokenizationTests(name:string, language:ILanguage,
var modeService = createMockModeService();
var tokenizationSupport = createTokenizationSupport(modeService, new SimpleMode('mock.mode'), lexer);
var tokenizationSupport = createTokenizationSupport(modeService, new MockMode(), lexer);
executeTests(tokenizationSupport, tests);
}
......
......@@ -8,6 +8,7 @@ import * as modes from 'vs/editor/common/modes';
import {AbstractState} from 'vs/editor/common/modes/abstractState';
import {RichEditSupport} from 'vs/editor/common/modes/supports/richEditSupport';
import {TokenizationSupport} from 'vs/editor/common/modes/supports/tokenizationSupport';
import {MockMode} from 'vs/editor/test/common/mocks/mockMode';
export class CommentState extends AbstractState {
......@@ -29,12 +30,13 @@ export class CommentState extends AbstractState {
}
}
export class CommentMode implements modes.IMode {
export class CommentMode extends MockMode {
public tokenizationSupport: modes.ITokenizationSupport;
public richEditSupport: modes.IRichEditSupport;
constructor(commentsConfig:modes.ICommentsConfiguration) {
super();
this.tokenizationSupport = new TokenizationSupport(this, {
getInitialState: () => new CommentState(this, 0)
}, false, false);
......@@ -43,27 +45,9 @@ export class CommentMode implements modes.IMode {
comments:commentsConfig
};
}
public getId():string {
return 'tests.commentMode';
}
public toSimplifiedMode(): modes.IMode {
return this;
}
}
export class TestingMode implements modes.IMode {
public getId():string {
return 'testing';
}
public toSimplifiedMode(): modes.IMode {
return this;
}
}
export abstract class AbstractIndentingMode extends TestingMode {
export abstract class AbstractIndentingMode extends MockMode {
public getElectricCharacters():string[] {
return null;
......@@ -100,7 +84,7 @@ export class ModelState1 extends AbstractState {
}
}
export class ModelMode1 extends TestingMode {
export class ModelMode1 extends MockMode {
public calledFor:string[];
public tokenizationSupport: modes.ITokenizationSupport;
......@@ -141,7 +125,7 @@ export class ModelState2 extends AbstractState {
}
}
export class ModelMode2 extends TestingMode {
export class ModelMode2 extends MockMode {
public calledFor:any[];
public tokenizationSupport: modes.ITokenizationSupport;
......@@ -155,78 +139,12 @@ export class ModelMode2 extends TestingMode {
}
}
export class BracketState extends AbstractState {
private allResults:{
[key:string]:modes.ITokenizationResult;
};
constructor(mode:modes.IMode) {
super(mode);
this.allResults = null;
}
public makeClone():BracketState {
return this;
}
public equals(other: modes.IState):boolean {
return true;
}
public tokenize(stream:modes.IStream):modes.ITokenizationResult {
this.initializeAllResults();
stream.setTokenRules('{}[]()', '');
var token= stream.nextToken();
// Strade compiler bug: can't reference self in Object return creation.
var state:modes.IState = this;
if (this.allResults.hasOwnProperty(token)) {
return this.allResults[token];
} else {
return {
type: '',
nextState: state
};
}
}
public initializeAllResults(): void {
if (this.allResults !== null) {
return;
}
this.allResults = {};
var brackets:any= {
'{': '}',
'[': ']',
'(': ')'
};
var type= 1;
var state:modes.IState = this;
for (var x in brackets) {
this.allResults[x]= {
type: 'bracket' + type,
nextState: state
};
this.allResults[brackets[x]] = {
type: 'bracket' + type,
nextState: state
};
type++;
}
}
}
export class BracketMode extends TestingMode {
export class BracketMode extends MockMode {
public tokenizationSupport: modes.ITokenizationSupport;
public richEditSupport: modes.IRichEditSupport;
constructor() {
super();
this.tokenizationSupport = new TokenizationSupport(this, {
getInitialState: () => new BracketState(this)
}, false, false);
this.richEditSupport = new RichEditSupport(this.getId(), null, {
brackets: [
['{', '}'],
......@@ -267,7 +185,7 @@ export class NState extends AbstractState {
}
}
export class NMode extends TestingMode {
export class NMode extends MockMode {
private n:number;
......
......@@ -10,7 +10,7 @@ import javascriptMode = require('vs/languages/javascript/common/javascript');
import EditorCommon = require('vs/editor/common/editorCommon');
import Modes = require('vs/editor/common/modes');
import modesUtil = require('vs/editor/test/common/modesUtil');
import {createLineContext} from 'vs/editor/test/common/modesTestUtils';
import {createMockLineContext} from 'vs/editor/test/common/modesTestUtils';
suite('JS - Auto Indent', () => {
......@@ -63,7 +63,7 @@ suite('JS - Auto Indent', () => {
function testElectricCharacter(line:string, offset:number, expected:Modes.IElectricAction): void {
let state = _mode.tokenizationSupport.getInitialState();
var lineTokens = _mode.tokenizationSupport.tokenize(line, state);
let actual = _mode.richEditSupport.electricCharacter.onElectricCharacter(createLineContext(line, lineTokens), offset);
let actual = _mode.richEditSupport.electricCharacter.onElectricCharacter(createMockLineContext(line, lineTokens), offset);
assert.deepEqual(actual, expected, 'LINE <<<' + line + '>>>, OFFSET: <<<' + offset + '>>>');
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册