Path: blob/main/extensions/emmet/src/test/toggleComment.test.ts
4774 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 'mocha';6import * as assert from 'assert';7import { Selection } from 'vscode';8import { withRandomFileEditor, closeAllEditors } from './testUtils';9import { toggleComment as toggleCommentImpl } from '../toggleComment';1011function toggleComment(): Thenable<boolean> {12const result = toggleCommentImpl();13assert.ok(result);14return result!;15}1617suite('Tests for Toggle Comment action from Emmet (HTML)', () => {18teardown(closeAllEditors);1920const contents = `21<div class="hello">22<ul>23<li><span>Hello</span></li>24<li><span>There</span></li>25<div><li><span>Bye</span></li></div>26</ul>27<ul>28<!-- <li>Previously Commented Node</li> -->29<li>Another Node</li>30</ul>31<span/>32<style>33.boo {34margin: 10px;35padding: 20px;36}37.hoo {38margin: 10px;39padding: 20px;40}41</style>42</div>43`;4445test('toggle comment with multiple cursors, but no selection (HTML)', () => {46const expectedContents = `47<div class="hello">48<ul>49<li><!-- <span>Hello</span> --></li>50<!-- <li><span>There</span></li> -->51<!-- <div><li><span>Bye</span></li></div> -->52</ul>53<!-- <ul>54<li>Previously Commented Node</li>55<li>Another Node</li>56</ul> -->57<span/>58<style>59.boo {60/* margin: 10px; */61padding: 20px;62}63/* .hoo {64margin: 10px;65padding: 20px;66} */67</style>68</div>69`;70return withRandomFileEditor(contents, 'html', (editor, doc) => {71editor.selections = [72new Selection(3, 17, 3, 17), // cursor inside the inner span element73new Selection(4, 5, 4, 5), // cursor inside opening tag74new Selection(5, 35, 5, 35), // cursor inside closing tag75new Selection(7, 3, 7, 3), // cursor inside open tag of <ul> one of whose children is already commented76new Selection(14, 8, 14, 8), // cursor inside the css property inside the style tag77new Selection(18, 3, 18, 3) // cursor inside the css rule inside the style tag78];7980return toggleComment().then(() => {81assert.strictEqual(doc.getText(), expectedContents);82return Promise.resolve();83});84});85});8687test('toggle comment with multiple cursors and whole node selected (HTML)', () => {88const expectedContents = `89<div class="hello">90<ul>91<li><!-- <span>Hello</span> --></li>92<!-- <li><span>There</span></li> -->93<div><li><span>Bye</span></li></div>94</ul>95<!-- <ul>96<li>Previously Commented Node</li>97<li>Another Node</li>98</ul> -->99<span/>100<style>101.boo {102/* margin: 10px; */103padding: 20px;104}105/* .hoo {106margin: 10px;107padding: 20px;108} */109</style>110</div>111`;112return withRandomFileEditor(contents, 'html', (editor, doc) => {113editor.selections = [114new Selection(3, 7, 3, 25), // <span>Hello</span><115new Selection(4, 3, 4, 30), // <li><span>There</span></li>116new Selection(7, 2, 10, 7), // The <ul> one of whose children is already commented117new Selection(14, 4, 14, 17), // css property inside the style tag118new Selection(17, 3, 20, 4) // the css rule inside the style tag119];120121return toggleComment().then(() => {122assert.strictEqual(doc.getText(), expectedContents);123return Promise.resolve();124});125});126});127128test('toggle comment when multiple nodes are completely under single selection (HTML)', () => {129const expectedContents = `130<div class="hello">131<ul>132<!-- <li><span>Hello</span></li>133<li><span>There</span></li> -->134<div><li><span>Bye</span></li></div>135</ul>136<ul>137<!-- <li>Previously Commented Node</li> -->138<li>Another Node</li>139</ul>140<span/>141<style>142.boo {143/* margin: 10px;144padding: 20px; */145}146.hoo {147margin: 10px;148padding: 20px;149}150</style>151</div>152`;153return withRandomFileEditor(contents, 'html', (editor, doc) => {154editor.selections = [155new Selection(3, 4, 4, 30),156new Selection(14, 4, 15, 18) // 2 css properties inside the style tag157];158159return toggleComment().then(() => {160assert.strictEqual(doc.getText(), expectedContents);161return Promise.resolve();162});163});164});165166test('toggle comment when multiple nodes are partially under single selection (HTML)', () => {167const expectedContents = `168<div class="hello">169<ul>170<!-- <li><span>Hello</span></li>171<li><span>There</span></li> -->172<div><li><span>Bye</span></li></div>173</ul>174<!-- <ul>175<li>Previously Commented Node</li>176<li>Another Node</li>177</ul> -->178<span/>179<style>180.boo {181margin: 10px;182padding: 20px;183}184.hoo {185margin: 10px;186padding: 20px;187}188</style>189</div>190`;191return withRandomFileEditor(contents, 'html', (editor, doc) => {192editor.selections = [193new Selection(3, 24, 4, 20),194new Selection(7, 2, 9, 10) // The <ul> one of whose children is already commented195];196197return toggleComment().then(() => {198assert.strictEqual(doc.getText(), expectedContents);199return Promise.resolve();200});201});202});203204test('toggle comment with multiple cursors selecting parent and child nodes', () => {205const expectedContents = `206<div class="hello">207<ul>208<li><!-- <span>Hello</span> --></li>209<!-- <li><span>There</span></li> -->210<div><li><span>Bye</span></li></div>211</ul>212<!-- <ul>213<li>Previously Commented Node</li>214<li>Another Node</li>215</ul> -->216<span/>217<!-- <style>218.boo {219margin: 10px;220padding: 20px;221}222.hoo {223margin: 10px;224padding: 20px;225}226</style> -->227</div>228`;229return withRandomFileEditor(contents, 'html', (editor, doc) => {230editor.selections = [231new Selection(3, 17, 3, 17), // cursor inside the inner span element232new Selection(4, 5, 4, 5), // two cursors: one inside opening tag233new Selection(4, 17, 4, 17), // and the second inside the inner span element234new Selection(7, 3, 7, 3), // two cursors: one inside open tag of <ul> one of whose children is already commented235new Selection(9, 10, 9, 10), // and the second inside inner li element, whose parent is selected236new Selection(12, 3, 12, 3), // four nested cursors: one inside the style open tag237new Selection(14, 8, 14, 8), // the second inside the css property inside the style tag238new Selection(18, 3, 18, 3), // the third inside the css rule inside the style tag239new Selection(19, 8, 19, 8) // and the fourth inside the css property inside the style tag240];241242return toggleComment().then(() => {243assert.strictEqual(doc.getText(), expectedContents);244245return Promise.resolve();246});247});248});249250test('toggle comment within script template', () => {251const templateContents = `252<script type="text/template">253<li><span>Hello</span></li>254<li><!-- <span>There</span> --></li>255<div><li><span>Bye</span></li></div>256<span/>257</script>258`;259const expectedContents = `260<script type="text/template">261<!-- <li><span>Hello</span></li> -->262<li><span>There</span></li>263<div><li><!-- <span>Bye</span> --></li></div>264<span/>265</script>266`;267return withRandomFileEditor(templateContents, 'html', (editor, doc) => {268editor.selections = [269new Selection(2, 2, 2, 28), // select entire li element270new Selection(3, 17, 3, 17), // cursor inside the commented span271new Selection(4, 18, 4, 18), // cursor inside the noncommented span272];273return toggleComment().then(() => {274assert.strictEqual(doc.getText(), expectedContents);275return Promise.resolve();276});277});278});279});280281suite('Tests for Toggle Comment action from Emmet (CSS)', () => {282teardown(closeAllEditors);283284const contents = `285.one {286margin: 10px;287padding: 10px;288}289.two {290height: 42px;291display: none;292}293.three {294width: 42px;295}`;296297test('toggle comment with multiple cursors, but no selection (CSS)', () => {298const expectedContents = `299.one {300/* margin: 10px; */301padding: 10px;302}303/* .two {304height: 42px;305display: none;306} */307.three {308width: 42px;309}`;310return withRandomFileEditor(contents, 'css', (editor, doc) => {311editor.selections = [312new Selection(2, 5, 2, 5), // cursor inside a property313new Selection(5, 4, 5, 4), // cursor inside selector314];315316return toggleComment().then(() => {317assert.strictEqual(doc.getText(), expectedContents);318return toggleComment().then(() => {319assert.strictEqual(doc.getText(), contents);320return Promise.resolve();321});322});323});324});325326test('toggle comment with multiple cursors and whole node selected (CSS)', () => {327const expectedContents = `328.one {329/* margin: 10px; */330/* padding: 10px; */331}332/* .two {333height: 42px;334display: none;335} */336.three {337width: 42px;338}`;339return withRandomFileEditor(contents, 'css', (editor, doc) => {340editor.selections = [341new Selection(2, 2, 2, 15), // A property completely selected342new Selection(3, 0, 3, 16), // A property completely selected along with whitespace343new Selection(5, 1, 8, 2), // A rule completely selected344];345346return toggleComment().then(() => {347assert.strictEqual(doc.getText(), expectedContents);348//return toggleComment().then(() => {349//assert.strictEqual(doc.getText(), contents);350return Promise.resolve();351//});352});353});354});355356357358test('toggle comment when multiple nodes of same parent are completely under single selection (CSS)', () => {359const expectedContents = `360.one {361/* margin: 10px;362padding: 10px; */363}364/* .two {365height: 42px;366display: none;367}368.three {369width: 42px;370} */`;371return withRandomFileEditor(contents, 'css', (editor, doc) => {372editor.selections = [373new Selection(2, 0, 3, 16), // 2 properties completely under a single selection along with whitespace374new Selection(5, 1, 11, 2), // 2 rules completely under a single selection375];376377return toggleComment().then(() => {378assert.strictEqual(doc.getText(), expectedContents);379return toggleComment().then(() => {380assert.strictEqual(doc.getText(), contents);381return Promise.resolve();382});383});384});385});386387test('toggle comment when start and end of selection is inside properties of separate rules (CSS)', () => {388const expectedContents = `389.one {390margin: 10px;391/* padding: 10px;392}393.two {394height: 42px; */395display: none;396}397.three {398width: 42px;399}`;400return withRandomFileEditor(contents, 'css', (editor, doc) => {401editor.selections = [402new Selection(3, 7, 6, 6)403];404405return toggleComment().then(() => {406assert.strictEqual(doc.getText(), expectedContents);407return toggleComment().then(() => {408assert.strictEqual(doc.getText(), contents);409return Promise.resolve();410});411});412});413});414415test('toggle comment when selection spans properties of separate rules, with start in whitespace and end inside the property (CSS)', () => {416const expectedContents = `417.one {418margin: 10px;419/* padding: 10px;420}421.two {422height: 42px; */423display: none;424}425.three {426width: 42px;427}`;428return withRandomFileEditor(contents, 'css', (editor, doc) => {429editor.selections = [430new Selection(3, 0, 6, 6)431];432433return toggleComment().then(() => {434assert.strictEqual(doc.getText(), expectedContents);435return toggleComment().then(() => {436assert.strictEqual(doc.getText(), contents);437return Promise.resolve();438});439});440});441});442443test('toggle comment when selection spans properties of separate rules, with end in whitespace and start inside the property (CSS)', () => {444const expectedContents = `445.one {446margin: 10px;447/* padding: 10px;448}449.two {450height: 42px; */451display: none;452}453.three {454width: 42px;455}`;456return withRandomFileEditor(contents, 'css', (editor, doc) => {457editor.selections = [458new Selection(3, 7, 7, 0)459];460461return toggleComment().then(() => {462assert.strictEqual(doc.getText(), expectedContents);463return toggleComment().then(() => {464assert.strictEqual(doc.getText(), contents);465return Promise.resolve();466});467});468});469});470471test('toggle comment when selection spans properties of separate rules, with both start and end in whitespace (CSS)', () => {472const expectedContents = `473.one {474margin: 10px;475/* padding: 10px;476}477.two {478height: 42px; */479display: none;480}481.three {482width: 42px;483}`;484return withRandomFileEditor(contents, 'css', (editor, doc) => {485editor.selections = [486new Selection(3, 0, 7, 0)487];488489return toggleComment().then(() => {490assert.strictEqual(doc.getText(), expectedContents);491return toggleComment().then(() => {492assert.strictEqual(doc.getText(), contents);493return Promise.resolve();494});495});496});497});498499test('toggle comment when multiple nodes of same parent are partially under single selection (CSS)', () => {500const expectedContents = `501.one {502/* margin: 10px;503padding: 10px; */504}505/* .two {506height: 42px;507display: none;508}509.three {510width: 42px;511*/ }`;512return withRandomFileEditor(contents, 'css', (editor, doc) => {513editor.selections = [514new Selection(2, 7, 3, 10), // 2 properties partially under a single selection515new Selection(5, 2, 11, 0), // 2 rules partially under a single selection516];517518return toggleComment().then(() => {519assert.strictEqual(doc.getText(), expectedContents);520return toggleComment().then(() => {521assert.strictEqual(doc.getText(), contents);522return Promise.resolve();523});524});525});526});527528529});530531532suite('Tests for Toggle Comment action from Emmet in nested css (SCSS)', () => {533teardown(closeAllEditors);534535const contents = `536.one {537height: 42px;538539.two {540width: 42px;541}542543.three {544padding: 10px;545}546}`;547548test('toggle comment with multiple cursors selecting nested nodes (SCSS)', () => {549const expectedContents = `550.one {551/* height: 42px; */552553/* .two {554width: 42px;555} */556557.three {558/* padding: 10px; */559}560}`;561return withRandomFileEditor(contents, 'css', (editor, doc) => {562editor.selections = [563new Selection(2, 5, 2, 5), // cursor inside a property564new Selection(4, 4, 4, 4), // two cursors: one inside a nested rule565new Selection(5, 5, 5, 5), // and the second one inside a nested property566new Selection(9, 5, 9, 5) // cursor inside a property inside a nested rule567];568569return toggleComment().then(() => {570assert.strictEqual(doc.getText(), expectedContents);571return toggleComment().then(() => {572assert.strictEqual(doc.getText(), contents);573return Promise.resolve();574});575});576});577});578test('toggle comment with multiple cursors selecting several nested nodes (SCSS)', () => {579const expectedContents = `580/* .one {581height: 42px;582583.two {584width: 42px;585}586587.three {588padding: 10px;589}590} */`;591return withRandomFileEditor(contents, 'css', (editor, doc) => {592editor.selections = [593new Selection(1, 3, 1, 3), // cursor in the outside rule. And several cursors inside:594new Selection(2, 5, 2, 5), // cursor inside a property595new Selection(4, 4, 4, 4), // two cursors: one inside a nested rule596new Selection(5, 5, 5, 5), // and the second one inside a nested property597new Selection(9, 5, 9, 5) // cursor inside a property inside a nested rule598];599600return toggleComment().then(() => {601assert.strictEqual(doc.getText(), expectedContents);602return toggleComment().then(() => {603assert.strictEqual(doc.getText(), contents);604return Promise.resolve();605});606});607});608});609610test('toggle comment with multiple cursors, but no selection (SCSS)', () => {611const expectedContents = `612.one {613/* height: 42px; */614615/* .two {616width: 42px;617} */618619.three {620/* padding: 10px; */621}622}`;623return withRandomFileEditor(contents, 'css', (editor, doc) => {624editor.selections = [625new Selection(2, 5, 2, 5), // cursor inside a property626new Selection(4, 4, 4, 4), // cursor inside a nested rule627new Selection(9, 5, 9, 5) // cursor inside a property inside a nested rule628];629630return toggleComment().then(() => {631assert.strictEqual(doc.getText(), expectedContents);632//return toggleComment().then(() => {633// assert.strictEqual(doc.getText(), contents);634return Promise.resolve();635//});636});637});638});639640test('toggle comment with multiple cursors and whole node selected (CSS)', () => {641const expectedContents = `642.one {643/* height: 42px; */644645/* .two {646width: 42px;647} */648649.three {650/* padding: 10px; */651}652}`;653return withRandomFileEditor(contents, 'css', (editor, doc) => {654editor.selections = [655new Selection(2, 2, 2, 15), // A property completely selected656new Selection(4, 2, 6, 3), // A rule completely selected657new Selection(9, 3, 9, 17) // A property inside a nested rule completely selected658];659660return toggleComment().then(() => {661assert.strictEqual(doc.getText(), expectedContents);662return toggleComment().then(() => {663assert.strictEqual(doc.getText(), contents);664return Promise.resolve();665});666});667});668});669670671672test('toggle comment when multiple nodes are completely under single selection (CSS)', () => {673const expectedContents = `674.one {675/* height: 42px;676677.two {678width: 42px;679} */680681.three {682padding: 10px;683}684}`;685return withRandomFileEditor(contents, 'css', (editor, doc) => {686editor.selections = [687new Selection(2, 2, 6, 3), // A properties and a nested rule completely under a single selection688];689690return toggleComment().then(() => {691assert.strictEqual(doc.getText(), expectedContents);692return toggleComment().then(() => {693assert.strictEqual(doc.getText(), contents);694return Promise.resolve();695});696});697});698});699700test('toggle comment when multiple nodes are partially under single selection (CSS)', () => {701const expectedContents = `702.one {703/* height: 42px;704705.two {706width: 42px;707*/ }708709.three {710padding: 10px;711}712}`;713return withRandomFileEditor(contents, 'css', (editor, doc) => {714editor.selections = [715new Selection(2, 6, 6, 1), // A properties and a nested rule partially under a single selection716];717718return toggleComment().then(() => {719assert.strictEqual(doc.getText(), expectedContents);720return toggleComment().then(() => {721assert.strictEqual(doc.getText(), contents);722return Promise.resolve();723});724});725});726});727728test('toggle comment doesn\'t fail when start and end nodes differ HTML', () => {729const contents = `730<div>731<p>Hello</p>732</div>733`;734const expectedContents = `735<!-- <div>736<p>Hello</p>737</div> -->738`;739return withRandomFileEditor(contents, 'html', (editor, doc) => {740editor.selections = [741new Selection(1, 2, 2, 9), // <div> to <p> inclusive742];743744return toggleComment().then(() => {745assert.strictEqual(doc.getText(), expectedContents);746return toggleComment().then(() => {747assert.strictEqual(doc.getText(), contents);748return Promise.resolve();749});750});751});752});753});754755756