browser/devtools/sourceeditor/test/cm_multi_test.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 (function() {
michael@0 2 namespace = "multi_";
michael@0 3
michael@0 4 function hasSelections(cm) {
michael@0 5 var sels = cm.listSelections();
michael@0 6 var given = (arguments.length - 1) / 4;
michael@0 7 if (sels.length != given)
michael@0 8 throw new Failure("expected " + given + " selections, found " + sels.length);
michael@0 9 for (var i = 0, p = 1; i < given; i++, p += 4) {
michael@0 10 var anchor = Pos(arguments[p], arguments[p + 1]);
michael@0 11 var head = Pos(arguments[p + 2], arguments[p + 3]);
michael@0 12 eqPos(sels[i].anchor, anchor, "anchor of selection " + i);
michael@0 13 eqPos(sels[i].head, head, "head of selection " + i);
michael@0 14 }
michael@0 15 }
michael@0 16 function hasCursors(cm) {
michael@0 17 var sels = cm.listSelections();
michael@0 18 var given = (arguments.length - 1) / 2;
michael@0 19 if (sels.length != given)
michael@0 20 throw new Failure("expected " + given + " selections, found " + sels.length);
michael@0 21 for (var i = 0, p = 1; i < given; i++, p += 2) {
michael@0 22 eqPos(sels[i].anchor, sels[i].head, "something selected for " + i);
michael@0 23 var head = Pos(arguments[p], arguments[p + 1]);
michael@0 24 eqPos(sels[i].head, head, "selection " + i);
michael@0 25 }
michael@0 26 }
michael@0 27
michael@0 28 testCM("getSelection", function(cm) {
michael@0 29 select(cm, {anchor: Pos(0, 0), head: Pos(1, 2)}, {anchor: Pos(2, 2), head: Pos(2, 0)});
michael@0 30 eq(cm.getSelection(), "1234\n56\n90");
michael@0 31 eq(cm.getSelection(false).join("|"), "1234|56|90");
michael@0 32 eq(cm.getSelections().join("|"), "1234\n56|90");
michael@0 33 }, {value: "1234\n5678\n90"});
michael@0 34
michael@0 35 testCM("setSelection", function(cm) {
michael@0 36 select(cm, Pos(3, 0), Pos(0, 0), {anchor: Pos(2, 5), head: Pos(1, 0)});
michael@0 37 hasSelections(cm, 0, 0, 0, 0,
michael@0 38 2, 5, 1, 0,
michael@0 39 3, 0, 3, 0);
michael@0 40 cm.setSelection(Pos(1, 2), Pos(1, 1));
michael@0 41 hasSelections(cm, 1, 2, 1, 1);
michael@0 42 select(cm, {anchor: Pos(1, 1), head: Pos(2, 4)},
michael@0 43 {anchor: Pos(0, 0), head: Pos(1, 3)},
michael@0 44 Pos(3, 0), Pos(2, 2));
michael@0 45 hasSelections(cm, 0, 0, 2, 4,
michael@0 46 3, 0, 3, 0);
michael@0 47 cm.setSelections([{anchor: Pos(0, 1), head: Pos(0, 2)},
michael@0 48 {anchor: Pos(1, 1), head: Pos(1, 2)},
michael@0 49 {anchor: Pos(2, 1), head: Pos(2, 2)}], 1);
michael@0 50 eqPos(cm.getCursor("head"), Pos(1, 2));
michael@0 51 eqPos(cm.getCursor("anchor"), Pos(1, 1));
michael@0 52 eqPos(cm.getCursor("from"), Pos(1, 1));
michael@0 53 eqPos(cm.getCursor("to"), Pos(1, 2));
michael@0 54 cm.setCursor(Pos(1, 1));
michael@0 55 hasCursors(cm, 1, 1);
michael@0 56 }, {value: "abcde\nabcde\nabcde\n"});
michael@0 57
michael@0 58 testCM("somethingSelected", function(cm) {
michael@0 59 select(cm, Pos(0, 1), {anchor: Pos(0, 3), head: Pos(0, 5)});
michael@0 60 eq(cm.somethingSelected(), true);
michael@0 61 select(cm, Pos(0, 1), Pos(0, 3), Pos(0, 5));
michael@0 62 eq(cm.somethingSelected(), false);
michael@0 63 }, {value: "123456789"});
michael@0 64
michael@0 65 testCM("extendSelection", function(cm) {
michael@0 66 select(cm, Pos(0, 1), Pos(1, 1), Pos(2, 1));
michael@0 67 cm.setExtending(true);
michael@0 68 cm.extendSelections([Pos(0, 2), Pos(1, 0), Pos(2, 3)]);
michael@0 69 hasSelections(cm, 0, 1, 0, 2,
michael@0 70 1, 1, 1, 0,
michael@0 71 2, 1, 2, 3);
michael@0 72 cm.extendSelection(Pos(2, 4), Pos(2, 0));
michael@0 73 hasSelections(cm, 2, 4, 2, 0);
michael@0 74 }, {value: "1234\n1234\n1234"});
michael@0 75
michael@0 76 testCM("addSelection", function(cm) {
michael@0 77 select(cm, Pos(0, 1), Pos(1, 1));
michael@0 78 cm.addSelection(Pos(0, 0), Pos(0, 4));
michael@0 79 hasSelections(cm, 0, 0, 0, 4,
michael@0 80 1, 1, 1, 1);
michael@0 81 cm.addSelection(Pos(2, 2));
michael@0 82 hasSelections(cm, 0, 0, 0, 4,
michael@0 83 1, 1, 1, 1,
michael@0 84 2, 2, 2, 2);
michael@0 85 }, {value: "1234\n1234\n1234"});
michael@0 86
michael@0 87 testCM("replaceSelection", function(cm) {
michael@0 88 var selections = [{anchor: Pos(0, 0), head: Pos(0, 1)},
michael@0 89 {anchor: Pos(0, 2), head: Pos(0, 3)},
michael@0 90 {anchor: Pos(0, 4), head: Pos(0, 5)},
michael@0 91 {anchor: Pos(2, 1), head: Pos(2, 4)},
michael@0 92 {anchor: Pos(2, 5), head: Pos(2, 6)}];
michael@0 93 var val = "123456\n123456\n123456";
michael@0 94 cm.setValue(val);
michael@0 95 cm.setSelections(selections);
michael@0 96 cm.replaceSelection("ab", "around");
michael@0 97 eq(cm.getValue(), "ab2ab4ab6\n123456\n1ab5ab");
michael@0 98 hasSelections(cm, 0, 0, 0, 2,
michael@0 99 0, 3, 0, 5,
michael@0 100 0, 6, 0, 8,
michael@0 101 2, 1, 2, 3,
michael@0 102 2, 4, 2, 6);
michael@0 103 cm.setValue(val);
michael@0 104 cm.setSelections(selections);
michael@0 105 cm.replaceSelection("", "around");
michael@0 106 eq(cm.getValue(), "246\n123456\n15");
michael@0 107 hasSelections(cm, 0, 0, 0, 0,
michael@0 108 0, 1, 0, 1,
michael@0 109 0, 2, 0, 2,
michael@0 110 2, 1, 2, 1,
michael@0 111 2, 2, 2, 2);
michael@0 112 cm.setValue(val);
michael@0 113 cm.setSelections(selections);
michael@0 114 cm.replaceSelection("X\nY\nZ", "around");
michael@0 115 hasSelections(cm, 0, 0, 2, 1,
michael@0 116 2, 2, 4, 1,
michael@0 117 4, 2, 6, 1,
michael@0 118 8, 1, 10, 1,
michael@0 119 10, 2, 12, 1);
michael@0 120 cm.replaceSelection("a", "around");
michael@0 121 hasSelections(cm, 0, 0, 0, 1,
michael@0 122 0, 2, 0, 3,
michael@0 123 0, 4, 0, 5,
michael@0 124 2, 1, 2, 2,
michael@0 125 2, 3, 2, 4);
michael@0 126 cm.replaceSelection("xy", "start");
michael@0 127 hasSelections(cm, 0, 0, 0, 0,
michael@0 128 0, 3, 0, 3,
michael@0 129 0, 6, 0, 6,
michael@0 130 2, 1, 2, 1,
michael@0 131 2, 4, 2, 4);
michael@0 132 cm.replaceSelection("z\nf");
michael@0 133 hasSelections(cm, 1, 1, 1, 1,
michael@0 134 2, 1, 2, 1,
michael@0 135 3, 1, 3, 1,
michael@0 136 6, 1, 6, 1,
michael@0 137 7, 1, 7, 1);
michael@0 138 eq(cm.getValue(), "z\nfxy2z\nfxy4z\nfxy6\n123456\n1z\nfxy5z\nfxy");
michael@0 139 });
michael@0 140
michael@0 141 function select(cm) {
michael@0 142 var sels = [];
michael@0 143 for (var i = 1; i < arguments.length; i++) {
michael@0 144 var arg = arguments[i];
michael@0 145 if (arg.head) sels.push(arg);
michael@0 146 else sels.push({head: arg, anchor: arg});
michael@0 147 }
michael@0 148 cm.setSelections(sels, sels.length - 1);
michael@0 149 }
michael@0 150
michael@0 151 testCM("indentSelection", function(cm) {
michael@0 152 select(cm, Pos(0, 1), Pos(1, 1));
michael@0 153 cm.indentSelection(4);
michael@0 154 eq(cm.getValue(), " foo\n bar\nbaz");
michael@0 155
michael@0 156 select(cm, Pos(0, 2), Pos(0, 3), Pos(0, 4));
michael@0 157 cm.indentSelection(-2);
michael@0 158 eq(cm.getValue(), " foo\n bar\nbaz");
michael@0 159
michael@0 160 select(cm, {anchor: Pos(0, 0), head: Pos(1, 2)},
michael@0 161 {anchor: Pos(1, 3), head: Pos(2, 0)});
michael@0 162 cm.indentSelection(-2);
michael@0 163 eq(cm.getValue(), "foo\n bar\nbaz");
michael@0 164 }, {value: "foo\nbar\nbaz"});
michael@0 165
michael@0 166 testCM("killLine", function(cm) {
michael@0 167 select(cm, Pos(0, 1), Pos(0, 2), Pos(1, 1));
michael@0 168 cm.execCommand("killLine");
michael@0 169 eq(cm.getValue(), "f\nb\nbaz");
michael@0 170 cm.execCommand("killLine");
michael@0 171 eq(cm.getValue(), "fbbaz");
michael@0 172 cm.setValue("foo\nbar\nbaz");
michael@0 173 select(cm, Pos(0, 1), {anchor: Pos(0, 2), head: Pos(2, 1)});
michael@0 174 cm.execCommand("killLine");
michael@0 175 eq(cm.getValue(), "faz");
michael@0 176 }, {value: "foo\nbar\nbaz"});
michael@0 177
michael@0 178 testCM("deleteLine", function(cm) {
michael@0 179 select(cm, Pos(0, 0),
michael@0 180 {head: Pos(0, 1), anchor: Pos(2, 0)},
michael@0 181 Pos(4, 0));
michael@0 182 cm.execCommand("deleteLine");
michael@0 183 eq(cm.getValue(), "4\n6\n7");
michael@0 184 select(cm, Pos(2, 1));
michael@0 185 cm.execCommand("deleteLine");
michael@0 186 eq(cm.getValue(), "4\n6\n");
michael@0 187 }, {value: "1\n2\n3\n4\n5\n6\n7"});
michael@0 188
michael@0 189 testCM("deleteH", function(cm) {
michael@0 190 select(cm, Pos(0, 4), {anchor: Pos(1, 4), head: Pos(1, 5)});
michael@0 191 cm.execCommand("delWordAfter");
michael@0 192 eq(cm.getValue(), "foo bar baz\nabc ef ghi\n");
michael@0 193 cm.execCommand("delWordAfter");
michael@0 194 eq(cm.getValue(), "foo baz\nabc ghi\n");
michael@0 195 cm.execCommand("delCharBefore");
michael@0 196 cm.execCommand("delCharBefore");
michael@0 197 eq(cm.getValue(), "fo baz\nab ghi\n");
michael@0 198 select(cm, Pos(0, 3), Pos(0, 4), Pos(0, 5));
michael@0 199 cm.execCommand("delWordAfter");
michael@0 200 eq(cm.getValue(), "fo \nab ghi\n");
michael@0 201 }, {value: "foo bar baz\nabc def ghi\n"});
michael@0 202
michael@0 203 testCM("goLineStart", function(cm) {
michael@0 204 select(cm, Pos(0, 2), Pos(0, 3), Pos(1, 1));
michael@0 205 cm.execCommand("goLineStart");
michael@0 206 hasCursors(cm, 0, 0, 1, 0);
michael@0 207 select(cm, Pos(1, 1), Pos(0, 1));
michael@0 208 cm.setExtending(true);
michael@0 209 cm.execCommand("goLineStart");
michael@0 210 hasSelections(cm, 0, 1, 0, 0,
michael@0 211 1, 1, 1, 0);
michael@0 212 }, {value: "foo\nbar\nbaz"});
michael@0 213
michael@0 214 testCM("moveV", function(cm) {
michael@0 215 select(cm, Pos(0, 2), Pos(1, 2));
michael@0 216 cm.execCommand("goLineDown");
michael@0 217 hasCursors(cm, 1, 2, 2, 2);
michael@0 218 cm.execCommand("goLineUp");
michael@0 219 hasCursors(cm, 0, 2, 1, 2);
michael@0 220 cm.execCommand("goLineUp");
michael@0 221 hasCursors(cm, 0, 0, 0, 2);
michael@0 222 cm.execCommand("goLineUp");
michael@0 223 hasCursors(cm, 0, 0);
michael@0 224 select(cm, Pos(0, 2), Pos(1, 2));
michael@0 225 cm.setExtending(true);
michael@0 226 cm.execCommand("goLineDown");
michael@0 227 hasSelections(cm, 0, 2, 2, 2);
michael@0 228 }, {value: "12345\n12345\n12345"});
michael@0 229
michael@0 230 testCM("moveH", function(cm) {
michael@0 231 select(cm, Pos(0, 1), Pos(0, 3), Pos(0, 5), Pos(2, 3));
michael@0 232 cm.execCommand("goCharRight");
michael@0 233 hasCursors(cm, 0, 2, 0, 4, 1, 0, 2, 4);
michael@0 234 cm.execCommand("goCharLeft");
michael@0 235 hasCursors(cm, 0, 1, 0, 3, 0, 5, 2, 3);
michael@0 236 for (var i = 0; i < 15; i++)
michael@0 237 cm.execCommand("goCharRight");
michael@0 238 hasCursors(cm, 2, 4, 2, 5);
michael@0 239 }, {value: "12345\n12345\n12345"});
michael@0 240
michael@0 241 testCM("newlineAndIndent", function(cm) {
michael@0 242 select(cm, Pos(0, 5), Pos(1, 5));
michael@0 243 cm.execCommand("newlineAndIndent");
michael@0 244 hasCursors(cm, 1, 2, 3, 2);
michael@0 245 eq(cm.getValue(), "x = [\n 1];\ny = [\n 2];");
michael@0 246 cm.undo();
michael@0 247 eq(cm.getValue(), "x = [1];\ny = [2];");
michael@0 248 hasCursors(cm, 0, 5, 1, 5);
michael@0 249 select(cm, Pos(0, 5), Pos(0, 6));
michael@0 250 cm.execCommand("newlineAndIndent");
michael@0 251 hasCursors(cm, 1, 2, 2, 0);
michael@0 252 eq(cm.getValue(), "x = [\n 1\n];\ny = [2];");
michael@0 253 }, {value: "x = [1];\ny = [2];", mode: "javascript"});
michael@0 254
michael@0 255 testCM("goDocStartEnd", function(cm) {
michael@0 256 select(cm, Pos(0, 1), Pos(1, 1));
michael@0 257 cm.execCommand("goDocStart");
michael@0 258 hasCursors(cm, 0, 0);
michael@0 259 select(cm, Pos(0, 1), Pos(1, 1));
michael@0 260 cm.execCommand("goDocEnd");
michael@0 261 hasCursors(cm, 1, 3);
michael@0 262 select(cm, Pos(0, 1), Pos(1, 1));
michael@0 263 cm.setExtending(true);
michael@0 264 cm.execCommand("goDocEnd");
michael@0 265 hasSelections(cm, 1, 1, 1, 3);
michael@0 266 }, {value: "abc\ndef"});
michael@0 267
michael@0 268 testCM("selectionHistory", function(cm) {
michael@0 269 for (var i = 0; i < 3; ++i)
michael@0 270 cm.addSelection(Pos(0, i * 2), Pos(0, i * 2 + 1));
michael@0 271 cm.execCommand("undoSelection");
michael@0 272 eq(cm.getSelection(), "1\n2");
michael@0 273 cm.execCommand("undoSelection");
michael@0 274 eq(cm.getSelection(), "1");
michael@0 275 cm.execCommand("undoSelection");
michael@0 276 eq(cm.getSelection(), "");
michael@0 277 eqPos(cm.getCursor(), Pos(0, 0));
michael@0 278 cm.execCommand("redoSelection");
michael@0 279 eq(cm.getSelection(), "1");
michael@0 280 cm.execCommand("redoSelection");
michael@0 281 eq(cm.getSelection(), "1\n2");
michael@0 282 cm.execCommand("redoSelection");
michael@0 283 eq(cm.getSelection(), "1\n2\n3");
michael@0 284 }, {value: "1 2 3"});
michael@0 285 })();

mercurial