Path: blob/main/src/vs/editor/contrib/linesOperations/test/browser/copyLinesCommand.test.ts
5257 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 assert from 'assert';6import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';7import { Selection } from '../../../../common/core/selection.js';8import { CopyLinesCommand } from '../../browser/copyLinesCommand.js';9import { DuplicateSelectionAction } from '../../browser/linesOperations.js';10import { withTestCodeEditor } from '../../../../test/browser/testCodeEditor.js';11import { testCommand } from '../../../../test/browser/testCommand.js';1213function testCopyLinesDownCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {14testCommand(lines, null, selection, (accessor, sel) => new CopyLinesCommand(sel, true), expectedLines, expectedSelection);15}1617function testCopyLinesUpCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {18testCommand(lines, null, selection, (accessor, sel) => new CopyLinesCommand(sel, false), expectedLines, expectedSelection);19}2021suite('Editor Contrib - Copy Lines Command', () => {2223ensureNoDisposablesAreLeakedInTestSuite();2425test('copy first line down', function () {26testCopyLinesDownCommand(27[28'first',29'second line',30'third line',31'fourth line',32'fifth'33],34new Selection(1, 3, 1, 1),35[36'first',37'first',38'second line',39'third line',40'fourth line',41'fifth'42],43new Selection(2, 3, 2, 1)44);45});4647test('copy first line up', function () {48testCopyLinesUpCommand(49[50'first',51'second line',52'third line',53'fourth line',54'fifth'55],56new Selection(1, 3, 1, 1),57[58'first',59'first',60'second line',61'third line',62'fourth line',63'fifth'64],65new Selection(1, 3, 1, 1)66);67});6869test('copy last line down', function () {70testCopyLinesDownCommand(71[72'first',73'second line',74'third line',75'fourth line',76'fifth'77],78new Selection(5, 3, 5, 1),79[80'first',81'second line',82'third line',83'fourth line',84'fifth',85'fifth'86],87new Selection(6, 3, 6, 1)88);89});9091test('copy last line up', function () {92testCopyLinesUpCommand(93[94'first',95'second line',96'third line',97'fourth line',98'fifth'99],100new Selection(5, 3, 5, 1),101[102'first',103'second line',104'third line',105'fourth line',106'fifth',107'fifth'108],109new Selection(5, 3, 5, 1)110);111});112113test('issue #1322: copy line up', function () {114testCopyLinesUpCommand(115[116'first',117'second line',118'third line',119'fourth line',120'fifth'121],122new Selection(3, 11, 3, 11),123[124'first',125'second line',126'third line',127'third line',128'fourth line',129'fifth'130],131new Selection(3, 11, 3, 11)132);133});134135test('issue #1322: copy last line up', function () {136testCopyLinesUpCommand(137[138'first',139'second line',140'third line',141'fourth line',142'fifth'143],144new Selection(5, 6, 5, 6),145[146'first',147'second line',148'third line',149'fourth line',150'fifth',151'fifth'152],153new Selection(5, 6, 5, 6)154);155});156157test('copy many lines up', function () {158testCopyLinesUpCommand(159[160'first',161'second line',162'third line',163'fourth line',164'fifth'165],166new Selection(4, 3, 2, 1),167[168'first',169'second line',170'third line',171'fourth line',172'second line',173'third line',174'fourth line',175'fifth'176],177new Selection(4, 3, 2, 1)178);179});180181test('ignore empty selection', function () {182testCopyLinesUpCommand(183[184'first',185'second line',186'third line',187'fourth line',188'fifth'189],190new Selection(2, 1, 1, 1),191[192'first',193'first',194'second line',195'third line',196'fourth line',197'fifth'198],199new Selection(2, 1, 1, 1)200);201});202});203204suite('Editor Contrib - Duplicate Selection', () => {205206ensureNoDisposablesAreLeakedInTestSuite();207208const duplicateSelectionAction = new DuplicateSelectionAction();209210function testDuplicateSelectionAction(lines: string[], selections: Selection[], expectedLines: string[], expectedSelections: Selection[]): void {211withTestCodeEditor(lines.join('\n'), {}, (editor) => {212editor.setSelections(selections);213duplicateSelectionAction.run(null!, editor, {});214assert.deepStrictEqual(editor.getValue(), expectedLines.join('\n'));215assert.deepStrictEqual(editor.getSelections()!.map(s => s.toString()), expectedSelections.map(s => s.toString()));216});217}218219test('empty selection', function () {220testDuplicateSelectionAction(221[222'first',223'second line',224'third line',225'fourth line',226'fifth'227],228[new Selection(2, 2, 2, 2), new Selection(3, 2, 3, 2)],229[230'first',231'second line',232'second line',233'third line',234'third line',235'fourth line',236'fifth'237],238[new Selection(3, 2, 3, 2), new Selection(5, 2, 5, 2)]239);240});241242test('with selection', function () {243testDuplicateSelectionAction(244[245'first',246'second line',247'third line',248'fourth line',249'fifth'250],251[new Selection(2, 1, 2, 4), new Selection(3, 1, 3, 4)],252[253'first',254'secsecond line',255'thithird line',256'fourth line',257'fifth'258],259[new Selection(2, 4, 2, 7), new Selection(3, 4, 3, 7)]260);261});262});263264265