@@ -3,86 +3,107 @@ import * as path from 'path';
3
3
import * as td from 'testdouble' ;
4
4
import * as vscode from 'vscode' ;
5
5
import { 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' ;
7
7
import { ExtensionConfig } from '../../../types/config' ;
8
8
9
9
const 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 )
12
12
) ;
13
13
14
14
interface TestSelection {
15
15
selection : Selection ;
16
- content : string ;
16
+ expectedResult : string ;
17
17
}
18
18
19
19
describe ( '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}' ,
22
22
selection : new Selection ( 2 , 4 , 4 , 5 )
23
23
} ;
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}' ,
26
26
selection : new Selection ( 7 , 2 , 9 , 3 )
27
27
} ;
28
- const testSelection3 : TestSelection = {
29
- content : '}\n\ndoSomethingElse() {' ,
28
+ const javaScriptTestSelection3 : TestSelection = {
29
+ expectedResult : '}\n\ndoSomethingElse() {' ,
30
30
selection : new Selection ( 5 , 0 , 7 , 21 )
31
31
} ;
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 ;
33
38
34
39
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' ) ) ;
36
42
} ) ;
37
43
38
44
context ( 'generateSnippet' , ( ) => {
39
45
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 ] ) ) ;
41
47
} ) ;
42
48
43
49
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 ] )
46
52
) ;
47
53
} ) ;
48
54
49
55
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
54
60
] )
55
61
) ;
56
62
} ) ;
57
63
} ) ;
58
64
59
65
context ( 'generateCopyableText' , ( ) => {
60
66
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 ) ;
62
71
} ) ;
63
72
64
73
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
67
78
) ;
68
79
} ) ;
69
80
70
81
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
73
94
) ;
74
95
} ) ;
75
96
} ) ;
76
97
77
98
context ( 'wrapTextInMarkdownCodeBlock' , ( ) => {
78
99
it ( 'returns the text wrapped in a Markdown code block' , ( ) => {
79
100
const codeSnippet = 'console.log("Yo");' ;
80
- assert . equal ( wrapTextInMarkdownCodeBlock ( document , codeSnippet ) , '```\n' + codeSnippet + '\n```' ) ;
101
+ assert . equal ( wrapTextInMarkdownCodeBlock ( document1 , codeSnippet ) , '```\n' + codeSnippet + '\n```' ) ;
81
102
} ) ;
82
103
83
104
it ( 'returns the wrapped text with a language identifier' , ( ) => {
84
105
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```' ) ;
86
107
} ) ;
87
108
} ) ;
88
109
@@ -106,6 +127,22 @@ describe('Text Helpers', () => {
106
127
} ) ;
107
128
} ) ;
108
129
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
+
109
146
context ( 'isMarkdownCodeBlockFlavor' , ( ) => {
110
147
it ( 'returns true if value is "never"' , ( ) => {
111
148
assert . equal ( isMarkdownCodeBlockFlavor ( 'never' ) , true ) ;
0 commit comments