comparison .cms/lib/codemirror/test/sublime_test.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 (function() {
2 "use strict";
3
4 var Pos = CodeMirror.Pos;
5 namespace = "sublime_";
6
7 function stTest(name) {
8 var actions = Array.prototype.slice.call(arguments, 1);
9 testCM(name, function(cm) {
10 for (var i = 0; i < actions.length; i++) {
11 var action = actions[i];
12 if (typeof action == "string" && i == 0)
13 cm.setValue(action);
14 else if (typeof action == "string")
15 cm.execCommand(action);
16 else if (action instanceof Pos)
17 cm.setCursor(action);
18 else
19 action(cm);
20 }
21 });
22 }
23
24 function at(line, ch, msg) {
25 return function(cm) {
26 eq(cm.listSelections().length, 1);
27 eqCursorPos(cm.getCursor("head"), Pos(line, ch), msg);
28 eqCursorPos(cm.getCursor("anchor"), Pos(line, ch), msg);
29 };
30 }
31
32 function val(content, msg) {
33 return function(cm) { eq(cm.getValue(), content, msg); };
34 }
35
36 function argsToRanges(args) {
37 if (args.length % 4) throw new Error("Wrong number of arguments for ranges.");
38 var ranges = [];
39 for (var i = 0; i < args.length; i += 4)
40 ranges.push({anchor: Pos(args[i], args[i + 1]),
41 head: Pos(args[i + 2], args[i + 3])});
42 return ranges;
43 }
44
45 function setSel() {
46 var ranges = argsToRanges(arguments);
47 return function(cm) { cm.setSelections(ranges, 0); };
48 }
49
50 function hasSel() {
51 var ranges = argsToRanges(arguments);
52 return function(cm) {
53 var sels = cm.listSelections();
54 if (sels.length != ranges.length)
55 throw new Failure("Expected " + ranges.length + " selections, but found " + sels.length);
56 for (var i = 0; i < sels.length; i++) {
57 eqCharPos(sels[i].anchor, ranges[i].anchor, "anchor " + i);
58 eqCharPos(sels[i].head, ranges[i].head, "head " + i);
59 }
60 };
61 }
62
63 stTest("bySubword", "the foo_bar DooDahBah \n a FOOBar",
64 "goSubwordLeft", at(0, 0),
65 "goSubwordRight", at(0, 3),
66 "goSubwordRight", at(0, 7),
67 "goSubwordRight", at(0, 11),
68 "goSubwordRight", at(0, 15),
69 "goSubwordRight", at(0, 18),
70 "goSubwordRight", at(0, 21),
71 "goSubwordRight", at(0, 22),
72 "goSubwordRight", at(1, 0),
73 "goSubwordRight", at(1, 2),
74 "goSubwordRight", at(1, 6),
75 "goSubwordRight", at(1, 9),
76 "goSubwordLeft", at(1, 6),
77 "goSubwordLeft", at(1, 3),
78 "goSubwordLeft", at(1, 1),
79 "goSubwordLeft", at(1, 0),
80 "goSubwordLeft", at(0, 22),
81 "goSubwordLeft", at(0, 18),
82 "goSubwordLeft", at(0, 15),
83 "goSubwordLeft", at(0, 12),
84 "goSubwordLeft", at(0, 8),
85 "goSubwordLeft", at(0, 4),
86 "goSubwordLeft", at(0, 0));
87
88 stTest("splitSelectionByLine", "abc\ndef\nghi",
89 setSel(0, 1, 2, 2),
90 "splitSelectionByLine",
91 hasSel(0, 1, 0, 3,
92 1, 0, 1, 3,
93 2, 0, 2, 2));
94
95 stTest("splitSelectionByLineMulti", "abc\ndef\nghi\njkl",
96 setSel(0, 1, 1, 1,
97 1, 2, 3, 2,
98 3, 3, 3, 3),
99 "splitSelectionByLine",
100 hasSel(0, 1, 0, 3,
101 1, 0, 1, 1,
102 1, 2, 1, 3,
103 2, 0, 2, 3,
104 3, 0, 3, 2,
105 3, 3, 3, 3));
106
107 stTest("selectLine", "abc\ndef\nghi",
108 setSel(0, 1, 0, 1,
109 2, 0, 2, 1),
110 "selectLine",
111 hasSel(0, 0, 1, 0,
112 2, 0, 2, 3),
113 setSel(0, 1, 1, 0),
114 "selectLine",
115 hasSel(0, 0, 2, 0));
116
117 stTest("insertLineAfter", "abcde\nfghijkl\nmn",
118 setSel(0, 1, 0, 1,
119 0, 3, 0, 3,
120 1, 2, 1, 2,
121 1, 3, 1, 5), "insertLineAfter",
122 hasSel(1, 0, 1, 0,
123 3, 0, 3, 0), val("abcde\n\nfghijkl\n\nmn"));
124
125 stTest("insertLineBefore", "abcde\nfghijkl\nmn",
126 setSel(0, 1, 0, 1,
127 0, 3, 0, 3,
128 1, 2, 1, 2,
129 1, 3, 1, 5), "insertLineBefore",
130 hasSel(0, 0, 0, 0,
131 2, 0, 2, 0), val("\nabcde\n\nfghijkl\nmn"));
132
133 stTest("skipAndSelectNextOccurrence", "a foo bar\nfoobar foo",
134 setSel(0, 2, 0, 5), "skipAndSelectNextOccurrence", hasSel(1, 0, 1, 3),
135 "skipAndSelectNextOccurrence", hasSel(1, 7, 1, 10),
136 "skipAndSelectNextOccurrence", hasSel(0, 2, 0, 5),
137 Pos(0, 3), "skipAndSelectNextOccurrence", hasSel(0, 2, 0, 5),
138 "skipAndSelectNextOccurrence", hasSel(1, 7, 1, 10),
139 setSel(0, 6, 0, 9), "skipAndSelectNextOccurrence", hasSel(1, 3, 1, 6));
140
141 stTest("selectNextOccurrence", "a foo bar\nfoobar foo",
142 setSel(0, 2, 0, 5),
143 "selectNextOccurrence", hasSel(0, 2, 0, 5,
144 1, 0, 1, 3),
145 "selectNextOccurrence", hasSel(0, 2, 0, 5,
146 1, 0, 1, 3,
147 1, 7, 1, 10),
148 "selectNextOccurrence", hasSel(0, 2, 0, 5,
149 1, 0, 1, 3,
150 1, 7, 1, 10),
151 Pos(0, 3), "selectNextOccurrence", hasSel(0, 2, 0, 5),
152 "selectNextOccurrence", hasSel(0, 2, 0, 5,
153 1, 7, 1, 10),
154 setSel(0, 6, 0, 9),
155 "selectNextOccurrence", hasSel(0, 6, 0, 9,
156 1, 3, 1, 6));
157
158 stTest("selectScope", "foo(a) {\n bar[1, 2];\n}",
159 "selectScope", hasSel(0, 0, 2, 1),
160 Pos(0, 4), "selectScope", hasSel(0, 4, 0, 5),
161 Pos(0, 5), "selectScope", hasSel(0, 4, 0, 5),
162 Pos(0, 6), "selectScope", hasSel(0, 0, 2, 1),
163 Pos(0, 8), "selectScope", hasSel(0, 8, 2, 0),
164 Pos(1, 2), "selectScope", hasSel(0, 8, 2, 0),
165 Pos(1, 6), "selectScope", hasSel(1, 6, 1, 10),
166 Pos(1, 9), "selectScope", hasSel(1, 6, 1, 10),
167 "selectScope", hasSel(0, 8, 2, 0),
168 "selectScope", hasSel(0, 0, 2, 1));
169
170 stTest("goToBracket", "foo(a) {\n bar[1, 2];\n}",
171 Pos(0, 0), "goToBracket", at(0, 0),
172 Pos(0, 4), "goToBracket", at(0, 5), "goToBracket", at(0, 4),
173 Pos(0, 8), "goToBracket", at(2, 0), "goToBracket", at(0, 8),
174 Pos(1, 2), "goToBracket", at(2, 0),
175 Pos(1, 7), "goToBracket", at(1, 10), "goToBracket", at(1, 6));
176
177 stTest("swapLine", "1\n2\n3---\n4\n5",
178 "swapLineDown", val("2\n1\n3---\n4\n5"),
179 "swapLineUp", val("1\n2\n3---\n4\n5"),
180 "swapLineUp", val("1\n2\n3---\n4\n5"),
181 Pos(4, 1), "swapLineDown", val("1\n2\n3---\n4\n5"),
182 setSel(0, 1, 0, 1,
183 1, 0, 2, 0,
184 2, 2, 2, 2),
185 "swapLineDown", val("4\n1\n2\n3---\n5"),
186 hasSel(1, 1, 1, 1,
187 2, 0, 3, 0,
188 3, 2, 3, 2),
189 "swapLineUp", val("1\n2\n3---\n4\n5"),
190 hasSel(0, 1, 0, 1,
191 1, 0, 2, 0,
192 2, 2, 2, 2));
193
194 stTest("swapLineEmptyBottomSel", "1\n2\n3",
195 setSel(0, 1, 1, 0),
196 "swapLineDown", val("2\n1\n3"), hasSel(1, 1, 2, 0),
197 "swapLineUp", val("1\n2\n3"), hasSel(0, 1, 1, 0),
198 "swapLineUp", val("1\n2\n3"), hasSel(0, 0, 0, 0));
199
200 stTest("swapLineUpFromEnd", "a\nb\nc",
201 Pos(2, 1), "swapLineUp",
202 hasSel(1, 1, 1, 1), val("a\nc\nb"));
203
204 stTest("joinLines", "abc\ndef\nghi\njkl",
205 "joinLines", val("abc def\nghi\njkl"), at(0, 4),
206 "undo",
207 setSel(0, 2, 1, 1), "joinLines",
208 val("abc def ghi\njkl"), hasSel(0, 2, 0, 8),
209 "undo",
210 setSel(0, 1, 0, 1,
211 1, 1, 1, 1,
212 3, 1, 3, 1), "joinLines",
213 val("abc def ghi\njkl"), hasSel(0, 4, 0, 4,
214 0, 8, 0, 8,
215 1, 3, 1, 3));
216
217 stTest("duplicateLine", "abc\ndef\nghi",
218 Pos(1, 0), "duplicateLine", val("abc\ndef\ndef\nghi"), at(2, 0),
219 "undo",
220 setSel(0, 1, 0, 1,
221 1, 1, 1, 1,
222 2, 1, 2, 1), "duplicateLine",
223 val("abc\nabc\ndef\ndef\nghi\nghi"), hasSel(1, 1, 1, 1,
224 3, 1, 3, 1,
225 5, 1, 5, 1));
226 stTest("duplicateLineSelection", "abcdef",
227 setSel(0, 1, 0, 1,
228 0, 2, 0, 4,
229 0, 5, 0, 5),
230 "duplicateLine",
231 val("abcdef\nabcdcdef\nabcdcdef"), hasSel(2, 1, 2, 1,
232 2, 4, 2, 6,
233 2, 7, 2, 7));
234
235 stTest("sortLines", "c\nb\na\nC\nB\nA",
236 "sortLines", val("A\nB\nC\na\nb\nc"),
237 "undo",
238 setSel(0, 0, 2, 0,
239 3, 0, 5, 0),
240 "sortLines", val("b\nc\na\nB\nC\nA"),
241 hasSel(0, 0, 2, 0,
242 3, 0, 5, 0),
243 "undo",
244 setSel(1, 0, 5, 0), "sortLinesInsensitive", val("c\na\nB\nb\nC\nA"));
245
246 stTest("bookmarks", "abc\ndef\nghi\njkl",
247 Pos(0, 1), "toggleBookmark",
248 setSel(1, 1, 1, 2), "toggleBookmark",
249 setSel(2, 1, 2, 2), "toggleBookmark",
250 "nextBookmark", hasSel(0, 1, 0, 1),
251 "nextBookmark", hasSel(1, 1, 1, 2),
252 "nextBookmark", hasSel(2, 1, 2, 2),
253 "prevBookmark", hasSel(1, 1, 1, 2),
254 "prevBookmark", hasSel(0, 1, 0, 1),
255 "prevBookmark", hasSel(2, 1, 2, 2),
256 "prevBookmark", hasSel(1, 1, 1, 2),
257 "toggleBookmark",
258 "prevBookmark", hasSel(2, 1, 2, 2),
259 "prevBookmark", hasSel(0, 1, 0, 1),
260 "selectBookmarks", hasSel(0, 1, 0, 1,
261 2, 1, 2, 2),
262 "clearBookmarks",
263 Pos(0, 0), "selectBookmarks", at(0, 0));
264
265 stTest("smartBackspace", " foo\n bar",
266 setSel(0, 2, 0, 2, 1, 4, 1, 4, 1, 6, 1, 6), "smartBackspace",
267 val("foo\n br"))
268
269 stTest("upAndDowncaseAtCursor", "abc\ndef x\nghI",
270 setSel(0, 1, 0, 3,
271 1, 1, 1, 1,
272 1, 4, 1, 4), "upcaseAtCursor",
273 val("aBC\nDEF x\nghI"), hasSel(0, 1, 0, 3,
274 1, 3, 1, 3,
275 1, 4, 1, 4),
276 "downcaseAtCursor",
277 val("abc\ndef x\nghI"), hasSel(0, 1, 0, 3,
278 1, 3, 1, 3,
279 1, 4, 1, 4));
280
281 stTest("mark", "abc\ndef\nghi",
282 Pos(1, 1), "setSublimeMark",
283 Pos(2, 1), "selectToSublimeMark", hasSel(2, 1, 1, 1),
284 Pos(0, 1), "swapWithSublimeMark", at(1, 1), "swapWithSublimeMark", at(0, 1),
285 "deleteToSublimeMark", val("aef\nghi"),
286 "sublimeYank", val("abc\ndef\nghi"), at(1, 1));
287
288 stTest("findUnder", "foo foobar a",
289 "findUnder", hasSel(0, 4, 0, 7),
290 "findUnder", hasSel(0, 0, 0, 3),
291 "findUnderPrevious", hasSel(0, 4, 0, 7),
292 "findUnderPrevious", hasSel(0, 0, 0, 3),
293 Pos(0, 4), "findUnder", hasSel(0, 4, 0, 10),
294 Pos(0, 11), "findUnder", hasSel(0, 11, 0, 11));
295 })();