1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/sourceeditor/test/cm_sublime_test.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,297 @@ 1.4 +(function() { 1.5 + "use strict"; 1.6 + 1.7 + var Pos = CodeMirror.Pos; 1.8 + namespace = "sublime_"; 1.9 + 1.10 + function stTest(name) { 1.11 + var actions = Array.prototype.slice.call(arguments, 1); 1.12 + testCM(name, function(cm) { 1.13 + for (var i = 0; i < actions.length; i++) { 1.14 + var action = actions[i]; 1.15 + if (typeof action == "string" && i == 0) 1.16 + cm.setValue(action); 1.17 + else if (typeof action == "string") 1.18 + cm.execCommand(action); 1.19 + else if (action instanceof Pos) 1.20 + cm.setCursor(action); 1.21 + else 1.22 + action(cm); 1.23 + } 1.24 + }); 1.25 + } 1.26 + 1.27 + function at(line, ch, msg) { 1.28 + return function(cm) { 1.29 + eq(cm.listSelections().length, 1); 1.30 + eqPos(cm.getCursor("head"), Pos(line, ch), msg); 1.31 + eqPos(cm.getCursor("anchor"), Pos(line, ch), msg); 1.32 + }; 1.33 + } 1.34 + 1.35 + function val(content, msg) { 1.36 + return function(cm) { eq(cm.getValue(), content, msg); }; 1.37 + } 1.38 + 1.39 + function argsToRanges(args) { 1.40 + if (args.length % 4) throw new Error("Wrong number of arguments for ranges."); 1.41 + var ranges = []; 1.42 + for (var i = 0; i < args.length; i += 4) 1.43 + ranges.push({anchor: Pos(args[i], args[i + 1]), 1.44 + head: Pos(args[i + 2], args[i + 3])}); 1.45 + return ranges; 1.46 + } 1.47 + 1.48 + function setSel() { 1.49 + var ranges = argsToRanges(arguments); 1.50 + return function(cm) { cm.setSelections(ranges, 0); }; 1.51 + } 1.52 + 1.53 + function hasSel() { 1.54 + var ranges = argsToRanges(arguments); 1.55 + return function(cm) { 1.56 + var sels = cm.listSelections(); 1.57 + if (sels.length != ranges.length) 1.58 + throw new Failure("Expected " + ranges.length + " selections, but found " + sels.length); 1.59 + for (var i = 0; i < sels.length; i++) { 1.60 + eqPos(sels[i].anchor, ranges[i].anchor, "anchor " + i); 1.61 + eqPos(sels[i].head, ranges[i].head, "head " + i); 1.62 + } 1.63 + }; 1.64 + } 1.65 + 1.66 + stTest("bySubword", "the foo_bar DooDahBah \n a", 1.67 + "goSubwordLeft", at(0, 0), 1.68 + "goSubwordRight", at(0, 3), 1.69 + "goSubwordRight", at(0, 7), 1.70 + "goSubwordRight", at(0, 11), 1.71 + "goSubwordRight", at(0, 15), 1.72 + "goSubwordRight", at(0, 18), 1.73 + "goSubwordRight", at(0, 21), 1.74 + "goSubwordRight", at(0, 22), 1.75 + "goSubwordRight", at(1, 0), 1.76 + "goSubwordRight", at(1, 2), 1.77 + "goSubwordRight", at(1, 2), 1.78 + "goSubwordLeft", at(1, 1), 1.79 + "goSubwordLeft", at(1, 0), 1.80 + "goSubwordLeft", at(0, 22), 1.81 + "goSubwordLeft", at(0, 18), 1.82 + "goSubwordLeft", at(0, 15), 1.83 + "goSubwordLeft", at(0, 12), 1.84 + "goSubwordLeft", at(0, 8), 1.85 + "goSubwordLeft", at(0, 4), 1.86 + "goSubwordLeft", at(0, 0)); 1.87 + 1.88 + stTest("splitSelectionByLine", "abc\ndef\nghi", 1.89 + setSel(0, 1, 2, 2), 1.90 + "splitSelectionByLine", 1.91 + hasSel(0, 1, 0, 3, 1.92 + 1, 0, 1, 3, 1.93 + 2, 0, 2, 2)); 1.94 + 1.95 + stTest("splitSelectionByLineMulti", "abc\ndef\nghi\njkl", 1.96 + setSel(0, 1, 1, 1, 1.97 + 1, 2, 3, 2, 1.98 + 3, 3, 3, 3), 1.99 + "splitSelectionByLine", 1.100 + hasSel(0, 1, 0, 3, 1.101 + 1, 0, 1, 1, 1.102 + 1, 2, 1, 3, 1.103 + 2, 0, 2, 3, 1.104 + 3, 0, 3, 2, 1.105 + 3, 3, 3, 3)); 1.106 + 1.107 + stTest("selectLine", "abc\ndef\nghi", 1.108 + setSel(0, 1, 0, 1, 1.109 + 2, 0, 2, 1), 1.110 + "selectLine", 1.111 + hasSel(0, 0, 1, 0, 1.112 + 2, 0, 2, 3), 1.113 + setSel(0, 1, 1, 0), 1.114 + "selectLine", 1.115 + hasSel(0, 0, 2, 0)); 1.116 + 1.117 + stTest("insertLineAfter", "abcde\nfghijkl\nmn", 1.118 + setSel(0, 1, 0, 1, 1.119 + 0, 3, 0, 3, 1.120 + 1, 2, 1, 2, 1.121 + 1, 3, 1, 5), "insertLineAfter", 1.122 + hasSel(1, 0, 1, 0, 1.123 + 3, 0, 3, 0), val("abcde\n\nfghijkl\n\nmn")); 1.124 + 1.125 + stTest("insertLineBefore", "abcde\nfghijkl\nmn", 1.126 + setSel(0, 1, 0, 1, 1.127 + 0, 3, 0, 3, 1.128 + 1, 2, 1, 2, 1.129 + 1, 3, 1, 5), "insertLineBefore", 1.130 + hasSel(0, 0, 0, 0, 1.131 + 2, 0, 2, 0), val("\nabcde\n\nfghijkl\nmn")); 1.132 + 1.133 + stTest("selectNextOccurrence", "a foo bar\nfoobar foo", 1.134 + setSel(0, 2, 0, 5), 1.135 + "selectNextOccurrence", hasSel(0, 2, 0, 5, 1.136 + 1, 0, 1, 3), 1.137 + "selectNextOccurrence", hasSel(0, 2, 0, 5, 1.138 + 1, 0, 1, 3, 1.139 + 1, 7, 1, 10), 1.140 + "selectNextOccurrence", hasSel(0, 2, 0, 5, 1.141 + 1, 0, 1, 3, 1.142 + 1, 7, 1, 10), 1.143 + Pos(0, 3), "selectNextOccurrence", hasSel(0, 2, 0, 5), 1.144 + "selectNextOccurrence", hasSel(0, 2, 0, 5, 1.145 + 1, 7, 1, 10), 1.146 + setSel(0, 6, 0, 9), 1.147 + "selectNextOccurrence", hasSel(0, 6, 0, 9, 1.148 + 1, 3, 1, 6)); 1.149 + 1.150 + stTest("selectScope", "foo(a) {\n bar[1, 2];\n}", 1.151 + "selectScope", hasSel(0, 0, 2, 1), 1.152 + Pos(0, 4), "selectScope", hasSel(0, 4, 0, 5), 1.153 + Pos(0, 5), "selectScope", hasSel(0, 4, 0, 5), 1.154 + Pos(0, 6), "selectScope", hasSel(0, 0, 2, 1), 1.155 + Pos(0, 8), "selectScope", hasSel(0, 8, 2, 0), 1.156 + Pos(1, 2), "selectScope", hasSel(0, 8, 2, 0), 1.157 + Pos(1, 6), "selectScope", hasSel(1, 6, 1, 10), 1.158 + Pos(1, 9), "selectScope", hasSel(1, 6, 1, 10)); 1.159 + 1.160 + stTest("goToBracket", "foo(a) {\n bar[1, 2];\n}", 1.161 + Pos(0, 0), "goToBracket", at(0, 0), 1.162 + Pos(0, 4), "goToBracket", at(0, 5), "goToBracket", at(0, 4), 1.163 + Pos(0, 8), "goToBracket", at(2, 0), "goToBracket", at(0, 8), 1.164 + Pos(1, 2), "goToBracket", at(2, 0), 1.165 + Pos(1, 7), "goToBracket", at(1, 10), "goToBracket", at(1, 6)); 1.166 + 1.167 + stTest("swapLine", "1\n2\n3---\n4\n5", 1.168 + "swapLineDown", val("2\n1\n3---\n4\n5"), 1.169 + "swapLineUp", val("1\n2\n3---\n4\n5"), 1.170 + "swapLineUp", val("1\n2\n3---\n4\n5"), 1.171 + Pos(4, 1), "swapLineDown", val("1\n2\n3---\n4\n5"), 1.172 + setSel(0, 1, 0, 1, 1.173 + 1, 0, 2, 0, 1.174 + 2, 2, 2, 2), 1.175 + "swapLineDown", val("4\n1\n2\n3---\n5"), 1.176 + hasSel(1, 1, 1, 1, 1.177 + 2, 0, 3, 0, 1.178 + 3, 2, 3, 2), 1.179 + "swapLineUp", val("1\n2\n3---\n4\n5"), 1.180 + hasSel(0, 1, 0, 1, 1.181 + 1, 0, 2, 0, 1.182 + 2, 2, 2, 2)); 1.183 + 1.184 + stTest("swapLineUpFromEnd", "a\nb\nc", 1.185 + Pos(2, 1), "swapLineUp", 1.186 + hasSel(1, 1, 1, 1), val("a\nc\nb")); 1.187 + 1.188 + stTest("joinLines", "abc\ndef\nghi\njkl", 1.189 + "joinLines", val("abc def\nghi\njkl"), at(0, 4), 1.190 + "undo", 1.191 + setSel(0, 2, 1, 1), "joinLines", 1.192 + val("abc def ghi\njkl"), hasSel(0, 2, 0, 8), 1.193 + "undo", 1.194 + setSel(0, 1, 0, 1, 1.195 + 1, 1, 1, 1, 1.196 + 3, 1, 3, 1), "joinLines", 1.197 + val("abc def ghi\njkl"), hasSel(0, 4, 0, 4, 1.198 + 0, 8, 0, 8, 1.199 + 1, 3, 1, 3)); 1.200 + 1.201 + stTest("duplicateLine", "abc\ndef\nghi", 1.202 + Pos(1, 0), "duplicateLine", val("abc\ndef\ndef\nghi"), at(2, 0), 1.203 + "undo", 1.204 + setSel(0, 1, 0, 1, 1.205 + 1, 1, 1, 1, 1.206 + 2, 1, 2, 1), "duplicateLine", 1.207 + val("abc\nabc\ndef\ndef\nghi\nghi"), hasSel(1, 1, 1, 1, 1.208 + 3, 1, 3, 1, 1.209 + 5, 1, 5, 1)); 1.210 + stTest("duplicateLineSelection", "abcdef", 1.211 + setSel(0, 1, 0, 1, 1.212 + 0, 2, 0, 4, 1.213 + 0, 5, 0, 5), 1.214 + "duplicateLine", 1.215 + val("abcdef\nabcdcdef\nabcdcdef"), hasSel(2, 1, 2, 1, 1.216 + 2, 4, 2, 6, 1.217 + 2, 7, 2, 7)); 1.218 + 1.219 + stTest("selectLinesUpward", "123\n345\n789\n012", 1.220 + setSel(0, 1, 0, 1, 1.221 + 1, 1, 1, 3, 1.222 + 2, 0, 2, 0, 1.223 + 3, 0, 3, 0), 1.224 + "selectLinesUpward", 1.225 + hasSel(0, 1, 0, 1, 1.226 + 0, 3, 0, 3, 1.227 + 1, 0, 1, 0, 1.228 + 1, 1, 1, 3, 1.229 + 2, 0, 2, 0, 1.230 + 3, 0, 3, 0)); 1.231 + 1.232 + stTest("selectLinesDownward", "123\n345\n789\n012", 1.233 + setSel(0, 1, 0, 1, 1.234 + 1, 1, 1, 3, 1.235 + 2, 0, 2, 0, 1.236 + 3, 0, 3, 0), 1.237 + "selectLinesDownward", 1.238 + hasSel(0, 1, 0, 1, 1.239 + 1, 1, 1, 3, 1.240 + 2, 0, 2, 0, 1.241 + 2, 3, 2, 3, 1.242 + 3, 0, 3, 0)); 1.243 + 1.244 + stTest("sortLines", "c\nb\na\nC\nB\nA", 1.245 + "sortLines", val("A\nB\nC\na\nb\nc"), 1.246 + "undo", 1.247 + setSel(0, 0, 2, 0, 1.248 + 3, 0, 5, 0), 1.249 + "sortLines", val("a\nb\nc\nA\nB\nC"), 1.250 + hasSel(0, 0, 2, 1, 1.251 + 3, 0, 5, 1), 1.252 + "undo", 1.253 + setSel(1, 0, 4, 0), "sortLinesInsensitive", val("c\na\nB\nb\nC\nA")); 1.254 + 1.255 + stTest("bookmarks", "abc\ndef\nghi\njkl", 1.256 + Pos(0, 1), "toggleBookmark", 1.257 + setSel(1, 1, 1, 2), "toggleBookmark", 1.258 + setSel(2, 1, 2, 2), "toggleBookmark", 1.259 + "nextBookmark", hasSel(0, 1, 0, 1), 1.260 + "nextBookmark", hasSel(1, 1, 1, 2), 1.261 + "nextBookmark", hasSel(2, 1, 2, 2), 1.262 + "prevBookmark", hasSel(1, 1, 1, 2), 1.263 + "prevBookmark", hasSel(0, 1, 0, 1), 1.264 + "prevBookmark", hasSel(2, 1, 2, 2), 1.265 + "prevBookmark", hasSel(1, 1, 1, 2), 1.266 + "toggleBookmark", 1.267 + "prevBookmark", hasSel(2, 1, 2, 2), 1.268 + "prevBookmark", hasSel(0, 1, 0, 1), 1.269 + "selectBookmarks", hasSel(0, 1, 0, 1, 1.270 + 2, 1, 2, 2), 1.271 + "clearBookmarks", 1.272 + Pos(0, 0), "selectBookmarks", at(0, 0)); 1.273 + 1.274 + stTest("upAndDowncaseAtCursor", "abc\ndef x\nghI", 1.275 + setSel(0, 1, 0, 3, 1.276 + 1, 1, 1, 1, 1.277 + 1, 4, 1, 4), "upcaseAtCursor", 1.278 + val("aBC\nDEF x\nghI"), hasSel(0, 1, 0, 3, 1.279 + 1, 3, 1, 3, 1.280 + 1, 4, 1, 4), 1.281 + "downcaseAtCursor", 1.282 + val("abc\ndef x\nghI"), hasSel(0, 1, 0, 3, 1.283 + 1, 3, 1, 3, 1.284 + 1, 4, 1, 4)); 1.285 + 1.286 + stTest("mark", "abc\ndef\nghi", 1.287 + Pos(1, 1), "setSublimeMark", 1.288 + Pos(2, 1), "selectToSublimeMark", hasSel(2, 1, 1, 1), 1.289 + Pos(0, 1), "swapWithSublimeMark", at(1, 1), "swapWithSublimeMark", at(0, 1), 1.290 + "deleteToSublimeMark", val("aef\nghi"), 1.291 + "sublimeYank", val("abc\ndef\nghi"), at(1, 1)); 1.292 + 1.293 + stTest("findUnder", "foo foobar a", 1.294 + "findUnder", hasSel(0, 4, 0, 7), 1.295 + "findUnder", hasSel(0, 0, 0, 3), 1.296 + "findUnderPrevious", hasSel(0, 4, 0, 7), 1.297 + "findUnderPrevious", hasSel(0, 0, 0, 3), 1.298 + Pos(0, 4), "findUnder", hasSel(0, 4, 0, 10), 1.299 + Pos(0, 11), "findUnder", hasSel(0, 11, 0, 11)); 1.300 +})();