Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/preferences/test/common/smartSnippetInserter.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { SmartSnippetInserter } from '../../common/smartSnippetInserter.js';
8
import { createTextModel } from '../../../../../editor/test/common/testTextModel.js';
9
import { Position } from '../../../../../editor/common/core/position.js';
10
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
11
12
suite('SmartSnippetInserter', () => {
13
14
function testSmartSnippetInserter(text: string[], runner: (assert: (desiredPos: Position, pos: Position, prepend: string, append: string) => void) => void): void {
15
const model = createTextModel(text.join('\n'));
16
runner((desiredPos, pos, prepend, append) => {
17
const actual = SmartSnippetInserter.insertSnippet(model, desiredPos);
18
const expected = {
19
position: pos,
20
prepend,
21
append
22
};
23
assert.deepStrictEqual(actual, expected);
24
});
25
model.dispose();
26
}
27
28
test('empty text', () => {
29
testSmartSnippetInserter([
30
], (assert) => {
31
assert(new Position(1, 1), new Position(1, 1), '\n[', ']');
32
});
33
34
testSmartSnippetInserter([
35
' '
36
], (assert) => {
37
assert(new Position(1, 1), new Position(1, 2), '\n[', ']');
38
assert(new Position(1, 2), new Position(1, 2), '\n[', ']');
39
});
40
41
testSmartSnippetInserter([
42
'// just some text'
43
], (assert) => {
44
assert(new Position(1, 1), new Position(1, 18), '\n[', ']');
45
assert(new Position(1, 18), new Position(1, 18), '\n[', ']');
46
});
47
48
testSmartSnippetInserter([
49
'// just some text',
50
''
51
], (assert) => {
52
assert(new Position(1, 1), new Position(2, 1), '\n[', ']');
53
assert(new Position(1, 18), new Position(2, 1), '\n[', ']');
54
assert(new Position(2, 1), new Position(2, 1), '\n[', ']');
55
});
56
});
57
58
test('empty array 1', () => {
59
testSmartSnippetInserter([
60
'// just some text',
61
'[]'
62
], (assert) => {
63
assert(new Position(1, 1), new Position(2, 2), '', '');
64
assert(new Position(2, 1), new Position(2, 2), '', '');
65
assert(new Position(2, 2), new Position(2, 2), '', '');
66
assert(new Position(2, 3), new Position(2, 2), '', '');
67
});
68
});
69
70
test('empty array 2', () => {
71
testSmartSnippetInserter([
72
'// just some text',
73
'[',
74
']'
75
], (assert) => {
76
assert(new Position(1, 1), new Position(2, 2), '', '');
77
assert(new Position(2, 1), new Position(2, 2), '', '');
78
assert(new Position(2, 2), new Position(2, 2), '', '');
79
assert(new Position(3, 1), new Position(3, 1), '', '');
80
assert(new Position(3, 2), new Position(3, 1), '', '');
81
});
82
});
83
84
test('empty array 3', () => {
85
testSmartSnippetInserter([
86
'// just some text',
87
'[',
88
'// just some text',
89
']'
90
], (assert) => {
91
assert(new Position(1, 1), new Position(2, 2), '', '');
92
assert(new Position(2, 1), new Position(2, 2), '', '');
93
assert(new Position(2, 2), new Position(2, 2), '', '');
94
assert(new Position(3, 1), new Position(3, 1), '', '');
95
assert(new Position(3, 2), new Position(3, 1), '', '');
96
assert(new Position(4, 1), new Position(4, 1), '', '');
97
assert(new Position(4, 2), new Position(4, 1), '', '');
98
});
99
});
100
101
test('one element array 1', () => {
102
testSmartSnippetInserter([
103
'// just some text',
104
'[',
105
'{}',
106
']'
107
], (assert) => {
108
assert(new Position(1, 1), new Position(2, 2), '', ',');
109
assert(new Position(2, 1), new Position(2, 2), '', ',');
110
assert(new Position(2, 2), new Position(2, 2), '', ',');
111
assert(new Position(3, 1), new Position(3, 1), '', ',');
112
assert(new Position(3, 2), new Position(3, 1), '', ',');
113
assert(new Position(3, 3), new Position(3, 3), ',', '');
114
assert(new Position(4, 1), new Position(4, 1), ',', '');
115
assert(new Position(4, 2), new Position(4, 1), ',', '');
116
});
117
});
118
119
test('two elements array 1', () => {
120
testSmartSnippetInserter([
121
'// just some text',
122
'[',
123
'{},',
124
'{}',
125
']'
126
], (assert) => {
127
assert(new Position(1, 1), new Position(2, 2), '', ',');
128
assert(new Position(2, 1), new Position(2, 2), '', ',');
129
assert(new Position(2, 2), new Position(2, 2), '', ',');
130
assert(new Position(3, 1), new Position(3, 1), '', ',');
131
assert(new Position(3, 2), new Position(3, 1), '', ',');
132
assert(new Position(3, 3), new Position(3, 3), ',', '');
133
assert(new Position(3, 4), new Position(3, 4), '', ',');
134
assert(new Position(4, 1), new Position(4, 1), '', ',');
135
assert(new Position(4, 2), new Position(4, 1), '', ',');
136
assert(new Position(4, 3), new Position(4, 3), ',', '');
137
assert(new Position(5, 1), new Position(5, 1), ',', '');
138
assert(new Position(5, 2), new Position(5, 1), ',', '');
139
});
140
});
141
142
test('two elements array 2', () => {
143
testSmartSnippetInserter([
144
'// just some text',
145
'[',
146
'{},{}',
147
']'
148
], (assert) => {
149
assert(new Position(1, 1), new Position(2, 2), '', ',');
150
assert(new Position(2, 1), new Position(2, 2), '', ',');
151
assert(new Position(2, 2), new Position(2, 2), '', ',');
152
assert(new Position(3, 1), new Position(3, 1), '', ',');
153
assert(new Position(3, 2), new Position(3, 1), '', ',');
154
assert(new Position(3, 3), new Position(3, 3), ',', '');
155
assert(new Position(3, 4), new Position(3, 4), '', ',');
156
assert(new Position(3, 5), new Position(3, 4), '', ',');
157
assert(new Position(3, 6), new Position(3, 6), ',', '');
158
assert(new Position(4, 1), new Position(4, 1), ',', '');
159
assert(new Position(4, 2), new Position(4, 1), ',', '');
160
});
161
});
162
163
ensureNoDisposablesAreLeakedInTestSuite();
164
});
165
166