sublime_test.js (10420B)
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", 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 "selectScope", hasSel(0, 8, 2, 0), 157 "selectScope", hasSel(0, 0, 2, 1)); 158 159 stTest("goToBracket", "foo(a) {\n bar[1, 2];\n}", 160 Pos(0, 0), "goToBracket", at(0, 0), 161 Pos(0, 4), "goToBracket", at(0, 5), "goToBracket", at(0, 4), 162 Pos(0, 8), "goToBracket", at(2, 0), "goToBracket", at(0, 8), 163 Pos(1, 2), "goToBracket", at(2, 0), 164 Pos(1, 7), "goToBracket", at(1, 10), "goToBracket", at(1, 6)); 165 166 stTest("swapLine", "1\n2\n3---\n4\n5", 167 "swapLineDown", val("2\n1\n3---\n4\n5"), 168 "swapLineUp", val("1\n2\n3---\n4\n5"), 169 "swapLineUp", val("1\n2\n3---\n4\n5"), 170 Pos(4, 1), "swapLineDown", val("1\n2\n3---\n4\n5"), 171 setSel(0, 1, 0, 1, 172 1, 0, 2, 0, 173 2, 2, 2, 2), 174 "swapLineDown", val("4\n1\n2\n3---\n5"), 175 hasSel(1, 1, 1, 1, 176 2, 0, 3, 0, 177 3, 2, 3, 2), 178 "swapLineUp", val("1\n2\n3---\n4\n5"), 179 hasSel(0, 1, 0, 1, 180 1, 0, 2, 0, 181 2, 2, 2, 2)); 182 183 stTest("swapLineEmptyBottomSel", "1\n2\n3", 184 setSel(0, 1, 1, 0), 185 "swapLineDown", val("2\n1\n3"), hasSel(1, 1, 2, 0), 186 "swapLineUp", val("1\n2\n3"), hasSel(0, 1, 1, 0), 187 "swapLineUp", val("1\n2\n3"), hasSel(0, 0, 0, 0)); 188 189 stTest("swapLineUpFromEnd", "a\nb\nc", 190 Pos(2, 1), "swapLineUp", 191 hasSel(1, 1, 1, 1), val("a\nc\nb")); 192 193 stTest("joinLines", "abc\ndef\nghi\njkl", 194 "joinLines", val("abc def\nghi\njkl"), at(0, 4), 195 "undo", 196 setSel(0, 2, 1, 1), "joinLines", 197 val("abc def ghi\njkl"), hasSel(0, 2, 0, 8), 198 "undo", 199 setSel(0, 1, 0, 1, 200 1, 1, 1, 1, 201 3, 1, 3, 1), "joinLines", 202 val("abc def ghi\njkl"), hasSel(0, 4, 0, 4, 203 0, 8, 0, 8, 204 1, 3, 1, 3)); 205 206 stTest("duplicateLine", "abc\ndef\nghi", 207 Pos(1, 0), "duplicateLine", val("abc\ndef\ndef\nghi"), at(2, 0), 208 "undo", 209 setSel(0, 1, 0, 1, 210 1, 1, 1, 1, 211 2, 1, 2, 1), "duplicateLine", 212 val("abc\nabc\ndef\ndef\nghi\nghi"), hasSel(1, 1, 1, 1, 213 3, 1, 3, 1, 214 5, 1, 5, 1)); 215 stTest("duplicateLineSelection", "abcdef", 216 setSel(0, 1, 0, 1, 217 0, 2, 0, 4, 218 0, 5, 0, 5), 219 "duplicateLine", 220 val("abcdef\nabcdcdef\nabcdcdef"), hasSel(2, 1, 2, 1, 221 2, 4, 2, 6, 222 2, 7, 2, 7)); 223 224 stTest("sortLines", "c\nb\na\nC\nB\nA", 225 "sortLines", val("A\nB\nC\na\nb\nc"), 226 "undo", 227 setSel(0, 0, 2, 0, 228 3, 0, 5, 0), 229 "sortLines", val("b\nc\na\nB\nC\nA"), 230 hasSel(0, 0, 2, 0, 231 3, 0, 5, 0), 232 "undo", 233 setSel(1, 0, 5, 0), "sortLinesInsensitive", val("c\na\nB\nb\nC\nA")); 234 235 stTest("bookmarks", "abc\ndef\nghi\njkl", 236 Pos(0, 1), "toggleBookmark", 237 setSel(1, 1, 1, 2), "toggleBookmark", 238 setSel(2, 1, 2, 2), "toggleBookmark", 239 "nextBookmark", hasSel(0, 1, 0, 1), 240 "nextBookmark", hasSel(1, 1, 1, 2), 241 "nextBookmark", hasSel(2, 1, 2, 2), 242 "prevBookmark", hasSel(1, 1, 1, 2), 243 "prevBookmark", hasSel(0, 1, 0, 1), 244 "prevBookmark", hasSel(2, 1, 2, 2), 245 "prevBookmark", hasSel(1, 1, 1, 2), 246 "toggleBookmark", 247 "prevBookmark", hasSel(2, 1, 2, 2), 248 "prevBookmark", hasSel(0, 1, 0, 1), 249 "selectBookmarks", hasSel(0, 1, 0, 1, 250 2, 1, 2, 2), 251 "clearBookmarks", 252 Pos(0, 0), "selectBookmarks", at(0, 0)); 253 254 stTest("smartBackspace", " foo\n bar", 255 setSel(0, 2, 0, 2, 1, 4, 1, 4, 1, 6, 1, 6), "smartBackspace", 256 val("foo\n br")) 257 258 stTest("upAndDowncaseAtCursor", "abc\ndef x\nghI", 259 setSel(0, 1, 0, 3, 260 1, 1, 1, 1, 261 1, 4, 1, 4), "upcaseAtCursor", 262 val("aBC\nDEF x\nghI"), hasSel(0, 1, 0, 3, 263 1, 3, 1, 3, 264 1, 4, 1, 4), 265 "downcaseAtCursor", 266 val("abc\ndef x\nghI"), hasSel(0, 1, 0, 3, 267 1, 3, 1, 3, 268 1, 4, 1, 4)); 269 270 stTest("mark", "abc\ndef\nghi", 271 Pos(1, 1), "setSublimeMark", 272 Pos(2, 1), "selectToSublimeMark", hasSel(2, 1, 1, 1), 273 Pos(0, 1), "swapWithSublimeMark", at(1, 1), "swapWithSublimeMark", at(0, 1), 274 "deleteToSublimeMark", val("aef\nghi"), 275 "sublimeYank", val("abc\ndef\nghi"), at(1, 1)); 276 277 stTest("findUnder", "foo foobar a", 278 "findUnder", hasSel(0, 4, 0, 7), 279 "findUnder", hasSel(0, 0, 0, 3), 280 "findUnderPrevious", hasSel(0, 4, 0, 7), 281 "findUnderPrevious", hasSel(0, 0, 0, 3), 282 Pos(0, 4), "findUnder", hasSel(0, 4, 0, 10), 283 Pos(0, 11), "findUnder", hasSel(0, 11, 0, 11)); 284 })();