@@ -3,86 +3,107 @@ import * as path from 'path';
33import * as td from 'testdouble' ;
44import * as vscode from 'vscode' ;
55import { Position , Selection , TextDocument } from 'vscode' ;
6- import { generateCopyableText , generateSnippet , includeLanguageIdentifier , isMarkdownCodeBlockFlavor , wrapTextInMarkdownCodeBlock } from '../../../lib/textHelpers' ;
6+ import { generateCopyableText , generateSnippet , includeLanguageIdentifier , isMarkdownCodeBlockFlavor , replaceLeadingTabsWithSpaces , wrapTextInMarkdownCodeBlock } from '../../../lib/textHelpers' ;
77import { ExtensionConfig } from '../../../types/config' ;
88
99const fixturesPath = '/../../../../src/test/fixtures/' ;
10- const uri = vscode . Uri . file (
11- path . join ( __dirname + fixturesPath + 'javascript-example.js' )
10+ const uri = ( fileName : string ) => vscode . Uri . file (
11+ path . join ( __dirname + fixturesPath + fileName )
1212) ;
1313
1414interface TestSelection {
1515 selection : Selection ;
16- content : string ;
16+ expectedResult : string ;
1717}
1818
1919describe ( 'Text Helpers' , ( ) => {
20- const testSelection1 : TestSelection = {
21- content : 'if (aValue) {\n console.log(`Doing something with ${aValue}!`);\n}' ,
20+ const javaScriptTestSelection1 : TestSelection = {
21+ expectedResult : 'if (aValue) {\n console.log(`Doing something with ${aValue}!`);\n}' ,
2222 selection : new Selection ( 2 , 4 , 4 , 5 )
2323 } ;
24- const testSelection2 : TestSelection = {
25- content : 'doSomethingElse() {\n throw new Error(\'Nope!\');\n}' ,
24+ const javaScriptTestSelection2 : TestSelection = {
25+ expectedResult : 'doSomethingElse() {\n throw new Error(\'Nope!\');\n}' ,
2626 selection : new Selection ( 7 , 2 , 9 , 3 )
2727 } ;
28- const testSelection3 : TestSelection = {
29- content : '}\n\ndoSomethingElse() {' ,
28+ const javaScriptTestSelection3 : TestSelection = {
29+ expectedResult : '}\n\ndoSomethingElse() {' ,
3030 selection : new Selection ( 5 , 0 , 7 , 21 )
3131 } ;
32- let document : TextDocument ;
32+ const pythonTestSelection1 : TestSelection = {
33+ expectedResult : 'def fizzBuzz\n logging.info(" FizzBuzz")' ,
34+ selection : new Selection ( 2 , 1 , 3 , 27 )
35+ } ;
36+ let document1 : TextDocument ;
37+ let document2 : TextDocument ;
3338
3439 before ( async ( ) => {
35- document = await vscode . workspace . openTextDocument ( uri ) ;
40+ document1 = await vscode . workspace . openTextDocument ( uri ( 'javascript-example.js' ) ) ;
41+ document2 = await vscode . workspace . openTextDocument ( uri ( 'tabs-python-example.py' ) ) ;
3642 } ) ;
3743
3844 context ( 'generateSnippet' , ( ) => {
3945 it ( 'generates the correct snippet for a single selection' , async ( ) => {
40- assert . deepEqual ( testSelection1 . content , await generateSnippet ( document , [ testSelection1 . selection ] ) ) ;
46+ assert . deepEqual ( javaScriptTestSelection1 . expectedResult , await generateSnippet ( document1 , [ javaScriptTestSelection1 . selection ] ) ) ;
4147 } ) ;
4248
4349 it ( 'generates the correct snippet for multiple selections' , async ( ) => {
44- assert . deepEqual ( testSelection1 . content + '\n' + testSelection2 . content ,
45- await generateSnippet ( document , [ testSelection1 . selection , testSelection2 . selection ] )
50+ assert . deepEqual ( javaScriptTestSelection1 . expectedResult + '\n' + javaScriptTestSelection2 . expectedResult ,
51+ await generateSnippet ( document1 , [ javaScriptTestSelection1 . selection , javaScriptTestSelection2 . selection ] )
4652 ) ;
4753 } ) ;
4854
4955 it ( 'generates the correct snippet for multiple selections where one ends on the beginning of a newline' , async ( ) => {
50- assert . deepEqual ( testSelection1 . content + '\n' + testSelection2 . content ,
51- await generateSnippet ( document , [
52- new Selection ( testSelection1 . selection . start , new Position ( 5 , 0 ) ) ,
53- testSelection2 . selection
56+ assert . deepEqual ( javaScriptTestSelection1 . expectedResult + '\n' + javaScriptTestSelection2 . expectedResult ,
57+ await generateSnippet ( document1 , [
58+ new Selection ( javaScriptTestSelection1 . selection . start , new Position ( 5 , 0 ) ) ,
59+ javaScriptTestSelection2 . selection
5460 ] )
5561 ) ;
5662 } ) ;
5763 } ) ;
5864
5965 context ( 'generateCopyableText' , ( ) => {
6066 it ( 'generates the correct text' , ( ) => {
61- assert . deepEqual ( testSelection1 . content , generateCopyableText ( document , testSelection1 . selection ) ) ;
67+ const config : unknown = td . object ( { convertTabsToSpaces : { enabled : false , tabSize : 2 } } ) ;
68+
69+ assert . deepEqual ( generateCopyableText ( document1 , javaScriptTestSelection1 . selection , config as ExtensionConfig ) ,
70+ javaScriptTestSelection1 . expectedResult ) ;
6271 } ) ;
6372
6473 it ( 'generates the correct text when the cursor is on a newline' , ( ) => {
65- assert . deepEqual ( testSelection1 . content ,
66- generateCopyableText ( document , new Selection ( testSelection1 . selection . start , new Position ( 5 , 0 ) ) )
74+ const config : unknown = td . object ( { convertTabsToSpaces : { enabled : false , tabSize : 2 } } ) ;
75+
76+ assert . deepEqual ( generateCopyableText ( document1 , new Selection ( javaScriptTestSelection1 . selection . start , new Position ( 5 , 0 ) ) , config as ExtensionConfig ) ,
77+ javaScriptTestSelection1 . expectedResult
6778 ) ;
6879 } ) ;
6980
7081 it ( 'generates the correct text when selection contains empty line' , ( ) => {
71- assert . deepEqual ( testSelection3 . content ,
72- generateCopyableText ( document , testSelection3 . selection )
82+ const config : unknown = td . object ( { convertTabsToSpaces : { enabled : false , tabSize : 2 } } ) ;
83+
84+ assert . deepEqual ( generateCopyableText ( document1 , javaScriptTestSelection3 . selection , config as ExtensionConfig ) ,
85+ javaScriptTestSelection3 . expectedResult
86+ ) ;
87+ } ) ;
88+
89+ it ( 'generates the correct text when selection contains tabs and replacement is enabled' , ( ) => {
90+ const config : unknown = td . object ( { convertTabsToSpaces : { enabled : true , tabSize : 2 } } ) ;
91+
92+ assert . deepEqual ( generateCopyableText ( document2 , pythonTestSelection1 . selection , config as ExtensionConfig ) ,
93+ pythonTestSelection1 . expectedResult
7394 ) ;
7495 } ) ;
7596 } ) ;
7697
7798 context ( 'wrapTextInMarkdownCodeBlock' , ( ) => {
7899 it ( 'returns the text wrapped in a Markdown code block' , ( ) => {
79100 const codeSnippet = 'console.log("Yo");' ;
80- assert . equal ( wrapTextInMarkdownCodeBlock ( document , codeSnippet ) , '```\n' + codeSnippet + '\n```' ) ;
101+ assert . equal ( wrapTextInMarkdownCodeBlock ( document1 , codeSnippet ) , '```\n' + codeSnippet + '\n```' ) ;
81102 } ) ;
82103
83104 it ( 'returns the wrapped text with a language identifier' , ( ) => {
84105 const codeSnippet = 'console.log("Yo");' ;
85- assert . equal ( wrapTextInMarkdownCodeBlock ( document , codeSnippet , true ) , '```javascript\n' + codeSnippet + '\n```' ) ;
106+ assert . equal ( wrapTextInMarkdownCodeBlock ( document1 , codeSnippet , true ) , '```javascript\n' + codeSnippet + '\n```' ) ;
86107 } ) ;
87108 } ) ;
88109
@@ -106,6 +127,22 @@ describe('Text Helpers', () => {
106127 } ) ;
107128 } ) ;
108129
130+ context ( 'replaceLeadingTabsWithSpaces' , ( ) => {
131+ it ( 'returns the correct text with the default tabSize' , ( ) => {
132+ assert . equal ( replaceLeadingTabsWithSpaces ( ' ' ) , ' ' ) ;
133+ } ) ;
134+
135+ it ( 'returns the correct text with the custom tabSize' , ( ) => {
136+ assert . equal ( replaceLeadingTabsWithSpaces ( ' ' , 3 ) , ' ' ) ;
137+ } ) ;
138+
139+ it ( 'correctly replaces in a multiline string but does not replace tabs in the middle of the string' , ( ) => {
140+ assert . equal ( replaceLeadingTabsWithSpaces ( ` console.log(" Hello")
141+ console.log("World")` ) , ` console.log(" Hello")
142+ console.log("World")` ) ;
143+ } ) ;
144+ } ) ;
145+
109146 context ( 'isMarkdownCodeBlockFlavor' , ( ) => {
110147 it ( 'returns true if value is "never"' , ( ) => {
111148 assert . equal ( isMarkdownCodeBlockFlavor ( 'never' ) , true ) ;
0 commit comments