browser/devtools/sourceeditor/test/cm_sublime_test.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:4ed4f1a386b8
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 eqPos(cm.getCursor("head"), Pos(line, ch), msg);
28 eqPos(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 eqPos(sels[i].anchor, ranges[i].anchor, "anchor " + i);
58 eqPos(sels[i].head, ranges[i].head, "head " + i);
59 }
60 };
61 }
62
63 stTest("bySubword", "the foo_bar DooDahBah \n a",
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, 2),
75 "goSubwordLeft", at(1, 1),
76 "goSubwordLeft", at(1, 0),
77 "goSubwordLeft", at(0, 22),
78 "goSubwordLeft", at(0, 18),
79 "goSubwordLeft", at(0, 15),
80 "goSubwordLeft", at(0, 12),
81 "goSubwordLeft", at(0, 8),
82 "goSubwordLeft", at(0, 4),
83 "goSubwordLeft", at(0, 0));
84
85 stTest("splitSelectionByLine", "abc\ndef\nghi",
86 setSel(0, 1, 2, 2),
87 "splitSelectionByLine",
88 hasSel(0, 1, 0, 3,
89 1, 0, 1, 3,
90 2, 0, 2, 2));
91
92 stTest("splitSelectionByLineMulti", "abc\ndef\nghi\njkl",
93 setSel(0, 1, 1, 1,
94 1, 2, 3, 2,
95 3, 3, 3, 3),
96 "splitSelectionByLine",
97 hasSel(0, 1, 0, 3,
98 1, 0, 1, 1,
99 1, 2, 1, 3,
100 2, 0, 2, 3,
101 3, 0, 3, 2,
102 3, 3, 3, 3));
103
104 stTest("selectLine", "abc\ndef\nghi",
105 setSel(0, 1, 0, 1,
106 2, 0, 2, 1),
107 "selectLine",
108 hasSel(0, 0, 1, 0,
109 2, 0, 2, 3),
110 setSel(0, 1, 1, 0),
111 "selectLine",
112 hasSel(0, 0, 2, 0));
113
114 stTest("insertLineAfter", "abcde\nfghijkl\nmn",
115 setSel(0, 1, 0, 1,
116 0, 3, 0, 3,
117 1, 2, 1, 2,
118 1, 3, 1, 5), "insertLineAfter",
119 hasSel(1, 0, 1, 0,
120 3, 0, 3, 0), val("abcde\n\nfghijkl\n\nmn"));
121
122 stTest("insertLineBefore", "abcde\nfghijkl\nmn",
123 setSel(0, 1, 0, 1,
124 0, 3, 0, 3,
125 1, 2, 1, 2,
126 1, 3, 1, 5), "insertLineBefore",
127 hasSel(0, 0, 0, 0,
128 2, 0, 2, 0), val("\nabcde\n\nfghijkl\nmn"));
129
130 stTest("selectNextOccurrence", "a foo bar\nfoobar foo",
131 setSel(0, 2, 0, 5),
132 "selectNextOccurrence", hasSel(0, 2, 0, 5,
133 1, 0, 1, 3),
134 "selectNextOccurrence", hasSel(0, 2, 0, 5,
135 1, 0, 1, 3,
136 1, 7, 1, 10),
137 "selectNextOccurrence", hasSel(0, 2, 0, 5,
138 1, 0, 1, 3,
139 1, 7, 1, 10),
140 Pos(0, 3), "selectNextOccurrence", hasSel(0, 2, 0, 5),
141 "selectNextOccurrence", hasSel(0, 2, 0, 5,
142 1, 7, 1, 10),
143 setSel(0, 6, 0, 9),
144 "selectNextOccurrence", hasSel(0, 6, 0, 9,
145 1, 3, 1, 6));
146
147 stTest("selectScope", "foo(a) {\n bar[1, 2];\n}",
148 "selectScope", hasSel(0, 0, 2, 1),
149 Pos(0, 4), "selectScope", hasSel(0, 4, 0, 5),
150 Pos(0, 5), "selectScope", hasSel(0, 4, 0, 5),
151 Pos(0, 6), "selectScope", hasSel(0, 0, 2, 1),
152 Pos(0, 8), "selectScope", hasSel(0, 8, 2, 0),
153 Pos(1, 2), "selectScope", hasSel(0, 8, 2, 0),
154 Pos(1, 6), "selectScope", hasSel(1, 6, 1, 10),
155 Pos(1, 9), "selectScope", hasSel(1, 6, 1, 10));
156
157 stTest("goToBracket", "foo(a) {\n bar[1, 2];\n}",
158 Pos(0, 0), "goToBracket", at(0, 0),
159 Pos(0, 4), "goToBracket", at(0, 5), "goToBracket", at(0, 4),
160 Pos(0, 8), "goToBracket", at(2, 0), "goToBracket", at(0, 8),
161 Pos(1, 2), "goToBracket", at(2, 0),
162 Pos(1, 7), "goToBracket", at(1, 10), "goToBracket", at(1, 6));
163
164 stTest("swapLine", "1\n2\n3---\n4\n5",
165 "swapLineDown", val("2\n1\n3---\n4\n5"),
166 "swapLineUp", val("1\n2\n3---\n4\n5"),
167 "swapLineUp", val("1\n2\n3---\n4\n5"),
168 Pos(4, 1), "swapLineDown", val("1\n2\n3---\n4\n5"),
169 setSel(0, 1, 0, 1,
170 1, 0, 2, 0,
171 2, 2, 2, 2),
172 "swapLineDown", val("4\n1\n2\n3---\n5"),
173 hasSel(1, 1, 1, 1,
174 2, 0, 3, 0,
175 3, 2, 3, 2),
176 "swapLineUp", val("1\n2\n3---\n4\n5"),
177 hasSel(0, 1, 0, 1,
178 1, 0, 2, 0,
179 2, 2, 2, 2));
180
181 stTest("swapLineUpFromEnd", "a\nb\nc",
182 Pos(2, 1), "swapLineUp",
183 hasSel(1, 1, 1, 1), val("a\nc\nb"));
184
185 stTest("joinLines", "abc\ndef\nghi\njkl",
186 "joinLines", val("abc def\nghi\njkl"), at(0, 4),
187 "undo",
188 setSel(0, 2, 1, 1), "joinLines",
189 val("abc def ghi\njkl"), hasSel(0, 2, 0, 8),
190 "undo",
191 setSel(0, 1, 0, 1,
192 1, 1, 1, 1,
193 3, 1, 3, 1), "joinLines",
194 val("abc def ghi\njkl"), hasSel(0, 4, 0, 4,
195 0, 8, 0, 8,
196 1, 3, 1, 3));
197
198 stTest("duplicateLine", "abc\ndef\nghi",
199 Pos(1, 0), "duplicateLine", val("abc\ndef\ndef\nghi"), at(2, 0),
200 "undo",
201 setSel(0, 1, 0, 1,
202 1, 1, 1, 1,
203 2, 1, 2, 1), "duplicateLine",
204 val("abc\nabc\ndef\ndef\nghi\nghi"), hasSel(1, 1, 1, 1,
205 3, 1, 3, 1,
206 5, 1, 5, 1));
207 stTest("duplicateLineSelection", "abcdef",
208 setSel(0, 1, 0, 1,
209 0, 2, 0, 4,
210 0, 5, 0, 5),
211 "duplicateLine",
212 val("abcdef\nabcdcdef\nabcdcdef"), hasSel(2, 1, 2, 1,
213 2, 4, 2, 6,
214 2, 7, 2, 7));
215
216 stTest("selectLinesUpward", "123\n345\n789\n012",
217 setSel(0, 1, 0, 1,
218 1, 1, 1, 3,
219 2, 0, 2, 0,
220 3, 0, 3, 0),
221 "selectLinesUpward",
222 hasSel(0, 1, 0, 1,
223 0, 3, 0, 3,
224 1, 0, 1, 0,
225 1, 1, 1, 3,
226 2, 0, 2, 0,
227 3, 0, 3, 0));
228
229 stTest("selectLinesDownward", "123\n345\n789\n012",
230 setSel(0, 1, 0, 1,
231 1, 1, 1, 3,
232 2, 0, 2, 0,
233 3, 0, 3, 0),
234 "selectLinesDownward",
235 hasSel(0, 1, 0, 1,
236 1, 1, 1, 3,
237 2, 0, 2, 0,
238 2, 3, 2, 3,
239 3, 0, 3, 0));
240
241 stTest("sortLines", "c\nb\na\nC\nB\nA",
242 "sortLines", val("A\nB\nC\na\nb\nc"),
243 "undo",
244 setSel(0, 0, 2, 0,
245 3, 0, 5, 0),
246 "sortLines", val("a\nb\nc\nA\nB\nC"),
247 hasSel(0, 0, 2, 1,
248 3, 0, 5, 1),
249 "undo",
250 setSel(1, 0, 4, 0), "sortLinesInsensitive", val("c\na\nB\nb\nC\nA"));
251
252 stTest("bookmarks", "abc\ndef\nghi\njkl",
253 Pos(0, 1), "toggleBookmark",
254 setSel(1, 1, 1, 2), "toggleBookmark",
255 setSel(2, 1, 2, 2), "toggleBookmark",
256 "nextBookmark", hasSel(0, 1, 0, 1),
257 "nextBookmark", hasSel(1, 1, 1, 2),
258 "nextBookmark", hasSel(2, 1, 2, 2),
259 "prevBookmark", hasSel(1, 1, 1, 2),
260 "prevBookmark", hasSel(0, 1, 0, 1),
261 "prevBookmark", hasSel(2, 1, 2, 2),
262 "prevBookmark", hasSel(1, 1, 1, 2),
263 "toggleBookmark",
264 "prevBookmark", hasSel(2, 1, 2, 2),
265 "prevBookmark", hasSel(0, 1, 0, 1),
266 "selectBookmarks", hasSel(0, 1, 0, 1,
267 2, 1, 2, 2),
268 "clearBookmarks",
269 Pos(0, 0), "selectBookmarks", at(0, 0));
270
271 stTest("upAndDowncaseAtCursor", "abc\ndef x\nghI",
272 setSel(0, 1, 0, 3,
273 1, 1, 1, 1,
274 1, 4, 1, 4), "upcaseAtCursor",
275 val("aBC\nDEF x\nghI"), hasSel(0, 1, 0, 3,
276 1, 3, 1, 3,
277 1, 4, 1, 4),
278 "downcaseAtCursor",
279 val("abc\ndef x\nghI"), hasSel(0, 1, 0, 3,
280 1, 3, 1, 3,
281 1, 4, 1, 4));
282
283 stTest("mark", "abc\ndef\nghi",
284 Pos(1, 1), "setSublimeMark",
285 Pos(2, 1), "selectToSublimeMark", hasSel(2, 1, 1, 1),
286 Pos(0, 1), "swapWithSublimeMark", at(1, 1), "swapWithSublimeMark", at(0, 1),
287 "deleteToSublimeMark", val("aef\nghi"),
288 "sublimeYank", val("abc\ndef\nghi"), at(1, 1));
289
290 stTest("findUnder", "foo foobar a",
291 "findUnder", hasSel(0, 4, 0, 7),
292 "findUnder", hasSel(0, 0, 0, 3),
293 "findUnderPrevious", hasSel(0, 4, 0, 7),
294 "findUnderPrevious", hasSel(0, 0, 0, 3),
295 Pos(0, 4), "findUnder", hasSel(0, 4, 0, 10),
296 Pos(0, 11), "findUnder", hasSel(0, 11, 0, 11));
297 })();

mercurial