1
- import SelectionInfoBuilder from '../../lib/selection-info-builder ' ;
1
+ import TextEditor from '../../../ lib/adaptors/text-editor ' ;
2
2
import * as assert from 'assert' ;
3
+ import { mockType } from '../../helpers' ;
4
+ import * as vscode from 'vscode' ;
5
+
6
+ suite ( 'TextEditor' , ( ) => {
3
7
4
- suite ( 'SelectionInfoBuilder' , ( ) => {
5
8
test ( 'it extracts text from editor' , ( ) => {
6
- const textInfo = extractTextInfo ( [ 'SELECTED_TEXT' ] ) ;
7
- assert . equal ( textInfo . text , 'SELECTED_TEXT' ) ;
9
+ const editor = createEditorWithTexts ( [ 'SELECTED_TEXT' ] ) ;
10
+ assert . equal ( editor . selectedText , 'SELECTED_TEXT' ) ;
8
11
} ) ;
9
12
10
13
test ( 'it extracts a whole text if no text is currently selected' , ( ) => {
11
- const textInfo = extractTextInfo ( [ '' ] ) ;
12
- assert . equal ( textInfo . text , 'ENTIRE TEXT' ) ;
14
+ const editor = createEditorWithTexts ( [ '' ] ) ;
15
+ assert . equal ( editor . selectedText , 'ENTIRE TEXT' ) ;
13
16
} ) ;
14
17
15
18
test ( 'it extracts texts selected by all cursors and join them with newline character' , ( ) => {
16
- const textInfo = extractTextInfo ( [ 'SELECTED_TEXT_1' , 'SELECTED_TEXT_2' ] ) ;
17
- assert . equal ( textInfo . text , 'SELECTED_TEXT_1\nSELECTED_TEXT_2' ) ;
19
+ const editor = createEditorWithTexts ( [ 'SELECTED_TEXT_1' , 'SELECTED_TEXT_2' ] ) ;
20
+ assert . equal ( editor . selectedText , 'SELECTED_TEXT_1\nSELECTED_TEXT_2' ) ;
18
21
} ) ;
19
22
20
23
test ( 'it ignores cursor that is not selecting text if others are selecting one' , ( ) => {
21
- const textInfo = extractTextInfo ( [
24
+ const editor = createEditorWithTexts ( [
22
25
'' ,
23
26
'SELECTED_TEXT_1' ,
24
27
'' ,
25
28
'SELECTED_TEXT_2'
26
29
] ) ;
27
- assert . equal ( textInfo . text , 'SELECTED_TEXT_1\nSELECTED_TEXT_2' ) ;
30
+ assert . equal ( editor . selectedText , 'SELECTED_TEXT_1\nSELECTED_TEXT_2' ) ;
28
31
} ) ;
29
32
30
33
test ( 'it extracts the whole text if no cursors are selecting text' , ( ) => {
31
- const textInfo = extractTextInfo ( [ '' , '' , '' ] ) ;
32
- assert . equal ( textInfo . text , 'ENTIRE TEXT' ) ;
34
+ const editor = createEditorWithTexts ( [ '' , '' , '' ] ) ;
35
+ assert . equal ( editor . selectedText , 'ENTIRE TEXT' ) ;
33
36
} ) ;
34
37
35
38
test ( 'it extracts selected line ranges from editor' , ( ) => {
36
- const textInfo = extractTextInfo ( [ 'SELECTED_TEXT' ] ) ;
37
- assert . deepEqual ( textInfo . lineRanges , [
39
+ const editor = createEditorWithTexts ( [ 'SELECTED_TEXT' ] ) ;
40
+ assert . deepEqual ( editor . selectedLineRanges , [
38
41
{ start : 'START_LINE_1' , end : 'END_LINE_1' }
39
42
] ) ;
40
43
} ) ;
41
44
42
45
test ( 'it returns an empty list if no text is selected' , ( ) => {
43
- const textInfo = extractTextInfo ( [ '' ] ) ;
44
- assert . deepEqual ( textInfo . lineRanges , [ ] ) ;
46
+ const editor = createEditorWithTexts ( [ '' ] ) ;
47
+ assert . deepEqual ( editor . selectedLineRanges , [ ] ) ;
45
48
} ) ;
46
49
47
50
test ( 'it extracts all the line ranges of text selections' , ( ) => {
48
- const textInfo = extractTextInfo ( [ 'SELECTED_TEXT_1' , 'SELECTED_TEXT_2' ] ) ;
49
- assert . deepEqual ( textInfo . lineRanges , [
51
+ const editor = createEditorWithTexts ( [ 'SELECTED_TEXT_1' , 'SELECTED_TEXT_2' ] ) ;
52
+ assert . deepEqual ( editor . selectedLineRanges , [
50
53
{ start : 'START_LINE_1' , end : 'END_LINE_1' } ,
51
54
{ start : 'START_LINE_2' , end : 'END_LINE_2' }
52
55
] ) ;
53
56
} ) ;
54
57
55
58
test ( 'it skips all cursors that are not selecting any text' , ( ) => {
56
- const textInfo = extractTextInfo ( [ 'SELECTED_TEXT_1' , '' , 'SELECTED_TEXT_3' ] ) ;
57
- assert . deepEqual ( textInfo . lineRanges , [
59
+ const editor = createEditorWithTexts ( [ 'SELECTED_TEXT_1' , '' , 'SELECTED_TEXT_3' ] ) ;
60
+ assert . deepEqual ( editor . selectedLineRanges , [
58
61
{ start : 'START_LINE_1' , end : 'END_LINE_1' } ,
59
62
{ start : 'START_LINE_3' , end : 'END_LINE_3' }
60
63
] ) ;
@@ -65,8 +68,8 @@ suite('SelectionInfoBuilder', () => {
65
68
{ start : { line : 5 } , end : { line : 6 } , text : 'A' } ,
66
69
{ start : { line : 1 } , end : { line : 2 } , text : 'B' }
67
70
] ;
68
- const textInfo = extractTextInfoFromSelections ( selections ) ;
69
- assert . deepEqual ( textInfo . text , 'B\nA' ) ;
71
+ const editor = createEditorWithSelections ( selections ) ;
72
+ assert . deepEqual ( editor . selectedText , 'B\nA' ) ;
70
73
} ) ;
71
74
72
75
test ( 'it sorts the selections by ascending order of column number if in the same line' , ( ) => {
@@ -87,34 +90,33 @@ suite('SelectionInfoBuilder', () => {
87
90
text : 'C'
88
91
}
89
92
] ;
90
- const textInfo = extractTextInfoFromSelections ( selections ) ;
91
- assert . deepEqual ( textInfo . text , 'C\nB\nA' ) ;
93
+ const editor = createEditorWithSelections ( selections ) ;
94
+ assert . deepEqual ( editor . selectedText , 'C\nB\nA' ) ;
92
95
} ) ;
93
96
94
97
test ( 'it extracts a file name from editor' , ( ) => {
95
- const textInfo = extractTextInfo ( [ 'SELECTED_TEXT' ] ) ;
96
- assert . equal ( textInfo . fileName , 'FILENAME' ) ;
98
+ const editor = createEditorWithTexts ( [ 'SELECTED_TEXT' ] ) ;
99
+ assert . equal ( editor . fileName , 'FILENAME' ) ;
97
100
} ) ;
98
101
99
- function extractTextInfo ( selectedTexts : string [ ] ) {
102
+ function createEditorWithTexts ( selectedTexts : string [ ] ) {
100
103
const selections = selectedTexts . map ( ( text , i ) => ( {
101
104
text,
102
105
start : { line : `START_LINE_${ i + 1 } ` } ,
103
106
end : { line : `END_LINE_${ i + 1 } ` }
104
107
} ) ) ;
105
- return extractTextInfoFromSelections ( selections ) ;
108
+ return createEditorWithSelections ( selections ) ;
106
109
}
107
110
108
- function extractTextInfoFromSelections ( selections : any [ ] ) {
109
- const selectionInfoBuilder = new SelectionInfoBuilder ( ) ;
111
+ function createEditorWithSelections ( selections : any [ ] ) {
110
112
const selectionWithIsEmptyFlag = selections . map ( s =>
111
113
Object . assign ( { } , s , { isEmpty : ! s . text } )
112
114
) ;
113
- return selectionInfoBuilder . extract ( fakeEditor ( selectionWithIsEmptyFlag ) as any ) ;
115
+ return new TextEditor ( fakeEditor ( selectionWithIsEmptyFlag ) as any ) ;
114
116
}
115
117
116
118
function fakeEditor ( selections : any [ ] ) {
117
- return {
119
+ return mockType < vscode . TextEditor > ( {
118
120
selections,
119
121
selection : selections [ 0 ] ,
120
122
document : {
@@ -124,6 +126,6 @@ suite('SelectionInfoBuilder', () => {
124
126
return selection ? selection . text : this . _entireText ;
125
127
}
126
128
}
127
- } ;
129
+ } ) ;
128
130
}
129
131
} ) ;
0 commit comments