Path: blob/main/src/vs/editor/test/common/model/editableTextModelAuto.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { CharCode } from '../../../../base/common/charCode.js';6import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';7import { ISingleEditOperation } from '../../../common/core/editOperation.js';8import { Position } from '../../../common/core/position.js';9import { Range } from '../../../common/core/range.js';10import { testApplyEditsWithSyncedModels } from './editableTextModelTestUtils.js';1112const GENERATE_TESTS = false;1314suite('EditorModel Auto Tests', () => {1516ensureNoDisposablesAreLeakedInTestSuite();1718function editOp(startLineNumber: number, startColumn: number, endLineNumber: number, endColumn: number, text: string[]): ISingleEditOperation {19return {20range: new Range(startLineNumber, startColumn, endLineNumber, endColumn),21text: text.join('\n'),22forceMoveMarkers: false23};24}2526test('auto1', () => {27testApplyEditsWithSyncedModels(28[29'ioe',30'',31'yjct',32'',33'',34],35[36editOp(1, 2, 1, 2, ['b', 'r', 'fq']),37editOp(1, 4, 2, 1, ['', '']),38],39[40'ib',41'r',42'fqoe',43'',44'yjct',45'',46'',47]48);49});5051test('auto2', () => {52testApplyEditsWithSyncedModels(53[54'f',55'littnhskrq',56'utxvsizqnk',57'lslqz',58'jxn',59'gmm',60],61[62editOp(1, 2, 1, 2, ['', 'o']),63editOp(2, 4, 2, 4, ['zaq', 'avb']),64editOp(2, 5, 6, 2, ['jlr', 'zl', 'j']),65],66[67'f',68'o',69'litzaq',70'avbtjlr',71'zl',72'jmm',73]74);75});7677test('auto3', () => {78testApplyEditsWithSyncedModels(79[80'ofw',81'qsxmziuvzw',82'rp',83'qsnymek',84'elth',85'wmgzbwudxz',86'iwsdkndh',87'bujlbwb',88'asuouxfv',89'xuccnb',90],91[92editOp(4, 3, 4, 3, ['']),93],94[95'ofw',96'qsxmziuvzw',97'rp',98'qsnymek',99'elth',100'wmgzbwudxz',101'iwsdkndh',102'bujlbwb',103'asuouxfv',104'xuccnb',105]106);107});108109test('auto4', () => {110testApplyEditsWithSyncedModels(111[112'fefymj',113'qum',114'vmiwxxaiqq',115'dz',116'lnqdgorosf',117],118[119editOp(1, 3, 1, 5, ['hp']),120editOp(1, 7, 2, 1, ['kcg', '', 'mpx']),121editOp(2, 2, 2, 2, ['', 'aw', '']),122editOp(2, 2, 2, 2, ['vqr', 'mo']),123editOp(4, 2, 5, 3, ['xyc']),124],125[126'fehpmjkcg',127'',128'mpxq',129'aw',130'vqr',131'moum',132'vmiwxxaiqq',133'dxycqdgorosf',134]135);136});137});138139function getRandomInt(min: number, max: number): number {140return Math.floor(Math.random() * (max - min + 1)) + min;141}142143function getRandomString(minLength: number, maxLength: number): string {144const length = getRandomInt(minLength, maxLength);145let r = '';146for (let i = 0; i < length; i++) {147r += String.fromCharCode(getRandomInt(CharCode.a, CharCode.z));148}149return r;150}151152function generateFile(small: boolean): string {153const lineCount = getRandomInt(1, small ? 3 : 10);154const lines: string[] = [];155for (let i = 0; i < lineCount; i++) {156lines.push(getRandomString(0, small ? 3 : 10));157}158return lines.join('\n');159}160161function generateEdits(content: string): ITestModelEdit[] {162163const result: ITestModelEdit[] = [];164let cnt = getRandomInt(1, 5);165166let maxOffset = content.length;167168while (cnt > 0 && maxOffset > 0) {169170const offset = getRandomInt(0, maxOffset);171const length = getRandomInt(0, maxOffset - offset);172const text = generateFile(true);173174result.push({175offset: offset,176length: length,177text: text178});179180maxOffset = offset;181cnt--;182}183184result.reverse();185186return result;187}188189interface ITestModelEdit {190offset: number;191length: number;192text: string;193}194195class TestModel {196197public initialContent: string;198public resultingContent: string;199public edits: ISingleEditOperation[];200201private static _generateOffsetToPosition(content: string): Position[] {202const result: Position[] = [];203let lineNumber = 1;204let column = 1;205206for (let offset = 0, len = content.length; offset <= len; offset++) {207const ch = content.charAt(offset);208209result[offset] = new Position(lineNumber, column);210211if (ch === '\n') {212lineNumber++;213column = 1;214} else {215column++;216}217}218219return result;220}221222constructor() {223this.initialContent = generateFile(false);224225const edits = generateEdits(this.initialContent);226227const offsetToPosition = TestModel._generateOffsetToPosition(this.initialContent);228this.edits = [];229for (const edit of edits) {230const startPosition = offsetToPosition[edit.offset];231const endPosition = offsetToPosition[edit.offset + edit.length];232this.edits.push({233range: new Range(startPosition.lineNumber, startPosition.column, endPosition.lineNumber, endPosition.column),234text: edit.text235});236}237238this.resultingContent = this.initialContent;239for (let i = edits.length - 1; i >= 0; i--) {240this.resultingContent = (241this.resultingContent.substring(0, edits[i].offset) +242edits[i].text +243this.resultingContent.substring(edits[i].offset + edits[i].length)244);245}246}247248public print(): string {249let r: string[] = [];250r.push('testApplyEditsWithSyncedModels(');251r.push('\t[');252const initialLines = this.initialContent.split('\n');253r = r.concat(initialLines.map((i) => `\t\t'${i}',`));254r.push('\t],');255r.push('\t[');256r = r.concat(this.edits.map((i) => {257const text = `['` + i.text!.split('\n').join(`', '`) + `']`;258return `\t\teditOp(${i.range.startLineNumber}, ${i.range.startColumn}, ${i.range.endLineNumber}, ${i.range.endColumn}, ${text}),`;259}));260r.push('\t],');261r.push('\t[');262const resultLines = this.resultingContent.split('\n');263r = r.concat(resultLines.map((i) => `\t\t'${i}',`));264r.push('\t]');265r.push(');');266267return r.join('\n');268}269}270271if (GENERATE_TESTS) {272let number = 1;273while (true) {274275console.log('------BEGIN NEW TEST: ' + number);276277const testModel = new TestModel();278279// console.log(testModel.print());280281console.log('------END NEW TEST: ' + (number++));282283try {284testApplyEditsWithSyncedModels(285testModel.initialContent.split('\n'),286testModel.edits,287testModel.resultingContent.split('\n')288);289// throw new Error('a');290} catch (err) {291console.log(err);292console.log(testModel.print());293break;294}295296// break;297}298299}300301302